LLVM 20.0.0git
TargetLoweringObjectFile.cpp
Go to the documentation of this file.
1//===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
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 implements classes used to handle lowerings specific to common
10// object file formats.
11//
12//===----------------------------------------------------------------------===//
13
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/Function.h"
21#include "llvm/IR/Mangler.h"
22#include "llvm/IR/Module.h"
23#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/SectionKind.h"
31using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
37/// Initialize - this method must be called before any actual lowering is
38/// done. This specifies the current context for codegen, and gives the
39/// lowering implementations a chance to set up their default sections.
41 const TargetMachine &TM) {
42 // `Initialize` can be called more than once.
43 delete Mang;
44 Mang = new Mangler();
47
48 // Reset various EH DWARF encodings.
51
52 this->TM = &TM;
53}
54
56 delete Mang;
57}
58
60 // If target does not have LEB128 directives, we would need the
61 // call site encoding to be udata4 so that the alternative path
62 // for not having LEB128 directives could work.
63 if (!getContext().getAsmInfo()->hasLEB128Directives())
65 return CallSiteEncoding;
66}
67
68static bool isNullOrUndef(const Constant *C) {
69 // Check that the constant isn't all zeros or undefs.
70 if (C->isNullValue() || isa<UndefValue>(C))
71 return true;
72 if (!isa<ConstantAggregate>(C))
73 return false;
74 for (const auto *Operand : C->operand_values()) {
75 if (!isNullOrUndef(cast<Constant>(Operand)))
76 return false;
77 }
78 return true;
79}
80
81static bool isSuitableForBSS(const GlobalVariable *GV) {
82 const Constant *C = GV->getInitializer();
83
84 // Must have zero initializer.
85 if (!isNullOrUndef(C))
86 return false;
87
88 // Leave constant zeros in readonly constant sections, so they can be shared.
89 if (GV->isConstant())
90 return false;
91
92 // If the global has an explicit section specified, don't put it in BSS.
93 if (GV->hasSection())
94 return false;
95
96 // Otherwise, put it in BSS!
97 return true;
98}
99
100/// IsNullTerminatedString - Return true if the specified constant (which is
101/// known to have a type that is an array of 1/2/4 byte elements) ends with a
102/// nul value and contains no other nuls in it. Note that this is more general
103/// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
104static bool IsNullTerminatedString(const Constant *C) {
105 // First check: is we have constant array terminated with zero
106 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
107 unsigned NumElts = CDS->getNumElements();
108 assert(NumElts != 0 && "Can't have an empty CDS");
109
110 if (CDS->getElementAsInteger(NumElts-1) != 0)
111 return false; // Not null terminated.
112
113 // Verify that the null doesn't occur anywhere else in the string.
114 for (unsigned i = 0; i != NumElts-1; ++i)
115 if (CDS->getElementAsInteger(i) == 0)
116 return false;
117 return true;
118 }
119
120 // Another possibility: [1 x i8] zeroinitializer
121 if (isa<ConstantAggregateZero>(C))
122 return cast<ArrayType>(C->getType())->getNumElements() == 1;
123
124 return false;
125}
126
128 const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const {
129 assert(!Suffix.empty());
130
131 SmallString<60> NameStr;
132 NameStr += GV->getDataLayout().getPrivateGlobalPrefix();
133 TM.getNameWithPrefix(NameStr, GV, *Mang);
134 NameStr.append(Suffix.begin(), Suffix.end());
135 return getContext().getOrCreateSymbol(NameStr);
136}
137
139 const GlobalValue *GV, const TargetMachine &TM,
140 MachineModuleInfo *MMI) const {
141 return TM.getSymbol(GV);
142}
143
145 const DataLayout &,
146 const MCSymbol *Sym) const {
147}
148
150 Module &M) const {
153 M.getModuleFlagsMetadata(ModuleFlags);
154
155 MDNode *CFGProfile = nullptr;
156
157 for (const auto &MFE : ModuleFlags) {
158 StringRef Key = MFE.Key->getString();
159 if (Key == "CG Profile") {
160 CFGProfile = cast<MDNode>(MFE.Val);
161 break;
162 }
163 }
164
165 if (!CFGProfile)
166 return;
167
168 auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
169 if (!MDO)
170 return nullptr;
171 auto *V = cast<ValueAsMetadata>(MDO);
172 const Function *F = cast<Function>(V->getValue()->stripPointerCasts());
173 if (F->hasDLLImportStorageClass())
174 return nullptr;
175 return TM->getSymbol(F);
176 };
177
178 for (const auto &Edge : CFGProfile->operands()) {
179 MDNode *E = cast<MDNode>(Edge);
180 const MCSymbol *From = GetSym(E->getOperand(0));
181 const MCSymbol *To = GetSym(E->getOperand(1));
182 // Skip null functions. This can happen if functions are dead stripped after
183 // the CGProfile pass has been run.
184 if (!From || !To)
185 continue;
186 uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
187 ->getValue()
188 ->getUniqueInteger()
189 .getZExtValue();
190 Streamer.emitCGProfileEntry(
193 }
194}
195
196/// getKindForGlobal - This is a top-level target-independent classifier for
197/// a global object. Given a global variable and information from the TM, this
198/// function classifies the global in a target independent manner. This function
199/// may be overridden by the target implementation.
201 const TargetMachine &TM){
203 "Can only be used for global definitions");
204
205 // Functions are classified as text sections.
206 if (isa<Function>(GO))
207 return SectionKind::getText();
208
209 // Basic blocks are classified as text sections.
210 if (isa<BasicBlock>(GO))
211 return SectionKind::getText();
212
213 // Global variables require more detailed analysis.
214 const auto *GVar = cast<GlobalVariable>(GO);
215
216 // Handle thread-local data first.
217 if (GVar->isThreadLocal()) {
218 if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
219 // Zero-initialized TLS variables with local linkage always get classified
220 // as ThreadBSSLocal.
221 if (GVar->hasLocalLinkage()) {
223 }
225 }
227 }
228
229 // Variables with common linkage always get classified as common.
230 if (GVar->hasCommonLinkage())
231 return SectionKind::getCommon();
232
233 // Most non-mergeable zero data can be put in the BSS section unless otherwise
234 // specified.
235 if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
236 if (GVar->hasLocalLinkage())
238 else if (GVar->hasExternalLinkage())
240 return SectionKind::getBSS();
241 }
242
243 // Global variables with '!exclude' should get the exclude section kind if
244 // they have an explicit section and no other metadata.
245 if (GVar->hasSection())
246 if (MDNode *MD = GVar->getMetadata(LLVMContext::MD_exclude))
247 if (!MD->getNumOperands())
249
250 // If the global is marked constant, we can put it into a mergable section,
251 // a mergable string section, or general .data if it contains relocations.
252 if (GVar->isConstant()) {
253 // If the initializer for the global contains something that requires a
254 // relocation, then we may have to drop this into a writable data section
255 // even though it is marked const.
256 const Constant *C = GVar->getInitializer();
257 if (!C->needsRelocation()) {
258 // If the global is required to have a unique address, it can't be put
259 // into a mergable section: just drop it into the general read-only
260 // section instead.
261 if (!GVar->hasGlobalUnnamedAddr())
263
264 // If initializer is a null-terminated string, put it in a "cstring"
265 // section of the right width.
266 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
267 if (IntegerType *ITy =
268 dyn_cast<IntegerType>(ATy->getElementType())) {
269 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
270 ITy->getBitWidth() == 32) &&
272 if (ITy->getBitWidth() == 8)
274 if (ITy->getBitWidth() == 16)
276
277 assert(ITy->getBitWidth() == 32 && "Unknown width");
279 }
280 }
281 }
282
283 // Otherwise, just drop it into a mergable constant section. If we have
284 // a section for this size, use it, otherwise use the arbitrary sized
285 // mergable section.
286 switch (
287 GVar->getDataLayout().getTypeAllocSize(C->getType())) {
288 case 4: return SectionKind::getMergeableConst4();
289 case 8: return SectionKind::getMergeableConst8();
290 case 16: return SectionKind::getMergeableConst16();
291 case 32: return SectionKind::getMergeableConst32();
292 default:
294 }
295
296 } else {
297 // In static, ROPI and RWPI relocation models, the linker will resolve
298 // all addresses, so the relocation entries will actually be constants by
299 // the time the app starts up. However, we can't put this into a
300 // mergable section, because the linker doesn't take relocations into
301 // consideration when it tries to merge entries in the section.
302 Reloc::Model ReloModel = TM.getRelocationModel();
303 if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
304 ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
305 !C->needsDynamicRelocation())
307
308 // Otherwise, the dynamic linker needs to fix it up, put it in the
309 // writable data.rel section.
311 }
312 }
313
314 // Okay, this isn't a constant.
315 return SectionKind::getData();
316}
317
318/// This method computes the appropriate section to emit the specified global
319/// variable or function definition. This should not be passed external (or
320/// available externally) globals.
322 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
323 // Select section name.
324 if (GO->hasSection())
325 return getExplicitSectionGlobal(GO, Kind, TM);
326
327 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
328 auto Attrs = GVar->getAttributes();
329 if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
330 (Attrs.hasAttribute("data-section") && Kind.isData()) ||
331 (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
332 (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) {
333 return getExplicitSectionGlobal(GO, Kind, TM);
334 }
335 }
336
337 // Use default section depending on the 'type' of global
338 return SelectSectionForGlobal(GO, Kind, TM);
339}
340
341/// This method computes the appropriate section to emit the specified global
342/// variable or function definition. This should not be passed external (or
343/// available externally) globals.
344MCSection *
346 const TargetMachine &TM) const {
347 return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
348}
349
351 const Function &F, const TargetMachine &TM) const {
352 Align Alignment(1);
353 return getSectionForConstant(F.getDataLayout(),
354 SectionKind::getReadOnly(), /*C=*/nullptr,
355 Alignment);
356}
357
359 bool UsesLabelDifference, const Function &F) const {
360 // In PIC mode, we need to emit the jump table to the same section as the
361 // function body itself, otherwise the label differences won't make sense.
362 // FIXME: Need a better predicate for this: what about custom entries?
363 if (UsesLabelDifference)
364 return true;
365
366 // We should also do if the section name is NULL or function is declared
367 // in discardable section
368 // FIXME: this isn't the right predicate, should be based on the MCSection
369 // for the function.
370 return F.isWeakForLinker();
371}
372
373/// Given a mergable constant with the specified size and relocation
374/// information, return a section that it should be placed in.
376 const DataLayout &DL, SectionKind Kind, const Constant *C,
377 Align &Alignment) const {
378 if (Kind.isReadOnly() && ReadOnlySection != nullptr)
379 return ReadOnlySection;
380
381 return DataSection;
382}
383
385 const Function &F, const MachineBasicBlock &MBB,
386 const TargetMachine &TM) const {
387 return nullptr;
388}
389
391 const Function &F, const TargetMachine &TM) const {
392 return nullptr;
393}
394
395/// getTTypeGlobalReference - Return an MCExpr to use for a
396/// reference to the specified global variable from exception
397/// handling information.
399 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
400 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
401 const MCSymbolRefExpr *Ref =
403
404 return getTTypeReference(Ref, Encoding, Streamer);
405}
406
408getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
409 MCStreamer &Streamer) const {
410 switch (Encoding & 0x70) {
411 default:
412 report_fatal_error("We do not support this DWARF encoding yet!");
414 // Do nothing special
415 return Sym;
417 // Emit a label to the streamer for the current position. This gives us
418 // .-foo addressing.
420 Streamer.emitLabel(PCSym);
421 const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
423 }
424 }
425}
426
428 // FIXME: It's not clear what, if any, default this should have - perhaps a
429 // null return could mean 'no location' & we should just do that here.
431}
432
434 SmallVectorImpl<char> &OutName, const GlobalValue *GV,
435 const TargetMachine &TM) const {
436 Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
437}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file contains constants used for implementing Dwarf debug support.
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isNullOrUndef(const Constant *C)
static bool IsNullTerminatedString(const Constant *C)
IsNullTerminatedString - Return true if the specified constant (which is known to have a type that is...
static bool isSuitableForBSS(const GlobalVariable *GV)
Class to represent array types.
Definition: DerivedTypes.h:371
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:584
This is an important base class in LLVM.
Definition: Constant.h:42
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:289
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:110
bool isDeclarationForLinker() const
Definition: GlobalValue.h:618
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:124
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Class to represent integer types.
Definition: DerivedTypes.h:40
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:617
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:346
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel=false)
MCSection * ReadOnlySection
Section that is readonly and can contain arbitrary initialized data.
MCContext & getContext() const
MCSection * DataSection
Section directive for standard data.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
Streaming machine code generation interface.
Definition: MCStreamer.h:213
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:414
virtual void emitCGProfileEntry(const MCSymbolRefExpr *From, const MCSymbolRefExpr *To, uint64_t Count)
Definition: MCStreamer.cpp:813
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Metadata node.
Definition: Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1430
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1428
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
This class contains meta information specific to a module.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:120
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
static SectionKind getThreadData()
Definition: SectionKind.h:207
static SectionKind getBSSExtern()
Definition: SectionKind.h:211
static SectionKind getMergeable2ByteCString()
Definition: SectionKind.h:196
static SectionKind getExclude()
Definition: SectionKind.h:189
static SectionKind getBSSLocal()
Definition: SectionKind.h:210
static SectionKind getMergeableConst4()
Definition: SectionKind.h:202
static SectionKind getCommon()
Definition: SectionKind.h:212
static SectionKind getText()
Definition: SectionKind.h:190
static SectionKind getThreadBSSLocal()
Definition: SectionKind.h:208
static SectionKind getReadOnlyWithRel()
Definition: SectionKind.h:214
static SectionKind getData()
Definition: SectionKind.h:213
static SectionKind getMergeableConst8()
Definition: SectionKind.h:203
static SectionKind getBSS()
Definition: SectionKind.h:209
static SectionKind getThreadBSS()
Definition: SectionKind.h:206
static SectionKind getMergeableConst16()
Definition: SectionKind.h:204
static SectionKind getMergeable4ByteCString()
Definition: SectionKind.h:199
static SectionKind getMergeable1ByteCString()
Definition: SectionKind.h:193
static SectionKind getReadOnly()
Definition: SectionKind.h:192
static SectionKind getMergeableConst32()
Definition: SectionKind.h:205
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
iterator begin() const
Definition: StringRef.h:111
iterator end() const
Definition: StringRef.h:113
void emitCGProfileMetadata(MCStreamer &Streamer, Module &M) const
Emit Call Graph Profile metadata.
virtual void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, const TargetMachine &TM) const
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const
unsigned PersonalityEncoding
PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values for EH.
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
virtual MCSection * getSectionForJumpTable(const Function &F, const TargetMachine &TM) const
virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, const Function &F) const
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
virtual MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const =0
virtual MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const
Given a constant with the SectionKind, return a section that it should be placed in.
MCSymbol * getSymbolWithGlobalValueBase(const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const
Return the MCSymbol for a private symbol with global value name as its base, with the specified suffi...
virtual MCSection * getSectionForMachineBasicBlock(const Function &F, const MachineBasicBlock &MBB, const TargetMachine &TM) const
virtual const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const
Create a symbol reference to describe the given TLS variable when emitting the address in debug info.
virtual const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const
Return an MCExpr to use for a reference to the specified global variable from exception handling info...
virtual MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const =0
Targets should implement this method to assign a section to globals with an explicit section specfied...
const MCExpr * getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, MCStreamer &Streamer) const
virtual MCSection * getUniqueSectionForFunction(const Function &F, const TargetMachine &TM) const
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
bool isPositionIndependent() const
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
TargetOptions Options
MCSymbol * getSymbol(const GlobalValue *GV) const
CodeModel::Model getCodeModel() const
Returns the code model.
void getNameWithPrefix(SmallVectorImpl< char > &Name, const GlobalValue *GV, Mangler &Mang, bool MayAlwaysUsePrivate=false) const
unsigned NoZerosInBSS
NoZerosInBSS - By default some codegens place zero-initialized data to .bss section.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ ROPI_RWPI
Definition: CodeGen.h:25
@ DW_EH_PE_pcrel
Definition: Dwarf.h:851
@ DW_EH_PE_absptr
Definition: Dwarf.h:840
@ DW_EH_PE_udata4
Definition: Dwarf.h:844
@ DW_EH_PE_uleb128
Definition: Dwarf.h:842
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ Ref
The access may reference the value stored in memory.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39