LLVM  10.0.0svn
TargetSelect.cpp
Go to the documentation of this file.
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This just asks the TargetRegistry for the appropriate target to use, and
10 // allows the user to specify a specific one on the commandline with -march=x,
11 // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
12 // calling selectTarget().
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/Triple.h"
18 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Host.h"
23 
24 using namespace llvm;
25 
27  Triple TT;
28 
29  // MCJIT can generate code for remote targets, but the old JIT and Interpreter
30  // must use the host architecture.
31  if (WhichEngine != EngineKind::Interpreter && M)
32  TT.setTriple(M->getTargetTriple());
33 
34  return selectTarget(TT, MArch, MCPU, MAttrs);
35 }
36 
37 /// selectTarget - Pick a target either via -march or by guessing the native
38 /// arch. Add any CPU features specified via -mcpu or -mattr.
40  StringRef MArch,
41  StringRef MCPU,
42  const SmallVectorImpl<std::string>& MAttrs) {
43  Triple TheTriple(TargetTriple);
44  if (TheTriple.getTriple().empty())
45  TheTriple.setTriple(sys::getProcessTriple());
46 
47  // Adjust the triple to match what the user requested.
48  const Target *TheTarget = nullptr;
49  if (!MArch.empty()) {
51  [&](const Target &T) { return MArch == T.getName(); });
52 
53  if (I == TargetRegistry::targets().end()) {
54  if (ErrorStr)
55  *ErrorStr = "No available targets are compatible with this -march, "
56  "see -version for the available targets.\n";
57  return nullptr;
58  }
59 
60  TheTarget = &*I;
61 
62  // Adjust the triple to match (if known), otherwise stick with the
63  // requested/host triple.
65  if (Type != Triple::UnknownArch)
66  TheTriple.setArch(Type);
67  } else {
68  std::string Error;
69  TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
70  if (!TheTarget) {
71  if (ErrorStr)
72  *ErrorStr = Error;
73  return nullptr;
74  }
75  }
76 
77  // Package up features to be passed to target/subtarget
78  std::string FeaturesStr;
79  if (!MAttrs.empty()) {
80  SubtargetFeatures Features;
81  for (unsigned i = 0; i != MAttrs.size(); ++i)
82  Features.AddFeature(MAttrs[i]);
83  FeaturesStr = Features.getString();
84  }
85 
86  // FIXME: non-iOS ARM FastISel is broken with MCJIT.
87  if (TheTriple.getArch() == Triple::arm &&
88  !TheTriple.isiOS() &&
89  OptLevel == CodeGenOpt::None) {
90  OptLevel = CodeGenOpt::Less;
91  }
92 
93  // Allocate a target...
95  TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
96  Options, RelocModel, CMModel, OptLevel,
97  /*JIT*/ true);
98  Target->Options.EmulatedTLS = EmulatedTLS;
99  Target->Options.ExplicitEmulatedTLS = true;
100 
101  assert(Target && "Could not allocate target machine!");
102  return Target;
103 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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.
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.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:296
bool isiOS() const
Is this an iOS triple.
Definition: Triple.h:462
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:1164
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:1193
size_t size() const
Definition: SmallVector.h:52
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1530
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
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:263
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:367
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
TargetOptions Options
#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:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1168