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/GlobalValue.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalAlias.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/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());
58 void GlobalValue::setAlignment(unsigned Align) {
59 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
60 assert(Align <= MaximumAlignment &&
61 "Alignment is greater than MaximumAlignment!");
62 Alignment = Log2_32(Align) + 1;
63 assert(getAlignment() == Align && "Alignment representation error!");
66 bool GlobalValue::isDeclaration() const {
67 // Globals are definitions if they have an initializer.
68 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
69 return GV->getNumOperands() == 0;
71 // Functions are definitions if they have a body.
72 if (const Function *F = dyn_cast<Function>(this))
75 // Aliases are always definitions.
76 assert(isa<GlobalAlias>(this));
80 //===----------------------------------------------------------------------===//
81 // GlobalVariable Implementation
82 //===----------------------------------------------------------------------===//
84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85 Constant *InitVal, const Twine &Name,
86 ThreadLocalMode TLMode, unsigned AddressSpace)
87 : GlobalValue(PointerType::get(Ty, AddressSpace),
88 Value::GlobalVariableVal,
89 OperandTraits<GlobalVariable>::op_begin(this),
90 InitVal != 0, Link, Name),
91 isConstantGlobal(constant), threadLocalMode(TLMode) {
93 assert(InitVal->getType() == Ty &&
94 "Initializer should be the same type as the GlobalVariable!");
98 LeakDetector::addGarbageObject(this);
101 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
102 LinkageTypes Link, Constant *InitVal,
104 GlobalVariable *Before, ThreadLocalMode TLMode,
105 unsigned AddressSpace)
106 : GlobalValue(PointerType::get(Ty, AddressSpace),
107 Value::GlobalVariableVal,
108 OperandTraits<GlobalVariable>::op_begin(this),
109 InitVal != 0, Link, Name),
110 isConstantGlobal(constant), threadLocalMode(TLMode) {
112 assert(InitVal->getType() == Ty &&
113 "Initializer should be the same type as the GlobalVariable!");
117 LeakDetector::addGarbageObject(this);
120 Before->getParent()->getGlobalList().insert(Before, this);
122 M.getGlobalList().push_back(this);
125 void GlobalVariable::setParent(Module *parent) {
127 LeakDetector::addGarbageObject(this);
130 LeakDetector::removeGarbageObject(this);
133 void GlobalVariable::removeFromParent() {
134 getParent()->getGlobalList().remove(this);
137 void GlobalVariable::eraseFromParent() {
138 getParent()->getGlobalList().erase(this);
141 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
143 // If you call this, then you better know this GVar has a constant
144 // initializer worth replacing. Enforce that here.
145 assert(getNumOperands() == 1 &&
146 "Attempt to replace uses of Constants on a GVar with no initializer");
148 // And, since you know it has an initializer, the From value better be
149 // the initializer :)
150 assert(getOperand(0) == From &&
151 "Attempt to replace wrong constant initializer in GVar");
153 // And, you better have a constant for the replacement value
154 assert(isa<Constant>(To) &&
155 "Attempt to replace GVar initializer with non-constant");
157 // Okay, preconditions out of the way, replace the constant initializer.
158 this->setOperand(0, cast<Constant>(To));
161 void GlobalVariable::setInitializer(Constant *InitVal) {
163 if (hasInitializer()) {
168 assert(InitVal->getType() == getType()->getElementType() &&
169 "Initializer type must match GlobalVariable type");
170 if (!hasInitializer())
172 Op<0>().set(InitVal);
176 /// copyAttributesFrom - copy all additional attributes (those not needed to
177 /// create a GlobalVariable) from the GlobalVariable Src to this one.
178 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
179 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
180 GlobalValue::copyAttributesFrom(Src);
181 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
182 setThreadLocal(SrcVar->isThreadLocal());
186 //===----------------------------------------------------------------------===//
187 // GlobalAlias Implementation
188 //===----------------------------------------------------------------------===//
190 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
191 const Twine &Name, Constant* aliasee,
192 Module *ParentModule)
193 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
194 LeakDetector::addGarbageObject(this);
197 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
201 ParentModule->getAliasList().push_back(this);
204 void GlobalAlias::setParent(Module *parent) {
206 LeakDetector::addGarbageObject(this);
209 LeakDetector::removeGarbageObject(this);
212 void GlobalAlias::removeFromParent() {
213 getParent()->getAliasList().remove(this);
216 void GlobalAlias::eraseFromParent() {
217 getParent()->getAliasList().erase(this);
220 void GlobalAlias::setAliasee(Constant *Aliasee) {
221 assert((!Aliasee || Aliasee->getType() == getType()) &&
222 "Alias and aliasee types should match!");
224 setOperand(0, Aliasee);
227 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
228 const Constant *C = getAliasee();
229 if (C == 0) return 0;
231 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
234 const ConstantExpr *CE = cast<ConstantExpr>(C);
235 assert((CE->getOpcode() == Instruction::BitCast ||
236 CE->getOpcode() == Instruction::GetElementPtr) &&
237 "Unsupported aliasee");
239 return cast<GlobalValue>(CE->getOperand(0));
242 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
243 SmallPtrSet<const GlobalValue*, 3> Visited;
245 // Check if we need to stop early.
246 if (stopOnWeak && mayBeOverridden())
249 const GlobalValue *GV = getAliasedGlobal();
252 // Iterate over aliasing chain, stopping on weak alias if necessary.
253 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
254 if (stopOnWeak && GA->mayBeOverridden())
257 GV = GA->getAliasedGlobal();
259 if (!Visited.insert(GV))