blob: 6d547f3edf3f61bfdf7768590d98908e35ae00c2 [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,
Michael Gottesmanaf2f4942013-02-03 21:54:38 +000085 Constant *InitVal,
86 const Twine &Name, ThreadLocalMode TLMode,
87 unsigned AddressSpace,
88 bool isExternallyInitialized)
Hans Wennborgce718ff2012-06-23 11:37:03 +000089 : GlobalValue(PointerType::get(Ty, AddressSpace),
90 Value::GlobalVariableVal,
91 OperandTraits<GlobalVariable>::op_begin(this),
92 InitVal != 0, Link, Name),
Michael Gottesmanaf2f4942013-02-03 21:54:38 +000093 isConstantGlobal(constant), threadLocalMode(TLMode),
94 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattner96d83f62005-01-29 00:35:33 +000095 if (InitVal) {
96 assert(InitVal->getType() == Ty &&
Alkis Evlogimenos82439762004-08-05 11:28:34 +000097 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +000098 Op<0>() = InitVal;
Alkis Evlogimenos82439762004-08-05 11:28:34 +000099 }
Reid Spencere253cf62004-07-18 00:06:26 +0000100
101 LeakDetector::addGarbageObject(this);
Reid Spencere253cf62004-07-18 00:06:26 +0000102}
103
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000104GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
Owen Andersone9b11b42009-07-08 19:03:57 +0000105 LinkageTypes Link, Constant *InitVal,
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000106 const Twine &Name,
Hans Wennborgce718ff2012-06-23 11:37:03 +0000107 GlobalVariable *Before, ThreadLocalMode TLMode,
Michael Gottesmanaf2f4942013-02-03 21:54:38 +0000108 unsigned AddressSpace,
109 bool isExternallyInitialized)
Hans Wennborg6de8ffb2012-06-23 12:14:23 +0000110 : GlobalValue(PointerType::get(Ty, AddressSpace),
Owen Andersonc341f1c2009-07-08 23:50:31 +0000111 Value::GlobalVariableVal,
Gabor Greifefe65362008-05-10 08:32:32 +0000112 OperandTraits<GlobalVariable>::op_begin(this),
113 InitVal != 0, Link, Name),
Michael Gottesmanaf2f4942013-02-03 21:54:38 +0000114 isConstantGlobal(constant), threadLocalMode(TLMode),
115 isExternallyInitializedConstant(isExternallyInitialized) {
Chris Lattneradc95462006-09-30 21:31:26 +0000116 if (InitVal) {
117 assert(InitVal->getType() == Ty &&
118 "Initializer should be the same type as the GlobalVariable!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000119 Op<0>() = InitVal;
Chris Lattneradc95462006-09-30 21:31:26 +0000120 }
121
122 LeakDetector::addGarbageObject(this);
123
124 if (Before)
125 Before->getParent()->getGlobalList().insert(Before, this);
Owen Andersone9b11b42009-07-08 19:03:57 +0000126 else
127 M.getGlobalList().push_back(this);
Chris Lattneradc95462006-09-30 21:31:26 +0000128}
129
Reid Spencere253cf62004-07-18 00:06:26 +0000130void GlobalVariable::setParent(Module *parent) {
131 if (getParent())
132 LeakDetector::addGarbageObject(this);
133 Parent = parent;
134 if (getParent())
135 LeakDetector::removeGarbageObject(this);
136}
137
Chris Lattner4b833802004-10-11 22:21:39 +0000138void GlobalVariable::removeFromParent() {
139 getParent()->getGlobalList().remove(this);
140}
141
142void GlobalVariable::eraseFromParent() {
143 getParent()->getGlobalList().erase(this);
144}
145
Reid Spencere253cf62004-07-18 00:06:26 +0000146void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattnerd0ff1ad2005-10-04 18:13:04 +0000147 Use *U) {
Reid Spencere253cf62004-07-18 00:06:26 +0000148 // If you call this, then you better know this GVar has a constant
149 // initializer worth replacing. Enforce that here.
Misha Brukmanfd939082005-04-21 23:48:37 +0000150 assert(getNumOperands() == 1 &&
Reid Spencere253cf62004-07-18 00:06:26 +0000151 "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");
Misha Brukmanfd939082005-04-21 23:48:37 +0000161
Reid Spencere253cf62004-07-18 00:06:26 +0000162 // Okay, preconditions out of the way, replace the constant initializer.
Chris Lattner07d7c9d2004-08-04 02:27:17 +0000163 this->setOperand(0, cast<Constant>(To));
Reid Spencere253cf62004-07-18 00:06:26 +0000164}
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000165
Jeffrey Yasskindc1472b2009-11-17 00:43:13 +0000166void 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
Duncan Sands28c3cff2008-05-26 19:58:59 +0000181/// copyAttributesFrom - copy all additional attributes (those not needed to
182/// create a GlobalVariable) from the GlobalVariable Src to this one.
183void 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
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000191//===----------------------------------------------------------------------===//
192// GlobalAlias Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000195GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000196 const Twine &Name, Constant* aliasee,
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000197 Module *ParentModule)
Gabor Greifefe65362008-05-10 08:32:32 +0000198 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000199 LeakDetector::addGarbageObject(this);
200
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000201 if (aliasee)
202 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000203 Op<0>() = aliasee;
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000204
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000205 if (ParentModule)
206 ParentModule->getAliasList().push_back(this);
207}
208
209void GlobalAlias::setParent(Module *parent) {
210 if (getParent())
211 LeakDetector::addGarbageObject(this);
212 Parent = parent;
213 if (getParent())
214 LeakDetector::removeGarbageObject(this);
215}
216
217void GlobalAlias::removeFromParent() {
218 getParent()->getAliasList().remove(this);
219}
220
221void GlobalAlias::eraseFromParent() {
222 getParent()->getAliasList().erase(this);
223}
224
Chris Lattner9854e1b2011-07-14 18:01:49 +0000225void GlobalAlias::setAliasee(Constant *Aliasee) {
226 assert((!Aliasee || Aliasee->getType() == getType()) &&
227 "Alias and aliasee types should match!");
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000228
229 setOperand(0, Aliasee);
230}
231
Chris Lattner2bf6e6a2007-05-05 23:49:02 +0000232const GlobalValue *GlobalAlias::getAliasedGlobal() const {
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000233 const Constant *C = getAliasee();
Chris Lattner9854e1b2011-07-14 18:01:49 +0000234 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
Jay Foad42552742011-08-01 12:28:01 +0000244 return cast<GlobalValue>(CE->getOperand(0));
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000245}
246
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000247const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
Anton Korobeynikov89a3d3f2008-03-22 07:48:40 +0000248 SmallPtrSet<const GlobalValue*, 3> Visited;
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000249
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000250 // Check if we need to stop early.
Duncan Sands86062af2009-01-08 20:55:49 +0000251 if (stopOnWeak && mayBeOverridden())
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000252 return this;
253
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000254 const GlobalValue *GV = getAliasedGlobal();
255 Visited.insert(GV);
256
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000257 // Iterate over aliasing chain, stopping on weak alias if necessary.
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000258 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
Duncan Sands86062af2009-01-08 20:55:49 +0000259 if (stopOnWeak && GA->mayBeOverridden())
Anton Korobeynikov832b2a92008-09-09 18:23:48 +0000260 break;
261
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000262 GV = GA->getAliasedGlobal();
263
264 if (!Visited.insert(GV))
Chris Lattner9854e1b2011-07-14 18:01:49 +0000265 return 0;
Anton Korobeynikove846dd82008-03-11 22:28:56 +0000266 }
267
268 return GV;
269}