LLVM 17.0.0git
TargetMachine.cpp
Go to the documentation of this file.
1//===-- TargetMachine.cpp - General Target Information ---------------------==//
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 file describes the general parts of a Target machine.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/IR/Function.h"
16#include "llvm/IR/GlobalValue.h"
18#include "llvm/IR/Mangler.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCInstrInfo.h"
25using namespace llvm;
26
27//---------------------------------------------------------------------------
28// TargetMachine Class
29//
30
32 const Triple &TT, StringRef CPU, StringRef FS,
34 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT),
35 TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr),
36 MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
37 O0WantsFastISel(false), DefaultOptions(Options), Options(Options) {}
38
40
43}
44
45/// Reset the target options based on the function's attributes.
46/// setFunctionAttributes should have made the raw attribute value consistent
47/// with the command line flag if used.
48//
49// FIXME: This function needs to go away for a number of reasons:
50// a) global state on the TargetMachine is terrible in general,
51// b) these target options should be passed only on the function
52// and not on the TargetMachine (via TargetOptions) at all.
54#define RESET_OPTION(X, Y) \
55 do { \
56 Options.X = F.getFnAttribute(Y).getValueAsBool(); \
57 } while (0)
58
59 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
60 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
61 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
62 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
63 RESET_OPTION(ApproxFuncFPMath, "approx-func-fp-math");
64}
65
66/// Returns the code generation relocation model. The choices are static, PIC,
67/// and dynamic-no-pic.
69
70/// Get the IR-specified TLS model for Var.
72 switch (GV->getThreadLocalMode()) {
74 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
75 break;
84 }
85 llvm_unreachable("invalid TLS model");
86}
87
89 const GlobalValue *GV) const {
90 const Triple &TT = getTargetTriple();
92
93 // According to the llvm language reference, we should be able to
94 // just return false in here if we have a GV, as we know it is
95 // dso_preemptable. At this point in time, the various IR producers
96 // have not been transitioned to always produce a dso_local when it
97 // is possible to do so.
98 //
99 // As a result we still have some logic in here to improve the quality of the
100 // generated code.
101 if (!GV)
102 return false;
103
104 // If the IR producer requested that this GV be treated as dso local, obey.
105 if (GV->isDSOLocal())
106 return true;
107
108 if (TT.isOSBinFormatCOFF()) {
109 // DLLImport explicitly marks the GV as external.
110 if (GV->hasDLLImportStorageClass())
111 return false;
112
113 // On MinGW, variables that haven't been declared with DLLImport may still
114 // end up automatically imported by the linker. To make this feasible,
115 // don't assume the variables to be DSO local unless we actually know
116 // that for sure. This only has to be done for variables; for functions
117 // the linker can insert thunks for calling functions from another DLL.
118 if (TT.isWindowsGNUEnvironment() && GV->isDeclarationForLinker() &&
119 isa<GlobalVariable>(GV))
120 return false;
121
122 // Don't mark 'extern_weak' symbols as DSO local. If these symbols remain
123 // unresolved in the link, they can be resolved to zero, which is outside
124 // the current DSO.
125 if (GV->hasExternalWeakLinkage())
126 return false;
127
128 // Every other GV is local on COFF.
129 return true;
130 }
131
132 if (TT.isOSBinFormatGOFF())
133 return true;
134
135 if (TT.isOSBinFormatMachO()) {
136 if (RM == Reloc::Static)
137 return true;
138 return GV->isStrongDefinitionForLinker();
139 }
140
141 assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm() ||
142 TT.isOSBinFormatXCOFF());
143 return false;
144}
145
147
149 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
151 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
152 bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
153
154 TLSModel::Model Model;
155 if (IsSharedLibrary) {
156 if (IsLocal)
158 else
160 } else {
161 if (IsLocal)
162 Model = TLSModel::LocalExec;
163 else
164 Model = TLSModel::InitialExec;
165 }
166
167 // If the user specified a more specific model, use that.
168 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
169 if (SelectedModel > Model)
170 return SelectedModel;
171
172 return Model;
173}
174
175/// Returns the optimization level: None, Less, Default, or Aggressive.
177
179
182 return TargetTransformInfo(F.getParent()->getDataLayout());
183}
184
186 const GlobalValue *GV, Mangler &Mang,
187 bool MayAlwaysUsePrivate) const {
188 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
189 // Simple case: If GV is not private, it is not important to find out if
190 // private labels are legal in this case or not.
191 Mang.getNameWithPrefix(Name, GV, false);
192 return;
193 }
195 TLOF->getNameWithPrefix(Name, GV, *this);
196}
197
200 // XCOFF symbols could have special naming convention.
201 if (MCSymbol *TargetSymbol = TLOF->getTargetSymbol(GV, *this))
202 return TargetSymbol;
203
204 SmallString<128> NameStr;
205 getNameWithPrefix(NameStr, GV, TLOF->getMangler());
206 return TLOF->getContext().getOrCreateSymbol(NameStr);
207}
208
210 // Since Analysis can't depend on Target, use a std::function to invert the
211 // dependency.
212 return TargetIRAnalysis(
213 [this](const Function &F) { return this->getTargetTransformInfo(F); });
214}
215
216std::pair<int, int> TargetMachine::parseBinutilsVersion(StringRef Version) {
217 if (Version == "none")
218 return {INT_MAX, INT_MAX}; // Make binutilsIsAtLeast() return true.
219 std::pair<int, int> Ret;
220 if (!Version.consumeInteger(10, Ret.first) && Version.consume_front("."))
221 Version.consumeInteger(10, Ret.second);
222 return Ret;
223}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
std::string Name
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV)
Get the IR-specified TLS model for Var.
#define RESET_OPTION(X, Y)
This pass exposes codegen information to IR-level passes.
bool isDSOLocal() const
Definition: GlobalValue.h:301
bool hasPrivateLinkage() const
Definition: GlobalValue.h:522
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:524
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:267
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:274
bool isDeclarationForLinker() const
Definition: GlobalValue.h:614
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:627
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:201
MCContext & getContext() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
PIELevel::Level getPIELevel() const
Returns the PIE level (small or large model)
Definition: Module.cpp:604
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Analysis pass providing the TargetTransformInfo.
virtual void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const
virtual MCSymbol * getTargetSymbol(const GlobalValue *GV, const TargetMachine &TM) const
Targets that have a special convention for their symbols could use this hook to return a specialized ...
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
bool isPositionIndependent() const
const Triple & getTargetTriple() const
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
bool useEmulatedTLS() const
Returns true if this target uses emulated TLS.
virtual TargetLoweringObjectFile * getObjFileLowering() const
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
virtual TargetTransformInfo getTargetTransformInfo(const Function &F) const
Return a TargetTransformInfo for a given function.
CodeGenOpt::Level OptLevel
static std::pair< int, int > parseBinutilsVersion(StringRef Version)
TargetIRAnalysis getTargetIRAnalysis() const
Get a TargetIRAnalysis appropriate for the target.
TargetOptions Options
virtual ~TargetMachine()
MCSymbol * getSymbol(const GlobalValue *GV) const
bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const
void setOptLevel(CodeGenOpt::Level Level)
Overrides the optimization level.
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
TargetMachine(const Target &T, StringRef DataLayoutString, const Triple &TargetTriple, StringRef CPU, StringRef FS, const TargetOptions &Options)
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Level
Code generation optimization level.
Definition: CodeGen.h:57
@ GeneralDynamic
Definition: CodeGen.h:46
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Definition: BitVector.h:858