LLVM: lib/IR/Globals.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
27using namespace llvm;
28
29
30
31
32
33
34
35
37 sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
38 "unexpected GlobalValue size growth");
39
40
42 "unexpected GlobalObject size growth");
43
46 return F->isMaterializable();
47 return false;
48}
50
51
52
53void GlobalValue::destroyConstantImpl() {
55}
56
57Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
58 llvm_unreachable("Unsupported class for handleOperandChange()!");
59}
60
61
62
70 if (Src->hasSanitizerMetadata())
72 else
74}
75
78 return MD5Hash(GlobalIdentifier);
79}
80
83#define HANDLE_GLOBAL_VALUE(NAME) \
84 case Value::NAME##Val: \
85 return static_cast<NAME *>(this)->removeFromParent();
86#include "llvm/IR/Value.def"
87 default:
88 break;
89 }
91}
92
95#define HANDLE_GLOBAL_VALUE(NAME) \
96 case Value::NAME##Val: \
97 return static_cast<NAME *>(this)->eraseFromParent();
98#include "llvm/IR/Value.def"
99 default:
100 break;
101 }
103}
104
106
113
116
117
118
119 return false;
120 }
121
122
123
124 auto isDeduplicateComdat = [](const Comdat *C) {
126 };
130}
131
135
138 "Alignment is greater than MaximumAlignment!");
143}
144
147 "Alignment is greater than MaximumAlignment!");
152 "Alignment representation error!");
153}
154
160
164
165
166
167 Name.consume_front("\1");
168
169 std::string GlobalName;
171
172
173
174
175 if (FileName.empty())
176 GlobalName += "";
177 else
178 GlobalName += FileName;
179
181 }
182 GlobalName += Name;
183 return GlobalName;
184}
185
188 getParent()->getSourceFileName());
189}
190
193
195 return GO->getSection();
196 return "";
197 }
199}
200
203
206 return nullptr;
207 }
208
210 return nullptr;
212}
213
218 if (C)
219 C->addUser(this);
220}
221
227
229
231 return;
232
233
234
238
239
240
242}
243
250
255
259 MetadataMap.erase(this);
261}
262
265 Meta.NoAddress = true;
266 Meta.NoHWAddress = true;
268}
269
270StringRef GlobalObject::getSectionImpl() const {
273}
274
276
278 return;
279
280
281
285
286
287
289}
290
293 if (std::optional MaybePrefix = getSectionPrefix())
294 ExistingPrefix = *MaybePrefix;
295
296 if (ExistingPrefix == Prefix)
297 return false;
298
299 if (Prefix.empty()) {
300 setMetadata(LLVMContext::MD_section_prefix, nullptr);
301 return true;
302 }
304 setMetadata(LLVMContext::MD_section_prefix,
306 return true;
307}
308
311 [[maybe_unused]] StringRef MDName =
313 assert((MDName == "section_prefix" ||
314 (isa(this) && MDName == "function_section_prefix")) &&
315 "Metadata not match");
316 return cast(MD->getOperand(1))->getString();
317 }
318 return std::nullopt;
319}
320
321bool GlobalValue::isNobuiltinFnDef() const {
323 if ( || F->empty())
324 return false;
325 return F->hasFnAttribute(Attribute::NoBuiltin);
326}
327
329
331 return GV->getNumOperands() == 0;
332
333
335 return F->empty() && ->isMaterializable();
336
337
339 return false;
340}
341
343
344
346 return false;
347
348
349
350
351
353 return false;
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375 bool isELF = ( || Parent->getTargetTriple().isOSBinFormatELF());
377 return false;
378
379
380
381
382 bool isXCOFF = ( || Parent->getTargetTriple().isOSBinFormatXCOFF());
383 if (isXCOFF)
385 if (GV->hasAttribute("toc-data"))
386 return false;
387
388 return true;
389}
390
391template
396 Op(*GO);
397 return GO;
398 }
400 Op(*GA);
401 if (Aliases.insert(GA).second)
403 }
405 switch (CE->getOpcode()) {
406 case Instruction::Add: {
410 return nullptr;
412 }
413 case Instruction::Sub: {
415 return nullptr;
417 }
418 case Instruction::IntToPtr:
419 case Instruction::PtrToAddr:
420 case Instruction::PtrToInt:
421 case Instruction::BitCast:
422 case Instruction::AddrSpaceCast:
423 case Instruction::GetElementPtr:
425 default:
426 break;
427 }
428 }
429 return nullptr;
430}
431
436
439 if (!GO)
440 return false;
441
442 return GO->getMetadata(LLVMContext::MD_absolute_symbol);
443}
444
447 if (!GO)
448 return std::nullopt;
449
450 MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
451 if (!MD)
452 return std::nullopt;
453
455}
456
459 return false;
460
461
462
464 return true;
465
466
467
469 if (!Var->isConstant())
470 return false;
471
473}
474
475
476
477
478
483 : GlobalObject(Ty, Value::GlobalVariableVal, AllocMarker, Link, Name,
485 isConstantGlobal(constant),
488 "invalid type for global variable");
490 if (InitVal) {
492 "Initializer should be the same type as the GlobalVariable!");
493 Op<0>() = InitVal;
494 } else {
495 setGlobalVariableNumOperands(0);
496 }
497}
498
505 : GlobalVariable(Ty, constant, Link, InitVal, Name, TLMode,
508 : M.getDataLayout().getDefaultGlobalsAddressSpace(),
510 if (Before)
512 else
513 M.insertGlobalVariable(this);
514}
515
519
523
525 if (!InitVal) {
527
528
529
530 Op<0>().set(nullptr);
531 setGlobalVariableNumOperands(0);
532 }
533 } else {
535 "Initializer type must match GlobalVariable type");
536
537
538
540 setGlobalVariableNumOperands(1);
541 Op<0>().set(InitVal);
542 }
543}
544
546 assert(InitVal && "Can't compute type of null initializer");
549}
550
551
552
557 if (auto CM = Src->getCodeModel())
559}
560
565
567 unsigned CodeModelData = static_cast<unsigned>(CM) + 1;
569 unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
570 (CodeModelData << CodeModelShift);
573}
574
576 unsigned CodeModelData = 0;
578 unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
579 (CodeModelData << CodeModelShift);
581 assert(getCodeModel() == std::nullopt && "Code model representation error!");
582}
583
584
585
586
587
588GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
590 Module *ParentModule)
591 : GlobalValue(Ty, Value::GlobalAliasVal, AllocMarker, Link, Name,
593 setAliasee(Aliasee);
594 if (ParentModule)
596}
597
601 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
602}
603
609
615
621
625
627
629
632 "Alias and aliasee types should match!");
633 Op<0>().set(Aliasee);
634}
635
640
641
642
643
644
645GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
647 Module *ParentModule)
648 : GlobalObject(Ty, Value::GlobalIFuncVal, AllocMarker, Link, Name,
651 if (ParentModule)
653}
654
660
662
664
668
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
GlobalValue::SanitizerMetadata SanitizerMetadata
Definition Globals.cpp:244
static const GlobalObject * findBaseObject(const Constant *C, DenseSet< const GlobalAlias * > &Aliases, const Operation &Op)
Definition Globals.cpp:393
Module.h This file contains the declarations for the Module class.
PowerPC Reduce CR logical Operation
@ NoDeduplicate
No deduplication is performed.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
bool erase(const KeyT &Val)
Implements a dense probed hash-table based set.
Lightweight error class with error context and mandatory checking.
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:628
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:636
LLVM_ABI void setAliasee(Constant *Aliasee)
These methods retrieve and set alias target.
Definition Globals.cpp:630
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:598
LLVM_ABI void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
Definition Globals.cpp:626
LLVM_ABI void applyAlongResolverPath(function_ref< void(const GlobalValue &)> Op) const
Definition Globals.cpp:669
LLVM_ABI const Function * getResolverFunction() const
Definition Globals.cpp:665
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing module, but does not delete it.
Definition Globals.cpp:661
static LLVM_ABI GlobalIFunc * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Resolver, Module *Parent)
If a parent module is specified, the ifunc is automatically inserted into the end of the specified mo...
Definition Globals.cpp:655
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:663
const Constant * getResolver() const
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
LLVM_ABI bool setSectionPrefix(StringRef Prefix)
If existing prefix is different from Prefix, set it to Prefix.
Definition Globals.cpp:291
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
LLVM_ABI void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition Globals.cpp:145
GlobalObject(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace=0)
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:214
LLVM_ABI void copyAttributesFrom(const GlobalObject *Src)
Definition Globals.cpp:155
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:275
LLVM_ABI ~GlobalObject()
Definition Globals.cpp:105
LLVM_ABI std::optional< StringRef > getSectionPrefix() const
Get the section prefix for this global object.
Definition Globals.cpp:309
LLVM_ABI void clearMetadata()
Erase all metadata attached to this Value.
bool hasSection() const
Check if this global has a custom object file section.
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
LLVM_ABI bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
Definition Globals.cpp:342
unsigned HasSanitizerMetadata
True if this symbol has sanitizer metadata available.
bool hasPartition() const
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:77
LLVM_ABI const SanitizerMetadata & getSanitizerMetadata() const
Definition Globals.cpp:245
unsigned HasPartition
True if this symbol has a partition name assigned (see https://lld.llvm.org/Partitions....
LLVM_ABI void removeSanitizerMetadata()
Definition Globals.cpp:256
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:328
LinkageTypes getLinkage() const
void setUnnamedAddr(UnnamedAddr Val)
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
bool hasDefaultVisibility() const
LLVM_ABI bool isAbsoluteSymbolRef() const
Returns whether this is a reference to an absolute symbol.
Definition Globals.cpp:437
void setDLLStorageClass(DLLStorageClassTypes C)
LLVM_ABI const Comdat * getComdat() const
Definition Globals.cpp:201
void setThreadLocalMode(ThreadLocalMode Val)
bool hasSanitizerMetadata() const
unsigned getAddressSpace() const
LLVM_ABI StringRef getSection() const
Definition Globals.cpp:191
LLVM_ABI StringRef getPartition() const
Definition Globals.cpp:222
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:432
void setDSOLocal(bool Local)
LLVM_ABI std::optional< ConstantRange > getAbsoluteSymbolRange() const
If this is an absolute symbol reference, returns the range of the symbol, otherwise returns std::null...
Definition Globals.cpp:445
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:93
static bool isExternalLinkage(LinkageTypes Linkage)
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
PointerType * getType() const
Global values are always pointers.
LLVM_ABI void copyAttributesFrom(const GlobalValue *Src)
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition Globals.cpp:63
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:161
LLVM_ABI void setNoSanitizeMetadata()
Definition Globals.cpp:263
LLVM_ABI bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Definition Globals.cpp:107
void setVisibility(VisibilityTypes V)
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:132
LLVM_ABI bool canBenefitFromLocalAlias() const
Definition Globals.cpp:114
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
bool hasAtLeastLocalUnnamedAddr() const
Returns true if this value's address is not significant in this module.
unsigned getGlobalValueSubClassData() const
void setGlobalValueSubClassData(unsigned V)
LLVM_ABI bool isMaterializable() const
If this function's Module is being lazily streamed in functions from disk or some other source,...
Definition Globals.cpp:44
bool hasGlobalUnnamedAddr() const
LLVM_ABI Error materialize()
Make sure this GlobalValue is fully read.
Definition Globals.cpp:49
LLVM_ABI void setSanitizerMetadata(SanitizerMetadata Meta)
Definition Globals.cpp:251
bool hasLinkOnceODRLinkage() const
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition Globals.cpp:457
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing module, but does not delete it.
Definition Globals.cpp:81
LinkageTypes
An enumeration for the kinds of linkage for global values.
Type * getValueType() const
GlobalValue(Type *Ty, ValueTy VTy, AllocInfo AllocInfo, LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace)
LLVM_ABI void setPartition(StringRef Part)
Definition Globals.cpp:228
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition Globals.cpp:524
bool isExternallyInitialized() const
bool hasInitializer() const
Definitions have initializers, declarations don't.
LLVM_ABI void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
Definition Globals.cpp:516
std::optional< CodeModel::Model > getCodeModel() const
Get the custom code model of this global if it has one.
void setAttributes(AttributeSet A)
Set attribute list for this global.
LLVM_ABI void replaceInitializer(Constant *InitVal)
replaceInitializer - Sets the initializer for this global variable, and sets the value type of the gl...
Definition Globals.cpp:545
LLVM_ABI void clearCodeModel()
Remove the code model for this global.
Definition Globals.cpp:575
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition Globals.cpp:553
LLVM_ABI void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition Globals.cpp:566
void setExternallyInitialized(bool Val)
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:520
LLVM_ABI void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
Definition Globals.cpp:561
LLVM_ABI GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage, Constant *Initializer=nullptr, const Twine &Name="", ThreadLocalMode=NotThreadLocal, unsigned AddressSpace=0, bool isExternallyInitialized=false)
GlobalVariable ctor - If a parent module is specified, the global is automatically inserted into the ...
Definition Globals.cpp:479
DenseMap< const GlobalValue *, StringRef > GlobalValuePartitions
Collection of per-GlobalValue partitions used in this context.
DenseMap< const GlobalValue *, GlobalValue::SanitizerMetadata > GlobalValueSanitizerMetadata
DenseMap< const GlobalObject *, StringRef > GlobalObjectSections
Collection of per-GlobalObject sections used in this context.
LLVMContextImpl *const pImpl
LLVM_ABI MDNode * createGlobalObjectSectionPrefix(StringRef Prefix)
Return metadata containing the section prefix for a global object.
A Module instance is used to store all the information related to an LLVM module.
void removeIFunc(GlobalIFunc *IFunc)
Detach IFunc from the list but don't delete it.
void insertIFunc(GlobalIFunc *IFunc)
Insert IFunc at the end of the alias list and take ownership.
llvm::Error materialize(GlobalValue *GV)
Make sure the GlobalValue is fully read.
bool getSemanticInterposition() const
Returns whether semantic interposition is to be respected.
void removeAlias(GlobalAlias *Alias)
Detach Alias from the list but don't delete it.
void eraseIFunc(GlobalIFunc *IFunc)
Remove IFunc from the list and delete it.
void eraseAlias(GlobalAlias *Alias)
Remove Alias from the list and delete it.
void eraseGlobalVariable(GlobalVariable *GV)
Remove global variable GV from the list and delete it.
void insertGlobalVariable(GlobalVariable *GV)
Insert global variable GV at the end of the global variable list and take ownership.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void insertAlias(GlobalAlias *Alias)
Insert Alias at the end of the alias list and take ownership.
void removeGlobalVariable(GlobalVariable *GV)
Detach global variable GV from the list but don't delete it.
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
StringRef save(const char *S)
void dropAllReferences()
Drop all references to operands.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
static constexpr uint64_t MaximumAlignment
LLVM_ABI const Value * stripPointerCastsAndAliases() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
unsigned getValueID() const
Return an ID for the concrete type of this object.
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
std::pair< iterator, bool > insert(const ValueT &V)
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
uint64_t MD5Hash(const FunctionId &Obj)
This is an optimization pass for GlobalISel generic memory operations.
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
constexpr char GlobalIdentifierDelimiter
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
This struct is a compact representation of a valid (non-zero power of two) alignment.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.