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/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/Triple.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/GlobalAlias.h" 20 #include "llvm/IR/GlobalValue.h" 21 #include "llvm/IR/GlobalVariable.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 if (const Function *F = dyn_cast<Function>(this)) 33 return F->isMaterializable(); 34 return false; 35 } 36 std::error_code GlobalValue::materialize() { 37 return getParent()->materialize(this); 38 } 39 40 /// Override destroyConstantImpl to make sure it doesn't get called on 41 /// GlobalValue's because they shouldn't be treated like other constants. 42 void GlobalValue::destroyConstantImpl() { 43 llvm_unreachable("You can't GV->destroyConstantImpl()!"); 44 } 45 46 Value *GlobalValue::handleOperandChangeImpl(Value *From, Value *To) { 47 llvm_unreachable("Unsupported class for handleOperandChange()!"); 48 } 49 50 /// copyAttributesFrom - copy all additional attributes (those not needed to 51 /// create a GlobalValue) from the GlobalValue Src to this one. 52 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { 53 setVisibility(Src->getVisibility()); 54 setUnnamedAddr(Src->hasUnnamedAddr()); 55 setDLLStorageClass(Src->getDLLStorageClass()); 56 } 57 58 unsigned GlobalValue::getAlignment() const { 59 if (auto *GA = dyn_cast<GlobalAlias>(this)) { 60 // In general we cannot compute this at the IR level, but we try. 61 if (const GlobalObject *GO = GA->getBaseObject()) 62 return GO->getAlignment(); 63 64 // FIXME: we should also be able to handle: 65 // Alias = Global + Offset 66 // Alias = Absolute 67 return 0; 68 } 69 return cast<GlobalObject>(this)->getAlignment(); 70 } 71 72 void GlobalObject::setAlignment(unsigned Align) { 73 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 74 assert(Align <= MaximumAlignment && 75 "Alignment is greater than MaximumAlignment!"); 76 unsigned AlignmentData = Log2_32(Align) + 1; 77 unsigned OldData = getGlobalValueSubClassData(); 78 setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); 79 assert(getAlignment() == Align && "Alignment representation error!"); 80 } 81 82 unsigned GlobalObject::getGlobalObjectSubClassData() const { 83 unsigned ValueData = getGlobalValueSubClassData(); 84 return ValueData >> AlignmentBits; 85 } 86 87 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) { 88 unsigned OldData = getGlobalValueSubClassData(); 89 setGlobalValueSubClassData((OldData & AlignmentMask) | 90 (Val << AlignmentBits)); 91 assert(getGlobalObjectSubClassData() == Val && "representation error"); 92 } 93 94 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) { 95 GlobalValue::copyAttributesFrom(Src); 96 if (const auto *GV = dyn_cast<GlobalObject>(Src)) { 97 setAlignment(GV->getAlignment()); 98 setSection(GV->getSection()); 99 } 100 } 101 102 std::string GlobalValue::getGlobalIdentifier(StringRef Name, 103 GlobalValue::LinkageTypes Linkage, 104 StringRef FileName) { 105 106 // Value names may be prefixed with a binary '1' to indicate 107 // that the backend should not modify the symbols due to any platform 108 // naming convention. Do not include that '1' in the PGO profile name. 109 if (Name[0] == '\1') 110 Name = Name.substr(1); 111 112 std::string NewName = Name; 113 if (llvm::GlobalValue::isLocalLinkage(Linkage)) { 114 // For local symbols, prepend the main file name to distinguish them. 115 // Do not include the full path in the file name since there's no guarantee 116 // that it will stay the same, e.g., if the files are checked out from 117 // version control in different locations. 118 if (FileName.empty()) 119 NewName = NewName.insert(0, "<unknown>:"); 120 else 121 NewName = NewName.insert(0, FileName.str() + ":"); 122 } 123 return NewName; 124 } 125 126 std::string GlobalValue::getGlobalIdentifier() const { 127 return getGlobalIdentifier(getName(), getLinkage(), 128 getParent()->getSourceFileName()); 129 } 130 131 const char *GlobalValue::getSection() const { 132 if (auto *GA = dyn_cast<GlobalAlias>(this)) { 133 // In general we cannot compute this at the IR level, but we try. 134 if (const GlobalObject *GO = GA->getBaseObject()) 135 return GO->getSection(); 136 return ""; 137 } 138 return cast<GlobalObject>(this)->getSection(); 139 } 140 141 Comdat *GlobalValue::getComdat() { 142 if (auto *GA = dyn_cast<GlobalAlias>(this)) { 143 // In general we cannot compute this at the IR level, but we try. 144 if (const GlobalObject *GO = GA->getBaseObject()) 145 return const_cast<GlobalObject *>(GO)->getComdat(); 146 return nullptr; 147 } 148 // ifunc and its resolver are separate things so don't use resolver comdat. 149 if (isa<GlobalIFunc>(this)) 150 return nullptr; 151 return cast<GlobalObject>(this)->getComdat(); 152 } 153 154 void GlobalObject::setSection(StringRef S) { Section = S; } 155 156 bool GlobalValue::isDeclaration() const { 157 // Globals are definitions if they have an initializer. 158 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 159 return GV->getNumOperands() == 0; 160 161 // Functions are definitions if they have a body. 162 if (const Function *F = dyn_cast<Function>(this)) 163 return F->empty() && !F->isMaterializable(); 164 165 // Aliases and ifuncs are always definitions. 166 assert(isa<GlobalIndirectSymbol>(this)); 167 return false; 168 } 169 170 bool GlobalValue::canIncreaseAlignment() const { 171 // Firstly, can only increase the alignment of a global if it 172 // is a strong definition. 173 if (!isStrongDefinitionForLinker()) 174 return false; 175 176 // It also has to either not have a section defined, or, not have 177 // alignment specified. (If it is assigned a section, the global 178 // could be densely packed with other objects in the section, and 179 // increasing the alignment could cause padding issues.) 180 if (hasSection() && getAlignment() > 0) 181 return false; 182 183 // On ELF platforms, we're further restricted in that we can't 184 // increase the alignment of any variable which might be emitted 185 // into a shared library, and which is exported. If the main 186 // executable accesses a variable found in a shared-lib, the main 187 // exe actually allocates memory for and exports the symbol ITSELF, 188 // overriding the symbol found in the library. That is, at link 189 // time, the observed alignment of the variable is copied into the 190 // executable binary. (A COPY relocation is also generated, to copy 191 // the initial data from the shadowed variable in the shared-lib 192 // into the location in the main binary, before running code.) 193 // 194 // And thus, even though you might think you are defining the 195 // global, and allocating the memory for the global in your object 196 // file, and thus should be able to set the alignment arbitrarily, 197 // that's not actually true. Doing so can cause an ABI breakage; an 198 // executable might have already been built with the previous 199 // alignment of the variable, and then assuming an increased 200 // alignment will be incorrect. 201 202 // Conservatively assume ELF if there's no parent pointer. 203 bool isELF = 204 (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF()); 205 if (isELF && hasDefaultVisibility() && !hasLocalLinkage()) 206 return false; 207 208 return true; 209 } 210 211 //===----------------------------------------------------------------------===// 212 // GlobalVariable Implementation 213 //===----------------------------------------------------------------------===// 214 215 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 216 Constant *InitVal, const Twine &Name, 217 ThreadLocalMode TLMode, unsigned AddressSpace, 218 bool isExternallyInitialized) 219 : GlobalObject(Ty, Value::GlobalVariableVal, 220 OperandTraits<GlobalVariable>::op_begin(this), 221 InitVal != nullptr, Link, Name, AddressSpace), 222 isConstantGlobal(constant), 223 isExternallyInitializedConstant(isExternallyInitialized) { 224 setThreadLocalMode(TLMode); 225 if (InitVal) { 226 assert(InitVal->getType() == Ty && 227 "Initializer should be the same type as the GlobalVariable!"); 228 Op<0>() = InitVal; 229 } 230 } 231 232 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 233 LinkageTypes Link, Constant *InitVal, 234 const Twine &Name, GlobalVariable *Before, 235 ThreadLocalMode TLMode, unsigned AddressSpace, 236 bool isExternallyInitialized) 237 : GlobalObject(Ty, Value::GlobalVariableVal, 238 OperandTraits<GlobalVariable>::op_begin(this), 239 InitVal != nullptr, Link, Name, AddressSpace), 240 isConstantGlobal(constant), 241 isExternallyInitializedConstant(isExternallyInitialized) { 242 setThreadLocalMode(TLMode); 243 if (InitVal) { 244 assert(InitVal->getType() == Ty && 245 "Initializer should be the same type as the GlobalVariable!"); 246 Op<0>() = InitVal; 247 } 248 249 if (Before) 250 Before->getParent()->getGlobalList().insert(Before->getIterator(), this); 251 else 252 M.getGlobalList().push_back(this); 253 } 254 255 void GlobalVariable::setParent(Module *parent) { 256 Parent = parent; 257 } 258 259 void GlobalVariable::removeFromParent() { 260 getParent()->getGlobalList().remove(getIterator()); 261 } 262 263 void GlobalVariable::eraseFromParent() { 264 getParent()->getGlobalList().erase(getIterator()); 265 } 266 267 void GlobalVariable::setInitializer(Constant *InitVal) { 268 if (!InitVal) { 269 if (hasInitializer()) { 270 // Note, the num operands is used to compute the offset of the operand, so 271 // the order here matters. Clearing the operand then clearing the num 272 // operands ensures we have the correct offset to the operand. 273 Op<0>().set(nullptr); 274 setGlobalVariableNumOperands(0); 275 } 276 } else { 277 assert(InitVal->getType() == getValueType() && 278 "Initializer type must match GlobalVariable type"); 279 // Note, the num operands is used to compute the offset of the operand, so 280 // the order here matters. We need to set num operands to 1 first so that 281 // we get the correct offset to the first operand when we set it. 282 if (!hasInitializer()) 283 setGlobalVariableNumOperands(1); 284 Op<0>().set(InitVal); 285 } 286 } 287 288 /// Copy all additional attributes (those not needed to create a GlobalVariable) 289 /// from the GlobalVariable Src to this one. 290 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { 291 GlobalObject::copyAttributesFrom(Src); 292 if (const GlobalVariable *SrcVar = dyn_cast<GlobalVariable>(Src)) { 293 setThreadLocalMode(SrcVar->getThreadLocalMode()); 294 setExternallyInitialized(SrcVar->isExternallyInitialized()); 295 } 296 } 297 298 299 //===----------------------------------------------------------------------===// 300 // GlobalIndirectSymbol Implementation 301 //===----------------------------------------------------------------------===// 302 303 GlobalIndirectSymbol::GlobalIndirectSymbol(Type *Ty, ValueTy VTy, 304 unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, 305 Constant *Symbol) 306 : GlobalValue(Ty, VTy, &Op<0>(), 1, Linkage, Name, AddressSpace) { 307 Op<0>() = Symbol; 308 } 309 310 311 //===----------------------------------------------------------------------===// 312 // GlobalAlias Implementation 313 //===----------------------------------------------------------------------===// 314 315 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 316 const Twine &Name, Constant *Aliasee, 317 Module *ParentModule) 318 : GlobalIndirectSymbol(Ty, Value::GlobalAliasVal, AddressSpace, Link, Name, 319 Aliasee) { 320 if (ParentModule) 321 ParentModule->getAliasList().push_back(this); 322 } 323 324 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 325 LinkageTypes Link, const Twine &Name, 326 Constant *Aliasee, Module *ParentModule) { 327 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); 328 } 329 330 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 331 LinkageTypes Linkage, const Twine &Name, 332 Module *Parent) { 333 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); 334 } 335 336 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 337 LinkageTypes Linkage, const Twine &Name, 338 GlobalValue *Aliasee) { 339 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); 340 } 341 342 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, 343 GlobalValue *Aliasee) { 344 PointerType *PTy = Aliasee->getType(); 345 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, 346 Aliasee); 347 } 348 349 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { 350 return create(Aliasee->getLinkage(), Name, Aliasee); 351 } 352 353 void GlobalAlias::setParent(Module *parent) { 354 Parent = parent; 355 } 356 357 void GlobalAlias::removeFromParent() { 358 getParent()->getAliasList().remove(getIterator()); 359 } 360 361 void GlobalAlias::eraseFromParent() { 362 getParent()->getAliasList().erase(getIterator()); 363 } 364 365 void GlobalAlias::setAliasee(Constant *Aliasee) { 366 assert((!Aliasee || Aliasee->getType() == getType()) && 367 "Alias and aliasee types should match!"); 368 setIndirectSymbol(Aliasee); 369 } 370 371 //===----------------------------------------------------------------------===// 372 // GlobalIFunc Implementation 373 //===----------------------------------------------------------------------===// 374 375 GlobalIFunc::GlobalIFunc(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 376 const Twine &Name, Constant *Resolver, 377 Module *ParentModule) 378 : GlobalIndirectSymbol(Ty, Value::GlobalIFuncVal, AddressSpace, Link, Name, 379 Resolver) { 380 if (ParentModule) 381 ParentModule->getIFuncList().push_back(this); 382 } 383 384 GlobalIFunc *GlobalIFunc::create(Type *Ty, unsigned AddressSpace, 385 LinkageTypes Link, const Twine &Name, 386 Constant *Resolver, Module *ParentModule) { 387 return new GlobalIFunc(Ty, AddressSpace, Link, Name, Resolver, ParentModule); 388 } 389 390 void GlobalIFunc::setParent(Module *parent) { 391 Parent = parent; 392 } 393 394 void GlobalIFunc::removeFromParent() { 395 getParent()->getIFuncList().remove(getIterator()); 396 } 397 398 void GlobalIFunc::eraseFromParent() { 399 getParent()->getIFuncList().erase(getIterator()); 400 } 401