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);
104 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
105 LinkageTypes Link, Constant *InitVal,
107 GlobalVariable *Before, ThreadLocalMode TLMode,
108 unsigned AddressSpace,
109 bool isExternallyInitialized)
110 : GlobalValue(PointerType::get(Ty, AddressSpace),
111 Value::GlobalVariableVal,
112 OperandTraits<GlobalVariable>::op_begin(this),
113 InitVal != 0, Link, Name),
114 isConstantGlobal(constant), threadLocalMode(TLMode),
115 isExternallyInitializedConstant(isExternallyInitialized) {
117 assert(InitVal->getType() == Ty &&
118 "Initializer should be the same type as the GlobalVariable!");
122 LeakDetector::addGarbageObject(this);
125 Before->getParent()->getGlobalList().insert(Before, this);
127 M.getGlobalList().push_back(this);
130 void GlobalVariable::setParent(Module *parent) {
132 LeakDetector::addGarbageObject(this);
135 LeakDetector::removeGarbageObject(this);
138 void GlobalVariable::removeFromParent() {
139 getParent()->getGlobalList().remove(this);
142 void GlobalVariable::eraseFromParent() {
143 getParent()->getGlobalList().erase(this);
146 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
148 // If you call this, then you better know this GVar has a constant
149 // initializer worth replacing. Enforce that here.
150 assert(getNumOperands() == 1 &&
151 "Attempt to replace uses of Constants on a GVar with no initializer");
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");
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");
162 // Okay, preconditions out of the way, replace the constant initializer.
163 this->setOperand(0, cast<Constant>(To));
166 void GlobalVariable::setInitializer(Constant *InitVal) {
168 if (hasInitializer()) {
173 assert(InitVal->getType() == getType()->getElementType() &&
174 "Initializer type must match GlobalVariable type");
175 if (!hasInitializer())
177 Op<0>().set(InitVal);
181 /// copyAttributesFrom - copy all additional attributes (those not needed to
182 /// create a GlobalVariable) from the GlobalVariable Src to this one.
183 void 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());
191 //===----------------------------------------------------------------------===//
192 // GlobalAlias Implementation
193 //===----------------------------------------------------------------------===//
195 GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link,
196 const Twine &Name, Constant* aliasee,
197 Module *ParentModule)
198 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
199 LeakDetector::addGarbageObject(this);
202 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
206 ParentModule->getAliasList().push_back(this);
209 void GlobalAlias::setParent(Module *parent) {
211 LeakDetector::addGarbageObject(this);
214 LeakDetector::removeGarbageObject(this);
217 void GlobalAlias::removeFromParent() {
218 getParent()->getAliasList().remove(this);
221 void GlobalAlias::eraseFromParent() {
222 getParent()->getAliasList().erase(this);
225 void GlobalAlias::setAliasee(Constant *Aliasee) {
226 assert((!Aliasee || Aliasee->getType() == getType()) &&
227 "Alias and aliasee types should match!");
229 setOperand(0, Aliasee);
232 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
233 const Constant *C = getAliasee();
234 if (C == 0) return 0;
236 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
239 const ConstantExpr *CE = cast<ConstantExpr>(C);
240 assert((CE->getOpcode() == Instruction::BitCast ||
241 CE->getOpcode() == Instruction::GetElementPtr) &&
242 "Unsupported aliasee");
244 return cast<GlobalValue>(CE->getOperand(0));
247 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
248 SmallPtrSet<const GlobalValue*, 3> Visited;
250 // Check if we need to stop early.
251 if (stopOnWeak && mayBeOverridden())
254 const GlobalValue *GV = getAliasedGlobal();
257 // Iterate over aliasing chain, stopping on weak alias if necessary.
258 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
259 if (stopOnWeak && GA->mayBeOverridden())
262 GV = GA->getAliasedGlobal();
264 if (!Visited.insert(GV))