1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the GlobalValue & GlobalVariable classes for the VMCore
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// library.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalVariable.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalAlias.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Module.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallPtrSet.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/LeakDetector.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                            GlobalValue Class
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool GlobalValue::isMaterializable() const {
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getParent() && getParent()->isMaterializable(this);
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool GlobalValue::isDematerializable() const {
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getParent() && getParent()->isDematerializable(this);
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool GlobalValue::Materialize(std::string *ErrInfo) {
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getParent()->Materialize(this, ErrInfo);
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalValue::Dematerialize() {
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getParent()->Dematerialize(this);
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Override destroyConstant to make sure it doesn't get called on
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// GlobalValue's because they shouldn't be treated like other constants.
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalValue::destroyConstant() {
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("You can't GV->destroyConstant()!");
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// copyAttributesFrom - copy all additional attributes (those not needed to
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// create a GlobalValue) from the GlobalValue Src to this one.
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setAlignment(Src->getAlignment());
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setSection(Src->getSection());
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setVisibility(Src->getVisibility());
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setUnnamedAddr(Src->hasUnnamedAddr());
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalValue::setAlignment(unsigned Align) {
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Align <= MaximumAlignment &&
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Alignment is greater than MaximumAlignment!");
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Alignment = Log2_32(Align) + 1;
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getAlignment() == Align && "Alignment representation error!");
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool GlobalValue::isDeclaration() const {
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Globals are definitions if they have an initializer.
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return GV->getNumOperands() == 0;
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Functions are definitions if they have a body.
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const Function *F = dyn_cast<Function>(this))
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return F->empty();
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Aliases are always definitions.
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(isa<GlobalAlias>(this));
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GlobalVariable Implementation
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanGlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Constant *InitVal, const Twine &Name,
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               bool ThreadLocal, unsigned AddressSpace)
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : GlobalValue(PointerType::get(Ty, AddressSpace),
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                Value::GlobalVariableVal,
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                OperandTraits<GlobalVariable>::op_begin(this),
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                InitVal != 0, Link, Name),
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InitVal) {
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(InitVal->getType() == Ty &&
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer should be the same type as the GlobalVariable!");
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op<0>() = InitVal;
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LeakDetector::addGarbageObject(this);
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanGlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               LinkageTypes Link, Constant *InitVal,
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const Twine &Name,
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               GlobalVariable *Before, bool ThreadLocal,
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               unsigned AddressSpace)
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : GlobalValue(PointerType::get(Ty, AddressSpace),
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                Value::GlobalVariableVal,
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                OperandTraits<GlobalVariable>::op_begin(this),
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                InitVal != 0, Link, Name),
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InitVal) {
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(InitVal->getType() == Ty &&
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer should be the same type as the GlobalVariable!");
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op<0>() = InitVal;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LeakDetector::addGarbageObject(this);
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Before)
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Before->getParent()->getGlobalList().insert(Before, this);
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    M.getGlobalList().push_back(this);
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::setParent(Module *parent) {
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getParent())
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LeakDetector::addGarbageObject(this);
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Parent = parent;
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getParent())
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LeakDetector::removeGarbageObject(this);
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::removeFromParent() {
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getParent()->getGlobalList().remove(this);
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::eraseFromParent() {
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getParent()->getGlobalList().erase(this);
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Use *U) {
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If you call this, then you better know this GVar has a constant
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // initializer worth replacing. Enforce that here.
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getNumOperands() == 1 &&
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Attempt to replace uses of Constants on a GVar with no initializer");
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // And, since you know it has an initializer, the From value better be
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the initializer :)
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getOperand(0) == From &&
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Attempt to replace wrong constant initializer in GVar");
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // And, you better have a constant for the replacement value
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<Constant>(To) &&
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Attempt to replace GVar initializer with non-constant");
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Okay, preconditions out of the way, replace the constant initializer.
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  this->setOperand(0, cast<Constant>(To));
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::setInitializer(Constant *InitVal) {
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InitVal == 0) {
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (hasInitializer()) {
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Op<0>().set(0);
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumOperands = 0;
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(InitVal->getType() == getType()->getElementType() &&
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer type must match GlobalVariable type");
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!hasInitializer())
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NumOperands = 1;
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op<0>().set(InitVal);
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// copyAttributesFrom - copy all additional attributes (those not needed to
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// create a GlobalVariable) from the GlobalVariable Src to this one.
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GlobalValue::copyAttributesFrom(Src);
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setThreadLocal(SrcVar->isThreadLocal());
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GlobalAlias Implementation
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanGlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         const Twine &Name, Constant* aliasee,
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Module *ParentModule)
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LeakDetector::addGarbageObject(this);
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (aliasee)
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op<0>() = aliasee;
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ParentModule)
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ParentModule->getAliasList().push_back(this);
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalAlias::setParent(Module *parent) {
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getParent())
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LeakDetector::addGarbageObject(this);
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Parent = parent;
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getParent())
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LeakDetector::removeGarbageObject(this);
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalAlias::removeFromParent() {
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getParent()->getAliasList().remove(this);
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid GlobalAlias::eraseFromParent() {
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getParent()->getAliasList().erase(this);
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid GlobalAlias::setAliasee(Constant *Aliasee) {
22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((!Aliasee || Aliasee->getType() == getType()) &&
22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Alias and aliasee types should match!");
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperand(0, Aliasee);
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst GlobalValue *GlobalAlias::getAliasedGlobal() const {
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Constant *C = getAliasee();
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (C == 0) return 0;
22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return GV;
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const ConstantExpr *CE = cast<ConstantExpr>(C);
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((CE->getOpcode() == Instruction::BitCast ||
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          CE->getOpcode() == Instruction::GetElementPtr) &&
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Unsupported aliasee");
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return cast<GlobalValue>(CE->getOperand(0));
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallPtrSet<const GlobalValue*, 3> Visited;
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check if we need to stop early.
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (stopOnWeak && mayBeOverridden())
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return this;
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalValue *GV = getAliasedGlobal();
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Visited.insert(GV);
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Iterate over aliasing chain, stopping on weak alias if necessary.
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (stopOnWeak && GA->mayBeOverridden())
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GV = GA->getAliasedGlobal();
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Visited.insert(GV))
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return 0;
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return GV;
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
264