xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Globals.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
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 the GlobalValue & GlobalVariable classes for the IR
10 // library.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LLVMContextImpl.h"
15 #include "llvm/IR/ConstantRange.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/GlobalAlias.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MD5.h"
26 #include "llvm/TargetParser/Triple.h"
27 using namespace llvm;
28 
29 //===----------------------------------------------------------------------===//
30 //                            GlobalValue Class
31 //===----------------------------------------------------------------------===//
32 
33 // GlobalValue should be a Constant, plus a type, a module, some flags, and an
34 // intrinsic ID. Add an assert to prevent people from accidentally growing
35 // GlobalValue while adding flags.
36 static_assert(sizeof(GlobalValue) ==
37                   sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
38               "unexpected GlobalValue size growth");
39 
40 // GlobalObject adds a comdat.
41 static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
42               "unexpected GlobalObject size growth");
43 
isMaterializable() const44 bool GlobalValue::isMaterializable() const {
45   if (const Function *F = dyn_cast<Function>(this))
46     return F->isMaterializable();
47   return false;
48 }
materialize()49 Error GlobalValue::materialize() { return getParent()->materialize(this); }
50 
51 /// Override destroyConstantImpl to make sure it doesn't get called on
52 /// GlobalValue's because they shouldn't be treated like other constants.
destroyConstantImpl()53 void GlobalValue::destroyConstantImpl() {
54   llvm_unreachable("You can't GV->destroyConstantImpl()!");
55 }
56 
handleOperandChangeImpl(Value * From,Value * To)57 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) {
58   llvm_unreachable("Unsupported class for handleOperandChange()!");
59 }
60 
61 /// copyAttributesFrom - copy all additional attributes (those not needed to
62 /// create a GlobalValue) from the GlobalValue Src to this one.
copyAttributesFrom(const GlobalValue * Src)63 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
64   setVisibility(Src->getVisibility());
65   setUnnamedAddr(Src->getUnnamedAddr());
66   setThreadLocalMode(Src->getThreadLocalMode());
67   setDLLStorageClass(Src->getDLLStorageClass());
68   setDSOLocal(Src->isDSOLocal());
69   setPartition(Src->getPartition());
70   if (Src->hasSanitizerMetadata())
71     setSanitizerMetadata(Src->getSanitizerMetadata());
72   else
73     removeSanitizerMetadata();
74 }
75 
76 GlobalValue::GUID
getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier)77 GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) {
78   return MD5Hash(GlobalIdentifier);
79 }
80 
removeFromParent()81 void GlobalValue::removeFromParent() {
82   switch (getValueID()) {
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   }
90   llvm_unreachable("not a global");
91 }
92 
eraseFromParent()93 void GlobalValue::eraseFromParent() {
94   switch (getValueID()) {
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   }
102   llvm_unreachable("not a global");
103 }
104 
~GlobalObject()105 GlobalObject::~GlobalObject() { setComdat(nullptr); }
106 
isInterposable() const107 bool GlobalValue::isInterposable() const {
108   if (isInterposableLinkage(getLinkage()))
109     return true;
110   return getParent() && getParent()->getSemanticInterposition() &&
111          !isDSOLocal();
112 }
113 
canBenefitFromLocalAlias() const114 bool GlobalValue::canBenefitFromLocalAlias() const {
115   if (isTagged()) {
116     // Cannot create local aliases to MTE tagged globals. The address of a
117     // tagged global includes a tag that is assigned by the loader in the
118     // GOT.
119     return false;
120   }
121   // See AsmPrinter::getSymbolPreferLocal(). For a deduplicate comdat kind,
122   // references to a discarded local symbol from outside the group are not
123   // allowed, so avoid the local alias.
124   auto isDeduplicateComdat = [](const Comdat *C) {
125     return C && C->getSelectionKind() != Comdat::NoDeduplicate;
126   };
127   return hasDefaultVisibility() &&
128          GlobalObject::isExternalLinkage(getLinkage()) && !isDeclaration() &&
129          !isa<GlobalIFunc>(this) && !isDeduplicateComdat(getComdat());
130 }
131 
getDataLayout() const132 const DataLayout &GlobalValue::getDataLayout() const {
133   return getParent()->getDataLayout();
134 }
135 
setAlignment(MaybeAlign Align)136 void GlobalObject::setAlignment(MaybeAlign Align) {
137   assert((!Align || *Align <= MaximumAlignment) &&
138          "Alignment is greater than MaximumAlignment!");
139   unsigned AlignmentData = encode(Align);
140   unsigned OldData = getGlobalValueSubClassData();
141   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
142   assert(getAlign() == Align && "Alignment representation error!");
143 }
144 
setAlignment(Align Align)145 void GlobalObject::setAlignment(Align Align) {
146   assert(Align <= MaximumAlignment &&
147          "Alignment is greater than MaximumAlignment!");
148   unsigned AlignmentData = encode(Align);
149   unsigned OldData = getGlobalValueSubClassData();
150   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
151   assert(getAlign() && *getAlign() == Align &&
152          "Alignment representation error!");
153 }
154 
copyAttributesFrom(const GlobalObject * Src)155 void GlobalObject::copyAttributesFrom(const GlobalObject *Src) {
156   GlobalValue::copyAttributesFrom(Src);
157   setAlignment(Src->getAlign());
158   setSection(Src->getSection());
159 }
160 
getGlobalIdentifier(StringRef Name,GlobalValue::LinkageTypes Linkage,StringRef FileName)161 std::string GlobalValue::getGlobalIdentifier(StringRef Name,
162                                              GlobalValue::LinkageTypes Linkage,
163                                              StringRef FileName) {
164   // Value names may be prefixed with a binary '1' to indicate
165   // that the backend should not modify the symbols due to any platform
166   // naming convention. Do not include that '1' in the PGO profile name.
167   Name.consume_front("\1");
168 
169   std::string GlobalName;
170   if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
171     // For local symbols, prepend the main file name to distinguish them.
172     // Do not include the full path in the file name since there's no guarantee
173     // that it will stay the same, e.g., if the files are checked out from
174     // version control in different locations.
175     if (FileName.empty())
176       GlobalName += "<unknown>";
177     else
178       GlobalName += FileName;
179 
180     GlobalName += GlobalIdentifierDelimiter;
181   }
182   GlobalName += Name;
183   return GlobalName;
184 }
185 
getGlobalIdentifier() const186 std::string GlobalValue::getGlobalIdentifier() const {
187   return getGlobalIdentifier(getName(), getLinkage(),
188                              getParent()->getSourceFileName());
189 }
190 
getSection() const191 StringRef GlobalValue::getSection() const {
192   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
193     // In general we cannot compute this at the IR level, but we try.
194     if (const GlobalObject *GO = GA->getAliaseeObject())
195       return GO->getSection();
196     return "";
197   }
198   return cast<GlobalObject>(this)->getSection();
199 }
200 
getComdat() const201 const Comdat *GlobalValue::getComdat() const {
202   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
203     // In general we cannot compute this at the IR level, but we try.
204     if (const GlobalObject *GO = GA->getAliaseeObject())
205       return const_cast<GlobalObject *>(GO)->getComdat();
206     return nullptr;
207   }
208   // ifunc and its resolver are separate things so don't use resolver comdat.
209   if (isa<GlobalIFunc>(this))
210     return nullptr;
211   return cast<GlobalObject>(this)->getComdat();
212 }
213 
setComdat(Comdat * C)214 void GlobalObject::setComdat(Comdat *C) {
215   if (ObjComdat)
216     ObjComdat->removeUser(this);
217   ObjComdat = C;
218   if (C)
219     C->addUser(this);
220 }
221 
getPartition() const222 StringRef GlobalValue::getPartition() const {
223   if (!hasPartition())
224     return "";
225   return getContext().pImpl->GlobalValuePartitions[this];
226 }
227 
setPartition(StringRef S)228 void GlobalValue::setPartition(StringRef S) {
229   // Do nothing if we're clearing the partition and it is already empty.
230   if (!hasPartition() && S.empty())
231     return;
232 
233   // Get or create a stable partition name string and put it in the table in the
234   // context.
235   if (!S.empty())
236     S = getContext().pImpl->Saver.save(S);
237   getContext().pImpl->GlobalValuePartitions[this] = S;
238 
239   // Update the HasPartition field. Setting the partition to the empty string
240   // means this global no longer has a partition.
241   HasPartition = !S.empty();
242 }
243 
244 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
getSanitizerMetadata() const245 const SanitizerMetadata &GlobalValue::getSanitizerMetadata() const {
246   assert(hasSanitizerMetadata());
247   assert(getContext().pImpl->GlobalValueSanitizerMetadata.count(this));
248   return getContext().pImpl->GlobalValueSanitizerMetadata[this];
249 }
250 
setSanitizerMetadata(SanitizerMetadata Meta)251 void GlobalValue::setSanitizerMetadata(SanitizerMetadata Meta) {
252   getContext().pImpl->GlobalValueSanitizerMetadata[this] = Meta;
253   HasSanitizerMetadata = true;
254 }
255 
removeSanitizerMetadata()256 void GlobalValue::removeSanitizerMetadata() {
257   DenseMap<const GlobalValue *, SanitizerMetadata> &MetadataMap =
258       getContext().pImpl->GlobalValueSanitizerMetadata;
259   MetadataMap.erase(this);
260   HasSanitizerMetadata = false;
261 }
262 
setNoSanitizeMetadata()263 void GlobalValue::setNoSanitizeMetadata() {
264   SanitizerMetadata Meta;
265   Meta.NoAddress = true;
266   Meta.NoHWAddress = true;
267   setSanitizerMetadata(Meta);
268 }
269 
getSectionImpl() const270 StringRef GlobalObject::getSectionImpl() const {
271   assert(hasSection());
272   return getContext().pImpl->GlobalObjectSections[this];
273 }
274 
setSection(StringRef S)275 void GlobalObject::setSection(StringRef S) {
276   // Do nothing if we're clearing the section and it is already empty.
277   if (!hasSection() && S.empty())
278     return;
279 
280   // Get or create a stable section name string and put it in the table in the
281   // context.
282   if (!S.empty())
283     S = getContext().pImpl->Saver.save(S);
284   getContext().pImpl->GlobalObjectSections[this] = S;
285 
286   // Update the HasSectionHashEntryBit. Setting the section to the empty string
287   // means this global no longer has a section.
288   setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
289 }
290 
setSectionPrefix(StringRef Prefix)291 void GlobalObject::setSectionPrefix(StringRef Prefix) {
292   MDBuilder MDB(getContext());
293   setMetadata(LLVMContext::MD_section_prefix,
294               MDB.createGlobalObjectSectionPrefix(Prefix));
295 }
296 
getSectionPrefix() const297 std::optional<StringRef> GlobalObject::getSectionPrefix() const {
298   if (MDNode *MD = getMetadata(LLVMContext::MD_section_prefix)) {
299     [[maybe_unused]] StringRef MDName =
300         cast<MDString>(MD->getOperand(0))->getString();
301     assert((MDName == "section_prefix" ||
302             (isa<Function>(this) && MDName == "function_section_prefix")) &&
303            "Metadata not match");
304     return cast<MDString>(MD->getOperand(1))->getString();
305   }
306   return std::nullopt;
307 }
308 
isNobuiltinFnDef() const309 bool GlobalValue::isNobuiltinFnDef() const {
310   const Function *F = dyn_cast<Function>(this);
311   if (!F || F->empty())
312     return false;
313   return F->hasFnAttribute(Attribute::NoBuiltin);
314 }
315 
isDeclaration() const316 bool GlobalValue::isDeclaration() const {
317   // Globals are definitions if they have an initializer.
318   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
319     return GV->getNumOperands() == 0;
320 
321   // Functions are definitions if they have a body.
322   if (const Function *F = dyn_cast<Function>(this))
323     return F->empty() && !F->isMaterializable();
324 
325   // Aliases and ifuncs are always definitions.
326   assert(isa<GlobalAlias>(this) || isa<GlobalIFunc>(this));
327   return false;
328 }
329 
canIncreaseAlignment() const330 bool GlobalObject::canIncreaseAlignment() const {
331   // Firstly, can only increase the alignment of a global if it
332   // is a strong definition.
333   if (!isStrongDefinitionForLinker())
334     return false;
335 
336   // It also has to either not have a section defined, or, not have
337   // alignment specified. (If it is assigned a section, the global
338   // could be densely packed with other objects in the section, and
339   // increasing the alignment could cause padding issues.)
340   if (hasSection() && getAlign())
341     return false;
342 
343   // On ELF platforms, we're further restricted in that we can't
344   // increase the alignment of any variable which might be emitted
345   // into a shared library, and which is exported. If the main
346   // executable accesses a variable found in a shared-lib, the main
347   // exe actually allocates memory for and exports the symbol ITSELF,
348   // overriding the symbol found in the library. That is, at link
349   // time, the observed alignment of the variable is copied into the
350   // executable binary. (A COPY relocation is also generated, to copy
351   // the initial data from the shadowed variable in the shared-lib
352   // into the location in the main binary, before running code.)
353   //
354   // And thus, even though you might think you are defining the
355   // global, and allocating the memory for the global in your object
356   // file, and thus should be able to set the alignment arbitrarily,
357   // that's not actually true. Doing so can cause an ABI breakage; an
358   // executable might have already been built with the previous
359   // alignment of the variable, and then assuming an increased
360   // alignment will be incorrect.
361 
362   // Conservatively assume ELF if there's no parent pointer.
363   bool isELF = (!Parent || Parent->getTargetTriple().isOSBinFormatELF());
364   if (isELF && !isDSOLocal())
365     return false;
366 
367   // GV with toc-data attribute is defined in a TOC entry. To mitigate TOC
368   // overflow, the alignment of such symbol should not be increased. Otherwise,
369   // padding is needed thus more TOC entries are wasted.
370   bool isXCOFF = (!Parent || Parent->getTargetTriple().isOSBinFormatXCOFF());
371   if (isXCOFF)
372     if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
373       if (GV->hasAttribute("toc-data"))
374         return false;
375 
376   return true;
377 }
378 
379 template <typename Operation>
380 static const GlobalObject *
findBaseObject(const Constant * C,DenseSet<const GlobalAlias * > & Aliases,const Operation & Op)381 findBaseObject(const Constant *C, DenseSet<const GlobalAlias *> &Aliases,
382                const Operation &Op) {
383   if (auto *GO = dyn_cast<GlobalObject>(C)) {
384     Op(*GO);
385     return GO;
386   }
387   if (auto *GA = dyn_cast<GlobalAlias>(C)) {
388     Op(*GA);
389     if (Aliases.insert(GA).second)
390       return findBaseObject(GA->getOperand(0), Aliases, Op);
391   }
392   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
393     switch (CE->getOpcode()) {
394     case Instruction::Add: {
395       auto *LHS = findBaseObject(CE->getOperand(0), Aliases, Op);
396       auto *RHS = findBaseObject(CE->getOperand(1), Aliases, Op);
397       if (LHS && RHS)
398         return nullptr;
399       return LHS ? LHS : RHS;
400     }
401     case Instruction::Sub: {
402       if (findBaseObject(CE->getOperand(1), Aliases, Op))
403         return nullptr;
404       return findBaseObject(CE->getOperand(0), Aliases, Op);
405     }
406     case Instruction::IntToPtr:
407     case Instruction::PtrToInt:
408     case Instruction::BitCast:
409     case Instruction::GetElementPtr:
410       return findBaseObject(CE->getOperand(0), Aliases, Op);
411     default:
412       break;
413     }
414   }
415   return nullptr;
416 }
417 
getAliaseeObject() const418 const GlobalObject *GlobalValue::getAliaseeObject() const {
419   DenseSet<const GlobalAlias *> Aliases;
420   return findBaseObject(this, Aliases, [](const GlobalValue &) {});
421 }
422 
isAbsoluteSymbolRef() const423 bool GlobalValue::isAbsoluteSymbolRef() const {
424   auto *GO = dyn_cast<GlobalObject>(this);
425   if (!GO)
426     return false;
427 
428   return GO->getMetadata(LLVMContext::MD_absolute_symbol);
429 }
430 
getAbsoluteSymbolRange() const431 std::optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const {
432   auto *GO = dyn_cast<GlobalObject>(this);
433   if (!GO)
434     return std::nullopt;
435 
436   MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol);
437   if (!MD)
438     return std::nullopt;
439 
440   return getConstantRangeFromMetadata(*MD);
441 }
442 
canBeOmittedFromSymbolTable() const443 bool GlobalValue::canBeOmittedFromSymbolTable() const {
444   if (!hasLinkOnceODRLinkage())
445     return false;
446 
447   // We assume that anyone who sets global unnamed_addr on a non-constant
448   // knows what they're doing.
449   if (hasGlobalUnnamedAddr())
450     return true;
451 
452   // If it is a non constant variable, it needs to be uniqued across shared
453   // objects.
454   if (auto *Var = dyn_cast<GlobalVariable>(this))
455     if (!Var->isConstant())
456       return false;
457 
458   return hasAtLeastLocalUnnamedAddr();
459 }
460 
461 //===----------------------------------------------------------------------===//
462 // GlobalVariable Implementation
463 //===----------------------------------------------------------------------===//
464 
GlobalVariable(Type * Ty,bool constant,LinkageTypes Link,Constant * InitVal,const Twine & Name,ThreadLocalMode TLMode,unsigned AddressSpace,bool isExternallyInitialized)465 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
466                                Constant *InitVal, const Twine &Name,
467                                ThreadLocalMode TLMode, unsigned AddressSpace,
468                                bool isExternallyInitialized)
469     : GlobalObject(Ty, Value::GlobalVariableVal, AllocMarker, Link, Name,
470                    AddressSpace),
471       isConstantGlobal(constant),
472       isExternallyInitializedConstant(isExternallyInitialized) {
473   assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) &&
474          "invalid type for global variable");
475   setThreadLocalMode(TLMode);
476   if (InitVal) {
477     assert(InitVal->getType() == Ty &&
478            "Initializer should be the same type as the GlobalVariable!");
479     Op<0>() = InitVal;
480   } else {
481     setGlobalVariableNumOperands(0);
482   }
483 }
484 
GlobalVariable(Module & M,Type * Ty,bool constant,LinkageTypes Link,Constant * InitVal,const Twine & Name,GlobalVariable * Before,ThreadLocalMode TLMode,std::optional<unsigned> AddressSpace,bool isExternallyInitialized)485 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
486                                LinkageTypes Link, Constant *InitVal,
487                                const Twine &Name, GlobalVariable *Before,
488                                ThreadLocalMode TLMode,
489                                std::optional<unsigned> AddressSpace,
490                                bool isExternallyInitialized)
491     : GlobalVariable(Ty, constant, Link, InitVal, Name, TLMode,
492                      AddressSpace
493                          ? *AddressSpace
494                          : M.getDataLayout().getDefaultGlobalsAddressSpace(),
495                      isExternallyInitialized) {
496   if (Before)
497     Before->getParent()->insertGlobalVariable(Before->getIterator(), this);
498   else
499     M.insertGlobalVariable(this);
500 }
501 
removeFromParent()502 void GlobalVariable::removeFromParent() {
503   getParent()->removeGlobalVariable(this);
504 }
505 
eraseFromParent()506 void GlobalVariable::eraseFromParent() {
507   getParent()->eraseGlobalVariable(this);
508 }
509 
setInitializer(Constant * InitVal)510 void GlobalVariable::setInitializer(Constant *InitVal) {
511   if (!InitVal) {
512     if (hasInitializer()) {
513       // Note, the num operands is used to compute the offset of the operand, so
514       // the order here matters.  Clearing the operand then clearing the num
515       // operands ensures we have the correct offset to the operand.
516       Op<0>().set(nullptr);
517       setGlobalVariableNumOperands(0);
518     }
519   } else {
520     assert(InitVal->getType() == getValueType() &&
521            "Initializer type must match GlobalVariable type");
522     // Note, the num operands is used to compute the offset of the operand, so
523     // the order here matters.  We need to set num operands to 1 first so that
524     // we get the correct offset to the first operand when we set it.
525     if (!hasInitializer())
526       setGlobalVariableNumOperands(1);
527     Op<0>().set(InitVal);
528   }
529 }
530 
replaceInitializer(Constant * InitVal)531 void GlobalVariable::replaceInitializer(Constant *InitVal) {
532   assert(InitVal && "Can't compute type of null initializer");
533   ValueType = InitVal->getType();
534   setInitializer(InitVal);
535 }
536 
537 /// Copy all additional attributes (those not needed to create a GlobalVariable)
538 /// from the GlobalVariable Src to this one.
copyAttributesFrom(const GlobalVariable * Src)539 void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) {
540   GlobalObject::copyAttributesFrom(Src);
541   setExternallyInitialized(Src->isExternallyInitialized());
542   setAttributes(Src->getAttributes());
543   if (auto CM = Src->getCodeModel())
544     setCodeModel(*CM);
545 }
546 
dropAllReferences()547 void GlobalVariable::dropAllReferences() {
548   User::dropAllReferences();
549   clearMetadata();
550 }
551 
setCodeModel(CodeModel::Model CM)552 void GlobalVariable::setCodeModel(CodeModel::Model CM) {
553   unsigned CodeModelData = static_cast<unsigned>(CM) + 1;
554   unsigned OldData = getGlobalValueSubClassData();
555   unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
556                      (CodeModelData << CodeModelShift);
557   setGlobalValueSubClassData(NewData);
558   assert(getCodeModel() == CM && "Code model representation error!");
559 }
560 
clearCodeModel()561 void GlobalVariable::clearCodeModel() {
562   unsigned CodeModelData = 0;
563   unsigned OldData = getGlobalValueSubClassData();
564   unsigned NewData = (OldData & ~(CodeModelMask << CodeModelShift)) |
565                      (CodeModelData << CodeModelShift);
566   setGlobalValueSubClassData(NewData);
567   assert(getCodeModel() == std::nullopt && "Code model representation error!");
568 }
569 
570 //===----------------------------------------------------------------------===//
571 // GlobalAlias Implementation
572 //===----------------------------------------------------------------------===//
573 
GlobalAlias(Type * Ty,unsigned AddressSpace,LinkageTypes Link,const Twine & Name,Constant * Aliasee,Module * ParentModule)574 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
575                          const Twine &Name, Constant *Aliasee,
576                          Module *ParentModule)
577     : GlobalValue(Ty, Value::GlobalAliasVal, AllocMarker, Link, Name,
578                   AddressSpace) {
579   setAliasee(Aliasee);
580   if (ParentModule)
581     ParentModule->insertAlias(this);
582 }
583 
create(Type * Ty,unsigned AddressSpace,LinkageTypes Link,const Twine & Name,Constant * Aliasee,Module * ParentModule)584 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
585                                  LinkageTypes Link, const Twine &Name,
586                                  Constant *Aliasee, Module *ParentModule) {
587   return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule);
588 }
589 
create(Type * Ty,unsigned AddressSpace,LinkageTypes Linkage,const Twine & Name,Module * Parent)590 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
591                                  LinkageTypes Linkage, const Twine &Name,
592                                  Module *Parent) {
593   return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent);
594 }
595 
create(Type * Ty,unsigned AddressSpace,LinkageTypes Linkage,const Twine & Name,GlobalValue * Aliasee)596 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
597                                  LinkageTypes Linkage, const Twine &Name,
598                                  GlobalValue *Aliasee) {
599   return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent());
600 }
601 
create(LinkageTypes Link,const Twine & Name,GlobalValue * Aliasee)602 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
603                                  GlobalValue *Aliasee) {
604   return create(Aliasee->getValueType(), Aliasee->getAddressSpace(), Link, Name,
605                 Aliasee);
606 }
607 
create(const Twine & Name,GlobalValue * Aliasee)608 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
609   return create(Aliasee->getLinkage(), Name, Aliasee);
610 }
611 
removeFromParent()612 void GlobalAlias::removeFromParent() { getParent()->removeAlias(this); }
613 
eraseFromParent()614 void GlobalAlias::eraseFromParent() { getParent()->eraseAlias(this); }
615 
setAliasee(Constant * Aliasee)616 void GlobalAlias::setAliasee(Constant *Aliasee) {
617   assert((!Aliasee || Aliasee->getType() == getType()) &&
618          "Alias and aliasee types should match!");
619   Op<0>().set(Aliasee);
620 }
621 
getAliaseeObject() const622 const GlobalObject *GlobalAlias::getAliaseeObject() const {
623   DenseSet<const GlobalAlias *> Aliases;
624   return findBaseObject(getOperand(0), Aliases, [](const GlobalValue &) {});
625 }
626 
627 //===----------------------------------------------------------------------===//
628 // GlobalIFunc Implementation
629 //===----------------------------------------------------------------------===//
630 
GlobalIFunc(Type * Ty,unsigned AddressSpace,LinkageTypes Link,const Twine & Name,Constant * Resolver,Module * ParentModule)631 GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
632                          const Twine &Name, Constant *Resolver,
633                          Module *ParentModule)
634     : GlobalObject(Ty, Value::GlobalIFuncVal, AllocMarker, Link, Name,
635                    AddressSpace) {
636   setResolver(Resolver);
637   if (ParentModule)
638     ParentModule->insertIFunc(this);
639 }
640 
create(Type * Ty,unsigned AddressSpace,LinkageTypes Link,const Twine & Name,Constant * Resolver,Module * ParentModule)641 GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace,
642                                  LinkageTypes Link, const Twine &Name,
643                                  Constant *Resolver, Module *ParentModule) {
644   return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule);
645 }
646 
removeFromParent()647 void GlobalIFunc::removeFromParent() { getParent()->removeIFunc(this); }
648 
eraseFromParent()649 void GlobalIFunc::eraseFromParent() { getParent()->eraseIFunc(this); }
650 
getResolverFunction() const651 const Function *GlobalIFunc::getResolverFunction() const {
652   return dyn_cast<Function>(getResolver()->stripPointerCastsAndAliases());
653 }
654 
applyAlongResolverPath(function_ref<void (const GlobalValue &)> Op) const655 void GlobalIFunc::applyAlongResolverPath(
656     function_ref<void(const GlobalValue &)> Op) const {
657   DenseSet<const GlobalAlias *> Aliases;
658   findBaseObject(getResolver(), Aliases, Op);
659 }
660