LLVM  8.0.1
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.
41  StringRef MArch,
42  StringRef MCPU,
43  const SmallVectorImpl<std::string>& MAttrs) {
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...
96  TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
97  Options, RelocModel, CMModel, OptLevel,
98  /*JIT*/ true);
99  Target->Options.EmulatedTLS = EmulatedTLS;
100  Target->Options.ExplicitEmulatedTLS = true;
101 
102  assert(Target && "Could not allocate target machine!");
103  return Target;
104 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
std::string getString() const
Returns features as a string.
const FeatureBitset Features
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
const char * getName() const
getName - Get the target name.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:290
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
bool isiOS() const
Is this an iOS triple.
Definition: Triple.h:456
static iterator_range< iterator > targets()
unsigned ExplicitEmulatedTLS
Whether -emulated-tls or -no-emulated-tls is set.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void setTriple(const Twine &Str)
setTriple - Set all components to the new triple Str.
Definition: Triple.cpp:1142
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1214
size_t size() const
Definition: SmallVector.h:53
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1430
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:258
Target - Wrapper for Target specific information.
Manages the enabling and disabling of subtarget specific features.
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library...
const std::string & getTriple() const
Definition: Triple.h:361
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
TargetOptions Options
Definition: TargetMachine.h:97
#define I(x, y, z)
Definition: MD5.cpp:58
TargetMachine * selectTarget()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1146