10b57cec5SDimitry Andric //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the GlobalValue & GlobalVariable classes for the IR 100b57cec5SDimitry Andric // library. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "LLVMContextImpl.h" 150b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 160b57cec5SDimitry Andric #include "llvm/IR/ConstantRange.h" 170b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 180b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 190b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 200b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 210b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 220b57cec5SDimitry Andric #include "llvm/IR/Module.h" 230b57cec5SDimitry Andric #include "llvm/Support/Error.h" 240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 250b57cec5SDimitry Andric using namespace llvm; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 280b57cec5SDimitry Andric // GlobalValue Class 290b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric // GlobalValue should be a Constant, plus a type, a module, some flags, and an 320b57cec5SDimitry Andric // intrinsic ID. Add an assert to prevent people from accidentally growing 330b57cec5SDimitry Andric // GlobalValue while adding flags. 340b57cec5SDimitry Andric static_assert(sizeof(GlobalValue) == 350b57cec5SDimitry Andric sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned), 360b57cec5SDimitry Andric "unexpected GlobalValue size growth"); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // GlobalObject adds a comdat. 390b57cec5SDimitry Andric static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *), 400b57cec5SDimitry Andric "unexpected GlobalObject size growth"); 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric bool GlobalValue::isMaterializable() const { 430b57cec5SDimitry Andric if (const Function *F = dyn_cast<Function>(this)) 440b57cec5SDimitry Andric return F->isMaterializable(); 450b57cec5SDimitry Andric return false; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric Error GlobalValue::materialize() { 480b57cec5SDimitry Andric return getParent()->materialize(this); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /// Override destroyConstantImpl to make sure it doesn't get called on 520b57cec5SDimitry Andric /// GlobalValue's because they shouldn't be treated like other constants. 530b57cec5SDimitry Andric void GlobalValue::destroyConstantImpl() { 540b57cec5SDimitry Andric llvm_unreachable("You can't GV->destroyConstantImpl()!"); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) { 580b57cec5SDimitry Andric llvm_unreachable("Unsupported class for handleOperandChange()!"); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric /// copyAttributesFrom - copy all additional attributes (those not needed to 620b57cec5SDimitry Andric /// create a GlobalValue) from the GlobalValue Src to this one. 630b57cec5SDimitry Andric void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 640b57cec5SDimitry Andric setVisibility(Src->getVisibility()); 650b57cec5SDimitry Andric setUnnamedAddr(Src->getUnnamedAddr()); 665ffd83dbSDimitry Andric setThreadLocalMode(Src->getThreadLocalMode()); 670b57cec5SDimitry Andric setDLLStorageClass(Src->getDLLStorageClass()); 680b57cec5SDimitry Andric setDSOLocal(Src->isDSOLocal()); 690b57cec5SDimitry Andric setPartition(Src->getPartition()); 70*81ad6265SDimitry Andric if (Src->hasSanitizerMetadata()) 71*81ad6265SDimitry Andric setSanitizerMetadata(Src->getSanitizerMetadata()); 72*81ad6265SDimitry Andric else 73*81ad6265SDimitry Andric removeSanitizerMetadata(); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric void GlobalValue::removeFromParent() { 770b57cec5SDimitry Andric switch (getValueID()) { 780b57cec5SDimitry Andric #define HANDLE_GLOBAL_VALUE(NAME) \ 790b57cec5SDimitry Andric case Value::NAME##Val: \ 800b57cec5SDimitry Andric return static_cast<NAME *>(this)->removeFromParent(); 810b57cec5SDimitry Andric #include "llvm/IR/Value.def" 820b57cec5SDimitry Andric default: 830b57cec5SDimitry Andric break; 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric llvm_unreachable("not a global"); 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric void GlobalValue::eraseFromParent() { 890b57cec5SDimitry Andric switch (getValueID()) { 900b57cec5SDimitry Andric #define HANDLE_GLOBAL_VALUE(NAME) \ 910b57cec5SDimitry Andric case Value::NAME##Val: \ 920b57cec5SDimitry Andric return static_cast<NAME *>(this)->eraseFromParent(); 930b57cec5SDimitry Andric #include "llvm/IR/Value.def" 940b57cec5SDimitry Andric default: 950b57cec5SDimitry Andric break; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric llvm_unreachable("not a global"); 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 10004eeddc0SDimitry Andric GlobalObject::~GlobalObject() { setComdat(nullptr); } 10104eeddc0SDimitry Andric 1025ffd83dbSDimitry Andric bool GlobalValue::isInterposable() const { 1035ffd83dbSDimitry Andric if (isInterposableLinkage(getLinkage())) 1045ffd83dbSDimitry Andric return true; 1055ffd83dbSDimitry Andric return getParent() && getParent()->getSemanticInterposition() && 1065ffd83dbSDimitry Andric !isDSOLocal(); 1070b57cec5SDimitry Andric } 1085ffd83dbSDimitry Andric 1095ffd83dbSDimitry Andric bool GlobalValue::canBenefitFromLocalAlias() const { 11004eeddc0SDimitry Andric // See AsmPrinter::getSymbolPreferLocal(). For a deduplicate comdat kind, 11104eeddc0SDimitry Andric // references to a discarded local symbol from outside the group are not 11204eeddc0SDimitry Andric // allowed, so avoid the local alias. 11304eeddc0SDimitry Andric auto isDeduplicateComdat = [](const Comdat *C) { 11404eeddc0SDimitry Andric return C && C->getSelectionKind() != Comdat::NoDeduplicate; 11504eeddc0SDimitry Andric }; 1168833aad7SDimitry Andric return hasDefaultVisibility() && 1178833aad7SDimitry Andric GlobalObject::isExternalLinkage(getLinkage()) && !isDeclaration() && 11804eeddc0SDimitry Andric !isa<GlobalIFunc>(this) && !isDeduplicateComdat(getComdat()); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric unsigned GlobalValue::getAddressSpace() const { 1220b57cec5SDimitry Andric PointerType *PtrTy = getType(); 1230b57cec5SDimitry Andric return PtrTy->getAddressSpace(); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1268bcb0991SDimitry Andric void GlobalObject::setAlignment(MaybeAlign Align) { 1275ffd83dbSDimitry Andric assert((!Align || *Align <= MaximumAlignment) && 1280b57cec5SDimitry Andric "Alignment is greater than MaximumAlignment!"); 1298bcb0991SDimitry Andric unsigned AlignmentData = encode(Align); 1300b57cec5SDimitry Andric unsigned OldData = getGlobalValueSubClassData(); 1310b57cec5SDimitry Andric setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); 1328bcb0991SDimitry Andric assert(MaybeAlign(getAlignment()) == Align && 1338bcb0991SDimitry Andric "Alignment representation error!"); 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric void GlobalObject::copyAttributesFrom(const GlobalObject *Src) { 1370b57cec5SDimitry Andric GlobalValue::copyAttributesFrom(Src); 1380eae32dcSDimitry Andric setAlignment(Src->getAlign()); 1390b57cec5SDimitry Andric setSection(Src->getSection()); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric std::string GlobalValue::getGlobalIdentifier(StringRef Name, 1430b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage, 1440b57cec5SDimitry Andric StringRef FileName) { 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Value names may be prefixed with a binary '1' to indicate 1470b57cec5SDimitry Andric // that the backend should not modify the symbols due to any platform 1480b57cec5SDimitry Andric // naming convention. Do not include that '1' in the PGO profile name. 1490b57cec5SDimitry Andric if (Name[0] == '\1') 1500b57cec5SDimitry Andric Name = Name.substr(1); 1510b57cec5SDimitry Andric 1525ffd83dbSDimitry Andric std::string NewName = std::string(Name); 1530b57cec5SDimitry Andric if (llvm::GlobalValue::isLocalLinkage(Linkage)) { 1540b57cec5SDimitry Andric // For local symbols, prepend the main file name to distinguish them. 1550b57cec5SDimitry Andric // Do not include the full path in the file name since there's no guarantee 1560b57cec5SDimitry Andric // that it will stay the same, e.g., if the files are checked out from 1570b57cec5SDimitry Andric // version control in different locations. 1580b57cec5SDimitry Andric if (FileName.empty()) 1590b57cec5SDimitry Andric NewName = NewName.insert(0, "<unknown>:"); 1600b57cec5SDimitry Andric else 1610b57cec5SDimitry Andric NewName = NewName.insert(0, FileName.str() + ":"); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric return NewName; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric std::string GlobalValue::getGlobalIdentifier() const { 1670b57cec5SDimitry Andric return getGlobalIdentifier(getName(), getLinkage(), 1680b57cec5SDimitry Andric getParent()->getSourceFileName()); 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric StringRef GlobalValue::getSection() const { 1720b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(this)) { 1730b57cec5SDimitry Andric // In general we cannot compute this at the IR level, but we try. 174349cc55cSDimitry Andric if (const GlobalObject *GO = GA->getAliaseeObject()) 1750b57cec5SDimitry Andric return GO->getSection(); 1760b57cec5SDimitry Andric return ""; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric return cast<GlobalObject>(this)->getSection(); 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric const Comdat *GlobalValue::getComdat() const { 1820b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(this)) { 1830b57cec5SDimitry Andric // In general we cannot compute this at the IR level, but we try. 184349cc55cSDimitry Andric if (const GlobalObject *GO = GA->getAliaseeObject()) 1850b57cec5SDimitry Andric return const_cast<GlobalObject *>(GO)->getComdat(); 1860b57cec5SDimitry Andric return nullptr; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric // ifunc and its resolver are separate things so don't use resolver comdat. 1890b57cec5SDimitry Andric if (isa<GlobalIFunc>(this)) 1900b57cec5SDimitry Andric return nullptr; 1910b57cec5SDimitry Andric return cast<GlobalObject>(this)->getComdat(); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 19404eeddc0SDimitry Andric void GlobalObject::setComdat(Comdat *C) { 19504eeddc0SDimitry Andric if (ObjComdat) 19604eeddc0SDimitry Andric ObjComdat->removeUser(this); 19704eeddc0SDimitry Andric ObjComdat = C; 19804eeddc0SDimitry Andric if (C) 19904eeddc0SDimitry Andric C->addUser(this); 20004eeddc0SDimitry Andric } 20104eeddc0SDimitry Andric 2020b57cec5SDimitry Andric StringRef GlobalValue::getPartition() const { 2030b57cec5SDimitry Andric if (!hasPartition()) 2040b57cec5SDimitry Andric return ""; 2050b57cec5SDimitry Andric return getContext().pImpl->GlobalValuePartitions[this]; 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric void GlobalValue::setPartition(StringRef S) { 2090b57cec5SDimitry Andric // Do nothing if we're clearing the partition and it is already empty. 2100b57cec5SDimitry Andric if (!hasPartition() && S.empty()) 2110b57cec5SDimitry Andric return; 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric // Get or create a stable partition name string and put it in the table in the 2140b57cec5SDimitry Andric // context. 2150b57cec5SDimitry Andric if (!S.empty()) 2160b57cec5SDimitry Andric S = getContext().pImpl->Saver.save(S); 2170b57cec5SDimitry Andric getContext().pImpl->GlobalValuePartitions[this] = S; 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric // Update the HasPartition field. Setting the partition to the empty string 2200b57cec5SDimitry Andric // means this global no longer has a partition. 2210b57cec5SDimitry Andric HasPartition = !S.empty(); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 224*81ad6265SDimitry Andric using SanitizerMetadata = GlobalValue::SanitizerMetadata; 225*81ad6265SDimitry Andric const SanitizerMetadata &GlobalValue::getSanitizerMetadata() const { 226*81ad6265SDimitry Andric assert(hasSanitizerMetadata()); 227*81ad6265SDimitry Andric assert(getContext().pImpl->GlobalValueSanitizerMetadata.count(this)); 228*81ad6265SDimitry Andric return getContext().pImpl->GlobalValueSanitizerMetadata[this]; 229*81ad6265SDimitry Andric } 230*81ad6265SDimitry Andric 231*81ad6265SDimitry Andric void GlobalValue::setSanitizerMetadata(SanitizerMetadata Meta) { 232*81ad6265SDimitry Andric getContext().pImpl->GlobalValueSanitizerMetadata[this] = Meta; 233*81ad6265SDimitry Andric HasSanitizerMetadata = true; 234*81ad6265SDimitry Andric } 235*81ad6265SDimitry Andric 236*81ad6265SDimitry Andric void GlobalValue::removeSanitizerMetadata() { 237*81ad6265SDimitry Andric DenseMap<const GlobalValue *, SanitizerMetadata> &MetadataMap = 238*81ad6265SDimitry Andric getContext().pImpl->GlobalValueSanitizerMetadata; 239*81ad6265SDimitry Andric MetadataMap.erase(this); 240*81ad6265SDimitry Andric HasSanitizerMetadata = false; 241*81ad6265SDimitry Andric } 242*81ad6265SDimitry Andric 2430b57cec5SDimitry Andric StringRef GlobalObject::getSectionImpl() const { 2440b57cec5SDimitry Andric assert(hasSection()); 2450b57cec5SDimitry Andric return getContext().pImpl->GlobalObjectSections[this]; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric void GlobalObject::setSection(StringRef S) { 2490b57cec5SDimitry Andric // Do nothing if we're clearing the section and it is already empty. 2500b57cec5SDimitry Andric if (!hasSection() && S.empty()) 2510b57cec5SDimitry Andric return; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Get or create a stable section name string and put it in the table in the 2540b57cec5SDimitry Andric // context. 2550b57cec5SDimitry Andric if (!S.empty()) 2560b57cec5SDimitry Andric S = getContext().pImpl->Saver.save(S); 2570b57cec5SDimitry Andric getContext().pImpl->GlobalObjectSections[this] = S; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // Update the HasSectionHashEntryBit. Setting the section to the empty string 2600b57cec5SDimitry Andric // means this global no longer has a section. 2610b57cec5SDimitry Andric setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty()); 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric bool GlobalValue::isDeclaration() const { 2650b57cec5SDimitry Andric // Globals are definitions if they have an initializer. 2660b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 2670b57cec5SDimitry Andric return GV->getNumOperands() == 0; 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // Functions are definitions if they have a body. 2700b57cec5SDimitry Andric if (const Function *F = dyn_cast<Function>(this)) 2710b57cec5SDimitry Andric return F->empty() && !F->isMaterializable(); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // Aliases and ifuncs are always definitions. 274349cc55cSDimitry Andric assert(isa<GlobalAlias>(this) || isa<GlobalIFunc>(this)); 2750b57cec5SDimitry Andric return false; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2785ffd83dbSDimitry Andric bool GlobalObject::canIncreaseAlignment() const { 2790b57cec5SDimitry Andric // Firstly, can only increase the alignment of a global if it 2800b57cec5SDimitry Andric // is a strong definition. 2810b57cec5SDimitry Andric if (!isStrongDefinitionForLinker()) 2820b57cec5SDimitry Andric return false; 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // It also has to either not have a section defined, or, not have 2850b57cec5SDimitry Andric // alignment specified. (If it is assigned a section, the global 2860b57cec5SDimitry Andric // could be densely packed with other objects in the section, and 2870b57cec5SDimitry Andric // increasing the alignment could cause padding issues.) 288*81ad6265SDimitry Andric if (hasSection() && getAlign()) 2890b57cec5SDimitry Andric return false; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric // On ELF platforms, we're further restricted in that we can't 2920b57cec5SDimitry Andric // increase the alignment of any variable which might be emitted 2930b57cec5SDimitry Andric // into a shared library, and which is exported. If the main 2940b57cec5SDimitry Andric // executable accesses a variable found in a shared-lib, the main 2950b57cec5SDimitry Andric // exe actually allocates memory for and exports the symbol ITSELF, 2960b57cec5SDimitry Andric // overriding the symbol found in the library. That is, at link 2970b57cec5SDimitry Andric // time, the observed alignment of the variable is copied into the 2980b57cec5SDimitry Andric // executable binary. (A COPY relocation is also generated, to copy 2990b57cec5SDimitry Andric // the initial data from the shadowed variable in the shared-lib 3000b57cec5SDimitry Andric // into the location in the main binary, before running code.) 3010b57cec5SDimitry Andric // 3020b57cec5SDimitry Andric // And thus, even though you might think you are defining the 3030b57cec5SDimitry Andric // global, and allocating the memory for the global in your object 3040b57cec5SDimitry Andric // file, and thus should be able to set the alignment arbitrarily, 3050b57cec5SDimitry Andric // that's not actually true. Doing so can cause an ABI breakage; an 3060b57cec5SDimitry Andric // executable might have already been built with the previous 3070b57cec5SDimitry Andric // alignment of the variable, and then assuming an increased 3080b57cec5SDimitry Andric // alignment will be incorrect. 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric // Conservatively assume ELF if there's no parent pointer. 3110b57cec5SDimitry Andric bool isELF = 3120b57cec5SDimitry Andric (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF()); 3130b57cec5SDimitry Andric if (isELF && !isDSOLocal()) 3140b57cec5SDimitry Andric return false; 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric return true; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 319349cc55cSDimitry Andric static const GlobalObject * 320349cc55cSDimitry Andric findBaseObject(const Constant *C, DenseSet<const GlobalAlias *> &Aliases) { 321349cc55cSDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(C)) 3220b57cec5SDimitry Andric return GO; 323349cc55cSDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(C)) 324349cc55cSDimitry Andric if (Aliases.insert(GA).second) 325349cc55cSDimitry Andric return findBaseObject(GA->getOperand(0), Aliases); 326349cc55cSDimitry Andric if (auto *CE = dyn_cast<ConstantExpr>(C)) { 327349cc55cSDimitry Andric switch (CE->getOpcode()) { 328349cc55cSDimitry Andric case Instruction::Add: { 329349cc55cSDimitry Andric auto *LHS = findBaseObject(CE->getOperand(0), Aliases); 330349cc55cSDimitry Andric auto *RHS = findBaseObject(CE->getOperand(1), Aliases); 331349cc55cSDimitry Andric if (LHS && RHS) 3320b57cec5SDimitry Andric return nullptr; 333349cc55cSDimitry Andric return LHS ? LHS : RHS; 334349cc55cSDimitry Andric } 335349cc55cSDimitry Andric case Instruction::Sub: { 336349cc55cSDimitry Andric if (findBaseObject(CE->getOperand(1), Aliases)) 337349cc55cSDimitry Andric return nullptr; 338349cc55cSDimitry Andric return findBaseObject(CE->getOperand(0), Aliases); 339349cc55cSDimitry Andric } 340349cc55cSDimitry Andric case Instruction::IntToPtr: 341349cc55cSDimitry Andric case Instruction::PtrToInt: 342349cc55cSDimitry Andric case Instruction::BitCast: 343349cc55cSDimitry Andric case Instruction::GetElementPtr: 344349cc55cSDimitry Andric return findBaseObject(CE->getOperand(0), Aliases); 345349cc55cSDimitry Andric default: 346349cc55cSDimitry Andric break; 347349cc55cSDimitry Andric } 348349cc55cSDimitry Andric } 349349cc55cSDimitry Andric return nullptr; 350349cc55cSDimitry Andric } 351349cc55cSDimitry Andric 352349cc55cSDimitry Andric const GlobalObject *GlobalValue::getAliaseeObject() const { 353349cc55cSDimitry Andric DenseSet<const GlobalAlias *> Aliases; 354349cc55cSDimitry Andric return findBaseObject(this, Aliases); 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric bool GlobalValue::isAbsoluteSymbolRef() const { 3580b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(this); 3590b57cec5SDimitry Andric if (!GO) 3600b57cec5SDimitry Andric return false; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric return GO->getMetadata(LLVMContext::MD_absolute_symbol); 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric Optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const { 3660b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(this); 3670b57cec5SDimitry Andric if (!GO) 3680b57cec5SDimitry Andric return None; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol); 3710b57cec5SDimitry Andric if (!MD) 3720b57cec5SDimitry Andric return None; 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric return getConstantRangeFromMetadata(*MD); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric bool GlobalValue::canBeOmittedFromSymbolTable() const { 3780b57cec5SDimitry Andric if (!hasLinkOnceODRLinkage()) 3790b57cec5SDimitry Andric return false; 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // We assume that anyone who sets global unnamed_addr on a non-constant 3820b57cec5SDimitry Andric // knows what they're doing. 3830b57cec5SDimitry Andric if (hasGlobalUnnamedAddr()) 3840b57cec5SDimitry Andric return true; 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric // If it is a non constant variable, it needs to be uniqued across shared 3870b57cec5SDimitry Andric // objects. 3880b57cec5SDimitry Andric if (auto *Var = dyn_cast<GlobalVariable>(this)) 3890b57cec5SDimitry Andric if (!Var->isConstant()) 3900b57cec5SDimitry Andric return false; 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric return hasAtLeastLocalUnnamedAddr(); 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3960b57cec5SDimitry Andric // GlobalVariable Implementation 3970b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 4000b57cec5SDimitry Andric Constant *InitVal, const Twine &Name, 4010b57cec5SDimitry Andric ThreadLocalMode TLMode, unsigned AddressSpace, 4020b57cec5SDimitry Andric bool isExternallyInitialized) 4030b57cec5SDimitry Andric : GlobalObject(Ty, Value::GlobalVariableVal, 4040b57cec5SDimitry Andric OperandTraits<GlobalVariable>::op_begin(this), 4050b57cec5SDimitry Andric InitVal != nullptr, Link, Name, AddressSpace), 4060b57cec5SDimitry Andric isConstantGlobal(constant), 4070b57cec5SDimitry Andric isExternallyInitializedConstant(isExternallyInitialized) { 4080b57cec5SDimitry Andric assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && 4090b57cec5SDimitry Andric "invalid type for global variable"); 4100b57cec5SDimitry Andric setThreadLocalMode(TLMode); 4110b57cec5SDimitry Andric if (InitVal) { 4120b57cec5SDimitry Andric assert(InitVal->getType() == Ty && 4130b57cec5SDimitry Andric "Initializer should be the same type as the GlobalVariable!"); 4140b57cec5SDimitry Andric Op<0>() = InitVal; 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 4190b57cec5SDimitry Andric LinkageTypes Link, Constant *InitVal, 4200b57cec5SDimitry Andric const Twine &Name, GlobalVariable *Before, 421e8d8bef9SDimitry Andric ThreadLocalMode TLMode, 422e8d8bef9SDimitry Andric Optional<unsigned> AddressSpace, 4230b57cec5SDimitry Andric bool isExternallyInitialized) 4240b57cec5SDimitry Andric : GlobalObject(Ty, Value::GlobalVariableVal, 4250b57cec5SDimitry Andric OperandTraits<GlobalVariable>::op_begin(this), 426e8d8bef9SDimitry Andric InitVal != nullptr, Link, Name, 427e8d8bef9SDimitry Andric AddressSpace 428e8d8bef9SDimitry Andric ? *AddressSpace 429e8d8bef9SDimitry Andric : M.getDataLayout().getDefaultGlobalsAddressSpace()), 4300b57cec5SDimitry Andric isConstantGlobal(constant), 4310b57cec5SDimitry Andric isExternallyInitializedConstant(isExternallyInitialized) { 4320b57cec5SDimitry Andric assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && 4330b57cec5SDimitry Andric "invalid type for global variable"); 4340b57cec5SDimitry Andric setThreadLocalMode(TLMode); 4350b57cec5SDimitry Andric if (InitVal) { 4360b57cec5SDimitry Andric assert(InitVal->getType() == Ty && 4370b57cec5SDimitry Andric "Initializer should be the same type as the GlobalVariable!"); 4380b57cec5SDimitry Andric Op<0>() = InitVal; 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric if (Before) 4420b57cec5SDimitry Andric Before->getParent()->getGlobalList().insert(Before->getIterator(), this); 4430b57cec5SDimitry Andric else 4440b57cec5SDimitry Andric M.getGlobalList().push_back(this); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric void GlobalVariable::removeFromParent() { 4480b57cec5SDimitry Andric getParent()->getGlobalList().remove(getIterator()); 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric void GlobalVariable::eraseFromParent() { 4520b57cec5SDimitry Andric getParent()->getGlobalList().erase(getIterator()); 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric void GlobalVariable::setInitializer(Constant *InitVal) { 4560b57cec5SDimitry Andric if (!InitVal) { 4570b57cec5SDimitry Andric if (hasInitializer()) { 4580b57cec5SDimitry Andric // Note, the num operands is used to compute the offset of the operand, so 4590b57cec5SDimitry Andric // the order here matters. Clearing the operand then clearing the num 4600b57cec5SDimitry Andric // operands ensures we have the correct offset to the operand. 4610b57cec5SDimitry Andric Op<0>().set(nullptr); 4620b57cec5SDimitry Andric setGlobalVariableNumOperands(0); 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric } else { 4650b57cec5SDimitry Andric assert(InitVal->getType() == getValueType() && 4660b57cec5SDimitry Andric "Initializer type must match GlobalVariable type"); 4670b57cec5SDimitry Andric // Note, the num operands is used to compute the offset of the operand, so 4680b57cec5SDimitry Andric // the order here matters. We need to set num operands to 1 first so that 4690b57cec5SDimitry Andric // we get the correct offset to the first operand when we set it. 4700b57cec5SDimitry Andric if (!hasInitializer()) 4710b57cec5SDimitry Andric setGlobalVariableNumOperands(1); 4720b57cec5SDimitry Andric Op<0>().set(InitVal); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric /// Copy all additional attributes (those not needed to create a GlobalVariable) 4770b57cec5SDimitry Andric /// from the GlobalVariable Src to this one. 4780b57cec5SDimitry Andric void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) { 4790b57cec5SDimitry Andric GlobalObject::copyAttributesFrom(Src); 4800b57cec5SDimitry Andric setExternallyInitialized(Src->isExternallyInitialized()); 4810b57cec5SDimitry Andric setAttributes(Src->getAttributes()); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric void GlobalVariable::dropAllReferences() { 4850b57cec5SDimitry Andric User::dropAllReferences(); 4860b57cec5SDimitry Andric clearMetadata(); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4900b57cec5SDimitry Andric // GlobalAlias Implementation 4910b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 4940b57cec5SDimitry Andric const Twine &Name, Constant *Aliasee, 4950b57cec5SDimitry Andric Module *ParentModule) 496349cc55cSDimitry Andric : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name, 497349cc55cSDimitry Andric AddressSpace) { 498349cc55cSDimitry Andric setAliasee(Aliasee); 4990b57cec5SDimitry Andric if (ParentModule) 5000b57cec5SDimitry Andric ParentModule->getAliasList().push_back(this); 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 5040b57cec5SDimitry Andric LinkageTypes Link, const Twine &Name, 5050b57cec5SDimitry Andric Constant *Aliasee, Module *ParentModule) { 5060b57cec5SDimitry Andric return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 5100b57cec5SDimitry Andric LinkageTypes Linkage, const Twine &Name, 5110b57cec5SDimitry Andric Module *Parent) { 5120b57cec5SDimitry Andric return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 5160b57cec5SDimitry Andric LinkageTypes Linkage, const Twine &Name, 5170b57cec5SDimitry Andric GlobalValue *Aliasee) { 5180b57cec5SDimitry Andric return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, 5220b57cec5SDimitry Andric GlobalValue *Aliasee) { 523fe6060f1SDimitry Andric return create(Aliasee->getValueType(), Aliasee->getAddressSpace(), Link, Name, 5240b57cec5SDimitry Andric Aliasee); 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { 5280b57cec5SDimitry Andric return create(Aliasee->getLinkage(), Name, Aliasee); 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric void GlobalAlias::removeFromParent() { 5320b57cec5SDimitry Andric getParent()->getAliasList().remove(getIterator()); 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric void GlobalAlias::eraseFromParent() { 5360b57cec5SDimitry Andric getParent()->getAliasList().erase(getIterator()); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric void GlobalAlias::setAliasee(Constant *Aliasee) { 5400b57cec5SDimitry Andric assert((!Aliasee || Aliasee->getType() == getType()) && 5410b57cec5SDimitry Andric "Alias and aliasee types should match!"); 542349cc55cSDimitry Andric Op<0>().set(Aliasee); 543349cc55cSDimitry Andric } 544349cc55cSDimitry Andric 545349cc55cSDimitry Andric const GlobalObject *GlobalAlias::getAliaseeObject() const { 546349cc55cSDimitry Andric DenseSet<const GlobalAlias *> Aliases; 547349cc55cSDimitry Andric return findBaseObject(getOperand(0), Aliases); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5510b57cec5SDimitry Andric // GlobalIFunc Implementation 5520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 5550b57cec5SDimitry Andric const Twine &Name, Constant *Resolver, 5560b57cec5SDimitry Andric Module *ParentModule) 557349cc55cSDimitry Andric : GlobalObject(Ty, Value::GlobalIFuncVal, &Op<0>(), 1, Link, Name, 558349cc55cSDimitry Andric AddressSpace) { 559349cc55cSDimitry Andric setResolver(Resolver); 5600b57cec5SDimitry Andric if (ParentModule) 5610b57cec5SDimitry Andric ParentModule->getIFuncList().push_back(this); 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace, 5650b57cec5SDimitry Andric LinkageTypes Link, const Twine &Name, 5660b57cec5SDimitry Andric Constant *Resolver, Module *ParentModule) { 5670b57cec5SDimitry Andric return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule); 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric void GlobalIFunc::removeFromParent() { 5710b57cec5SDimitry Andric getParent()->getIFuncList().remove(getIterator()); 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric void GlobalIFunc::eraseFromParent() { 5750b57cec5SDimitry Andric getParent()->getIFuncList().erase(getIterator()); 5760b57cec5SDimitry Andric } 577349cc55cSDimitry Andric 578349cc55cSDimitry Andric const Function *GlobalIFunc::getResolverFunction() const { 579349cc55cSDimitry Andric DenseSet<const GlobalAlias *> Aliases; 580349cc55cSDimitry Andric return dyn_cast<Function>(findBaseObject(getResolver(), Aliases)); 581349cc55cSDimitry Andric } 582