LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Module.h"
22 #include "llvm/Support/Host.h"
25 
26 using namespace llvm;
27 
29  Triple TT;
30 
31  // MCJIT can generate code for remote targets, but the old JIT and Interpreter
32  // must use the host architecture.
33  if (UseMCJIT && WhichEngine != EngineKind::Interpreter && M)
34  TT.setTriple(M->getTargetTriple());
35 
36  return selectTarget(TT, MArch, MCPU, MAttrs);
37 }
38 
39 /// selectTarget - Pick a target either via -march or by guessing the native
40 /// arch. Add any CPU features specified via -mcpu or -mattr.
45  Triple TheTriple(TargetTriple);
46  if (TheTriple.getTriple().empty())
47  TheTriple.setTriple(sys::getProcessTriple());
48 
49  // Adjust the triple to match what the user requested.
50  const Target *TheTarget = 0;
51  if (!MArch.empty()) {
53  ie = TargetRegistry::end(); it != ie; ++it) {
54  if (MArch == it->getName()) {
55  TheTarget = &*it;
56  break;
57  }
58  }
59 
60  if (!TheTarget) {
61  if (ErrorStr)
62  *ErrorStr = "No available targets are compatible with this -march, "
63  "see -version for the available targets.\n";
64  return 0;
65  }
66 
67  // Adjust the triple to match (if known), otherwise stick with the
68  // requested/host triple.
70  if (Type != Triple::UnknownArch)
71  TheTriple.setArch(Type);
72  } else {
73  std::string Error;
74  TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
75  if (TheTarget == 0) {
76  if (ErrorStr)
77  *ErrorStr = Error;
78  return 0;
79  }
80  }
81 
82  // Package up features to be passed to target/subtarget
83  std::string FeaturesStr;
84  if (!MAttrs.empty()) {
85  SubtargetFeatures Features;
86  for (unsigned i = 0; i != MAttrs.size(); ++i)
87  Features.AddFeature(MAttrs[i]);
88  FeaturesStr = Features.getString();
89  }
90 
91  // FIXME: non-iOS ARM FastISel is broken with MCJIT.
92  if (UseMCJIT &&
93  TheTriple.getArch() == Triple::arm &&
94  !TheTriple.isiOS() &&
95  OptLevel == CodeGenOpt::None) {
96  OptLevel = CodeGenOpt::Less;
97  }
98 
99  // Allocate a target...
100  TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
101  MCPU, FeaturesStr,
102  Options,
103  RelocModel, CMModel,
104  OptLevel);
105  assert(Target && "Could not allocate target machine!");
106  return Target;
107 }
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
const std::string & getTargetTriple() const
Definition: Module.h:237
static iterator end()
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:172
bool isiOS() const
Is this an iOS triple.
Definition: Triple.h:308
const std::string & getTriple() const
Definition: Triple.h:223
cl::list< std::string > MAttrs("mattr", cl::CommaSeparated, cl::desc("Target specific attributes (-mattr=help for details)"), cl::value_desc("a1,+a2,-a3,..."))
static iterator begin()
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:616
std::string getString() const
Features string accessors.
std::string getProcessTriple()
Definition: Host.cpp:724
void AddFeature(const StringRef String, bool IsEnabled=true)
Adding Features.
TargetMachine * createTargetMachine(StringRef Triple, StringRef CPU, StringRef Features, const TargetOptions &Options, Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
cl::opt< std::string > MCPU("mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"), cl::value_desc("cpu-name"), cl::init(""))
static ArchType getArchTypeForLLVMName(StringRef Str)
Definition: Triple.cpp:161
cl::opt< std::string > MArch("march", cl::desc("Architecture to generate code for (see --version)"))
TargetMachine * selectTarget()
void setArch(ArchType Kind)
Definition: Triple.cpp:620
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110