• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
isMaterializable() const30 bool GlobalValue::isMaterializable() const {
31   return getParent() && getParent()->isMaterializable(this);
32 }
isDematerializable() const33 bool GlobalValue::isDematerializable() const {
34   return getParent() && getParent()->isDematerializable(this);
35 }
Materialize(std::string * ErrInfo)36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37   return getParent()->Materialize(this, ErrInfo);
38 }
Dematerialize()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.
destroyConstant()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.
copyAttributesFrom(const GlobalValue * Src)51 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52   setAlignment(Src->getAlignment());
53   setSection(Src->getSection());
54   setVisibility(Src->getVisibility());
55   setUnnamedAddr(Src->hasUnnamedAddr());
56 }
57 
setAlignment(unsigned Align)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!");
64 }
65 
isDeclaration() const66 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;
70 
71   // Functions are definitions if they have a body.
72   if (const Function *F = dyn_cast<Function>(this))
73     return F->empty();
74 
75   // Aliases are always definitions.
76   assert(isa<GlobalAlias>(this));
77   return false;
78 }
79 
80 //===----------------------------------------------------------------------===//
81 // GlobalVariable Implementation
82 //===----------------------------------------------------------------------===//
83 
GlobalVariable(Type * Ty,bool constant,LinkageTypes Link,Constant * InitVal,const Twine & Name,ThreadLocalMode TLMode,unsigned AddressSpace,bool isExternallyInitialized)84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85                                Constant *InitVal,
86                                const Twine &Name, ThreadLocalMode TLMode,
87                                unsigned AddressSpace,
88                                bool isExternallyInitialized)
89   : GlobalValue(PointerType::get(Ty, AddressSpace),
90                 Value::GlobalVariableVal,
91                 OperandTraits<GlobalVariable>::op_begin(this),
92                 InitVal != 0, Link, Name),
93     isConstantGlobal(constant), threadLocalMode(TLMode),
94     isExternallyInitializedConstant(isExternallyInitialized) {
95   if (InitVal) {
96     assert(InitVal->getType() == Ty &&
97            "Initializer should be the same type as the GlobalVariable!");
98     Op<0>() = InitVal;
99   }
100 
101   LeakDetector::addGarbageObject(this);
102 }
103 
GlobalVariable(Module & M,Type * Ty,bool constant,LinkageTypes Link,Constant * InitVal,const Twine & Name,GlobalVariable * Before,ThreadLocalMode TLMode,unsigned AddressSpace,bool isExternallyInitialized)104 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
105                                LinkageTypes Link, Constant *InitVal,
106                                const Twine &Name,
107                                GlobalVariable *Before, ThreadLocalMode TLMode,
108                                unsigned AddressSpace,
109                                bool isExternallyInitialized)
110   : GlobalValue(PointerType::get(Ty, AddressSpace),
111                 Value::GlobalVariableVal,
112                 OperandTraits<GlobalVariable>::op_begin(this),
113                 InitVal != 0, Link, Name),
114     isConstantGlobal(constant), threadLocalMode(TLMode),
115     isExternallyInitializedConstant(isExternallyInitialized) {
116   if (InitVal) {
117     assert(InitVal->getType() == Ty &&
118            "Initializer should be the same type as the GlobalVariable!");
119     Op<0>() = InitVal;
120   }
121 
122   LeakDetector::addGarbageObject(this);
123 
124   if (Before)
125     Before->getParent()->getGlobalList().insert(Before, this);
126   else
127     M.getGlobalList().push_back(this);
128 }
129 
setParent(Module * parent)130 void GlobalVariable::setParent(Module *parent) {
131   if (getParent())
132     LeakDetector::addGarbageObject(this);
133   Parent = parent;
134   if (getParent())
135     LeakDetector::removeGarbageObject(this);
136 }
137 
removeFromParent()138 void GlobalVariable::removeFromParent() {
139   getParent()->getGlobalList().remove(this);
140 }
141 
eraseFromParent()142 void GlobalVariable::eraseFromParent() {
143   getParent()->getGlobalList().erase(this);
144 }
145 
replaceUsesOfWithOnConstant(Value * From,Value * To,Use * U)146 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
147                                                  Use *U) {
148   // If you call this, then you better know this GVar has a constant
149   // initializer worth replacing. Enforce that here.
150   assert(getNumOperands() == 1 &&
151          "Attempt to replace uses of Constants on a GVar with no initializer");
152 
153   // And, since you know it has an initializer, the From value better be
154   // the initializer :)
155   assert(getOperand(0) == From &&
156          "Attempt to replace wrong constant initializer in GVar");
157 
158   // And, you better have a constant for the replacement value
159   assert(isa<Constant>(To) &&
160          "Attempt to replace GVar initializer with non-constant");
161 
162   // Okay, preconditions out of the way, replace the constant initializer.
163   this->setOperand(0, cast<Constant>(To));
164 }
165 
setInitializer(Constant * InitVal)166 void GlobalVariable::setInitializer(Constant *InitVal) {
167   if (InitVal == 0) {
168     if (hasInitializer()) {
169       Op<0>().set(0);
170       NumOperands = 0;
171     }
172   } else {
173     assert(InitVal->getType() == getType()->getElementType() &&
174            "Initializer type must match GlobalVariable type");
175     if (!hasInitializer())
176       NumOperands = 1;
177     Op<0>().set(InitVal);
178   }
179 }
180 
181 /// copyAttributesFrom - copy all additional attributes (those not needed to
182 /// create a GlobalVariable) from the GlobalVariable Src to this one.
copyAttributesFrom(const GlobalValue * Src)183 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
184   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
185   GlobalValue::copyAttributesFrom(Src);
186   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
187   setThreadLocal(SrcVar->isThreadLocal());
188 }
189 
190 
191 //===----------------------------------------------------------------------===//
192 // GlobalAlias Implementation
193 //===----------------------------------------------------------------------===//
194 
GlobalAlias(Type * Ty,LinkageTypes Link,const Twine & Name,Constant * aliasee,Module * ParentModule)195 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
196                          const Twine &Name, Constant* aliasee,
197                          Module *ParentModule)
198   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
199   LeakDetector::addGarbageObject(this);
200 
201   if (aliasee)
202     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
203   Op<0>() = aliasee;
204 
205   if (ParentModule)
206     ParentModule->getAliasList().push_back(this);
207 }
208 
setParent(Module * parent)209 void GlobalAlias::setParent(Module *parent) {
210   if (getParent())
211     LeakDetector::addGarbageObject(this);
212   Parent = parent;
213   if (getParent())
214     LeakDetector::removeGarbageObject(this);
215 }
216 
removeFromParent()217 void GlobalAlias::removeFromParent() {
218   getParent()->getAliasList().remove(this);
219 }
220 
eraseFromParent()221 void GlobalAlias::eraseFromParent() {
222   getParent()->getAliasList().erase(this);
223 }
224 
setAliasee(Constant * Aliasee)225 void GlobalAlias::setAliasee(Constant *Aliasee) {
226   assert((!Aliasee || Aliasee->getType() == getType()) &&
227          "Alias and aliasee types should match!");
228 
229   setOperand(0, Aliasee);
230 }
231 
getAliasedGlobal() const232 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
233   const Constant *C = getAliasee();
234   if (C == 0) return 0;
235 
236   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
237     return GV;
238 
239   const ConstantExpr *CE = cast<ConstantExpr>(C);
240   assert((CE->getOpcode() == Instruction::BitCast ||
241           CE->getOpcode() == Instruction::GetElementPtr) &&
242          "Unsupported aliasee");
243 
244   return cast<GlobalValue>(CE->getOperand(0));
245 }
246 
resolveAliasedGlobal(bool stopOnWeak) const247 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
248   SmallPtrSet<const GlobalValue*, 3> Visited;
249 
250   // Check if we need to stop early.
251   if (stopOnWeak && mayBeOverridden())
252     return this;
253 
254   const GlobalValue *GV = getAliasedGlobal();
255   Visited.insert(GV);
256 
257   // Iterate over aliasing chain, stopping on weak alias if necessary.
258   while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
259     if (stopOnWeak && GA->mayBeOverridden())
260       break;
261 
262     GV = GA->getAliasedGlobal();
263 
264     if (!Visited.insert(GV))
265       return 0;
266   }
267 
268   return GV;
269 }
270