Run clang-format in small sections of code to make a patch easier to read.
[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/LeakDetector.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/ErrorHandling.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 const DataLayout *GlobalValue::getDataLayout() const {
44   return getParent()->getDataLayout();
45 }
46
47 /// Override destroyConstant to make sure it doesn't get called on
48 /// GlobalValue's because they shouldn't be treated like other constants.
49 void GlobalValue::destroyConstant() {
50   llvm_unreachable("You can't GV->destroyConstant()!");
51 }
52
53 /// copyAttributesFrom - copy all additional attributes (those not needed to
54 /// create a GlobalValue) from the GlobalValue Src to this one.
55 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
56   if (!isa<GlobalAlias>(this)) {
57     setAlignment(Src->getAlignment());
58     setSection(Src->getSection());
59   }
60
61   setVisibility(Src->getVisibility());
62   setUnnamedAddr(Src->hasUnnamedAddr());
63   setDLLStorageClass(Src->getDLLStorageClass());
64 }
65
66 unsigned GlobalValue::getAlignment() const {
67   if (auto *GA = dyn_cast<GlobalAlias>(this))
68     return GA->getAliasedGlobal()->getAlignment();
69
70   return (1u << Alignment) >> 1;
71 }
72
73 void GlobalValue::setAlignment(unsigned Align) {
74   assert((!isa<GlobalAlias>(this)) &&
75          "GlobalAlias should not have an alignment!");
76   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
77   assert(Align <= MaximumAlignment &&
78          "Alignment is greater than MaximumAlignment!");
79   Alignment = Log2_32(Align) + 1;
80   assert(getAlignment() == Align && "Alignment representation error!");
81 }
82
83 const std::string &GlobalValue::getSection() const {
84   if (auto *GA = dyn_cast<GlobalAlias>(this))
85     return GA->getAliasedGlobal()->getSection();
86   return Section;
87 }
88
89 void GlobalValue::setSection(StringRef S) {
90   assert(!isa<GlobalAlias>(this) && "GlobalAlias should not have a section!");
91   Section = S;
92 }
93
94 bool GlobalValue::isDeclaration() const {
95   // Globals are definitions if they have an initializer.
96   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
97     return GV->getNumOperands() == 0;
98
99   // Functions are definitions if they have a body.
100   if (const Function *F = dyn_cast<Function>(this))
101     return F->empty();
102
103   // Aliases are always definitions.
104   assert(isa<GlobalAlias>(this));
105   return false;
106 }
107
108 //===----------------------------------------------------------------------===//
109 // GlobalVariable Implementation
110 //===----------------------------------------------------------------------===//
111
112 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
113                                Constant *InitVal, const Twine &Name,
114                                ThreadLocalMode TLMode, unsigned AddressSpace,
115                                bool isExternallyInitialized)
116     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
117                   OperandTraits<GlobalVariable>::op_begin(this),
118                   InitVal != nullptr, Link, Name),
119       isConstantGlobal(constant), threadLocalMode(TLMode),
120       isExternallyInitializedConstant(isExternallyInitialized) {
121   if (InitVal) {
122     assert(InitVal->getType() == Ty &&
123            "Initializer should be the same type as the GlobalVariable!");
124     Op<0>() = InitVal;
125   }
126
127   LeakDetector::addGarbageObject(this);
128 }
129
130 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
131                                LinkageTypes Link, Constant *InitVal,
132                                const Twine &Name, GlobalVariable *Before,
133                                ThreadLocalMode TLMode, unsigned AddressSpace,
134                                bool isExternallyInitialized)
135     : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
136                   OperandTraits<GlobalVariable>::op_begin(this),
137                   InitVal != nullptr, Link, Name),
138       isConstantGlobal(constant), threadLocalMode(TLMode),
139       isExternallyInitializedConstant(isExternallyInitialized) {
140   if (InitVal) {
141     assert(InitVal->getType() == Ty &&
142            "Initializer should be the same type as the GlobalVariable!");
143     Op<0>() = InitVal;
144   }
145
146   LeakDetector::addGarbageObject(this);
147
148   if (Before)
149     Before->getParent()->getGlobalList().insert(Before, this);
150   else
151     M.getGlobalList().push_back(this);
152 }
153
154 void GlobalVariable::setParent(Module *parent) {
155   if (getParent())
156     LeakDetector::addGarbageObject(this);
157   Parent = parent;
158   if (getParent())
159     LeakDetector::removeGarbageObject(this);
160 }
161
162 void GlobalVariable::removeFromParent() {
163   getParent()->getGlobalList().remove(this);
164 }
165
166 void GlobalVariable::eraseFromParent() {
167   getParent()->getGlobalList().erase(this);
168 }
169
170 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
171                                                  Use *U) {
172   // If you call this, then you better know this GVar has a constant
173   // initializer worth replacing. Enforce that here.
174   assert(getNumOperands() == 1 &&
175          "Attempt to replace uses of Constants on a GVar with no initializer");
176
177   // And, since you know it has an initializer, the From value better be
178   // the initializer :)
179   assert(getOperand(0) == From &&
180          "Attempt to replace wrong constant initializer in GVar");
181
182   // And, you better have a constant for the replacement value
183   assert(isa<Constant>(To) &&
184          "Attempt to replace GVar initializer with non-constant");
185
186   // Okay, preconditions out of the way, replace the constant initializer.
187   this->setOperand(0, cast<Constant>(To));
188 }
189
190 void GlobalVariable::setInitializer(Constant *InitVal) {
191   if (!InitVal) {
192     if (hasInitializer()) {
193       Op<0>().set(nullptr);
194       NumOperands = 0;
195     }
196   } else {
197     assert(InitVal->getType() == getType()->getElementType() &&
198            "Initializer type must match GlobalVariable type");
199     if (!hasInitializer())
200       NumOperands = 1;
201     Op<0>().set(InitVal);
202   }
203 }
204
205 /// copyAttributesFrom - copy all additional attributes (those not needed to
206 /// create a GlobalVariable) from the GlobalVariable Src to this one.
207 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
208   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
209   GlobalValue::copyAttributesFrom(Src);
210   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
211   setThreadLocalMode(SrcVar->getThreadLocalMode());
212 }
213
214
215 //===----------------------------------------------------------------------===//
216 // GlobalAlias Implementation
217 //===----------------------------------------------------------------------===//
218
219 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
220                          const Twine &Name, Constant* aliasee,
221                          Module *ParentModule)
222   : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
223   LeakDetector::addGarbageObject(this);
224
225   if (aliasee)
226     assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
227   Op<0>() = aliasee;
228
229   if (ParentModule)
230     ParentModule->getAliasList().push_back(this);
231 }
232
233 void GlobalAlias::setParent(Module *parent) {
234   if (getParent())
235     LeakDetector::addGarbageObject(this);
236   Parent = parent;
237   if (getParent())
238     LeakDetector::removeGarbageObject(this);
239 }
240
241 void GlobalAlias::removeFromParent() {
242   getParent()->getAliasList().remove(this);
243 }
244
245 void GlobalAlias::eraseFromParent() {
246   getParent()->getAliasList().erase(this);
247 }
248
249 void GlobalAlias::setAliasee(Constant *Aliasee) {
250   assert((!Aliasee || Aliasee->getType() == getType()) &&
251          "Alias and aliasee types should match!");
252
253   setOperand(0, Aliasee);
254 }
255
256 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
257   Constant *C = GA->getAliasee();
258   assert(C && "Must alias something");
259
260   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
261     return GV;
262
263   ConstantExpr *CE = cast<ConstantExpr>(C);
264   assert((CE->getOpcode() == Instruction::BitCast ||
265           CE->getOpcode() == Instruction::AddrSpaceCast ||
266           CE->getOpcode() == Instruction::GetElementPtr) &&
267          "Unsupported aliasee");
268
269   return cast<GlobalValue>(CE->getOperand(0));
270 }
271
272 GlobalValue *GlobalAlias::getAliasedGlobal() {
273   SmallPtrSet<GlobalValue*, 3> Visited;
274
275   GlobalAlias *GA = this;
276
277   for (;;) {
278     GlobalValue *GV = getAliaseeGV(GA);
279     if (!Visited.insert(GV))
280       return nullptr;
281
282     // Iterate over aliasing chain.
283     GA = dyn_cast<GlobalAlias>(GV);
284     if (!GA)
285       return GV;
286   }
287 }