LLVM  3.7.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 
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 (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 = nullptr;
51  if (!MArch.empty()) {
52  auto I = std::find_if(
54  [&](const Target &T) { return MArch == T.getName(); });
55 
56  if (I == TargetRegistry::targets().end()) {
57  if (ErrorStr)
58  *ErrorStr = "No available targets are compatible with this -march, "
59  "see -version for the available targets.\n";
60  return nullptr;
61  }
62 
63  TheTarget = &*I;
64 
65  // Adjust the triple to match (if known), otherwise stick with the
66  // requested/host triple.
68  if (Type != Triple::UnknownArch)
69  TheTriple.setArch(Type);
70  } else {
71  std::string Error;
72  TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
73  if (!TheTarget) {
74  if (ErrorStr)
75  *ErrorStr = Error;
76  return nullptr;
77  }
78  }
79 
80  // Package up features to be passed to target/subtarget
81  std::string FeaturesStr;
82  if (!MAttrs.empty()) {
84  for (unsigned i = 0; i != MAttrs.size(); ++i)
85  Features.AddFeature(MAttrs[i]);
86  FeaturesStr = Features.getString();
87  }
88 
89  // FIXME: non-iOS ARM FastISel is broken with MCJIT.
90  if (TheTriple.getArch() == Triple::arm &&
91  !TheTriple.isiOS() &&
92  OptLevel == CodeGenOpt::None) {
93  OptLevel = CodeGenOpt::Less;
94  }
95 
96  // Allocate a target...
97  TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
98  MCPU, FeaturesStr,
99  Options,
100  RelocModel, CMModel,
101  OptLevel);
102  assert(Target && "Could not allocate target machine!");
103  return Target;
104 }
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
const char * getName() const
getName - Get the target name.
const FeatureBitset Features
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:232
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.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:242
static iterator_range< iterator > targets()
bool isiOS() const
Is this an iOS triple.
Definition: Triple.h:399
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:308
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:927
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:898
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(""))
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:219
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.
#define I(x, y, z)
Definition: MD5.cpp:54
TargetMachine * selectTarget()
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:931
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110