1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the GlobalValue & GlobalVariable classes for the IR 11 // library. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/GlobalValue.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/GlobalAlias.h" 20 #include "llvm/IR/GlobalVariable.h" 21 #include "llvm/IR/LeakDetector.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/Support/ErrorHandling.h" 24 using namespace llvm; 25 26 //===----------------------------------------------------------------------===// 27 // GlobalValue Class 28 //===----------------------------------------------------------------------===// 29 30 bool GlobalValue::isMaterializable() const { 31 return getParent() && getParent()->isMaterializable(this); 32 } 33 bool GlobalValue::isDematerializable() const { 34 return getParent() && getParent()->isDematerializable(this); 35 } 36 bool GlobalValue::Materialize(std::string *ErrInfo) { 37 return getParent()->Materialize(this, ErrInfo); 38 } 39 void GlobalValue::Dematerialize() { 40 getParent()->Dematerialize(this); 41 } 42 43 const DataLayout *GlobalValue::getDataLayout() const { 44 return getParent()->getDataLayout(); 45 } 46 47 /// Override destroyConstant to make sure it doesn't get called on 48 /// GlobalValue's because they shouldn't be treated like other constants. 49 void GlobalValue::destroyConstant() { 50 llvm_unreachable("You can't GV->destroyConstant()!"); 51 } 52 53 /// copyAttributesFrom - copy all additional attributes (those not needed to 54 /// create a GlobalValue) from the GlobalValue Src to this one. 55 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 56 setAlignment(Src->getAlignment()); 57 setSection(Src->getSection()); 58 setVisibility(Src->getVisibility()); 59 setUnnamedAddr(Src->hasUnnamedAddr()); 60 setDLLStorageClass(Src->getDLLStorageClass()); 61 } 62 63 void GlobalValue::setAlignment(unsigned Align) { 64 assert((!isa<GlobalAlias>(this) || !Align) && 65 "GlobalAlias should not have an alignment!"); 66 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 67 assert(Align <= MaximumAlignment && 68 "Alignment is greater than MaximumAlignment!"); 69 Alignment = Log2_32(Align) + 1; 70 assert(getAlignment() == Align && "Alignment representation error!"); 71 } 72 73 bool GlobalValue::isDeclaration() const { 74 // Globals are definitions if they have an initializer. 75 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 76 return GV->getNumOperands() == 0; 77 78 // Functions are definitions if they have a body. 79 if (const Function *F = dyn_cast<Function>(this)) 80 return F->empty(); 81 82 // Aliases are always definitions. 83 assert(isa<GlobalAlias>(this)); 84 return false; 85 } 86 87 //===----------------------------------------------------------------------===// 88 // GlobalVariable Implementation 89 //===----------------------------------------------------------------------===// 90 91 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 92 Constant *InitVal, 93 const Twine &Name, ThreadLocalMode TLMode, 94 unsigned AddressSpace, 95 bool isExternallyInitialized) 96 : GlobalValue(PointerType::get(Ty, AddressSpace), 97 Value::GlobalVariableVal, 98 OperandTraits<GlobalVariable>::op_begin(this), 99 InitVal != nullptr, Link, Name), 100 isConstantGlobal(constant), threadLocalMode(TLMode), 101 isExternallyInitializedConstant(isExternallyInitialized) { 102 if (InitVal) { 103 assert(InitVal->getType() == Ty && 104 "Initializer should be the same type as the GlobalVariable!"); 105 Op<0>() = InitVal; 106 } 107 108 LeakDetector::addGarbageObject(this); 109 } 110 111 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 112 LinkageTypes Link, Constant *InitVal, 113 const Twine &Name, 114 GlobalVariable *Before, ThreadLocalMode TLMode, 115 unsigned AddressSpace, 116 bool isExternallyInitialized) 117 : GlobalValue(PointerType::get(Ty, AddressSpace), 118 Value::GlobalVariableVal, 119 OperandTraits<GlobalVariable>::op_begin(this), 120 InitVal != nullptr, Link, Name), 121 isConstantGlobal(constant), threadLocalMode(TLMode), 122 isExternallyInitializedConstant(isExternallyInitialized) { 123 if (InitVal) { 124 assert(InitVal->getType() == Ty && 125 "Initializer should be the same type as the GlobalVariable!"); 126 Op<0>() = InitVal; 127 } 128 129 LeakDetector::addGarbageObject(this); 130 131 if (Before) 132 Before->getParent()->getGlobalList().insert(Before, this); 133 else 134 M.getGlobalList().push_back(this); 135 } 136 137 void GlobalVariable::setParent(Module *parent) { 138 if (getParent()) 139 LeakDetector::addGarbageObject(this); 140 Parent = parent; 141 if (getParent()) 142 LeakDetector::removeGarbageObject(this); 143 } 144 145 void GlobalVariable::removeFromParent() { 146 getParent()->getGlobalList().remove(this); 147 } 148 149 void GlobalVariable::eraseFromParent() { 150 getParent()->getGlobalList().erase(this); 151 } 152 153 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, 154 Use *U) { 155 // If you call this, then you better know this GVar has a constant 156 // initializer worth replacing. Enforce that here. 157 assert(getNumOperands() == 1 && 158 "Attempt to replace uses of Constants on a GVar with no initializer"); 159 160 // And, since you know it has an initializer, the From value better be 161 // the initializer :) 162 assert(getOperand(0) == From && 163 "Attempt to replace wrong constant initializer in GVar"); 164 165 // And, you better have a constant for the replacement value 166 assert(isa<Constant>(To) && 167 "Attempt to replace GVar initializer with non-constant"); 168 169 // Okay, preconditions out of the way, replace the constant initializer. 170 this->setOperand(0, cast<Constant>(To)); 171 } 172 173 void GlobalVariable::setInitializer(Constant *InitVal) { 174 if (!InitVal) { 175 if (hasInitializer()) { 176 Op<0>().set(nullptr); 177 NumOperands = 0; 178 } 179 } else { 180 assert(InitVal->getType() == getType()->getElementType() && 181 "Initializer type must match GlobalVariable type"); 182 if (!hasInitializer()) 183 NumOperands = 1; 184 Op<0>().set(InitVal); 185 } 186 } 187 188 /// copyAttributesFrom - copy all additional attributes (those not needed to 189 /// create a GlobalVariable) from the GlobalVariable Src to this one. 190 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { 191 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!"); 192 GlobalValue::copyAttributesFrom(Src); 193 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); 194 setThreadLocalMode(SrcVar->getThreadLocalMode()); 195 } 196 197 198 //===----------------------------------------------------------------------===// 199 // GlobalAlias Implementation 200 //===----------------------------------------------------------------------===// 201 202 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, 203 const Twine &Name, Constant* aliasee, 204 Module *ParentModule) 205 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { 206 LeakDetector::addGarbageObject(this); 207 208 if (aliasee) 209 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); 210 Op<0>() = aliasee; 211 212 if (ParentModule) 213 ParentModule->getAliasList().push_back(this); 214 } 215 216 void GlobalAlias::setParent(Module *parent) { 217 if (getParent()) 218 LeakDetector::addGarbageObject(this); 219 Parent = parent; 220 if (getParent()) 221 LeakDetector::removeGarbageObject(this); 222 } 223 224 void GlobalAlias::removeFromParent() { 225 getParent()->getAliasList().remove(this); 226 } 227 228 void GlobalAlias::eraseFromParent() { 229 getParent()->getAliasList().erase(this); 230 } 231 232 void GlobalAlias::setAliasee(Constant *Aliasee) { 233 assert((!Aliasee || Aliasee->getType() == getType()) && 234 "Alias and aliasee types should match!"); 235 236 setOperand(0, Aliasee); 237 } 238 239 static GlobalValue *getAliaseeGV(GlobalAlias *GA) { 240 Constant *C = GA->getAliasee(); 241 assert(C && "Must alias something"); 242 243 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 244 return GV; 245 246 ConstantExpr *CE = cast<ConstantExpr>(C); 247 assert((CE->getOpcode() == Instruction::BitCast || 248 CE->getOpcode() == Instruction::AddrSpaceCast || 249 CE->getOpcode() == Instruction::GetElementPtr) && 250 "Unsupported aliasee"); 251 252 return cast<GlobalValue>(CE->getOperand(0)); 253 } 254 255 GlobalValue *GlobalAlias::getAliasedGlobal() { 256 SmallPtrSet<GlobalValue*, 3> Visited; 257 258 GlobalAlias *GA = this; 259 260 for (;;) { 261 GlobalValue *GV = getAliaseeGV(GA); 262 if (!Visited.insert(GV)) 263 return nullptr; 264 265 // Iterate over aliasing chain. 266 GA = dyn_cast<GlobalAlias>(GV); 267 if (!GA) 268 return GV; 269 } 270 } 271