[SampleProfileLoader] Fix integer overflow in generateMDProfMetadata (#90217)
[llvm-project.git] / llvm / lib / Target / TargetMachine.cpp
blob8ddc742004292ba1b91a2450a0f7a05a33f38283
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Target/TargetMachine.h"
14 #include "llvm/Analysis/TargetTransformInfo.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/GlobalVariable.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"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/CodeGen.h"
25 #include "llvm/Target/TargetLoweringObjectFile.h"
26 using namespace llvm;
28 //---------------------------------------------------------------------------
29 // TargetMachine Class
32 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
33 const Triple &TT, StringRef CPU, StringRef FS,
34 const TargetOptions &Options)
35 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT),
36 TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr),
37 MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
38 O0WantsFastISel(false), Options(Options) {}
40 TargetMachine::~TargetMachine() = default;
42 bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
43 if (getTargetTriple().getArch() != Triple::x86_64)
44 return false;
46 // Remaining logic below is ELF-specific. For other object file formats where
47 // the large code model is mostly used for JIT compilation, just look at the
48 // code model.
49 if (!getTargetTriple().isOSBinFormatELF())
50 return getCodeModel() == CodeModel::Large;
52 auto *GO = GVal->getAliaseeObject();
54 // Be conservative if we can't find an underlying GlobalObject.
55 if (!GO)
56 return true;
58 auto *GV = dyn_cast<GlobalVariable>(GO);
60 auto IsPrefix = [](StringRef Name, StringRef Prefix) {
61 return Name.consume_front(Prefix) && (Name.empty() || Name[0] == '.');
64 // Functions/GlobalIFuncs are only large under the large code model.
65 if (!GV) {
66 // Handle explicit sections as we do for GlobalVariables with an explicit
67 // section, see comments below.
68 if (GO->hasSection()) {
69 StringRef Name = GO->getSection();
70 return IsPrefix(Name, ".ltext");
72 return getCodeModel() == CodeModel::Large;
75 if (GV->isThreadLocal())
76 return false;
78 // For x86-64, we treat an explicit GlobalVariable small code model to mean
79 // that the global should be placed in a small section, and ditto for large.
80 if (auto CM = GV->getCodeModel()) {
81 if (*CM == CodeModel::Small)
82 return false;
83 if (*CM == CodeModel::Large)
84 return true;
87 // Treat all globals in explicit sections as small, except for the standard
88 // large sections of .lbss, .ldata, .lrodata. This reduces the risk of linking
89 // together small and large sections, resulting in small references to large
90 // data sections. The code model attribute overrides this above.
91 if (GV->hasSection()) {
92 StringRef Name = GV->getSection();
93 return IsPrefix(Name, ".lbss") || IsPrefix(Name, ".ldata") ||
94 IsPrefix(Name, ".lrodata");
97 // Respect large data threshold for medium and large code models.
98 if (getCodeModel() == CodeModel::Medium ||
99 getCodeModel() == CodeModel::Large) {
100 if (!GV->getValueType()->isSized())
101 return true;
102 // Linker defined start/stop symbols can point to arbitrary points in the
103 // binary, so treat them as large.
104 if (GV->isDeclaration() && (GV->getName() == "__ehdr_start" ||
105 GV->getName().starts_with("__start_") ||
106 GV->getName().starts_with("__stop_")))
107 return true;
108 const DataLayout &DL = GV->getParent()->getDataLayout();
109 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
110 return Size == 0 || Size > LargeDataThreshold;
113 return false;
116 bool TargetMachine::isPositionIndependent() const {
117 return getRelocationModel() == Reloc::PIC_;
120 /// Reset the target options based on the function's attributes.
121 /// setFunctionAttributes should have made the raw attribute value consistent
122 /// with the command line flag if used.
124 // FIXME: This function needs to go away for a number of reasons:
125 // a) global state on the TargetMachine is terrible in general,
126 // b) these target options should be passed only on the function
127 // and not on the TargetMachine (via TargetOptions) at all.
128 void TargetMachine::resetTargetOptions(const Function &F) const {
129 #define RESET_OPTION(X, Y) \
130 do { \
131 Options.X = F.getFnAttribute(Y).getValueAsBool(); \
132 } while (0)
134 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
135 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
136 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
137 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
138 RESET_OPTION(ApproxFuncFPMath, "approx-func-fp-math");
141 /// Returns the code generation relocation model. The choices are static, PIC,
142 /// and dynamic-no-pic.
143 Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
145 uint64_t TargetMachine::getMaxCodeSize() const {
146 switch (getCodeModel()) {
147 case CodeModel::Tiny:
148 return llvm::maxUIntN(10);
149 case CodeModel::Small:
150 case CodeModel::Kernel:
151 case CodeModel::Medium:
152 return llvm::maxUIntN(31);
153 case CodeModel::Large:
154 return llvm::maxUIntN(64);
156 llvm_unreachable("Unhandled CodeModel enum");
159 /// Get the IR-specified TLS model for Var.
160 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
161 switch (GV->getThreadLocalMode()) {
162 case GlobalVariable::NotThreadLocal:
163 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
164 break;
165 case GlobalVariable::GeneralDynamicTLSModel:
166 return TLSModel::GeneralDynamic;
167 case GlobalVariable::LocalDynamicTLSModel:
168 return TLSModel::LocalDynamic;
169 case GlobalVariable::InitialExecTLSModel:
170 return TLSModel::InitialExec;
171 case GlobalVariable::LocalExecTLSModel:
172 return TLSModel::LocalExec;
174 llvm_unreachable("invalid TLS model");
177 bool TargetMachine::shouldAssumeDSOLocal(const GlobalValue *GV) const {
178 const Triple &TT = getTargetTriple();
179 Reloc::Model RM = getRelocationModel();
181 // According to the llvm language reference, we should be able to
182 // just return false in here if we have a GV, as we know it is
183 // dso_preemptable. At this point in time, the various IR producers
184 // have not been transitioned to always produce a dso_local when it
185 // is possible to do so.
187 // As a result we still have some logic in here to improve the quality of the
188 // generated code.
189 if (!GV)
190 return false;
192 // If the IR producer requested that this GV be treated as dso local, obey.
193 if (GV->isDSOLocal())
194 return true;
196 if (TT.isOSBinFormatCOFF()) {
197 // DLLImport explicitly marks the GV as external.
198 if (GV->hasDLLImportStorageClass())
199 return false;
201 // On MinGW, variables that haven't been declared with DLLImport may still
202 // end up automatically imported by the linker. To make this feasible,
203 // don't assume the variables to be DSO local unless we actually know
204 // that for sure. This only has to be done for variables; for functions
205 // the linker can insert thunks for calling functions from another DLL.
206 if (TT.isWindowsGNUEnvironment() && GV->isDeclarationForLinker() &&
207 isa<GlobalVariable>(GV))
208 return false;
210 // Don't mark 'extern_weak' symbols as DSO local. If these symbols remain
211 // unresolved in the link, they can be resolved to zero, which is outside
212 // the current DSO.
213 if (GV->hasExternalWeakLinkage())
214 return false;
216 // Every other GV is local on COFF.
217 return true;
220 if (TT.isOSBinFormatGOFF())
221 return true;
223 if (TT.isOSBinFormatMachO()) {
224 if (RM == Reloc::Static)
225 return true;
226 return GV->isStrongDefinitionForLinker();
229 assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm() ||
230 TT.isOSBinFormatXCOFF());
231 return false;
234 bool TargetMachine::useEmulatedTLS() const { return Options.EmulatedTLS; }
235 bool TargetMachine::useTLSDESC() const { return Options.EnableTLSDESC; }
237 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
238 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
239 Reloc::Model RM = getRelocationModel();
240 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
241 bool IsLocal = shouldAssumeDSOLocal(GV);
243 TLSModel::Model Model;
244 if (IsSharedLibrary) {
245 if (IsLocal)
246 Model = TLSModel::LocalDynamic;
247 else
248 Model = TLSModel::GeneralDynamic;
249 } else {
250 if (IsLocal)
251 Model = TLSModel::LocalExec;
252 else
253 Model = TLSModel::InitialExec;
256 // If the user specified a more specific model, use that.
257 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
258 if (SelectedModel > Model)
259 return SelectedModel;
261 return Model;
264 /// Returns the optimization level: None, Less, Default, or Aggressive.
265 CodeGenOptLevel TargetMachine::getOptLevel() const { return OptLevel; }
267 void TargetMachine::setOptLevel(CodeGenOptLevel Level) { OptLevel = Level; }
269 TargetTransformInfo
270 TargetMachine::getTargetTransformInfo(const Function &F) const {
271 return TargetTransformInfo(F.getParent()->getDataLayout());
274 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
275 const GlobalValue *GV, Mangler &Mang,
276 bool MayAlwaysUsePrivate) const {
277 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
278 // Simple case: If GV is not private, it is not important to find out if
279 // private labels are legal in this case or not.
280 Mang.getNameWithPrefix(Name, GV, false);
281 return;
283 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
284 TLOF->getNameWithPrefix(Name, GV, *this);
287 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
288 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
289 // XCOFF symbols could have special naming convention.
290 if (MCSymbol *TargetSymbol = TLOF->getTargetSymbol(GV, *this))
291 return TargetSymbol;
293 SmallString<128> NameStr;
294 getNameWithPrefix(NameStr, GV, TLOF->getMangler());
295 return TLOF->getContext().getOrCreateSymbol(NameStr);
298 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() const {
299 // Since Analysis can't depend on Target, use a std::function to invert the
300 // dependency.
301 return TargetIRAnalysis(
302 [this](const Function &F) { return this->getTargetTransformInfo(F); });
305 std::pair<int, int> TargetMachine::parseBinutilsVersion(StringRef Version) {
306 if (Version == "none")
307 return {INT_MAX, INT_MAX}; // Make binutilsIsAtLeast() return true.
308 std::pair<int, int> Ret;
309 if (!Version.consumeInteger(10, Ret.first) && Version.consume_front("."))
310 Version.consumeInteger(10, Ret.second);
311 return Ret;