When promoting an alloca to registers discard any lifetime intrinsics.
[llvm/stm8.git] / lib / VMCore / Globals.cpp
blob60000ad1b50e3e2fa127b73a8542278bb82bdbfd
1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the GlobalValue & GlobalVariable classes for the VMCore
11 // library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/LeakDetector.h"
23 using namespace llvm;
25 //===----------------------------------------------------------------------===//
26 // GlobalValue Class
27 //===----------------------------------------------------------------------===//
29 bool GlobalValue::isMaterializable() const {
30 return getParent() && getParent()->isMaterializable(this);
32 bool GlobalValue::isDematerializable() const {
33 return getParent() && getParent()->isDematerializable(this);
35 bool GlobalValue::Materialize(std::string *ErrInfo) {
36 return getParent()->Materialize(this, ErrInfo);
38 void GlobalValue::Dematerialize() {
39 getParent()->Dematerialize(this);
42 /// Override destroyConstant to make sure it doesn't get called on
43 /// GlobalValue's because they shouldn't be treated like other constants.
44 void GlobalValue::destroyConstant() {
45 llvm_unreachable("You can't GV->destroyConstant()!");
48 /// copyAttributesFrom - copy all additional attributes (those not needed to
49 /// create a GlobalValue) from the GlobalValue Src to this one.
50 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
51 setAlignment(Src->getAlignment());
52 setSection(Src->getSection());
53 setVisibility(Src->getVisibility());
56 void GlobalValue::setAlignment(unsigned Align) {
57 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
58 assert(Align <= MaximumAlignment &&
59 "Alignment is greater than MaximumAlignment!");
60 Alignment = Log2_32(Align) + 1;
61 assert(getAlignment() == Align && "Alignment representation error!");
64 //===----------------------------------------------------------------------===//
65 // GlobalVariable Implementation
66 //===----------------------------------------------------------------------===//
68 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
69 Constant *InitVal, const Twine &Name,
70 bool ThreadLocal, unsigned AddressSpace)
71 : GlobalValue(PointerType::get(Ty, AddressSpace),
72 Value::GlobalVariableVal,
73 OperandTraits<GlobalVariable>::op_begin(this),
74 InitVal != 0, Link, Name),
75 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
76 if (InitVal) {
77 assert(InitVal->getType() == Ty &&
78 "Initializer should be the same type as the GlobalVariable!");
79 Op<0>() = InitVal;
82 LeakDetector::addGarbageObject(this);
85 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
86 LinkageTypes Link, Constant *InitVal,
87 const Twine &Name,
88 GlobalVariable *Before, bool ThreadLocal,
89 unsigned AddressSpace)
90 : GlobalValue(PointerType::get(Ty, AddressSpace),
91 Value::GlobalVariableVal,
92 OperandTraits<GlobalVariable>::op_begin(this),
93 InitVal != 0, Link, Name),
94 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
95 if (InitVal) {
96 assert(InitVal->getType() == Ty &&
97 "Initializer should be the same type as the GlobalVariable!");
98 Op<0>() = InitVal;
101 LeakDetector::addGarbageObject(this);
103 if (Before)
104 Before->getParent()->getGlobalList().insert(Before, this);
105 else
106 M.getGlobalList().push_back(this);
109 void GlobalVariable::setParent(Module *parent) {
110 if (getParent())
111 LeakDetector::addGarbageObject(this);
112 Parent = parent;
113 if (getParent())
114 LeakDetector::removeGarbageObject(this);
117 void GlobalVariable::removeFromParent() {
118 getParent()->getGlobalList().remove(this);
121 void GlobalVariable::eraseFromParent() {
122 getParent()->getGlobalList().erase(this);
125 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
126 Use *U) {
127 // If you call this, then you better know this GVar has a constant
128 // initializer worth replacing. Enforce that here.
129 assert(getNumOperands() == 1 &&
130 "Attempt to replace uses of Constants on a GVar with no initializer");
132 // And, since you know it has an initializer, the From value better be
133 // the initializer :)
134 assert(getOperand(0) == From &&
135 "Attempt to replace wrong constant initializer in GVar");
137 // And, you better have a constant for the replacement value
138 assert(isa<Constant>(To) &&
139 "Attempt to replace GVar initializer with non-constant");
141 // Okay, preconditions out of the way, replace the constant initializer.
142 this->setOperand(0, cast<Constant>(To));
145 void GlobalVariable::setInitializer(Constant *InitVal) {
146 if (InitVal == 0) {
147 if (hasInitializer()) {
148 Op<0>().set(0);
149 NumOperands = 0;
151 } else {
152 assert(InitVal->getType() == getType()->getElementType() &&
153 "Initializer type must match GlobalVariable type");
154 if (!hasInitializer())
155 NumOperands = 1;
156 Op<0>().set(InitVal);
160 /// copyAttributesFrom - copy all additional attributes (those not needed to
161 /// create a GlobalVariable) from the GlobalVariable Src to this one.
162 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
163 assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
164 GlobalValue::copyAttributesFrom(Src);
165 const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
166 setThreadLocal(SrcVar->isThreadLocal());
170 //===----------------------------------------------------------------------===//
171 // GlobalAlias Implementation
172 //===----------------------------------------------------------------------===//
174 GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
175 const Twine &Name, Constant* aliasee,
176 Module *ParentModule)
177 : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
178 LeakDetector::addGarbageObject(this);
180 if (aliasee)
181 assert(aliasee->getType() == Ty && "Alias and aliasee types should match!");
182 Op<0>() = aliasee;
184 if (ParentModule)
185 ParentModule->getAliasList().push_back(this);
188 void GlobalAlias::setParent(Module *parent) {
189 if (getParent())
190 LeakDetector::addGarbageObject(this);
191 Parent = parent;
192 if (getParent())
193 LeakDetector::removeGarbageObject(this);
196 void GlobalAlias::removeFromParent() {
197 getParent()->getAliasList().remove(this);
200 void GlobalAlias::eraseFromParent() {
201 getParent()->getAliasList().erase(this);
204 bool GlobalAlias::isDeclaration() const {
205 const GlobalValue* AV = getAliasedGlobal();
206 if (AV)
207 return AV->isDeclaration();
208 else
209 return false;
212 void GlobalAlias::setAliasee(Constant *Aliasee)
214 if (Aliasee)
215 assert(Aliasee->getType() == getType() &&
216 "Alias and aliasee types should match!");
218 setOperand(0, Aliasee);
221 const GlobalValue *GlobalAlias::getAliasedGlobal() const {
222 const Constant *C = getAliasee();
223 if (C) {
224 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
225 return GV;
226 else {
227 const ConstantExpr *CE = 0;
228 if ((CE = dyn_cast<ConstantExpr>(C)) &&
229 (CE->getOpcode() == Instruction::BitCast ||
230 CE->getOpcode() == Instruction::GetElementPtr))
231 return dyn_cast<GlobalValue>(CE->getOperand(0));
232 else
233 llvm_unreachable("Unsupported aliasee");
236 return 0;
239 const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const {
240 SmallPtrSet<const GlobalValue*, 3> Visited;
242 // Check if we need to stop early.
243 if (stopOnWeak && mayBeOverridden())
244 return this;
246 const GlobalValue *GV = getAliasedGlobal();
247 Visited.insert(GV);
249 // Iterate over aliasing chain, stopping on weak alias if necessary.
250 while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
251 if (stopOnWeak && GA->mayBeOverridden())
252 break;
254 GV = GA->getAliasedGlobal();
256 if (!Visited.insert(GV))
257 return NULL;
260 return GV;