LLVM  4.0.0
TargetSelect.cpp
Go to the documentation of this file.
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This just asks the TargetRegistry for the appropriate target to use, and
11 // allows the user to specify a specific one on the commandline with -march=x,
12 // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
13 // calling selectTarget().
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Module.h"
21 #include "llvm/Support/Host.h"
24 
25 using namespace llvm;
26 
28  Triple TT;
29 
30  // MCJIT can generate code for remote targets, but the old JIT and Interpreter
31  // must use the host architecture.
32  if (WhichEngine != EngineKind::Interpreter && M)
33  TT.setTriple(M->getTargetTriple());
34 
35  return selectTarget(TT, MArch, MCPU, MAttrs);
36 }
37 
38 /// selectTarget - Pick a target either via -march or by guessing the native
39 /// arch. Add any CPU features specified via -mcpu or -mattr.
44  Triple TheTriple(TargetTriple);
45  if (TheTriple.getTriple().empty())
46  TheTriple.setTriple(sys::getProcessTriple());
47 
48  // Adjust the triple to match what the user requested.
49  const Target *TheTarget = nullptr;
50  if (!MArch.empty()) {
52  [&](const Target &T) { return MArch == T.getName(); });
53 
54  if (I == TargetRegistry::targets().end()) {
55  if (ErrorStr)
56  *ErrorStr = "No available targets are compatible with this -march, "
57  "see -version for the available targets.\n";
58  return nullptr;
59  }
60 
61  TheTarget = &*I;
62 
63  // Adjust the triple to match (if known), otherwise stick with the
64  // requested/host triple.
66  if (Type != Triple::UnknownArch)
67  TheTriple.setArch(Type);
68  } else {
69  std::string Error;
70  TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
71  if (!TheTarget) {
72  if (ErrorStr)
73  *ErrorStr = Error;
74  return nullptr;
75  }
76  }
77 
78  // Package up features to be passed to target/subtarget
79  std::string FeaturesStr;
80  if (!MAttrs.empty()) {
82  for (unsigned i = 0; i != MAttrs.size(); ++i)
83  Features.AddFeature(MAttrs[i]);
84  FeaturesStr = Features.getString();
85  }
86 
87  // FIXME: non-iOS ARM FastISel is broken with MCJIT.
88  if (TheTriple.getArch() == Triple::arm &&
89  !TheTriple.isiOS() &&
90  OptLevel == CodeGenOpt::None) {
91  OptLevel = CodeGenOpt::Less;
92  }
93 
94  // Allocate a target...
95  TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
96  MCPU, FeaturesStr,
97  Options,
98  RelocModel, CMModel,
99  OptLevel);
100  assert(Target && "Could not allocate target machine!");
101  return Target;
102 }
size_t i
const char * getName() const
getName - Get the target name.
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
void AddFeature(StringRef String, bool Enable=true)
Adding Features.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:270
static iterator_range< iterator > targets()
bool isiOS() const
Is this an iOS triple.
Definition: Triple.h:436
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
const std::string & getTriple() const
Definition: Triple.h:341
cl::list< std::string > MAttrs("mattr", cl::CommaSeparated, cl::desc("Target specific attributes (-mattr=help for details)"), cl::value_desc("a1,+a2,-a3,..."))
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1076
std::string getString() const
Features string accessors.
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1477
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
cl::opt< std::string > MCPU("mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"), cl::value_desc("cpu-name"), cl::init(""))
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
Module.h This file contains the declarations for the Module class.
static ArchType getArchTypeForLLVMName(StringRef Str)
getArchTypeForLLVMName - The canonical type for the given LLVM architecture name (e.g., "x86").
Definition: Triple.cpp:249
static const char * Target
cl::opt< std::string > MArch("march", cl::desc("Architecture to generate code for (see --version)"))
Target - Wrapper for Target specific information.
SubtargetFeatures - Manages the enabling and disabling of subtarget specific features.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
TargetMachine * selectTarget()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const FeatureBitset Features
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:764
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1080