LLVM: lib/IR/Globals.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
26using namespace llvm;
27
28
29
30
31
32
33
34
36 sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
37 "unexpected GlobalValue size growth");
38
39
41 "unexpected GlobalObject size growth");
42
44 if (const Function *F = dyn_cast(this))
45 return F->isMaterializable();
46 return false;
47}
49
50
51
52void GlobalValue::destroyConstantImpl() {
54}
55
57 llvm_unreachable("Unsupported class for handleOperandChange()!");
58}
59
60
61
69 if (Src->hasSanitizerMetadata())
71 else
73}
74
76 return MD5Hash(GlobalName);
77}
78
81#define HANDLE_GLOBAL_VALUE(NAME) \
82 case Value::NAME##Val: \
83 return static_cast<NAME *>(this)->removeFromParent();
84#include "llvm/IR/Value.def"
85 default:
86 break;
87 }
89}
90
93#define HANDLE_GLOBAL_VALUE(NAME) \
94 case Value::NAME##Val: \
95 return static_cast<NAME *>(this)->eraseFromParent();
96#include "llvm/IR/Value.def"
97 default:
98 break;
99 }
101}
102
104
107 return true;
110}
111
114
115
116
117 return false;
118 }
119
120
121
122 auto isDeduplicateComdat = [](const Comdat *C) {
124 };
127 !isa(this) && !isDeduplicateComdat(getComdat());
128}
129
132}
133
136 "Alignment is greater than MaximumAlignment!");
141}
142
145 "Alignment is greater than MaximumAlignment!");
150 "Alignment representation error!");
151}
152
157}
158
162
163
164
165 Name.consume_front("\1");
166
167 std::string GlobalName;
169
170
171
172
173 if (FileName.empty())
174 GlobalName += "";
175 else
176 GlobalName += FileName;
177
179 }
180 GlobalName += Name;
181 return GlobalName;
182}
183
186 getParent()->getSourceFileName());
187}
188
190 if (auto *GA = dyn_cast(this)) {
191
193 return GO->getSection();
194 return "";
195 }
196 return cast(this)->getSection();
197}
198
200 if (auto *GA = dyn_cast(this)) {
201
204 return nullptr;
205 }
206
207 if (isa(this))
208 return nullptr;
209 return cast(this)->getComdat();
210}
211
216 if (C)
217 C->addUser(this);
218}
219
222 return "";
224}
225
227
229 return;
230
231
232
236
237
238
240}
241
245 assert(getContext().pImpl->GlobalValueSanitizerMetadata.count(this));
247}
248
252}
253
257 MetadataMap.erase(this);
259}
260
263 Meta.NoAddress = true;
264 Meta.NoHWAddress = true;
266}
267
268StringRef GlobalObject::getSectionImpl() const {
271}
272
274
276 return;
277
278
279
283
284
285
287}
288
289bool GlobalValue::isNobuiltinFnDef() const {
290 const Function *F = dyn_cast(this);
291 if ( || F->empty())
292 return false;
293 return F->hasFnAttribute(Attribute::NoBuiltin);
294}
295
297
298 if (const GlobalVariable *GV = dyn_cast(this))
299 return GV->getNumOperands() == 0;
300
301
302 if (const Function *F = dyn_cast(this))
303 return F->empty() && ->isMaterializable();
304
305
306 assert(isa(this) || isa(this));
307 return false;
308}
309
311
312
314 return false;
315
316
317
318
319
321 return false;
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 bool isELF =
346 return false;
347
348
349
350
351 bool isXCOFF =
353 if (isXCOFF)
354 if (const GlobalVariable *GV = dyn_cast(this))
355 if (GV->hasAttribute("toc-data"))
356 return false;
357
358 return true;
359}
360
361template
365 if (auto *GO = dyn_cast(C)) {
366 Op(*GO);
367 return GO;
368 }
369 if (auto *GA = dyn_cast(C)) {
370 Op(*GA);
371 if (Aliases.insert(GA).second)
373 }
374 if (auto *CE = dyn_cast(C)) {
375 switch (CE->getOpcode()) {
376 case Instruction::Add: {
380 return nullptr;
382 }
383 case Instruction::Sub: {
385 return nullptr;
387 }
388 case Instruction::IntToPtr:
389 case Instruction::PtrToInt:
390 case Instruction::BitCast:
391 case Instruction::GetElementPtr:
393 default:
394 break;
395 }
396 }
397 return nullptr;
398}
399
403}
404
406 auto *GO = dyn_cast(this);
407 if (!GO)
408 return false;
409
410 return GO->getMetadata(LLVMContext::MD_absolute_symbol);
411}
412
414 auto *GO = dyn_cast(this);
415 if (!GO)
416 return std::nullopt;
417
418 MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
419 if (!MD)
420 return std::nullopt;
421
423}
424
427 return false;
428
429
430
432 return true;
433
434
435
436 if (auto *Var = dyn_cast(this))
437 if (!Var->isConstant())
438 return false;
439
441}
442
443
444
445
446
450 bool isExternallyInitialized)
453 isConstantGlobal(constant),
454 isExternallyInitializedConstant(isExternallyInitialized) {
456 "invalid type for global variable");
458 if (InitVal) {
460 "Initializer should be the same type as the GlobalVariable!");
461 Op<0>() = InitVal;
462 } else {
463 setGlobalVariableNumOperands(0);
464 }
465}
466
472 bool isExternallyInitialized)
476 : M.getDataLayout().getDefaultGlobalsAddressSpace(),
477 isExternallyInitialized) {
479 Before->getParent()->insertGlobalVariable(Before->getIterator(), this);
480 else
481 M.insertGlobalVariable(this);
482}
483
486}
487
490}
491
493 if (!InitVal) {
495
496
497
498 Op<0>().set(nullptr);
499 setGlobalVariableNumOperands(0);
500 }
501 } else {
503 "Initializer type must match GlobalVariable type");
504
505
506
508 setGlobalVariableNumOperands(1);
509 Op<0>().set(InitVal);
510 }
511}
512
514 assert(InitVal && "Can't compute type of null initializer");
517}
518
519
520
525 if (auto CM = Src->getCodeModel())
527}
528
532}
533
535 unsigned CodeModelData = static_cast<unsigned>(CM) + 1;
537 unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
538 (CodeModelData << CodeModelShift);
541}
542
543
544
545
546
547GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
549 Module *ParentModule)
552 setAliasee(Aliasee);
553 if (ParentModule)
555}
556
561}
562
567}
568
573}
574
578 Aliasee);
579}
580
583}
584
586
588
591 "Alias and aliasee types should match!");
592 Op<0>().set(Aliasee);
593}
594
598}
599
600
601
602
603
604GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
606 Module *ParentModule)
610 if (ParentModule)
612}
613
618}
619
621
623
626}
627
632}
BlockVerifier::State From
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static const GlobalObject * findBaseObject(const Constant *C, DenseSet< const GlobalAlias * > &Aliases, const Operation &Op)
Module.h This file contains the declarations for the Module class.
PowerPC Reduce CR logical Operation
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
@ NoDeduplicate
No deduplication is performed.
This is an important base class in LLVM.
This class represents an Operation in the Expression.
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.
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
const GlobalObject * getAliaseeObject() const
void setAliasee(Constant *Aliasee)
These methods retrieve and set alias target.
static 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...
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
void applyAlongResolverPath(function_ref< void(const GlobalValue &)> Op) const
const Function * getResolverFunction() const
void removeFromParent()
This method unlinks 'this' from the containing module, but does not delete it.
static 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...
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
const Constant * getResolver() const
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
void setComdat(Comdat *C)
void copyAttributesFrom(const GlobalObject *Src)
void setSection(StringRef S)
Change the section for this global.
void clearMetadata()
Erase all metadata attached to this Value.
bool hasSection() const
Check if this global has a custom object file section.
bool canIncreaseAlignment() const
Returns true if the alignment of the value can be unilaterally increased.
unsigned HasSanitizerMetadata
True if this symbol has sanitizer metadata available.
bool hasPartition() const
const SanitizerMetadata & getSanitizerMetadata() const
unsigned HasPartition
True if this symbol has a partition name assigned (see https://lld.llvm.org/Partitions....
void removeSanitizerMetadata()
static bool isLocalLinkage(LinkageTypes Linkage)
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
void setUnnamedAddr(UnnamedAddr Val)
bool hasDefaultVisibility() const
bool isAbsoluteSymbolRef() const
Returns whether this is a reference to an absolute symbol.
void setDLLStorageClass(DLLStorageClassTypes C)
const Comdat * getComdat() const
void setThreadLocalMode(ThreadLocalMode Val)
bool hasSanitizerMetadata() const
unsigned getAddressSpace() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
StringRef getSection() const
StringRef getPartition() const
Module * getParent()
Get the module that this global value is contained inside of...
const GlobalObject * getAliaseeObject() const
void setDSOLocal(bool Local)
std::optional< ConstantRange > getAbsoluteSymbolRange() const
If this is an absolute symbol reference, returns the range of the symbol, otherwise returns std::null...
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
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.
void copyAttributesFrom(const GlobalValue *Src)
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
void setNoSanitizeMetadata()
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
void setVisibility(VisibilityTypes V)
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
bool canBenefitFromLocalAlias() const
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)
bool isMaterializable() const
If this function's Module is being lazily streamed in functions from disk or some other source,...
bool hasGlobalUnnamedAddr() const
Error materialize()
Make sure this GlobalValue is fully read.
void setSanitizerMetadata(SanitizerMetadata Meta)
bool hasLinkOnceODRLinkage() const
bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
void removeFromParent()
This method unlinks 'this' from the containing module, but does not delete it.
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
LinkageTypes
An enumeration for the kinds of linkage for global values.
Type * getValueType() const
void setPartition(StringRef Part)
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
bool hasInitializer() const
Definitions have initializers, declarations don't.
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
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.
void replaceInitializer(Constant *InitVal)
replaceInitializer - Sets the initializer for this global variable, and sets the value type of the gl...
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
void setExternallyInitialized(bool Val)
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
void dropAllReferences()
Drop all references in preparation to destroy the GlobalVariable.
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 ...
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
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.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
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.
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 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.
Triple - Helper class for working with autoconf configuration names.
bool isOSBinFormatXCOFF() const
Tests whether the OS uses the XCOFF binary format.
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
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.
bool isFunctionTy() const
True if this is an instance of FunctionType.
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
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.
LLVMContext & getContext() const
All values hold a context through their type.
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.
#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.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
DWARFExpression::Operation Op
constexpr char GlobalIdentifierDelimiter
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.