Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1 | //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the GlobalValue & GlobalVariable classes for the VMCore |
| 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 16 | #include "llvm/GlobalVariable.h" |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalAlias.h" |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LeakDetector.h" |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // GlobalValue Class |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 29 | bool GlobalValue::isMaterializable() const { |
Nick Lewycky | 780d2fe | 2010-02-15 21:27:20 +0000 | [diff] [blame] | 30 | return getParent() && getParent()->isMaterializable(this); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 31 | } |
| 32 | bool GlobalValue::isDematerializable() const { |
Nick Lewycky | 780d2fe | 2010-02-15 21:27:20 +0000 | [diff] [blame] | 33 | return getParent() && getParent()->isDematerializable(this); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 34 | } |
| 35 | bool GlobalValue::Materialize(std::string *ErrInfo) { |
| 36 | return getParent()->Materialize(this, ErrInfo); |
| 37 | } |
| 38 | void GlobalValue::Dematerialize() { |
| 39 | getParent()->Dematerialize(this); |
| 40 | } |
| 41 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | /// Override destroyConstant to make sure it doesn't get called on |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 43 | /// GlobalValue's because they shouldn't be treated like other constants. |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 44 | void GlobalValue::destroyConstant() { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 45 | llvm_unreachable("You can't GV->destroyConstant()!"); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 46 | } |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 47 | |
| 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()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 54 | setUnnamedAddr(Src->hasUnnamedAddr()); |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 57 | void GlobalValue::setAlignment(unsigned Align) { |
| 58 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 59 | assert(Align <= MaximumAlignment && |
| 60 | "Alignment is greater than MaximumAlignment!"); |
| 61 | Alignment = Log2_32(Align) + 1; |
| 62 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 63 | } |
Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 64 | |
| 65 | bool GlobalValue::isDeclaration() const { |
Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 66 | // Globals are definitions if they have an initializer. |
Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 67 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) |
| 68 | return GV->getNumOperands() == 0; |
| 69 | |
Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 70 | // Functions are definitions if they have a body. |
Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 71 | if (const Function *F = dyn_cast<Function>(this)) |
| 72 | return F->empty(); |
Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 81210d2 | 2011-07-14 20:22:18 +0000 | [diff] [blame] | 74 | // Aliases are always definitions. |
| 75 | assert(isa<GlobalAlias>(this)); |
Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 78 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
| 80 | // GlobalVariable Implementation |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 83 | GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 84 | Constant *InitVal, const Twine &Name, |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 85 | bool ThreadLocal, unsigned AddressSpace) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 86 | : GlobalValue(PointerType::get(Ty, AddressSpace), |
Owen Anderson | 5948fdf | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 87 | Value::GlobalVariableVal, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 88 | OperandTraits<GlobalVariable>::op_begin(this), |
| 89 | InitVal != 0, Link, Name), |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 90 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 91 | if (InitVal) { |
| 92 | assert(InitVal->getType() == Ty && |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 93 | "Initializer should be the same type as the GlobalVariable!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 94 | Op<0>() = InitVal; |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 95 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 96 | |
| 97 | LeakDetector::addGarbageObject(this); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 100 | GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 101 | LinkageTypes Link, Constant *InitVal, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 102 | const Twine &Name, |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 103 | GlobalVariable *Before, bool ThreadLocal, |
| 104 | unsigned AddressSpace) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 105 | : GlobalValue(PointerType::get(Ty, AddressSpace), |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 106 | Value::GlobalVariableVal, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 107 | OperandTraits<GlobalVariable>::op_begin(this), |
| 108 | InitVal != 0, Link, Name), |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 109 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 110 | if (InitVal) { |
| 111 | assert(InitVal->getType() == Ty && |
| 112 | "Initializer should be the same type as the GlobalVariable!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 113 | Op<0>() = InitVal; |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | LeakDetector::addGarbageObject(this); |
| 117 | |
| 118 | if (Before) |
| 119 | Before->getParent()->getGlobalList().insert(Before, this); |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 120 | else |
| 121 | M.getGlobalList().push_back(this); |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 124 | void GlobalVariable::setParent(Module *parent) { |
| 125 | if (getParent()) |
| 126 | LeakDetector::addGarbageObject(this); |
| 127 | Parent = parent; |
| 128 | if (getParent()) |
| 129 | LeakDetector::removeGarbageObject(this); |
| 130 | } |
| 131 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 132 | void GlobalVariable::removeFromParent() { |
| 133 | getParent()->getGlobalList().remove(this); |
| 134 | } |
| 135 | |
| 136 | void GlobalVariable::eraseFromParent() { |
| 137 | getParent()->getGlobalList().erase(this); |
| 138 | } |
| 139 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 140 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 141 | Use *U) { |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 142 | // If you call this, then you better know this GVar has a constant |
| 143 | // initializer worth replacing. Enforce that here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 144 | assert(getNumOperands() == 1 && |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 145 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 146 | |
| 147 | // And, since you know it has an initializer, the From value better be |
| 148 | // the initializer :) |
| 149 | assert(getOperand(0) == From && |
| 150 | "Attempt to replace wrong constant initializer in GVar"); |
| 151 | |
| 152 | // And, you better have a constant for the replacement value |
| 153 | assert(isa<Constant>(To) && |
| 154 | "Attempt to replace GVar initializer with non-constant"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 155 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 156 | // Okay, preconditions out of the way, replace the constant initializer. |
Chris Lattner | 37b570a | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 157 | this->setOperand(0, cast<Constant>(To)); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 158 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 159 | |
Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 160 | void GlobalVariable::setInitializer(Constant *InitVal) { |
| 161 | if (InitVal == 0) { |
| 162 | if (hasInitializer()) { |
| 163 | Op<0>().set(0); |
| 164 | NumOperands = 0; |
| 165 | } |
| 166 | } else { |
| 167 | assert(InitVal->getType() == getType()->getElementType() && |
| 168 | "Initializer type must match GlobalVariable type"); |
| 169 | if (!hasInitializer()) |
| 170 | NumOperands = 1; |
| 171 | Op<0>().set(InitVal); |
| 172 | } |
| 173 | } |
| 174 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 175 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 176 | /// create a GlobalVariable) from the GlobalVariable Src to this one. |
| 177 | void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { |
| 178 | assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!"); |
| 179 | GlobalValue::copyAttributesFrom(Src); |
| 180 | const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); |
| 181 | setThreadLocal(SrcVar->isThreadLocal()); |
| 182 | } |
| 183 | |
| 184 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 185 | //===----------------------------------------------------------------------===// |
| 186 | // GlobalAlias Implementation |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 189 | GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 190 | const Twine &Name, Constant* aliasee, |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 191 | Module *ParentModule) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 192 | : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 193 | LeakDetector::addGarbageObject(this); |
| 194 | |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 195 | if (aliasee) |
| 196 | assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 197 | Op<0>() = aliasee; |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 198 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 199 | if (ParentModule) |
| 200 | ParentModule->getAliasList().push_back(this); |
| 201 | } |
| 202 | |
| 203 | void GlobalAlias::setParent(Module *parent) { |
| 204 | if (getParent()) |
| 205 | LeakDetector::addGarbageObject(this); |
| 206 | Parent = parent; |
| 207 | if (getParent()) |
| 208 | LeakDetector::removeGarbageObject(this); |
| 209 | } |
| 210 | |
| 211 | void GlobalAlias::removeFromParent() { |
| 212 | getParent()->getAliasList().remove(this); |
| 213 | } |
| 214 | |
| 215 | void GlobalAlias::eraseFromParent() { |
| 216 | getParent()->getAliasList().erase(this); |
| 217 | } |
| 218 | |
Chris Lattner | 7961781 | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 219 | void GlobalAlias::setAliasee(Constant *Aliasee) { |
| 220 | assert((!Aliasee || Aliasee->getType() == getType()) && |
| 221 | "Alias and aliasee types should match!"); |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 222 | |
| 223 | setOperand(0, Aliasee); |
| 224 | } |
| 225 | |
Chris Lattner | c2d0530 | 2007-05-05 23:49:02 +0000 | [diff] [blame] | 226 | const GlobalValue *GlobalAlias::getAliasedGlobal() const { |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 227 | const Constant *C = getAliasee(); |
Chris Lattner | 7961781 | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 228 | if (C == 0) return 0; |
| 229 | |
| 230 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 231 | return GV; |
| 232 | |
| 233 | const ConstantExpr *CE = cast<ConstantExpr>(C); |
| 234 | assert((CE->getOpcode() == Instruction::BitCast || |
| 235 | CE->getOpcode() == Instruction::GetElementPtr) && |
| 236 | "Unsupported aliasee"); |
| 237 | |
| 238 | return dyn_cast<GlobalValue>(CE->getOperand(0)); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Anton Korobeynikov | 1a11404 | 2008-09-09 20:05:04 +0000 | [diff] [blame] | 241 | const GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) const { |
Anton Korobeynikov | 2fb3897 | 2008-03-22 07:48:40 +0000 | [diff] [blame] | 242 | SmallPtrSet<const GlobalValue*, 3> Visited; |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 243 | |
Anton Korobeynikov | ac2c655 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 244 | // Check if we need to stop early. |
Duncan Sands | 715b289 | 2009-01-08 20:55:49 +0000 | [diff] [blame] | 245 | if (stopOnWeak && mayBeOverridden()) |
Anton Korobeynikov | ac2c655 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 246 | return this; |
| 247 | |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 248 | const GlobalValue *GV = getAliasedGlobal(); |
| 249 | Visited.insert(GV); |
| 250 | |
Anton Korobeynikov | ac2c655 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 251 | // Iterate over aliasing chain, stopping on weak alias if necessary. |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 252 | while (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) { |
Duncan Sands | 715b289 | 2009-01-08 20:55:49 +0000 | [diff] [blame] | 253 | if (stopOnWeak && GA->mayBeOverridden()) |
Anton Korobeynikov | ac2c655 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 254 | break; |
| 255 | |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 256 | GV = GA->getAliasedGlobal(); |
| 257 | |
| 258 | if (!Visited.insert(GV)) |
Chris Lattner | 7961781 | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 259 | return 0; |
Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | return GV; |
| 263 | } |