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