1*0b57cec5SDimitry Andric //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements the GlobalValue & GlobalVariable classes for the IR 10*0b57cec5SDimitry Andric // library. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "LLVMContextImpl.h" 15*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 17*0b57cec5SDimitry Andric #include "llvm/IR/ConstantRange.h" 18*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 19*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 20*0b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 21*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 22*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 23*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 24*0b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 25*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 26*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 27*0b57cec5SDimitry Andric using namespace llvm; 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 30*0b57cec5SDimitry Andric // GlobalValue Class 31*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric // GlobalValue should be a Constant, plus a type, a module, some flags, and an 34*0b57cec5SDimitry Andric // intrinsic ID. Add an assert to prevent people from accidentally growing 35*0b57cec5SDimitry Andric // GlobalValue while adding flags. 36*0b57cec5SDimitry Andric static_assert(sizeof(GlobalValue) == 37*0b57cec5SDimitry Andric sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned), 38*0b57cec5SDimitry Andric "unexpected GlobalValue size growth"); 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric // GlobalObject adds a comdat. 41*0b57cec5SDimitry Andric static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *), 42*0b57cec5SDimitry Andric "unexpected GlobalObject size growth"); 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric bool GlobalValue::isMaterializable() const { 45*0b57cec5SDimitry Andric if (const Function *F = dyn_cast<Function>(this)) 46*0b57cec5SDimitry Andric return F->isMaterializable(); 47*0b57cec5SDimitry Andric return false; 48*0b57cec5SDimitry Andric } 49*0b57cec5SDimitry Andric Error GlobalValue::materialize() { 50*0b57cec5SDimitry Andric return getParent()->materialize(this); 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric /// Override destroyConstantImpl to make sure it doesn't get called on 54*0b57cec5SDimitry Andric /// GlobalValue's because they shouldn't be treated like other constants. 55*0b57cec5SDimitry Andric void GlobalValue::destroyConstantImpl() { 56*0b57cec5SDimitry Andric llvm_unreachable("You can't GV->destroyConstantImpl()!"); 57*0b57cec5SDimitry Andric } 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) { 60*0b57cec5SDimitry Andric llvm_unreachable("Unsupported class for handleOperandChange()!"); 61*0b57cec5SDimitry Andric } 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric /// copyAttributesFrom - copy all additional attributes (those not needed to 64*0b57cec5SDimitry Andric /// create a GlobalValue) from the GlobalValue Src to this one. 65*0b57cec5SDimitry Andric void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 66*0b57cec5SDimitry Andric setVisibility(Src->getVisibility()); 67*0b57cec5SDimitry Andric setUnnamedAddr(Src->getUnnamedAddr()); 68*0b57cec5SDimitry Andric setDLLStorageClass(Src->getDLLStorageClass()); 69*0b57cec5SDimitry Andric setDSOLocal(Src->isDSOLocal()); 70*0b57cec5SDimitry Andric setPartition(Src->getPartition()); 71*0b57cec5SDimitry Andric } 72*0b57cec5SDimitry Andric 73*0b57cec5SDimitry Andric void GlobalValue::removeFromParent() { 74*0b57cec5SDimitry Andric switch (getValueID()) { 75*0b57cec5SDimitry Andric #define HANDLE_GLOBAL_VALUE(NAME) \ 76*0b57cec5SDimitry Andric case Value::NAME##Val: \ 77*0b57cec5SDimitry Andric return static_cast<NAME *>(this)->removeFromParent(); 78*0b57cec5SDimitry Andric #include "llvm/IR/Value.def" 79*0b57cec5SDimitry Andric default: 80*0b57cec5SDimitry Andric break; 81*0b57cec5SDimitry Andric } 82*0b57cec5SDimitry Andric llvm_unreachable("not a global"); 83*0b57cec5SDimitry Andric } 84*0b57cec5SDimitry Andric 85*0b57cec5SDimitry Andric void GlobalValue::eraseFromParent() { 86*0b57cec5SDimitry Andric switch (getValueID()) { 87*0b57cec5SDimitry Andric #define HANDLE_GLOBAL_VALUE(NAME) \ 88*0b57cec5SDimitry Andric case Value::NAME##Val: \ 89*0b57cec5SDimitry Andric return static_cast<NAME *>(this)->eraseFromParent(); 90*0b57cec5SDimitry Andric #include "llvm/IR/Value.def" 91*0b57cec5SDimitry Andric default: 92*0b57cec5SDimitry Andric break; 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric llvm_unreachable("not a global"); 95*0b57cec5SDimitry Andric } 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric unsigned GlobalValue::getAlignment() const { 98*0b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(this)) { 99*0b57cec5SDimitry Andric // In general we cannot compute this at the IR level, but we try. 100*0b57cec5SDimitry Andric if (const GlobalObject *GO = GA->getBaseObject()) 101*0b57cec5SDimitry Andric return GO->getAlignment(); 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric // FIXME: we should also be able to handle: 104*0b57cec5SDimitry Andric // Alias = Global + Offset 105*0b57cec5SDimitry Andric // Alias = Absolute 106*0b57cec5SDimitry Andric return 0; 107*0b57cec5SDimitry Andric } 108*0b57cec5SDimitry Andric return cast<GlobalObject>(this)->getAlignment(); 109*0b57cec5SDimitry Andric } 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric unsigned GlobalValue::getAddressSpace() const { 112*0b57cec5SDimitry Andric PointerType *PtrTy = getType(); 113*0b57cec5SDimitry Andric return PtrTy->getAddressSpace(); 114*0b57cec5SDimitry Andric } 115*0b57cec5SDimitry Andric 116*0b57cec5SDimitry Andric void GlobalObject::setAlignment(unsigned Align) { 117*0b57cec5SDimitry Andric assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 118*0b57cec5SDimitry Andric assert(Align <= MaximumAlignment && 119*0b57cec5SDimitry Andric "Alignment is greater than MaximumAlignment!"); 120*0b57cec5SDimitry Andric unsigned AlignmentData = Log2_32(Align) + 1; 121*0b57cec5SDimitry Andric unsigned OldData = getGlobalValueSubClassData(); 122*0b57cec5SDimitry Andric setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); 123*0b57cec5SDimitry Andric assert(getAlignment() == Align && "Alignment representation error!"); 124*0b57cec5SDimitry Andric } 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric void GlobalObject::copyAttributesFrom(const GlobalObject *Src) { 127*0b57cec5SDimitry Andric GlobalValue::copyAttributesFrom(Src); 128*0b57cec5SDimitry Andric setAlignment(Src->getAlignment()); 129*0b57cec5SDimitry Andric setSection(Src->getSection()); 130*0b57cec5SDimitry Andric } 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric std::string GlobalValue::getGlobalIdentifier(StringRef Name, 133*0b57cec5SDimitry Andric GlobalValue::LinkageTypes Linkage, 134*0b57cec5SDimitry Andric StringRef FileName) { 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric // Value names may be prefixed with a binary '1' to indicate 137*0b57cec5SDimitry Andric // that the backend should not modify the symbols due to any platform 138*0b57cec5SDimitry Andric // naming convention. Do not include that '1' in the PGO profile name. 139*0b57cec5SDimitry Andric if (Name[0] == '\1') 140*0b57cec5SDimitry Andric Name = Name.substr(1); 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric std::string NewName = Name; 143*0b57cec5SDimitry Andric if (llvm::GlobalValue::isLocalLinkage(Linkage)) { 144*0b57cec5SDimitry Andric // For local symbols, prepend the main file name to distinguish them. 145*0b57cec5SDimitry Andric // Do not include the full path in the file name since there's no guarantee 146*0b57cec5SDimitry Andric // that it will stay the same, e.g., if the files are checked out from 147*0b57cec5SDimitry Andric // version control in different locations. 148*0b57cec5SDimitry Andric if (FileName.empty()) 149*0b57cec5SDimitry Andric NewName = NewName.insert(0, "<unknown>:"); 150*0b57cec5SDimitry Andric else 151*0b57cec5SDimitry Andric NewName = NewName.insert(0, FileName.str() + ":"); 152*0b57cec5SDimitry Andric } 153*0b57cec5SDimitry Andric return NewName; 154*0b57cec5SDimitry Andric } 155*0b57cec5SDimitry Andric 156*0b57cec5SDimitry Andric std::string GlobalValue::getGlobalIdentifier() const { 157*0b57cec5SDimitry Andric return getGlobalIdentifier(getName(), getLinkage(), 158*0b57cec5SDimitry Andric getParent()->getSourceFileName()); 159*0b57cec5SDimitry Andric } 160*0b57cec5SDimitry Andric 161*0b57cec5SDimitry Andric StringRef GlobalValue::getSection() const { 162*0b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(this)) { 163*0b57cec5SDimitry Andric // In general we cannot compute this at the IR level, but we try. 164*0b57cec5SDimitry Andric if (const GlobalObject *GO = GA->getBaseObject()) 165*0b57cec5SDimitry Andric return GO->getSection(); 166*0b57cec5SDimitry Andric return ""; 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric return cast<GlobalObject>(this)->getSection(); 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric const Comdat *GlobalValue::getComdat() const { 172*0b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(this)) { 173*0b57cec5SDimitry Andric // In general we cannot compute this at the IR level, but we try. 174*0b57cec5SDimitry Andric if (const GlobalObject *GO = GA->getBaseObject()) 175*0b57cec5SDimitry Andric return const_cast<GlobalObject *>(GO)->getComdat(); 176*0b57cec5SDimitry Andric return nullptr; 177*0b57cec5SDimitry Andric } 178*0b57cec5SDimitry Andric // ifunc and its resolver are separate things so don't use resolver comdat. 179*0b57cec5SDimitry Andric if (isa<GlobalIFunc>(this)) 180*0b57cec5SDimitry Andric return nullptr; 181*0b57cec5SDimitry Andric return cast<GlobalObject>(this)->getComdat(); 182*0b57cec5SDimitry Andric } 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric StringRef GlobalValue::getPartition() const { 185*0b57cec5SDimitry Andric if (!hasPartition()) 186*0b57cec5SDimitry Andric return ""; 187*0b57cec5SDimitry Andric return getContext().pImpl->GlobalValuePartitions[this]; 188*0b57cec5SDimitry Andric } 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric void GlobalValue::setPartition(StringRef S) { 191*0b57cec5SDimitry Andric // Do nothing if we're clearing the partition and it is already empty. 192*0b57cec5SDimitry Andric if (!hasPartition() && S.empty()) 193*0b57cec5SDimitry Andric return; 194*0b57cec5SDimitry Andric 195*0b57cec5SDimitry Andric // Get or create a stable partition name string and put it in the table in the 196*0b57cec5SDimitry Andric // context. 197*0b57cec5SDimitry Andric if (!S.empty()) 198*0b57cec5SDimitry Andric S = getContext().pImpl->Saver.save(S); 199*0b57cec5SDimitry Andric getContext().pImpl->GlobalValuePartitions[this] = S; 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric // Update the HasPartition field. Setting the partition to the empty string 202*0b57cec5SDimitry Andric // means this global no longer has a partition. 203*0b57cec5SDimitry Andric HasPartition = !S.empty(); 204*0b57cec5SDimitry Andric } 205*0b57cec5SDimitry Andric 206*0b57cec5SDimitry Andric StringRef GlobalObject::getSectionImpl() const { 207*0b57cec5SDimitry Andric assert(hasSection()); 208*0b57cec5SDimitry Andric return getContext().pImpl->GlobalObjectSections[this]; 209*0b57cec5SDimitry Andric } 210*0b57cec5SDimitry Andric 211*0b57cec5SDimitry Andric void GlobalObject::setSection(StringRef S) { 212*0b57cec5SDimitry Andric // Do nothing if we're clearing the section and it is already empty. 213*0b57cec5SDimitry Andric if (!hasSection() && S.empty()) 214*0b57cec5SDimitry Andric return; 215*0b57cec5SDimitry Andric 216*0b57cec5SDimitry Andric // Get or create a stable section name string and put it in the table in the 217*0b57cec5SDimitry Andric // context. 218*0b57cec5SDimitry Andric if (!S.empty()) 219*0b57cec5SDimitry Andric S = getContext().pImpl->Saver.save(S); 220*0b57cec5SDimitry Andric getContext().pImpl->GlobalObjectSections[this] = S; 221*0b57cec5SDimitry Andric 222*0b57cec5SDimitry Andric // Update the HasSectionHashEntryBit. Setting the section to the empty string 223*0b57cec5SDimitry Andric // means this global no longer has a section. 224*0b57cec5SDimitry Andric setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty()); 225*0b57cec5SDimitry Andric } 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric bool GlobalValue::isDeclaration() const { 228*0b57cec5SDimitry Andric // Globals are definitions if they have an initializer. 229*0b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 230*0b57cec5SDimitry Andric return GV->getNumOperands() == 0; 231*0b57cec5SDimitry Andric 232*0b57cec5SDimitry Andric // Functions are definitions if they have a body. 233*0b57cec5SDimitry Andric if (const Function *F = dyn_cast<Function>(this)) 234*0b57cec5SDimitry Andric return F->empty() && !F->isMaterializable(); 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric // Aliases and ifuncs are always definitions. 237*0b57cec5SDimitry Andric assert(isa<GlobalIndirectSymbol>(this)); 238*0b57cec5SDimitry Andric return false; 239*0b57cec5SDimitry Andric } 240*0b57cec5SDimitry Andric 241*0b57cec5SDimitry Andric bool GlobalValue::canIncreaseAlignment() const { 242*0b57cec5SDimitry Andric // Firstly, can only increase the alignment of a global if it 243*0b57cec5SDimitry Andric // is a strong definition. 244*0b57cec5SDimitry Andric if (!isStrongDefinitionForLinker()) 245*0b57cec5SDimitry Andric return false; 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric // It also has to either not have a section defined, or, not have 248*0b57cec5SDimitry Andric // alignment specified. (If it is assigned a section, the global 249*0b57cec5SDimitry Andric // could be densely packed with other objects in the section, and 250*0b57cec5SDimitry Andric // increasing the alignment could cause padding issues.) 251*0b57cec5SDimitry Andric if (hasSection() && getAlignment() > 0) 252*0b57cec5SDimitry Andric return false; 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric // On ELF platforms, we're further restricted in that we can't 255*0b57cec5SDimitry Andric // increase the alignment of any variable which might be emitted 256*0b57cec5SDimitry Andric // into a shared library, and which is exported. If the main 257*0b57cec5SDimitry Andric // executable accesses a variable found in a shared-lib, the main 258*0b57cec5SDimitry Andric // exe actually allocates memory for and exports the symbol ITSELF, 259*0b57cec5SDimitry Andric // overriding the symbol found in the library. That is, at link 260*0b57cec5SDimitry Andric // time, the observed alignment of the variable is copied into the 261*0b57cec5SDimitry Andric // executable binary. (A COPY relocation is also generated, to copy 262*0b57cec5SDimitry Andric // the initial data from the shadowed variable in the shared-lib 263*0b57cec5SDimitry Andric // into the location in the main binary, before running code.) 264*0b57cec5SDimitry Andric // 265*0b57cec5SDimitry Andric // And thus, even though you might think you are defining the 266*0b57cec5SDimitry Andric // global, and allocating the memory for the global in your object 267*0b57cec5SDimitry Andric // file, and thus should be able to set the alignment arbitrarily, 268*0b57cec5SDimitry Andric // that's not actually true. Doing so can cause an ABI breakage; an 269*0b57cec5SDimitry Andric // executable might have already been built with the previous 270*0b57cec5SDimitry Andric // alignment of the variable, and then assuming an increased 271*0b57cec5SDimitry Andric // alignment will be incorrect. 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric // Conservatively assume ELF if there's no parent pointer. 274*0b57cec5SDimitry Andric bool isELF = 275*0b57cec5SDimitry Andric (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF()); 276*0b57cec5SDimitry Andric if (isELF && !isDSOLocal()) 277*0b57cec5SDimitry Andric return false; 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric return true; 280*0b57cec5SDimitry Andric } 281*0b57cec5SDimitry Andric 282*0b57cec5SDimitry Andric const GlobalObject *GlobalValue::getBaseObject() const { 283*0b57cec5SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(this)) 284*0b57cec5SDimitry Andric return GO; 285*0b57cec5SDimitry Andric if (auto *GA = dyn_cast<GlobalIndirectSymbol>(this)) 286*0b57cec5SDimitry Andric return GA->getBaseObject(); 287*0b57cec5SDimitry Andric return nullptr; 288*0b57cec5SDimitry Andric } 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric bool GlobalValue::isAbsoluteSymbolRef() const { 291*0b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(this); 292*0b57cec5SDimitry Andric if (!GO) 293*0b57cec5SDimitry Andric return false; 294*0b57cec5SDimitry Andric 295*0b57cec5SDimitry Andric return GO->getMetadata(LLVMContext::MD_absolute_symbol); 296*0b57cec5SDimitry Andric } 297*0b57cec5SDimitry Andric 298*0b57cec5SDimitry Andric Optional<ConstantRange> GlobalValue::getAbsoluteSymbolRange() const { 299*0b57cec5SDimitry Andric auto *GO = dyn_cast<GlobalObject>(this); 300*0b57cec5SDimitry Andric if (!GO) 301*0b57cec5SDimitry Andric return None; 302*0b57cec5SDimitry Andric 303*0b57cec5SDimitry Andric MDNode *MD = GO->getMetadata(LLVMContext::MD_absolute_symbol); 304*0b57cec5SDimitry Andric if (!MD) 305*0b57cec5SDimitry Andric return None; 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric return getConstantRangeFromMetadata(*MD); 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric bool GlobalValue::canBeOmittedFromSymbolTable() const { 311*0b57cec5SDimitry Andric if (!hasLinkOnceODRLinkage()) 312*0b57cec5SDimitry Andric return false; 313*0b57cec5SDimitry Andric 314*0b57cec5SDimitry Andric // We assume that anyone who sets global unnamed_addr on a non-constant 315*0b57cec5SDimitry Andric // knows what they're doing. 316*0b57cec5SDimitry Andric if (hasGlobalUnnamedAddr()) 317*0b57cec5SDimitry Andric return true; 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric // If it is a non constant variable, it needs to be uniqued across shared 320*0b57cec5SDimitry Andric // objects. 321*0b57cec5SDimitry Andric if (auto *Var = dyn_cast<GlobalVariable>(this)) 322*0b57cec5SDimitry Andric if (!Var->isConstant()) 323*0b57cec5SDimitry Andric return false; 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric return hasAtLeastLocalUnnamedAddr(); 326*0b57cec5SDimitry Andric } 327*0b57cec5SDimitry Andric 328*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 329*0b57cec5SDimitry Andric // GlobalVariable Implementation 330*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 333*0b57cec5SDimitry Andric Constant *InitVal, const Twine &Name, 334*0b57cec5SDimitry Andric ThreadLocalMode TLMode, unsigned AddressSpace, 335*0b57cec5SDimitry Andric bool isExternallyInitialized) 336*0b57cec5SDimitry Andric : GlobalObject(Ty, Value::GlobalVariableVal, 337*0b57cec5SDimitry Andric OperandTraits<GlobalVariable>::op_begin(this), 338*0b57cec5SDimitry Andric InitVal != nullptr, Link, Name, AddressSpace), 339*0b57cec5SDimitry Andric isConstantGlobal(constant), 340*0b57cec5SDimitry Andric isExternallyInitializedConstant(isExternallyInitialized) { 341*0b57cec5SDimitry Andric assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && 342*0b57cec5SDimitry Andric "invalid type for global variable"); 343*0b57cec5SDimitry Andric setThreadLocalMode(TLMode); 344*0b57cec5SDimitry Andric if (InitVal) { 345*0b57cec5SDimitry Andric assert(InitVal->getType() == Ty && 346*0b57cec5SDimitry Andric "Initializer should be the same type as the GlobalVariable!"); 347*0b57cec5SDimitry Andric Op<0>() = InitVal; 348*0b57cec5SDimitry Andric } 349*0b57cec5SDimitry Andric } 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 352*0b57cec5SDimitry Andric LinkageTypes Link, Constant *InitVal, 353*0b57cec5SDimitry Andric const Twine &Name, GlobalVariable *Before, 354*0b57cec5SDimitry Andric ThreadLocalMode TLMode, unsigned AddressSpace, 355*0b57cec5SDimitry Andric bool isExternallyInitialized) 356*0b57cec5SDimitry Andric : GlobalObject(Ty, Value::GlobalVariableVal, 357*0b57cec5SDimitry Andric OperandTraits<GlobalVariable>::op_begin(this), 358*0b57cec5SDimitry Andric InitVal != nullptr, Link, Name, AddressSpace), 359*0b57cec5SDimitry Andric isConstantGlobal(constant), 360*0b57cec5SDimitry Andric isExternallyInitializedConstant(isExternallyInitialized) { 361*0b57cec5SDimitry Andric assert(!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && 362*0b57cec5SDimitry Andric "invalid type for global variable"); 363*0b57cec5SDimitry Andric setThreadLocalMode(TLMode); 364*0b57cec5SDimitry Andric if (InitVal) { 365*0b57cec5SDimitry Andric assert(InitVal->getType() == Ty && 366*0b57cec5SDimitry Andric "Initializer should be the same type as the GlobalVariable!"); 367*0b57cec5SDimitry Andric Op<0>() = InitVal; 368*0b57cec5SDimitry Andric } 369*0b57cec5SDimitry Andric 370*0b57cec5SDimitry Andric if (Before) 371*0b57cec5SDimitry Andric Before->getParent()->getGlobalList().insert(Before->getIterator(), this); 372*0b57cec5SDimitry Andric else 373*0b57cec5SDimitry Andric M.getGlobalList().push_back(this); 374*0b57cec5SDimitry Andric } 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric void GlobalVariable::removeFromParent() { 377*0b57cec5SDimitry Andric getParent()->getGlobalList().remove(getIterator()); 378*0b57cec5SDimitry Andric } 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric void GlobalVariable::eraseFromParent() { 381*0b57cec5SDimitry Andric getParent()->getGlobalList().erase(getIterator()); 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric void GlobalVariable::setInitializer(Constant *InitVal) { 385*0b57cec5SDimitry Andric if (!InitVal) { 386*0b57cec5SDimitry Andric if (hasInitializer()) { 387*0b57cec5SDimitry Andric // Note, the num operands is used to compute the offset of the operand, so 388*0b57cec5SDimitry Andric // the order here matters. Clearing the operand then clearing the num 389*0b57cec5SDimitry Andric // operands ensures we have the correct offset to the operand. 390*0b57cec5SDimitry Andric Op<0>().set(nullptr); 391*0b57cec5SDimitry Andric setGlobalVariableNumOperands(0); 392*0b57cec5SDimitry Andric } 393*0b57cec5SDimitry Andric } else { 394*0b57cec5SDimitry Andric assert(InitVal->getType() == getValueType() && 395*0b57cec5SDimitry Andric "Initializer type must match GlobalVariable type"); 396*0b57cec5SDimitry Andric // Note, the num operands is used to compute the offset of the operand, so 397*0b57cec5SDimitry Andric // the order here matters. We need to set num operands to 1 first so that 398*0b57cec5SDimitry Andric // we get the correct offset to the first operand when we set it. 399*0b57cec5SDimitry Andric if (!hasInitializer()) 400*0b57cec5SDimitry Andric setGlobalVariableNumOperands(1); 401*0b57cec5SDimitry Andric Op<0>().set(InitVal); 402*0b57cec5SDimitry Andric } 403*0b57cec5SDimitry Andric } 404*0b57cec5SDimitry Andric 405*0b57cec5SDimitry Andric /// Copy all additional attributes (those not needed to create a GlobalVariable) 406*0b57cec5SDimitry Andric /// from the GlobalVariable Src to this one. 407*0b57cec5SDimitry Andric void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) { 408*0b57cec5SDimitry Andric GlobalObject::copyAttributesFrom(Src); 409*0b57cec5SDimitry Andric setThreadLocalMode(Src->getThreadLocalMode()); 410*0b57cec5SDimitry Andric setExternallyInitialized(Src->isExternallyInitialized()); 411*0b57cec5SDimitry Andric setAttributes(Src->getAttributes()); 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric void GlobalVariable::dropAllReferences() { 415*0b57cec5SDimitry Andric User::dropAllReferences(); 416*0b57cec5SDimitry Andric clearMetadata(); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 420*0b57cec5SDimitry Andric // GlobalIndirectSymbol Implementation 421*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 422*0b57cec5SDimitry Andric 423*0b57cec5SDimitry Andric GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy, 424*0b57cec5SDimitry Andric unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, 425*0b57cec5SDimitry Andric Constant *Symbol) 426*0b57cec5SDimitry Andric : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) { 427*0b57cec5SDimitry Andric Op<0>() = Symbol; 428*0b57cec5SDimitry Andric } 429*0b57cec5SDimitry Andric 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 432*0b57cec5SDimitry Andric // GlobalAlias Implementation 433*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 436*0b57cec5SDimitry Andric const Twine &Name, Constant *Aliasee, 437*0b57cec5SDimitry Andric Module *ParentModule) 438*0b57cec5SDimitry Andric : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name, 439*0b57cec5SDimitry Andric Aliasee) { 440*0b57cec5SDimitry Andric if (ParentModule) 441*0b57cec5SDimitry Andric ParentModule->getAliasList().push_back(this); 442*0b57cec5SDimitry Andric } 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 445*0b57cec5SDimitry Andric LinkageTypes Link, const Twine &Name, 446*0b57cec5SDimitry Andric Constant *Aliasee, Module *ParentModule) { 447*0b57cec5SDimitry Andric return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); 448*0b57cec5SDimitry Andric } 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 451*0b57cec5SDimitry Andric LinkageTypes Linkage, const Twine &Name, 452*0b57cec5SDimitry Andric Module *Parent) { 453*0b57cec5SDimitry Andric return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); 454*0b57cec5SDimitry Andric } 455*0b57cec5SDimitry Andric 456*0b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 457*0b57cec5SDimitry Andric LinkageTypes Linkage, const Twine &Name, 458*0b57cec5SDimitry Andric GlobalValue *Aliasee) { 459*0b57cec5SDimitry Andric return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, 463*0b57cec5SDimitry Andric GlobalValue *Aliasee) { 464*0b57cec5SDimitry Andric PointerType *PTy = Aliasee->getType(); 465*0b57cec5SDimitry Andric return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, 466*0b57cec5SDimitry Andric Aliasee); 467*0b57cec5SDimitry Andric } 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { 470*0b57cec5SDimitry Andric return create(Aliasee->getLinkage(), Name, Aliasee); 471*0b57cec5SDimitry Andric } 472*0b57cec5SDimitry Andric 473*0b57cec5SDimitry Andric void GlobalAlias::removeFromParent() { 474*0b57cec5SDimitry Andric getParent()->getAliasList().remove(getIterator()); 475*0b57cec5SDimitry Andric } 476*0b57cec5SDimitry Andric 477*0b57cec5SDimitry Andric void GlobalAlias::eraseFromParent() { 478*0b57cec5SDimitry Andric getParent()->getAliasList().erase(getIterator()); 479*0b57cec5SDimitry Andric } 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric void GlobalAlias::setAliasee(Constant *Aliasee) { 482*0b57cec5SDimitry Andric assert((!Aliasee || Aliasee->getType() == getType()) && 483*0b57cec5SDimitry Andric "Alias and aliasee types should match!"); 484*0b57cec5SDimitry Andric setIndirectSymbol(Aliasee); 485*0b57cec5SDimitry Andric } 486*0b57cec5SDimitry Andric 487*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 488*0b57cec5SDimitry Andric // GlobalIFunc Implementation 489*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 490*0b57cec5SDimitry Andric 491*0b57cec5SDimitry Andric GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 492*0b57cec5SDimitry Andric const Twine &Name, Constant *Resolver, 493*0b57cec5SDimitry Andric Module *ParentModule) 494*0b57cec5SDimitry Andric : GlobalIndirectSymbol(Ty, Value::GlobalIFuncVal, AddressSpace, Link, Name, 495*0b57cec5SDimitry Andric Resolver) { 496*0b57cec5SDimitry Andric if (ParentModule) 497*0b57cec5SDimitry Andric ParentModule->getIFuncList().push_back(this); 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace, 501*0b57cec5SDimitry Andric LinkageTypes Link, const Twine &Name, 502*0b57cec5SDimitry Andric Constant *Resolver, Module *ParentModule) { 503*0b57cec5SDimitry Andric return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule); 504*0b57cec5SDimitry Andric } 505*0b57cec5SDimitry Andric 506*0b57cec5SDimitry Andric void GlobalIFunc::removeFromParent() { 507*0b57cec5SDimitry Andric getParent()->getIFuncList().remove(getIterator()); 508*0b57cec5SDimitry Andric } 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric void GlobalIFunc::eraseFromParent() { 511*0b57cec5SDimitry Andric getParent()->getIFuncList().erase(getIterator()); 512*0b57cec5SDimitry Andric } 513