1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the IR
13 //===----------------------------------------------------------------------===//
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/Module.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LeakDetector.h"
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 bool GlobalValue::isMaterializable() const {
31 return getParent() && getParent()->isMaterializable(this);
33 bool GlobalValue::isDematerializable() const {
34 return getParent() && getParent()->isDematerializable(this);
36 bool GlobalValue::Materialize(std::string *ErrInfo) {
37 return getParent()->Materialize(this, ErrInfo);
39 void GlobalValue::Dematerialize() {
40 getParent()->Dematerialize(this);
43 /// Override destroyConstant to make sure it doesn't get called on
44 /// GlobalValue's because they shouldn't be treated like other constants.
45 void GlobalValue::destroyConstant() {
46 llvm_unreachable("You can't GV->destroyConstant()!");
49 /// copyAttributesFrom - copy all additional attributes (those not needed to
50 /// create a GlobalValue) from the GlobalValue Src to this one.
51 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
52 setAlignment(Src->getAlignment());
53 setSection(Src->getSection());
54 setVisibility(Src->getVisibility());
55 setUnnamedAddr(Src->hasUnnamedAddr());
58 void 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!");
66 bool GlobalValue::isDeclaration() const {
67 // Globals are definitions if they have an initializer.
68 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
69 return GV->getNumOperands() == 0;
71 // Functions are definitions if they have a body.
72 if (const Function *F = dyn_cast<Function>(this))
75 // Aliases are always definitions.
76 assert(isa<GlobalAlias>(this));
80 //===----------------------------------------------------------------------===//
81 // GlobalVariable Implementation
82 //===----------------------------------------------------------------------===//
84 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
86 const Twine &Name, ThreadLocalMode TLMode,
87 unsigned AddressSpace,
88 bool isExternallyInitialized)
89 : GlobalValue(PointerType::get(Ty, AddressSpace),
90 Value::GlobalVariableVal,
91 OperandTraits<GlobalVariable>::op_begin(this),
92 InitVal != 0, Link, Name),
93 isConstantGlobal(constant), threadLocalMode(TLMode),
94 isExternallyInitializedConstant(isExternallyInitialized) {
96 assert(InitVal->getType() == Ty &&
97 "Initializer should be the same type as the GlobalVariable!");
101 LeakDetector::addGarbageObject(this);
102 setAddressMaybeTaken(true);
105 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
106 LinkageTypes Link, Constant *InitVal,
108 GlobalVariable *Before, ThreadLocalMode TLMode,
109 unsigned AddressSpace,
110 bool isExternallyInitialized)
111 : GlobalValue(PointerType::get(Ty, AddressSpace),
112 Value::GlobalVariableVal,
113 OperandTraits<GlobalVariable>::op_begin(this),
114 InitVal != 0, Link, Name),
115 isConstantGlobal(constant), threadLocalMode(TLMode),
116 isExternallyInitializedConstant(isExternallyInitialized) {
118 assert(InitVal->getType() == Ty &&
119 "Initializer should be the same type as the GlobalVariable!");
123 LeakDetector::addGarbageObject(this);
126 Before->getParent()->getGlobalList().insert(Before, this);
128 M.getGlobalList().push_back(this);
129 setAddressMaybeTaken(true);
132 void GlobalVariable::setParent(Module *parent) {
134 LeakDetector::addGarbageObject(this);
137 LeakDetector::removeGarbageObject(this);
140 void GlobalVariable::removeFromParent() {
141 getParent()->getGlobalList().remove(this);
144 void GlobalVariable::eraseFromParent() {
145 getParent()->getGlobalList().erase(this);
148 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
150 // If you call this, then you better know this GVar has a constant
151 // initializer worth replacing. Enforce that here.
152 assert(getNumOperands() == 1 &&
153 "Attempt to replace uses of Constants on a GVar with no initializer");
155 // And, since you know it has an initializer, the From value better be
156 // the initializer :)
157 assert(getOperand(0) == From &&
158 "Attempt to replace wrong constant initializer in GVar");
160 // And, you better have a constant for the replacement value
161 assert(isa<Constant>(To) &&
162 "Attempt to replace GVar initializer with non-constant");
164 // Okay, preconditions out of the way, replace the constant initializer.
165 this->setOperand(0, cast<Constant>(To));
168 void GlobalVariable::setInitializer(Constant *InitVal) {
170 if (hasInitializer()) {
175 assert(InitVal->getType() == getType()->getElementType() &&
176 "Initializer type must match GlobalVariable type");
177 if (!hasInitializer())
179 Op<0>().set(InitVal);
183 /// copyAttributesFrom - copy all additional attributes (those not needed to
184 /// create a GlobalVariable) from the GlobalVariable Src to this one.
185 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
186 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
187 GlobalValue::copyAttributesFrom(Src);
188 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
189 setThreadLocal(SrcVar->isThreadLocal());
190 setAddressMaybeTaken(SrcVar->AddressMaybeTaken());
194 //===----------------------------------------------------------------------===//
195 // GlobalAlias Implementation
196 //===----------------------------------------------------------------------===//
198 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
199 const Twine &Name, Constant* aliasee,
200 Module *ParentModule)
201 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
202 LeakDetector::addGarbageObject(this);
205 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
209 ParentModule->getAliasList().push_back(this);
212 void GlobalAlias::setParent(Module *parent) {
214 LeakDetector::addGarbageObject(this);
217 LeakDetector::removeGarbageObject(this);
220 void GlobalAlias::removeFromParent() {
221 getParent()->getAliasList().remove(this);
224 void GlobalAlias::eraseFromParent() {
225 getParent()->getAliasList().erase(this);
228 void GlobalAlias::setAliasee(Constant *Aliasee) {
229 assert((!Aliasee || Aliasee->getType() == getType()) &&
230 "Alias and aliasee types should match!");
232 setOperand(0, Aliasee);
235 GlobalValue *GlobalAlias::getAliasedGlobal() {
236 Constant *C = getAliasee();
237 if (C == 0) return 0;
239 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
242 ConstantExpr *CE = cast<ConstantExpr>(C);
243 assert((CE->getOpcode() == Instruction::BitCast ||
244 CE->getOpcode() == Instruction::GetElementPtr) &&
245 "Unsupported aliasee");
247 return cast<GlobalValue>(CE->getOperand(0));
250 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
251 SmallPtrSet<GlobalValue*, 3> Visited;
253 // Check if we need to stop early.
254 if (stopOnWeak && mayBeOverridden())
257 GlobalValue *GV = getAliasedGlobal();
260 // Iterate over aliasing chain, stopping on weak alias if necessary.
261 while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
262 if (stopOnWeak && GA->mayBeOverridden())
265 GV = GA->getAliasedGlobal();
267 if (!Visited.insert(GV))