blob: 2e52aa302867bf321a4f803d68b49c41b4219391 [file] [log] [blame]
Chris Lattnercc041ba2006-01-24 04:13:11 +00001//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Reid Spencere253cf62004-07-18 00:06:26 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Reid Spencere253cf62004-07-18 00:06:26 +00008//===----------------------------------------------------------------------===//
9//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the GlobalValue & GlobalVariable classes for the IR
Reid Spencere253cf62004-07-18 00:06:26 +000011// library.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/GlobalValue.h"
Anton Korobeynikove846dd82008-03-11 22:28:56 +000016#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#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"
Torok Edwinab7c09b2009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/LeakDetector.h"
Reid Spencere253cf62004-07-18 00:06:26 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// GlobalValue Class
28//===----------------------------------------------------------------------===//
29
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000030bool GlobalValue::isMaterializable() const {
Nick Lewycky936c43b2010-02-15 21:27:20 +000031 return getParent() && getParent()->isMaterializable(this);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000032}
33bool GlobalValue::isDematerializable() const {
Nick Lewycky936c43b2010-02-15 21:27:20 +000034 return getParent() && getParent()->isDematerializable(this);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000035}
36bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
38}
39void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
41}
42
Misha Brukmanfd939082005-04-21 23:48:37 +000043/// Override destroyConstant to make sure it doesn't get called on
Reid Spencere253cf62004-07-18 00:06:26 +000044/// GlobalValue's because they shouldn't be treated like other constants.
Owen Anderson04fb7c32009-06-20 00:24:58 +000045void GlobalValue::destroyConstant() {
Torok Edwinc23197a2009-07-14 16:55:14 +000046 llvm_unreachable("You can't GV->destroyConstant()!");
Reid Spencere253cf62004-07-18 00:06:26 +000047}
Duncan Sands28c3cff2008-05-26 19:58:59 +000048
49/// copyAttributesFrom - copy all additional attributes (those not needed to
50/// create a GlobalValue) from the GlobalValue Src to this one.
51void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setAlignment(Src->getAlignment());
53 setSection(Src->getSection());
54 setVisibility(Src->getVisibility());
Chris Lattner1afcace2011-07-09 17:41:24 +000055 setUnnamedAddr(Src->hasUnnamedAddr());
Duncan Sands28c3cff2008-05-26 19:58:59 +000056}
57
Dan Gohman6bbe6712010-07-28 20:56:48 +000058void 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}
Chris Lattner6c482442011-07-14 18:10:41 +000065
66bool GlobalValue::isDeclaration() const {
Chris Lattner97d97302011-07-14 18:12:44 +000067 // Globals are definitions if they have an initializer.
Chris Lattner6c482442011-07-14 18:10:41 +000068 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
69 return GV->getNumOperands() == 0;
70
Chris Lattner97d97302011-07-14 18:12:44 +000071 // Functions are definitions if they have a body.
Chris Lattner6c482442011-07-14 18:10:41 +000072 if (const Function *F = dyn_cast<Function>(this))
73 return F->empty();
Chris Lattner97d97302011-07-14 18:12:44 +000074
Chris Lattner00632252011-07-14 20:22:18 +000075 // Aliases are always definitions.
76 assert(isa<GlobalAlias>(this));
Chris Lattner6c482442011-07-14 18:10:41 +000077 return false;
78}
Dan Gohman6bbe6712010-07-28 20:56:48 +000079
Reid Spencere253cf62004-07-18 00:06:26 +000080//===----------------------------------------------------------------------===//
81// GlobalVariable Implementation
82//===----------------------------------------------------------------------===//
83
Hans Wennborgce718ff2012-06-23 11:37:03 +000084GlobalVariable::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) {
Chris Lattner96d83f62005-01-29 00:35:33 +000092 if (InitVal) {
93 assert(InitVal->getType() == Ty &&
Alkis Evlogimenos82439762004-08-05 11:28:34 +000094 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +000095 Op<0>() = InitVal;
Alkis Evlogimenos82439762004-08-05 11:28:34 +000096 }
Reid Spencere253cf62004-07-18 00:06:26 +000097
98 LeakDetector::addGarbageObject(this);
Reid Spencere253cf62004-07-18 00:06:26 +000099}
100
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000101GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersone9b11b42009-07-08 19:03:57 +0000102 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000103 const Twine &Name,
Hans Wennborgce718ff2012-06-23 11:37:03 +0000104 GlobalVariable *Before, ThreadLocalMode TLMode,
Christopher Lambfe63fb92007-12-11 08:59:05 +0000105 unsigned AddressSpace)
Hans Wennborg6de8ffb2012-06-23 12:14:23 +0000106 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Andersonc341f1c2009-07-08 23:50:31 +0000107 Value::GlobalVariableVal,
Gabor Greifefe65362008-05-10 08:32:32 +0000108 OperandTraits<GlobalVariable>::op_begin(this),
109 InitVal != 0, Link, Name),
Hans Wennborgce718ff2012-06-23 11:37:03 +0000110 isConstantGlobal(constant), threadLocalMode(TLMode) {
Chris Lattneradc95462006-09-30 21:31:26 +0000111 if (InitVal) {
112 assert(InitVal->getType() == Ty &&
113 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000114 Op<0>() = InitVal;
Chris Lattneradc95462006-09-30 21:31:26 +0000115 }
116
117 LeakDetector::addGarbageObject(this);
118
119 if (Before)
120 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersone9b11b42009-07-08 19:03:57 +0000121 else
122 M.getGlobalList().push_back(this);
Chris Lattneradc95462006-09-30 21:31:26 +0000123}
124
Reid Spencere253cf62004-07-18 00:06:26 +0000125void GlobalVariable::setParent(Module *parent) {
126 if (getParent())
127 LeakDetector::addGarbageObject(this);
128 Parent = parent;
129 if (getParent())
130 LeakDetector::removeGarbageObject(this);
131}
132
Chris Lattner4b833802004-10-11 22:21:39 +0000133void GlobalVariable::removeFromParent() {
134 getParent()->getGlobalList().remove(this);
135}
136
137void GlobalVariable::eraseFromParent() {
138 getParent()->getGlobalList().erase(this);
139}
140
Reid Spencere253cf62004-07-18 00:06:26 +0000141void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +0000142 Use *U) {
Reid Spencere253cf62004-07-18 00:06:26 +0000143 // If you call this, then you better know this GVar has a constant
144 // initializer worth replacing. Enforce that here.
Misha Brukmanfd939082005-04-21 23:48:37 +0000145 assert(getNumOperands() == 1 &&
Reid Spencere253cf62004-07-18 00:06:26 +0000146 "Attempt to replace uses of Constants on a GVar with no initializer");
147
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");
152
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");
Misha Brukmanfd939082005-04-21 23:48:37 +0000156
Reid Spencere253cf62004-07-18 00:06:26 +0000157 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner07d7c9d2004-08-04 02:27:17 +0000158 this->setOperand(0, cast<Constant>(To));
Reid Spencere253cf62004-07-18 00:06:26 +0000159}
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000160
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000161void GlobalVariable::setInitializer(Constant *InitVal) {
162 if (InitVal == 0) {
163 if (hasInitializer()) {
164 Op<0>().set(0);
165 NumOperands = 0;
166 }
167 } else {
168 assert(InitVal->getType() == getType()->getElementType() &&
169 "Initializer type must match GlobalVariable type");
170 if (!hasInitializer())
171 NumOperands = 1;
172 Op<0>().set(InitVal);
173 }
174}
175
Duncan Sands28c3cff2008-05-26 19:58:59 +0000176/// copyAttributesFrom - copy all additional attributes (those not needed to
177/// create a GlobalVariable) from the GlobalVariable Src to this one.
178void 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());
183}
184
185
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000186//===----------------------------------------------------------------------===//
187// GlobalAlias Implementation
188//===----------------------------------------------------------------------===//
189
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000190GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000191 const Twine &Name, Constant* aliasee,
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000192 Module *ParentModule)
Gabor Greifefe65362008-05-10 08:32:32 +0000193 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000194 LeakDetector::addGarbageObject(this);
195
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000196 if (aliasee)
197 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000198 Op<0>() = aliasee;
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000199
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000200 if (ParentModule)
201 ParentModule->getAliasList().push_back(this);
202}
203
204void GlobalAlias::setParent(Module *parent) {
205 if (getParent())
206 LeakDetector::addGarbageObject(this);
207 Parent = parent;
208 if (getParent())
209 LeakDetector::removeGarbageObject(this);
210}
211
212void GlobalAlias::removeFromParent() {
213 getParent()->getAliasList().remove(this);
214}
215
216void GlobalAlias::eraseFromParent() {
217 getParent()->getAliasList().erase(this);
218}
219
Chris Lattner9854e1b2011-07-14 18:01:49 +0000220void GlobalAlias::setAliasee(Constant *Aliasee) {
221 assert((!Aliasee || Aliasee->getType() == getType()) &&
222 "Alias and aliasee types should match!");
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000223
224 setOperand(0, Aliasee);
225}
226
Chris Lattner2bf6e6a2007-05-05 23:49:02 +0000227const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000228 const Constant *C = getAliasee();
Chris Lattner9854e1b2011-07-14 18:01:49 +0000229 if (C == 0) return 0;
230
231 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
232 return GV;
233
234 const ConstantExpr *CE = cast<ConstantExpr>(C);
235 assert((CE->getOpcode() == Instruction::BitCast ||
236 CE->getOpcode() == Instruction::GetElementPtr) &&
237 "Unsupported aliasee");
238
Jay Foad42552742011-08-01 12:28:01 +0000239 return cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000240}
241
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000242const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov89a3d3f2008-03-22 07:48:40 +0000243 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000244
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000245 // Check if we need to stop early.
Duncan Sands86062af2009-01-08 20:55:49 +0000246 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000247 return this;
248
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000249 const GlobalValue *GV = getAliasedGlobal();
250 Visited.insert(GV);
251
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000252 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000253 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands86062af2009-01-08 20:55:49 +0000254 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000255 break;
256
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000257 GV = GA->getAliasedGlobal();
258
259 if (!Visited.insert(GV))
Chris Lattner9854e1b2011-07-14 18:01:49 +0000260 return 0;
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000261 }
262
263 return GV;
264}