xref: /freebsd/contrib/llvm-project/llvm/lib/Target/TargetMachine.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
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 
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/IR/Module.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 using namespace llvm;
29 
30 cl::opt<bool> NoKernelInfoEndLTO(
31     "no-kernel-info-end-lto",
32     cl::desc("remove the kernel-info pass at the end of the full LTO pipeline"),
33     cl::init(false), cl::Hidden);
34 
35 //---------------------------------------------------------------------------
36 // TargetMachine Class
37 //
38 
39 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
40                              const Triple &TT, StringRef CPU, StringRef FS,
41                              const TargetOptions &Options)
42     : TheTarget(T), DL(DataLayoutString), TargetTriple(TT),
43       TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr),
44       MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
45       O0WantsFastISel(false), Options(Options) {}
46 
47 TargetMachine::~TargetMachine() = default;
48 
49 Expected<std::unique_ptr<MCStreamer>>
50 TargetMachine::createMCStreamer(raw_pwrite_stream &Out,
51                                 raw_pwrite_stream *DwoOut,
52                                 CodeGenFileType FileType, MCContext &Ctx) {
53   return nullptr;
54 }
55 
56 bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
57   if (getTargetTriple().getArch() != Triple::x86_64)
58     return false;
59 
60   // Remaining logic below is ELF-specific. For other object file formats where
61   // the large code model is mostly used for JIT compilation, just look at the
62   // code model.
63   if (!getTargetTriple().isOSBinFormatELF())
64     return getCodeModel() == CodeModel::Large;
65 
66   auto *GO = GVal->getAliaseeObject();
67 
68   // Be conservative if we can't find an underlying GlobalObject.
69   if (!GO)
70     return true;
71 
72   auto *GV = dyn_cast<GlobalVariable>(GO);
73 
74   auto IsPrefix = [](StringRef Name, StringRef Prefix) {
75     return Name.consume_front(Prefix) && (Name.empty() || Name[0] == '.');
76   };
77 
78   // Functions/GlobalIFuncs are only large under the large code model.
79   if (!GV) {
80     // Handle explicit sections as we do for GlobalVariables with an explicit
81     // section, see comments below.
82     if (GO->hasSection()) {
83       StringRef Name = GO->getSection();
84       return IsPrefix(Name, ".ltext");
85     }
86     return getCodeModel() == CodeModel::Large;
87   }
88 
89   if (GV->isThreadLocal())
90     return false;
91 
92   // For x86-64, we treat an explicit GlobalVariable small code model to mean
93   // that the global should be placed in a small section, and ditto for large.
94   if (auto CM = GV->getCodeModel()) {
95     if (*CM == CodeModel::Small)
96       return false;
97     if (*CM == CodeModel::Large)
98       return true;
99   }
100 
101   // Treat all globals in explicit sections as small, except for the standard
102   // large sections of .lbss, .ldata, .lrodata. This reduces the risk of linking
103   // together small and large sections, resulting in small references to large
104   // data sections. The code model attribute overrides this above.
105   if (GV->hasSection()) {
106     StringRef Name = GV->getSection();
107     return IsPrefix(Name, ".lbss") || IsPrefix(Name, ".ldata") ||
108            IsPrefix(Name, ".lrodata");
109   }
110 
111   // Respect large data threshold for medium and large code models.
112   if (getCodeModel() == CodeModel::Medium ||
113       getCodeModel() == CodeModel::Large) {
114     if (!GV->getValueType()->isSized())
115       return true;
116     // Linker defined start/stop symbols can point to arbitrary points in the
117     // binary, so treat them as large.
118     if (GV->isDeclaration() && (GV->getName() == "__ehdr_start" ||
119                                 GV->getName().starts_with("__start_") ||
120                                 GV->getName().starts_with("__stop_")))
121       return true;
122     // Linkers do not currently support PT_GNU_RELRO for SHF_X86_64_LARGE
123     // sections; that would require the linker to emit more than one
124     // PT_GNU_RELRO because large sections are discontiguous by design, and most
125     // ELF dynamic loaders do not support that (bionic appears to support it but
126     // glibc/musl/FreeBSD/NetBSD/OpenBSD appear not to). With current linkers
127     // these sections will end up in .ldata which results in silently disabling
128     // RELRO. If this ever gets supported by downstream components in the future
129     // we could add an opt-in flag for moving these sections to .ldata.rel.ro
130     // which would trigger the creation of a second PT_GNU_RELRO.
131     if (!GV->isDeclarationForLinker() &&
132         TargetLoweringObjectFile::getKindForGlobal(GV, *this)
133             .isReadOnlyWithRel())
134       return false;
135     const DataLayout &DL = GV->getDataLayout();
136     uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
137     return Size == 0 || Size > LargeDataThreshold;
138   }
139 
140   return false;
141 }
142 
143 bool TargetMachine::isPositionIndependent() const {
144   return getRelocationModel() == Reloc::PIC_;
145 }
146 
147 /// Reset the target options based on the function's attributes.
148 /// setFunctionAttributes should have made the raw attribute value consistent
149 /// with the command line flag if used.
150 //
151 // FIXME: This function needs to go away for a number of reasons:
152 // a) global state on the TargetMachine is terrible in general,
153 // b) these target options should be passed only on the function
154 //    and not on the TargetMachine (via TargetOptions) at all.
155 void TargetMachine::resetTargetOptions(const Function &F) const {
156 #define RESET_OPTION(X, Y)                                              \
157   do {                                                                  \
158     Options.X = F.getFnAttribute(Y).getValueAsBool();     \
159   } while (0)
160 
161   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
162   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
163   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
164   RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
165   RESET_OPTION(ApproxFuncFPMath, "approx-func-fp-math");
166 }
167 
168 /// Returns the code generation relocation model. The choices are static, PIC,
169 /// and dynamic-no-pic.
170 Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
171 
172 uint64_t TargetMachine::getMaxCodeSize() const {
173   switch (getCodeModel()) {
174   case CodeModel::Tiny:
175     return llvm::maxUIntN(10);
176   case CodeModel::Small:
177   case CodeModel::Kernel:
178   case CodeModel::Medium:
179     return llvm::maxUIntN(31);
180   case CodeModel::Large:
181     return llvm::maxUIntN(64);
182   }
183   llvm_unreachable("Unhandled CodeModel enum");
184 }
185 
186 /// Get the IR-specified TLS model for Var.
187 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
188   switch (GV->getThreadLocalMode()) {
189   case GlobalVariable::NotThreadLocal:
190     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
191     break;
192   case GlobalVariable::GeneralDynamicTLSModel:
193     return TLSModel::GeneralDynamic;
194   case GlobalVariable::LocalDynamicTLSModel:
195     return TLSModel::LocalDynamic;
196   case GlobalVariable::InitialExecTLSModel:
197     return TLSModel::InitialExec;
198   case GlobalVariable::LocalExecTLSModel:
199     return TLSModel::LocalExec;
200   }
201   llvm_unreachable("invalid TLS model");
202 }
203 
204 bool TargetMachine::shouldAssumeDSOLocal(const GlobalValue *GV) const {
205   const Triple &TT = getTargetTriple();
206   Reloc::Model RM = getRelocationModel();
207 
208   // According to the llvm language reference, we should be able to
209   // just return false in here if we have a GV, as we know it is
210   // dso_preemptable.  At this point in time, the various IR producers
211   // have not been transitioned to always produce a dso_local when it
212   // is possible to do so.
213   //
214   // As a result we still have some logic in here to improve the quality of the
215   // generated code.
216   if (!GV)
217     return false;
218 
219   // If the IR producer requested that this GV be treated as dso local, obey.
220   if (GV->isDSOLocal())
221     return true;
222 
223   if (TT.isOSBinFormatCOFF()) {
224     // DLLImport explicitly marks the GV as external.
225     if (GV->hasDLLImportStorageClass())
226       return false;
227 
228     // On MinGW, variables that haven't been declared with DLLImport may still
229     // end up automatically imported by the linker. To make this feasible,
230     // don't assume the variables to be DSO local unless we actually know
231     // that for sure. This only has to be done for variables; for functions
232     // the linker can insert thunks for calling functions from another DLL.
233     if (TT.isOSCygMing() && GV->isDeclarationForLinker() &&
234         isa<GlobalVariable>(GV))
235       return false;
236 
237     // Don't mark 'extern_weak' symbols as DSO local. If these symbols remain
238     // unresolved in the link, they can be resolved to zero, which is outside
239     // the current DSO.
240     if (GV->hasExternalWeakLinkage())
241       return false;
242 
243     // Every other GV is local on COFF.
244     return true;
245   }
246 
247   if (TT.isOSBinFormatGOFF())
248     return true;
249 
250   if (TT.isOSBinFormatMachO()) {
251     if (RM == Reloc::Static)
252       return true;
253     return GV->isStrongDefinitionForLinker();
254   }
255 
256   assert(TT.isOSBinFormatELF() || TT.isOSBinFormatWasm() ||
257          TT.isOSBinFormatXCOFF());
258   return false;
259 }
260 
261 bool TargetMachine::useEmulatedTLS() const { return Options.EmulatedTLS; }
262 bool TargetMachine::useTLSDESC() const { return Options.EnableTLSDESC; }
263 
264 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
265   bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
266   Reloc::Model RM = getRelocationModel();
267   bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
268   bool IsLocal = shouldAssumeDSOLocal(GV);
269 
270   TLSModel::Model Model;
271   if (IsSharedLibrary) {
272     if (IsLocal)
273       Model = TLSModel::LocalDynamic;
274     else
275       Model = TLSModel::GeneralDynamic;
276   } else {
277     if (IsLocal)
278       Model = TLSModel::LocalExec;
279     else
280       Model = TLSModel::InitialExec;
281   }
282 
283   // If the user specified a more specific model, use that.
284   TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
285   if (SelectedModel > Model)
286     return SelectedModel;
287 
288   return Model;
289 }
290 
291 TargetTransformInfo
292 TargetMachine::getTargetTransformInfo(const Function &F) const {
293   return TargetTransformInfo(F.getDataLayout());
294 }
295 
296 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
297                                       const GlobalValue *GV, Mangler &Mang,
298                                       bool MayAlwaysUsePrivate) const {
299   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
300     // Simple case: If GV is not private, it is not important to find out if
301     // private labels are legal in this case or not.
302     Mang.getNameWithPrefix(Name, GV, false);
303     return;
304   }
305   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
306   TLOF->getNameWithPrefix(Name, GV, *this);
307 }
308 
309 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
310   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
311   // XCOFF symbols could have special naming convention.
312   if (MCSymbol *TargetSymbol = TLOF->getTargetSymbol(GV, *this))
313     return TargetSymbol;
314 
315   SmallString<128> NameStr;
316   getNameWithPrefix(NameStr, GV, TLOF->getMangler());
317   return TLOF->getContext().getOrCreateSymbol(NameStr);
318 }
319 
320 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() const {
321   // Since Analysis can't depend on Target, use a std::function to invert the
322   // dependency.
323   return TargetIRAnalysis(
324       [this](const Function &F) { return this->getTargetTransformInfo(F); });
325 }
326 
327 std::pair<int, int> TargetMachine::parseBinutilsVersion(StringRef Version) {
328   if (Version == "none")
329     return {INT_MAX, INT_MAX}; // Make binutilsIsAtLeast() return true.
330   std::pair<int, int> Ret;
331   if (!Version.consumeInteger(10, Ret.first) && Version.consume_front("."))
332     Version.consumeInteger(10, Ret.second);
333   return Ret;
334 }
335