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() { 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 return cast<GlobalObject>(this)->getComdat(); 149 } 150 151 void GlobalObject::setSection(StringRef S) { Section = S; } 152 153 bool GlobalValue::isDeclaration() const { 154 // Globals are definitions if they have an initializer. 155 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) 156 return GV->getNumOperands() == 0; 157 158 // Functions are definitions if they have a body. 159 if (const Function *F = dyn_cast<Function>(this)) 160 return F->empty() && !F->isMaterializable(); 161 162 // Aliases are always definitions. 163 assert(isa<GlobalAlias>(this)); 164 return false; 165 } 166 167 bool GlobalValue::canIncreaseAlignment() const { 168 // Firstly, can only increase the alignment of a global if it 169 // is a strong definition. 170 if (!isStrongDefinitionForLinker()) 171 return false; 172 173 // It also has to either not have a section defined, or, not have 174 // alignment specified. (If it is assigned a section, the global 175 // could be densely packed with other objects in the section, and 176 // increasing the alignment could cause padding issues.) 177 if (hasSection() && getAlignment() > 0) 178 return false; 179 180 // On ELF platforms, we're further restricted in that we can't 181 // increase the alignment of any variable which might be emitted 182 // into a shared library, and which is exported. If the main 183 // executable accesses a variable found in a shared-lib, the main 184 // exe actually allocates memory for and exports the symbol ITSELF, 185 // overriding the symbol found in the library. That is, at link 186 // time, the observed alignment of the variable is copied into the 187 // executable binary. (A COPY relocation is also generated, to copy 188 // the initial data from the shadowed variable in the shared-lib 189 // into the location in the main binary, before running code.) 190 // 191 // And thus, even though you might think you are defining the 192 // global, and allocating the memory for the global in your object 193 // file, and thus should be able to set the alignment arbitrarily, 194 // that's not actually true. Doing so can cause an ABI breakage; an 195 // executable might have already been built with the previous 196 // alignment of the variable, and then assuming an increased 197 // alignment will be incorrect. 198 199 // Conservatively assume ELF if there's no parent pointer. 200 bool isELF = 201 (!Parent || Triple(Parent->getTargetTriple()).isOSBinFormatELF()); 202 if (isELF && hasDefaultVisibility() && !hasLocalLinkage()) 203 return false; 204 205 return true; 206 } 207 208 //===----------------------------------------------------------------------===// 209 // GlobalVariable Implementation 210 //===----------------------------------------------------------------------===// 211 212 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, 213 Constant *InitVal, const Twine &Name, 214 ThreadLocalMode TLMode, unsigned AddressSpace, 215 bool isExternallyInitialized) 216 : GlobalObject(Ty, Value::GlobalVariableVal, 217 OperandTraits<GlobalVariable>::op_begin(this), 218 InitVal != nullptr, Link, Name, AddressSpace), 219 isConstantGlobal(constant), 220 isExternallyInitializedConstant(isExternallyInitialized) { 221 setThreadLocalMode(TLMode); 222 if (InitVal) { 223 assert(InitVal->getType() == Ty && 224 "Initializer should be the same type as the GlobalVariable!"); 225 Op<0>() = InitVal; 226 } 227 } 228 229 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, 230 LinkageTypes Link, Constant *InitVal, 231 const Twine &Name, GlobalVariable *Before, 232 ThreadLocalMode TLMode, unsigned AddressSpace, 233 bool isExternallyInitialized) 234 : GlobalObject(Ty, Value::GlobalVariableVal, 235 OperandTraits<GlobalVariable>::op_begin(this), 236 InitVal != nullptr, Link, Name, AddressSpace), 237 isConstantGlobal(constant), 238 isExternallyInitializedConstant(isExternallyInitialized) { 239 setThreadLocalMode(TLMode); 240 if (InitVal) { 241 assert(InitVal->getType() == Ty && 242 "Initializer should be the same type as the GlobalVariable!"); 243 Op<0>() = InitVal; 244 } 245 246 if (Before) 247 Before->getParent()->getGlobalList().insert(Before->getIterator(), this); 248 else 249 M.getGlobalList().push_back(this); 250 } 251 252 void GlobalVariable::setParent(Module *parent) { 253 Parent = parent; 254 } 255 256 void GlobalVariable::removeFromParent() { 257 getParent()->getGlobalList().remove(getIterator()); 258 } 259 260 void GlobalVariable::eraseFromParent() { 261 getParent()->getGlobalList().erase(getIterator()); 262 } 263 264 void GlobalVariable::setInitializer(Constant *InitVal) { 265 if (!InitVal) { 266 if (hasInitializer()) { 267 // Note, the num operands is used to compute the offset of the operand, so 268 // the order here matters. Clearing the operand then clearing the num 269 // operands ensures we have the correct offset to the operand. 270 Op<0>().set(nullptr); 271 setGlobalVariableNumOperands(0); 272 } 273 } else { 274 assert(InitVal->getType() == getValueType() && 275 "Initializer type must match GlobalVariable type"); 276 // Note, the num operands is used to compute the offset of the operand, so 277 // the order here matters. We need to set num operands to 1 first so that 278 // we get the correct offset to the first operand when we set it. 279 if (!hasInitializer()) 280 setGlobalVariableNumOperands(1); 281 Op<0>().set(InitVal); 282 } 283 } 284 285 /// Copy all additional attributes (those not needed to create a GlobalVariable) 286 /// from the GlobalVariable Src to this one. 287 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { 288 GlobalObject::copyAttributesFrom(Src); 289 if (const GlobalVariable *SrcVar = dyn_cast<GlobalVariable>(Src)) { 290 setThreadLocalMode(SrcVar->getThreadLocalMode()); 291 setExternallyInitialized(SrcVar->isExternallyInitialized()); 292 } 293 } 294 295 296 //===----------------------------------------------------------------------===// 297 // GlobalAlias Implementation 298 //===----------------------------------------------------------------------===// 299 300 GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, 301 const Twine &Name, Constant *Aliasee, 302 Module *ParentModule) 303 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name, 304 AddressSpace) { 305 Op<0>() = Aliasee; 306 307 if (ParentModule) 308 ParentModule->getAliasList().push_back(this); 309 } 310 311 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 312 LinkageTypes Link, const Twine &Name, 313 Constant *Aliasee, Module *ParentModule) { 314 return new GlobalAlias(Ty, AddressSpace, Link, Name, Aliasee, ParentModule); 315 } 316 317 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 318 LinkageTypes Linkage, const Twine &Name, 319 Module *Parent) { 320 return create(Ty, AddressSpace, Linkage, Name, nullptr, Parent); 321 } 322 323 GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace, 324 LinkageTypes Linkage, const Twine &Name, 325 GlobalValue *Aliasee) { 326 return create(Ty, AddressSpace, Linkage, Name, Aliasee, Aliasee->getParent()); 327 } 328 329 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, 330 GlobalValue *Aliasee) { 331 PointerType *PTy = Aliasee->getType(); 332 return create(PTy->getElementType(), PTy->getAddressSpace(), Link, Name, 333 Aliasee); 334 } 335 336 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { 337 return create(Aliasee->getLinkage(), Name, Aliasee); 338 } 339 340 void GlobalAlias::setParent(Module *parent) { 341 Parent = parent; 342 } 343 344 void GlobalAlias::removeFromParent() { 345 getParent()->getAliasList().remove(getIterator()); 346 } 347 348 void GlobalAlias::eraseFromParent() { 349 getParent()->getAliasList().erase(getIterator()); 350 } 351 352 void GlobalAlias::setAliasee(Constant *Aliasee) { 353 assert((!Aliasee || Aliasee->getType() == getType()) && 354 "Alias and aliasee types should match!"); 355 setOperand(0, Aliasee); 356 } 357