consolidate GlobalValue::isDeclaration into one
[oota-llvm.git] / lib / VMCore / Globals.cpp
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 VMCore
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/LeakDetector.h"
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 //                            GlobalValue Class
27 //===----------------------------------------------------------------------===//
28
29 bool GlobalValue::isMaterializable() const {
30   return getParent() && getParent()->isMaterializable(this);
31 }
32 bool GlobalValue::isDematerializable() const {
33   return getParent() && getParent()->isDematerializable(this);
34 }
35 bool GlobalValue::Materialize(std::string *ErrInfo) {
36   return getParent()->Materialize(this, ErrInfo);
37 }
38 void GlobalValue::Dematerialize() {
39   getParent()->Dematerialize(this);
40 }
41
42 /// Override destroyConstant to make sure it doesn't get called on
43 /// GlobalValue's because they shouldn't be treated like other constants.
44 void GlobalValue::destroyConstant() {
45   llvm_unreachable("You can't GV->destroyConstant()!");
46 }
47
48 /// copyAttributesFrom - copy all additional attributes (those not needed to
49 /// create a GlobalValue) from the GlobalValue Src to this one.
50 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51   setAlignment(Src->getAlignment());
52   setSection(Src->getSection());
53   setVisibility(Src->getVisibility());
54   setUnnamedAddr(Src->hasUnnamedAddr());
55 }
56
57 void GlobalValue::setAlignment(unsigned Align) {
58   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
59   assert(Align <= MaximumAlignment &&
60          "Alignment is greater than MaximumAlignment!");
61   Alignment = Log2_32(Align) + 1;
62   assert(getAlignment() == Align && "Alignment representation error!");
63 }
64
65 bool GlobalValue::isDeclaration() const {
66   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
67     return GV->getNumOperands() == 0;
68
69   if (const Function *F = dyn_cast<Function>(this))
70     return F->empty();
71   
72   const GlobalAlias *GA = cast<GlobalAlias>(this);
73   if (const GlobalValue *AV = GA->getAliasedGlobal())
74     return AV->isDeclaration();
75   return false;
76 }
77   
78 //===----------------------------------------------------------------------===//
79 // GlobalVariable Implementation
80 //===----------------------------------------------------------------------===//
81
82 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
83                                Constant *InitVal, const Twine &Name,
84                                bool ThreadLocal, unsigned AddressSpace)
85   : GlobalValue(PointerType::get(Ty, AddressSpace), 
86                 Value::GlobalVariableVal,
87                 OperandTraits<GlobalVariable>::op_begin(this),
88                 InitVal != 0, Link, Name),
89     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
90   if (InitVal) {
91     assert(InitVal->getType() == Ty &&
92            "Initializer should be the same type as the GlobalVariable!");
93     Op<0>() = InitVal;
94   }
95
96   LeakDetector::addGarbageObject(this);
97 }
98
99 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
100                                LinkageTypes Link, Constant *InitVal,
101                                const Twine &Name,
102                                GlobalVariable *Before, bool ThreadLocal,
103                                unsigned AddressSpace)
104   : GlobalValue(PointerType::get(Ty, AddressSpace), 
105                 Value::GlobalVariableVal,
106                 OperandTraits<GlobalVariable>::op_begin(this),
107                 InitVal != 0, Link, Name),
108     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
109   if (InitVal) {
110     assert(InitVal->getType() == Ty &&
111            "Initializer should be the same type as the GlobalVariable!");
112     Op<0>() = InitVal;
113   }
114   
115   LeakDetector::addGarbageObject(this);
116   
117   if (Before)
118     Before->getParent()->getGlobalList().insert(Before, this);
119   else
120     M.getGlobalList().push_back(this);
121 }
122
123 void GlobalVariable::setParent(Module *parent) {
124   if (getParent())
125     LeakDetector::addGarbageObject(this);
126   Parent = parent;
127   if (getParent())
128     LeakDetector::removeGarbageObject(this);
129 }
130
131 void GlobalVariable::removeFromParent() {
132   getParent()->getGlobalList().remove(this);
133 }
134
135 void GlobalVariable::eraseFromParent() {
136   getParent()->getGlobalList().erase(this);
137 }
138
139 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
140                                                  Use *U) {
141   // If you call this, then you better know this GVar has a constant
142   // initializer worth replacing. Enforce that here.
143   assert(getNumOperands() == 1 &&
144          "Attempt to replace uses of Constants on a GVar with no initializer");
145
146   // And, since you know it has an initializer, the From value better be
147   // the initializer :)
148   assert(getOperand(0) == From &&
149          "Attempt to replace wrong constant initializer in GVar");
150
151   // And, you better have a constant for the replacement value
152   assert(isa<Constant>(To) &&
153          "Attempt to replace GVar initializer with non-constant");
154
155   // Okay, preconditions out of the way, replace the constant initializer.
156   this->setOperand(0, cast<Constant>(To));
157 }
158
159 void GlobalVariable::setInitializer(Constant *InitVal) {
160   if (InitVal == 0) {
161     if (hasInitializer()) {
162       Op<0>().set(0);
163       NumOperands = 0;
164     }
165   } else {
166     assert(InitVal->getType() == getType()->getElementType() &&
167            "Initializer type must match GlobalVariable type");
168     if (!hasInitializer())
169       NumOperands = 1;
170     Op<0>().set(InitVal);
171   }
172 }
173
174 /// copyAttributesFrom - copy all additional attributes (those not needed to
175 /// create a GlobalVariable) from the GlobalVariable Src to this one.
176 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
177   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
178   GlobalValue::copyAttributesFrom(Src);
179   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
180   setThreadLocal(SrcVar->isThreadLocal());
181 }
182
183
184 //===----------------------------------------------------------------------===//
185 // GlobalAlias Implementation
186 //===----------------------------------------------------------------------===//
187
188 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
189                          const Twine &Name, Constant* aliasee,
190                          Module *ParentModule)
191   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
192   LeakDetector::addGarbageObject(this);
193
194   if (aliasee)
195     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
196   Op<0>() = aliasee;
197
198   if (ParentModule)
199     ParentModule->getAliasList().push_back(this);
200 }
201
202 void GlobalAlias::setParent(Module *parent) {
203   if (getParent())
204     LeakDetector::addGarbageObject(this);
205   Parent = parent;
206   if (getParent())
207     LeakDetector::removeGarbageObject(this);
208 }
209
210 void GlobalAlias::removeFromParent() {
211   getParent()->getAliasList().remove(this);
212 }
213
214 void GlobalAlias::eraseFromParent() {
215   getParent()->getAliasList().erase(this);
216 }
217
218 void GlobalAlias::setAliasee(Constant *Aliasee) {
219   assert((!Aliasee || Aliasee->getType() == getType()) &&
220          "Alias and aliasee types should match!");
221   
222   setOperand(0, Aliasee);
223 }
224
225 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
226   const Constant *C = getAliasee();
227   if (C == 0) return 0;
228   
229   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
230     return GV;
231
232   const ConstantExpr *CE = cast<ConstantExpr>(C);
233   assert((CE->getOpcode() == Instruction::BitCast || 
234           CE->getOpcode() == Instruction::GetElementPtr) &&
235          "Unsupported aliasee");
236   
237   return dyn_cast<GlobalValue>(CE->getOperand(0));
238 }
239
240 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
241   SmallPtrSet<const GlobalValue*, 3> Visited;
242
243   // Check if we need to stop early.
244   if (stopOnWeak && mayBeOverridden())
245     return this;
246
247   const GlobalValue *GV = getAliasedGlobal();
248   Visited.insert(GV);
249
250   // Iterate over aliasing chain, stopping on weak alias if necessary.
251   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
252     if (stopOnWeak && GA->mayBeOverridden())
253       break;
254
255     GV = GA->getAliasedGlobal();
256
257     if (!Visited.insert(GV))
258       return 0;
259   }
260
261   return GV;
262 }