1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the IR
13 //===----------------------------------------------------------------------===//
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/Module.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LeakDetector.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 bool GlobalValue::isMaterializable() const {
31 return getParent() && getParent()->isMaterializable(this);
33 bool GlobalValue::isDematerializable() const {
34 return getParent() && getParent()->isDematerializable(this);
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
39 void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
43 /// Override destroyConstant to make sure it doesn't get called on
44 /// GlobalValue's because they shouldn't be treated like other constants.
45 void GlobalValue::destroyConstant() {
46 llvm_unreachable("You can't GV->destroyConstant()!");
49 /// copyAttributesFrom - copy all additional attributes (those not needed to
50 /// create a GlobalValue) from the GlobalValue Src to this one.
51 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setAlignment(Src->getAlignment());
53 setSection(Src->getSection());
54 setVisibility(Src->getVisibility());
55 setUnnamedAddr(Src->hasUnnamedAddr());
56 setDLLStorageClass(Src->getDLLStorageClass());
59 void GlobalValue::setAlignment(unsigned Align) {
60 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
61 assert(Align <= MaximumAlignment &&
62 "Alignment is greater than MaximumAlignment!");
63 Alignment = Log2_32(Align) + 1;
64 assert(getAlignment() == Align && "Alignment representation error!");
67 bool GlobalValue::isDeclaration() const {
68 // Globals are definitions if they have an initializer.
69 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
70 return GV->getNumOperands() == 0;
72 // Functions are definitions if they have a body.
73 if (const Function *F = dyn_cast<Function>(this))
76 // Aliases are always definitions.
77 assert(isa<GlobalAlias>(this));
81 //===----------------------------------------------------------------------===//
82 // GlobalVariable Implementation
83 //===----------------------------------------------------------------------===//
85 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
87 const Twine &Name, ThreadLocalMode TLMode,
88 unsigned AddressSpace,
89 bool isExternallyInitialized)
90 : GlobalValue(PointerType::get(Ty, AddressSpace),
91 Value::GlobalVariableVal,
92 OperandTraits<GlobalVariable>::op_begin(this),
93 InitVal != 0, Link, Name),
94 isConstantGlobal(constant), threadLocalMode(TLMode),
95 isExternallyInitializedConstant(isExternallyInitialized) {
97 assert(InitVal->getType() == Ty &&
98 "Initializer should be the same type as the GlobalVariable!");
102 LeakDetector::addGarbageObject(this);
105 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
106 LinkageTypes Link, Constant *InitVal,
108 GlobalVariable *Before, ThreadLocalMode TLMode,
109 unsigned AddressSpace,
110 bool isExternallyInitialized)
111 : GlobalValue(PointerType::get(Ty, AddressSpace),
112 Value::GlobalVariableVal,
113 OperandTraits<GlobalVariable>::op_begin(this),
114 InitVal != 0, Link, Name),
115 isConstantGlobal(constant), threadLocalMode(TLMode),
116 isExternallyInitializedConstant(isExternallyInitialized) {
118 assert(InitVal->getType() == Ty &&
119 "Initializer should be the same type as the GlobalVariable!");
123 LeakDetector::addGarbageObject(this);
126 Before->getParent()->getGlobalList().insert(Before, this);
128 M.getGlobalList().push_back(this);
131 void GlobalVariable::setParent(Module *parent) {
133 LeakDetector::addGarbageObject(this);
136 LeakDetector::removeGarbageObject(this);
139 void GlobalVariable::removeFromParent() {
140 getParent()->getGlobalList().remove(this);
143 void GlobalVariable::eraseFromParent() {
144 getParent()->getGlobalList().erase(this);
147 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
149 // If you call this, then you better know this GVar has a constant
150 // initializer worth replacing. Enforce that here.
151 assert(getNumOperands() == 1 &&
152 "Attempt to replace uses of Constants on a GVar with no initializer");
154 // And, since you know it has an initializer, the From value better be
155 // the initializer :)
156 assert(getOperand(0) == From &&
157 "Attempt to replace wrong constant initializer in GVar");
159 // And, you better have a constant for the replacement value
160 assert(isa<Constant>(To) &&
161 "Attempt to replace GVar initializer with non-constant");
163 // Okay, preconditions out of the way, replace the constant initializer.
164 this->setOperand(0, cast<Constant>(To));
167 void GlobalVariable::setInitializer(Constant *InitVal) {
169 if (hasInitializer()) {
174 assert(InitVal->getType() == getType()->getElementType() &&
175 "Initializer type must match GlobalVariable type");
176 if (!hasInitializer())
178 Op<0>().set(InitVal);
182 /// copyAttributesFrom - copy all additional attributes (those not needed to
183 /// create a GlobalVariable) from the GlobalVariable Src to this one.
184 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
185 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
186 GlobalValue::copyAttributesFrom(Src);
187 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
188 setThreadLocalMode(SrcVar->getThreadLocalMode());
192 //===----------------------------------------------------------------------===//
193 // GlobalAlias Implementation
194 //===----------------------------------------------------------------------===//
196 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
197 const Twine &Name, Constant* aliasee,
198 Module *ParentModule)
199 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
200 LeakDetector::addGarbageObject(this);
203 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
207 ParentModule->getAliasList().push_back(this);
210 void GlobalAlias::setParent(Module *parent) {
212 LeakDetector::addGarbageObject(this);
215 LeakDetector::removeGarbageObject(this);
218 void GlobalAlias::removeFromParent() {
219 getParent()->getAliasList().remove(this);
222 void GlobalAlias::eraseFromParent() {
223 getParent()->getAliasList().erase(this);
226 void GlobalAlias::setAliasee(Constant *Aliasee) {
227 assert((!Aliasee || Aliasee->getType() == getType()) &&
228 "Alias and aliasee types should match!");
230 setOperand(0, Aliasee);
233 GlobalValue *GlobalAlias::getAliasedGlobal() {
234 Constant *C = getAliasee();
235 if (C == 0) return 0;
237 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
240 ConstantExpr *CE = cast<ConstantExpr>(C);
241 assert((CE->getOpcode() == Instruction::BitCast ||
242 CE->getOpcode() == Instruction::AddrSpaceCast ||
243 CE->getOpcode() == Instruction::GetElementPtr) &&
244 "Unsupported aliasee");
246 return cast<GlobalValue>(CE->getOperand(0));
249 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
250 SmallPtrSet<GlobalValue*, 3> Visited;
252 // Check if we need to stop early.
253 if (stopOnWeak && mayBeOverridden())
256 GlobalValue *GV = getAliasedGlobal();
259 // Iterate over aliasing chain, stopping on weak alias if necessary.
260 while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
261 if (stopOnWeak && GA->mayBeOverridden())
264 GV = GA->getAliasedGlobal();
266 if (!Visited.insert(GV))