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/IR/Operator.h" 24 #include "llvm/Support/ErrorHandling.h" 25 using namespace llvm; 26 27 //===----------------------------------------------------------------------===// 28 // GlobalValue Class 29 //===----------------------------------------------------------------------===// 30 31 bool GlobalValue::isMaterializable() const { 32 return getParent() && getParent()->isMaterializable(this); 33 } 34 bool GlobalValue::isDematerializable() const { 35 return getParent() && getParent()->isDematerializable(this); 36 } 37 bool GlobalValue::Materialize(std::string *ErrInfo) { 38 return getParent()->Materialize(this, ErrInfo); 39 } 40 void GlobalValue::Dematerialize() { 41 getParent()->Dematerialize(this); 42 } 43 44 const DataLayout *GlobalValue::getDataLayout() const { 45 return getParent()->getDataLayout(); 46 } 47 48 /// Override destroyConstant to make sure it doesn't get called on 49 /// GlobalValue's because they shouldn't be treated like other constants. 50 void GlobalValue::destroyConstant() { 51 llvm_unreachable("You can't GV->destroyConstant()!"); 52 } 53 54 /// copyAttributesFrom - copy all additional attributes (those not needed to 55 /// create a GlobalValue) from the GlobalValue Src to this one. 56 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 57 setVisibility(Src->getVisibility()); 58 setUnnamedAddr(Src->hasUnnamedAddr()); 59 setDLLStorageClass(Src->getDLLStorageClass()); 60 } 61 62 static const GlobalObject *getBaseObject(const Constant &C) { 63 // FIXME: We should probably return a base + offset pair for non-zero GEPs. 64 return dyn_cast<GlobalObject>(C.stripPointerCasts()); 65 } 66 67 unsigned GlobalValue::getAlignment() const { 68 if (auto *GA = dyn_cast<GlobalAlias>(this)) { 69 // In general we cannot compute this at the IR level, but we try. 70 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee())) 71 return GO->getAlignment(); 72 73 // FIXME: we should also be able to handle: 74 // Alias = Global + Offset 75 // Alias = Absolute 76 return 0; 77 } 78 return cast<GlobalObject>(this)->getAlignment(); 79 } 80 81 void GlobalObject::setAlignment(unsigned Align) { 82 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 83 assert(Align <= MaximumAlignment && 84 "Alignment is greater than MaximumAlignment!"); 85 setGlobalValueSubClassData(Log2_32(Align) + 1); 86 assert(getAlignment() == Align && "Alignment representation error!"); 87 } 88 89 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) { 90 const auto *GV = cast<GlobalObject>(Src); 91 GlobalValue::copyAttributesFrom(GV); 92 setAlignment(GV->getAlignment()); 93 setSection(GV->getSection()); 94 } 95 96 const char *GlobalValue::getSection() const { 97 if (auto *GA = dyn_cast<GlobalAlias>(this)) { 98 // In general we cannot compute this at the IR level, but we try. 99 if (const GlobalObject *GO = getBaseObject(*GA->getAliasee())) 100 return GO->getSection(); 101 return ""; 102 } 103 return cast<GlobalObject>(this)->getSection(); 104 } 105 106 void GlobalObject::setSection(StringRef S) { Section = S; } 107 108 bool GlobalValue::isDeclaration() const { 109 // Globals are definitions if they have an initializer. 110 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 111 return GV->getNumOperands() == 0; 112 113 // Functions are definitions if they have a body. 114 if (const Function *F = dyn_cast<Function>(this)) 115 return F->empty(); 116 117 // Aliases are always definitions. 118 assert(isa<GlobalAlias>(this)); 119 return false; 120 } 121 122 //===----------------------------------------------------------------------===// 123 // GlobalVariable Implementation 124 //===----------------------------------------------------------------------===// 125 126 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 127 Constant *InitVal, const Twine &Name, 128 ThreadLocalMode TLMode, unsigned AddressSpace, 129 bool isExternallyInitialized) 130 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, 131 OperandTraits<GlobalVariable>::op_begin(this), 132 InitVal != nullptr, Link, Name), 133 isConstantGlobal(constant), 134 isExternallyInitializedConstant(isExternallyInitialized) { 135 setThreadLocalMode(TLMode); 136 if (InitVal) { 137 assert(InitVal->getType() == Ty && 138 "Initializer should be the same type as the GlobalVariable!"); 139 Op<0>() = InitVal; 140 } 141 142 LeakDetector::addGarbageObject(this); 143 } 144 145 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 146 LinkageTypes Link, Constant *InitVal, 147 const Twine &Name, GlobalVariable *Before, 148 ThreadLocalMode TLMode, unsigned AddressSpace, 149 bool isExternallyInitialized) 150 : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, 151 OperandTraits<GlobalVariable>::op_begin(this), 152 InitVal != nullptr, Link, Name), 153 isConstantGlobal(constant), 154 isExternallyInitializedConstant(isExternallyInitialized) { 155 setThreadLocalMode(TLMode); 156 if (InitVal) { 157 assert(InitVal->getType() == Ty && 158 "Initializer should be the same type as the GlobalVariable!"); 159 Op<0>() = InitVal; 160 } 161 162 LeakDetector::addGarbageObject(this); 163 164 if (Before) 165 Before->getParent()->getGlobalList().insert(Before, this); 166 else 167 M.getGlobalList().push_back(this); 168 } 169 170 void GlobalVariable::setParent(Module *parent) { 171 if (getParent()) 172 LeakDetector::addGarbageObject(this); 173 Parent = parent; 174 if (getParent()) 175 LeakDetector::removeGarbageObject(this); 176 } 177 178 void GlobalVariable::removeFromParent() { 179 getParent()->getGlobalList().remove(this); 180 } 181 182 void GlobalVariable::eraseFromParent() { 183 getParent()->getGlobalList().erase(this); 184 } 185 186 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, 187 Use *U) { 188 // If you call this, then you better know this GVar has a constant 189 // initializer worth replacing. Enforce that here. 190 assert(getNumOperands() == 1 && 191 "Attempt to replace uses of Constants on a GVar with no initializer"); 192 193 // And, since you know it has an initializer, the From value better be 194 // the initializer :) 195 assert(getOperand(0) == From && 196 "Attempt to replace wrong constant initializer in GVar"); 197 198 // And, you better have a constant for the replacement value 199 assert(isa<Constant>(To) && 200 "Attempt to replace GVar initializer with non-constant"); 201 202 // Okay, preconditions out of the way, replace the constant initializer. 203 this->setOperand(0, cast<Constant>(To)); 204 } 205 206 void GlobalVariable::setInitializer(Constant *InitVal) { 207 if (!InitVal) { 208 if (hasInitializer()) { 209 Op<0>().set(nullptr); 210 NumOperands = 0; 211 } 212 } else { 213 assert(InitVal->getType() == getType()->getElementType() && 214 "Initializer type must match GlobalVariable type"); 215 if (!hasInitializer()) 216 NumOperands = 1; 217 Op<0>().set(InitVal); 218 } 219 } 220 221 /// copyAttributesFrom - copy all additional attributes (those not needed to 222 /// create a GlobalVariable) from the GlobalVariable Src to this one. 223 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { 224 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!"); 225 GlobalObject::copyAttributesFrom(Src); 226 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); 227 setThreadLocalMode(SrcVar->getThreadLocalMode()); 228 } 229 230 231 //===----------------------------------------------------------------------===// 232 // GlobalAlias Implementation 233 //===----------------------------------------------------------------------===// 234 235 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 236 const Twine &Name, Constant *Aliasee, 237 Module *ParentModule) 238 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal, 239 &Op<0>(), 1, Link, Name) { 240 LeakDetector::addGarbageObject(this); 241 Op<0>() = Aliasee; 242 243 if (ParentModule) 244 ParentModule->getAliasList().push_back(this); 245 } 246 247 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 248 LinkageTypes Link, const Twine &Name, 249 Constant *Aliasee, Module *ParentModule) { 250 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); 251 } 252 253 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 254 LinkageTypes Linkage, const Twine &Name, 255 Module *Parent) { 256 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); 257 } 258 259 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 260 LinkageTypes Linkage, const Twine &Name, 261 GlobalValue *Aliasee) { 262 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); 263 } 264 265 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, 266 GlobalValue *Aliasee) { 267 PointerType *PTy = Aliasee->getType(); 268 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, 269 Aliasee); 270 } 271 272 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { 273 return create(Aliasee->getLinkage(), Name, Aliasee); 274 } 275 276 void GlobalAlias::setParent(Module *parent) { 277 if (getParent()) 278 LeakDetector::addGarbageObject(this); 279 Parent = parent; 280 if (getParent()) 281 LeakDetector::removeGarbageObject(this); 282 } 283 284 void GlobalAlias::removeFromParent() { 285 getParent()->getAliasList().remove(this); 286 } 287 288 void GlobalAlias::eraseFromParent() { 289 getParent()->getAliasList().erase(this); 290 } 291 292 void GlobalAlias::setAliasee(Constant *Aliasee) { 293 assert((!Aliasee || Aliasee->getType() == getType()) && 294 "Alias and aliasee types should match!"); 295 setOperand(0, Aliasee); 296 } 297