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