Copy dll storage in copyAttributes.
[oota-llvm.git] / lib / IR / 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 IR
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 //                            GlobalValue Class
28 //===----------------------------------------------------------------------===//
29
30 bool GlobalValue::isMaterializable() const {
31   return getParent() && getParent()->isMaterializable(this);
32 }
33 bool GlobalValue::isDematerializable() const {
34   return getParent() && getParent()->isDematerializable(this);
35 }
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37   return getParent()->Materialize(this, ErrInfo);
38 }
39 void GlobalValue::Dematerialize() {
40   getParent()->Dematerialize(this);
41 }
42
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()!");
47 }
48
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());
57 }
58
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!");
65 }
66
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;
71
72   // Functions are definitions if they have a body.
73   if (const Function *F = dyn_cast<Function>(this))
74     return F->empty();
75
76   // Aliases are always definitions.
77   assert(isa<GlobalAlias>(this));
78   return false;
79 }
80   
81 //===----------------------------------------------------------------------===//
82 // GlobalVariable Implementation
83 //===----------------------------------------------------------------------===//
84
85 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
86                                Constant *InitVal,
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) {
96   if (InitVal) {
97     assert(InitVal->getType() == Ty &&
98            "Initializer should be the same type as the GlobalVariable!");
99     Op<0>() = InitVal;
100   }
101
102   LeakDetector::addGarbageObject(this);
103 }
104
105 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
106                                LinkageTypes Link, Constant *InitVal,
107                                const Twine &Name,
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) {
117   if (InitVal) {
118     assert(InitVal->getType() == Ty &&
119            "Initializer should be the same type as the GlobalVariable!");
120     Op<0>() = InitVal;
121   }
122   
123   LeakDetector::addGarbageObject(this);
124   
125   if (Before)
126     Before->getParent()->getGlobalList().insert(Before, this);
127   else
128     M.getGlobalList().push_back(this);
129 }
130
131 void GlobalVariable::setParent(Module *parent) {
132   if (getParent())
133     LeakDetector::addGarbageObject(this);
134   Parent = parent;
135   if (getParent())
136     LeakDetector::removeGarbageObject(this);
137 }
138
139 void GlobalVariable::removeFromParent() {
140   getParent()->getGlobalList().remove(this);
141 }
142
143 void GlobalVariable::eraseFromParent() {
144   getParent()->getGlobalList().erase(this);
145 }
146
147 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
148                                                  Use *U) {
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");
153
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");
158
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");
162
163   // Okay, preconditions out of the way, replace the constant initializer.
164   this->setOperand(0, cast<Constant>(To));
165 }
166
167 void GlobalVariable::setInitializer(Constant *InitVal) {
168   if (InitVal == 0) {
169     if (hasInitializer()) {
170       Op<0>().set(0);
171       NumOperands = 0;
172     }
173   } else {
174     assert(InitVal->getType() == getType()->getElementType() &&
175            "Initializer type must match GlobalVariable type");
176     if (!hasInitializer())
177       NumOperands = 1;
178     Op<0>().set(InitVal);
179   }
180 }
181
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());
189 }
190
191
192 //===----------------------------------------------------------------------===//
193 // GlobalAlias Implementation
194 //===----------------------------------------------------------------------===//
195
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);
201
202   if (aliasee)
203     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
204   Op<0>() = aliasee;
205
206   if (ParentModule)
207     ParentModule->getAliasList().push_back(this);
208 }
209
210 void GlobalAlias::setParent(Module *parent) {
211   if (getParent())
212     LeakDetector::addGarbageObject(this);
213   Parent = parent;
214   if (getParent())
215     LeakDetector::removeGarbageObject(this);
216 }
217
218 void GlobalAlias::removeFromParent() {
219   getParent()->getAliasList().remove(this);
220 }
221
222 void GlobalAlias::eraseFromParent() {
223   getParent()->getAliasList().erase(this);
224 }
225
226 void GlobalAlias::setAliasee(Constant *Aliasee) {
227   assert((!Aliasee || Aliasee->getType() == getType()) &&
228          "Alias and aliasee types should match!");
229   
230   setOperand(0, Aliasee);
231 }
232
233 GlobalValue *GlobalAlias::getAliasedGlobal() {
234   Constant *C = getAliasee();
235   if (C == 0) return 0;
236   
237   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
238     return GV;
239
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");
245   
246   return cast<GlobalValue>(CE->getOperand(0));
247 }
248
249 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
250   SmallPtrSet<GlobalValue*, 3> Visited;
251
252   // Check if we need to stop early.
253   if (stopOnWeak && mayBeOverridden())
254     return this;
255
256   GlobalValue *GV = getAliasedGlobal();
257   Visited.insert(GV);
258
259   // Iterate over aliasing chain, stopping on weak alias if necessary.
260   while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
261     if (stopOnWeak && GA->mayBeOverridden())
262       break;
263
264     GV = GA->getAliasedGlobal();
265
266     if (!Visited.insert(GV))
267       return 0;
268   }
269
270   return GV;
271 }