Globals.cpp revision 249423
1230130Smav//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2230130Smav//
3230130Smav//                     The LLVM Compiler Infrastructure
4230130Smav//
5230130Smav// This file is distributed under the University of Illinois Open Source
6230130Smav// License. See LICENSE.TXT for details.
7230130Smav//
8230130Smav//===----------------------------------------------------------------------===//
9230130Smav//
10230130Smav// This file implements the GlobalValue & GlobalVariable classes for the IR
11230130Smav// library.
12230130Smav//
13230130Smav//===----------------------------------------------------------------------===//
14230130Smav
15230130Smav#include "llvm/IR/GlobalValue.h"
16230130Smav#include "llvm/ADT/SmallPtrSet.h"
17230130Smav#include "llvm/IR/Constants.h"
18230130Smav#include "llvm/IR/DerivedTypes.h"
19230130Smav#include "llvm/IR/GlobalAlias.h"
20230130Smav#include "llvm/IR/GlobalVariable.h"
21230130Smav#include "llvm/IR/Module.h"
22230130Smav#include "llvm/Support/ErrorHandling.h"
23230130Smav#include "llvm/Support/LeakDetector.h"
24230130Smavusing namespace llvm;
25230130Smav
26230130Smav//===----------------------------------------------------------------------===//
27230130Smav//                            GlobalValue Class
28230130Smav//===----------------------------------------------------------------------===//
29230130Smav
30230130Smavbool GlobalValue::isMaterializable() const {
31230130Smav  return getParent() && getParent()->isMaterializable(this);
32230130Smav}
33230130Smavbool GlobalValue::isDematerializable() const {
34230130Smav  return getParent() && getParent()->isDematerializable(this);
35230130Smav}
36230130Smavbool GlobalValue::Materialize(std::string *ErrInfo) {
37230130Smav  return getParent()->Materialize(this, ErrInfo);
38230130Smav}
39230130Smavvoid GlobalValue::Dematerialize() {
40230130Smav  getParent()->Dematerialize(this);
41230130Smav}
42230130Smav
43230130Smav/// Override destroyConstant to make sure it doesn't get called on
44230130Smav/// GlobalValue's because they shouldn't be treated like other constants.
45230130Smavvoid GlobalValue::destroyConstant() {
46230130Smav  llvm_unreachable("You can't GV->destroyConstant()!");
47230130Smav}
48230130Smav
49230130Smav/// copyAttributesFrom - copy all additional attributes (those not needed to
50248786Smav/// create a GlobalValue) from the GlobalValue Src to this one.
51230130Smavvoid GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52230130Smav  setAlignment(Src->getAlignment());
53230130Smav  setSection(Src->getSection());
54230130Smav  setVisibility(Src->getVisibility());
55230130Smav  setUnnamedAddr(Src->hasUnnamedAddr());
56230130Smav}
57230130Smav
58230130Smavvoid GlobalValue::setAlignment(unsigned Align) {
59248786Smav  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
60230130Smav  assert(Align <= MaximumAlignment &&
61230130Smav         "Alignment is greater than MaximumAlignment!");
62248786Smav  Alignment = Log2_32(Align) + 1;
63230130Smav  assert(getAlignment() == Align && "Alignment representation error!");
64230130Smav}
65248786Smav
66230130Smavbool GlobalValue::isDeclaration() const {
67230130Smav  // Globals are definitions if they have an initializer.
68248786Smav  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
69230130Smav    return GV->getNumOperands() == 0;
70230130Smav
71248786Smav  // Functions are definitions if they have a body.
72230130Smav  if (const Function *F = dyn_cast<Function>(this))
73230130Smav    return F->empty();
74248786Smav
75230130Smav  // Aliases are always definitions.
76230130Smav  assert(isa<GlobalAlias>(this));
77248786Smav  return false;
78230130Smav}
79230130Smav
80248786Smav//===----------------------------------------------------------------------===//
81230130Smav// GlobalVariable Implementation
82230130Smav//===----------------------------------------------------------------------===//
83248786Smav
84230130SmavGlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
85230130Smav                               Constant *InitVal,
86248786Smav                               const Twine &Name, ThreadLocalMode TLMode,
87230130Smav                               unsigned AddressSpace,
88230130Smav                               bool isExternallyInitialized)
89248786Smav  : GlobalValue(PointerType::get(Ty, AddressSpace),
90230130Smav                Value::GlobalVariableVal,
91230130Smav                OperandTraits<GlobalVariable>::op_begin(this),
92248786Smav                InitVal != 0, Link, Name),
93230130Smav    isConstantGlobal(constant), threadLocalMode(TLMode),
94230130Smav    isExternallyInitializedConstant(isExternallyInitialized) {
95248786Smav  if (InitVal) {
96230130Smav    assert(InitVal->getType() == Ty &&
97230130Smav           "Initializer should be the same type as the GlobalVariable!");
98248786Smav    Op<0>() = InitVal;
99230130Smav  }
100230130Smav
101248786Smav  LeakDetector::addGarbageObject(this);
102230130Smav}
103230130Smav
104248786SmavGlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
105230130Smav                               LinkageTypes Link, Constant *InitVal,
106230130Smav                               const Twine &Name,
107248786Smav                               GlobalVariable *Before, ThreadLocalMode TLMode,
108230130Smav                               unsigned AddressSpace,
109230130Smav                               bool isExternallyInitialized)
110261792Shselasky  : GlobalValue(PointerType::get(Ty, AddressSpace),
111261792Shselasky                Value::GlobalVariableVal,
112261792Shselasky                OperandTraits<GlobalVariable>::op_begin(this),
113248786Smav                InitVal != 0, Link, Name),
114230130Smav    isConstantGlobal(constant), threadLocalMode(TLMode),
115230130Smav    isExternallyInitializedConstant(isExternallyInitialized) {
116261792Shselasky  if (InitVal) {
117261792Shselasky    assert(InitVal->getType() == Ty &&
118261792Shselasky           "Initializer should be the same type as the GlobalVariable!");
119261792Shselasky    Op<0>() = InitVal;
120261792Shselasky  }
121261792Shselasky
122248786Smav  LeakDetector::addGarbageObject(this);
123230130Smav
124230130Smav  if (Before)
125248786Smav    Before->getParent()->getGlobalList().insert(Before, this);
126230130Smav  else
127230130Smav    M.getGlobalList().push_back(this);
128248786Smav}
129230130Smav
130230130Smavvoid GlobalVariable::setParent(Module *parent) {
131248786Smav  if (getParent())
132230130Smav    LeakDetector::addGarbageObject(this);
133230130Smav  Parent = parent;
134248786Smav  if (getParent())
135230130Smav    LeakDetector::removeGarbageObject(this);
136230130Smav}
137248786Smav
138230130Smavvoid GlobalVariable::removeFromParent() {
139248786Smav  getParent()->getGlobalList().remove(this);
140248786Smav}
141248786Smav
142248786Smavvoid GlobalVariable::eraseFromParent() {
143230130Smav  getParent()->getGlobalList().erase(this);
144230130Smav}
145230130Smav
146230130Smavvoid GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
147230130Smav                                                 Use *U) {
148230130Smav  // If you call this, then you better know this GVar has a constant
149230130Smav  // initializer worth replacing. Enforce that here.
150230130Smav  assert(getNumOperands() == 1 &&
151230130Smav         "Attempt to replace uses of Constants on a GVar with no initializer");
152230130Smav
153230130Smav  // And, since you know it has an initializer, the From value better be
154230130Smav  // the initializer :)
155230130Smav  assert(getOperand(0) == From &&
156242352Smav         "Attempt to replace wrong constant initializer in GVar");
157230130Smav
158230130Smav  // And, you better have a constant for the replacement value
159230130Smav  assert(isa<Constant>(To) &&
160230130Smav         "Attempt to replace GVar initializer with non-constant");
161230130Smav
162230130Smav  // Okay, preconditions out of the way, replace the constant initializer.
163230130Smav  this->setOperand(0, cast<Constant>(To));
164230130Smav}
165230130Smav
166230130Smavvoid GlobalVariable::setInitializer(Constant *InitVal) {
167230130Smav  if (InitVal == 0) {
168230130Smav    if (hasInitializer()) {
169230130Smav      Op<0>().set(0);
170230130Smav      NumOperands = 0;
171230130Smav    }
172230130Smav  } else {
173230130Smav    assert(InitVal->getType() == getType()->getElementType() &&
174230130Smav           "Initializer type must match GlobalVariable type");
175230130Smav    if (!hasInitializer())
176230130Smav      NumOperands = 1;
177230130Smav    Op<0>().set(InitVal);
178230130Smav  }
179230130Smav}
180230130Smav
181230130Smav/// copyAttributesFrom - copy all additional attributes (those not needed to
182230130Smav/// create a GlobalVariable) from the GlobalVariable Src to this one.
183230130Smavvoid GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
184230130Smav  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
185230130Smav  GlobalValue::copyAttributesFrom(Src);
186230130Smav  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
187230130Smav  setThreadLocal(SrcVar->isThreadLocal());
188230130Smav}
189230130Smav
190230130Smav
191230130Smav//===----------------------------------------------------------------------===//
192230130Smav// GlobalAlias Implementation
193230130Smav//===----------------------------------------------------------------------===//
194230130Smav
195230130SmavGlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
196230130Smav                         const Twine &Name, Constant* aliasee,
197230130Smav                         Module *ParentModule)
198230130Smav  : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
199230130Smav  LeakDetector::addGarbageObject(this);
200230130Smav
201230130Smav  if (aliasee)
202230130Smav    assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
203230130Smav  Op<0>() = aliasee;
204230130Smav
205230130Smav  if (ParentModule)
206230130Smav    ParentModule->getAliasList().push_back(this);
207230130Smav}
208230130Smav
209230130Smavvoid GlobalAlias::setParent(Module *parent) {
210230130Smav  if (getParent())
211230130Smav    LeakDetector::addGarbageObject(this);
212230130Smav  Parent = parent;
213230130Smav  if (getParent())
214230130Smav    LeakDetector::removeGarbageObject(this);
215230130Smav}
216230130Smav
217230130Smavvoid GlobalAlias::removeFromParent() {
218230130Smav  getParent()->getAliasList().remove(this);
219230130Smav}
220230130Smav
221230130Smavvoid GlobalAlias::eraseFromParent() {
222230130Smav  getParent()->getAliasList().erase(this);
223230130Smav}
224230130Smav
225230130Smavvoid GlobalAlias::setAliasee(Constant *Aliasee) {
226230130Smav  assert((!Aliasee || Aliasee->getType() == getType()) &&
227230130Smav         "Alias and aliasee types should match!");
228230130Smav
229230130Smav  setOperand(0, Aliasee);
230230130Smav}
231230130Smav
232230130Smavconst GlobalValue *GlobalAlias::getAliasedGlobal() const {
233230130Smav  const Constant *C = getAliasee();
234230130Smav  if (C == 0) return 0;
235230130Smav
236230130Smav  if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
237230130Smav    return GV;
238230130Smav
239230130Smav  const ConstantExpr *CE = cast<ConstantExpr>(C);
240230130Smav  assert((CE->getOpcode() == Instruction::BitCast ||
241230130Smav          CE->getOpcode() == Instruction::GetElementPtr) &&
242230130Smav         "Unsupported aliasee");
243230130Smav
244230130Smav  return cast<GlobalValue>(CE->getOperand(0));
245230130Smav}
246230130Smav
247230130Smavconst GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
248230130Smav  SmallPtrSet<const GlobalValue*, 3> Visited;
249230130Smav
250230130Smav  // Check if we need to stop early.
251230130Smav  if (stopOnWeak && mayBeOverridden())
252230130Smav    return this;
253230130Smav
254230130Smav  const GlobalValue *GV = getAliasedGlobal();
255230130Smav  Visited.insert(GV);
256230130Smav
257230130Smav  // Iterate over aliasing chain, stopping on weak alias if necessary.
258230130Smav  while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
259230130Smav    if (stopOnWeak && GA->mayBeOverridden())
260230130Smav      break;
261230130Smav
262230130Smav    GV = GA->getAliasedGlobal();
263230130Smav
264230130Smav    if (!Visited.insert(GV))
265230130Smav      return 0;
266230130Smav  }
267230130Smav
268230130Smav  return GV;
269230130Smav}
270230130Smav