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