Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1 | //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the GlobalValue & GlobalVariable classes for the IR |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/GlobalValue.h" |
Anton Korobeynikov | e846dd8 | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LeakDetector.h" |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // GlobalValue Class |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 30 | bool GlobalValue::isMaterializable() const { |
Nick Lewycky | 936c43b | 2010-02-15 21:27:20 +0000 | [diff] [blame] | 31 | return getParent() && getParent()->isMaterializable(this); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 32 | } |
| 33 | bool GlobalValue::isDematerializable() const { |
Nick Lewycky | 936c43b | 2010-02-15 21:27:20 +0000 | [diff] [blame] | 34 | return getParent() && getParent()->isDematerializable(this); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 35 | } |
| 36 | bool GlobalValue::Materialize(std::string *ErrInfo) { |
| 37 | return getParent()->Materialize(this, ErrInfo); |
| 38 | } |
| 39 | void GlobalValue::Dematerialize() { |
| 40 | getParent()->Dematerialize(this); |
| 41 | } |
| 42 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 43 | /// Override destroyConstant to make sure it doesn't get called on |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 44 | /// GlobalValue's because they shouldn't be treated like other constants. |
Owen Anderson | 04fb7c3 | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 45 | void GlobalValue::destroyConstant() { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 46 | llvm_unreachable("You can't GV->destroyConstant()!"); |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 47 | } |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 48 | |
| 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()); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 55 | setUnnamedAddr(Src->hasUnnamedAddr()); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Dan Gohman | 6bbe671 | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 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!"); |
| 64 | } |
Chris Lattner | 6c48244 | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 65 | |
| 66 | bool GlobalValue::isDeclaration() const { |
Chris Lattner | 97d9730 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 67 | // Globals are definitions if they have an initializer. |
Chris Lattner | 6c48244 | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 68 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) |
| 69 | return GV->getNumOperands() == 0; |
| 70 | |
Chris Lattner | 97d9730 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 71 | // Functions are definitions if they have a body. |
Chris Lattner | 6c48244 | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 72 | if (const Function *F = dyn_cast<Function>(this)) |
| 73 | return F->empty(); |
Chris Lattner | 97d9730 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 0063225 | 2011-07-14 20:22:18 +0000 | [diff] [blame] | 75 | // Aliases are always definitions. |
| 76 | assert(isa<GlobalAlias>(this)); |
Chris Lattner | 6c48244 | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
Dan Gohman | 6bbe671 | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 79 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
| 81 | // GlobalVariable Implementation |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 84 | GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, |
Michael Gottesman | af2f494 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 85 | Constant *InitVal, |
| 86 | const Twine &Name, ThreadLocalMode TLMode, |
| 87 | unsigned AddressSpace, |
| 88 | bool isExternallyInitialized) |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 89 | : GlobalValue(PointerType::get(Ty, AddressSpace), |
| 90 | Value::GlobalVariableVal, |
| 91 | OperandTraits<GlobalVariable>::op_begin(this), |
| 92 | InitVal != 0, Link, Name), |
Michael Gottesman | af2f494 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 93 | isConstantGlobal(constant), threadLocalMode(TLMode), |
| 94 | isExternallyInitializedConstant(isExternallyInitialized) { |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 95 | if (InitVal) { |
| 96 | assert(InitVal->getType() == Ty && |
Alkis Evlogimenos | 8243976 | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 97 | "Initializer should be the same type as the GlobalVariable!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 98 | Op<0>() = InitVal; |
Alkis Evlogimenos | 8243976 | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 99 | } |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 100 | |
| 101 | LeakDetector::addGarbageObject(this); |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 104 | GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 105 | LinkageTypes Link, Constant *InitVal, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 106 | const Twine &Name, |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 107 | GlobalVariable *Before, ThreadLocalMode TLMode, |
Michael Gottesman | af2f494 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 108 | unsigned AddressSpace, |
| 109 | bool isExternallyInitialized) |
Hans Wennborg | 6de8ffb | 2012-06-23 12:14:23 +0000 | [diff] [blame] | 110 | : GlobalValue(PointerType::get(Ty, AddressSpace), |
Owen Anderson | c341f1c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 111 | Value::GlobalVariableVal, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 112 | OperandTraits<GlobalVariable>::op_begin(this), |
| 113 | InitVal != 0, Link, Name), |
Michael Gottesman | af2f494 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 114 | isConstantGlobal(constant), threadLocalMode(TLMode), |
| 115 | isExternallyInitializedConstant(isExternallyInitialized) { |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 116 | if (InitVal) { |
| 117 | assert(InitVal->getType() == Ty && |
| 118 | "Initializer should be the same type as the GlobalVariable!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 119 | Op<0>() = InitVal; |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | LeakDetector::addGarbageObject(this); |
| 123 | |
| 124 | if (Before) |
| 125 | Before->getParent()->getGlobalList().insert(Before, this); |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 126 | else |
| 127 | M.getGlobalList().push_back(this); |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 130 | void GlobalVariable::setParent(Module *parent) { |
| 131 | if (getParent()) |
| 132 | LeakDetector::addGarbageObject(this); |
| 133 | Parent = parent; |
| 134 | if (getParent()) |
| 135 | LeakDetector::removeGarbageObject(this); |
| 136 | } |
| 137 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 138 | void GlobalVariable::removeFromParent() { |
| 139 | getParent()->getGlobalList().remove(this); |
| 140 | } |
| 141 | |
| 142 | void GlobalVariable::eraseFromParent() { |
| 143 | getParent()->getGlobalList().erase(this); |
| 144 | } |
| 145 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 146 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 147 | Use *U) { |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 148 | // If you call this, then you better know this GVar has a constant |
| 149 | // initializer worth replacing. Enforce that here. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 150 | assert(getNumOperands() == 1 && |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 151 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 152 | |
| 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"); |
| 157 | |
| 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"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 161 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 162 | // Okay, preconditions out of the way, replace the constant initializer. |
Chris Lattner | 07d7c9d | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 163 | this->setOperand(0, cast<Constant>(To)); |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 164 | } |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 165 | |
Jeffrey Yasskin | dc1472b | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 166 | void GlobalVariable::setInitializer(Constant *InitVal) { |
| 167 | if (InitVal == 0) { |
| 168 | if (hasInitializer()) { |
| 169 | Op<0>().set(0); |
| 170 | NumOperands = 0; |
| 171 | } |
| 172 | } else { |
| 173 | assert(InitVal->getType() == getType()->getElementType() && |
| 174 | "Initializer type must match GlobalVariable type"); |
| 175 | if (!hasInitializer()) |
| 176 | NumOperands = 1; |
| 177 | Op<0>().set(InitVal); |
| 178 | } |
| 179 | } |
| 180 | |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 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()); |
| 188 | } |
| 189 | |
| 190 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
| 192 | // GlobalAlias Implementation |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 195 | GlobalAlias::GlobalAlias(Type *Ty, LinkageTypes Link, |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 196 | const Twine &Name, Constant* aliasee, |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 197 | Module *ParentModule) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 198 | : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 199 | LeakDetector::addGarbageObject(this); |
| 200 | |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 201 | if (aliasee) |
| 202 | assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 203 | Op<0>() = aliasee; |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 204 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 205 | if (ParentModule) |
| 206 | ParentModule->getAliasList().push_back(this); |
| 207 | } |
| 208 | |
| 209 | void GlobalAlias::setParent(Module *parent) { |
| 210 | if (getParent()) |
| 211 | LeakDetector::addGarbageObject(this); |
| 212 | Parent = parent; |
| 213 | if (getParent()) |
| 214 | LeakDetector::removeGarbageObject(this); |
| 215 | } |
| 216 | |
| 217 | void GlobalAlias::removeFromParent() { |
| 218 | getParent()->getAliasList().remove(this); |
| 219 | } |
| 220 | |
| 221 | void GlobalAlias::eraseFromParent() { |
| 222 | getParent()->getAliasList().erase(this); |
| 223 | } |
| 224 | |
Chris Lattner | 9854e1b | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 225 | void GlobalAlias::setAliasee(Constant *Aliasee) { |
| 226 | assert((!Aliasee || Aliasee->getType() == getType()) && |
| 227 | "Alias and aliasee types should match!"); |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 228 | |
| 229 | setOperand(0, Aliasee); |
| 230 | } |
| 231 | |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 232 | GlobalValue *GlobalAlias::getAliasedGlobal() { |
| 233 | Constant *C = getAliasee(); |
Chris Lattner | 9854e1b | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 234 | if (C == 0) return 0; |
| 235 | |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 236 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
Chris Lattner | 9854e1b | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 237 | return GV; |
| 238 | |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 239 | ConstantExpr *CE = cast<ConstantExpr>(C); |
Chris Lattner | 9854e1b | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 240 | assert((CE->getOpcode() == Instruction::BitCast || |
| 241 | CE->getOpcode() == Instruction::GetElementPtr) && |
| 242 | "Unsupported aliasee"); |
| 243 | |
Jay Foad | 4255274 | 2011-08-01 12:28:01 +0000 | [diff] [blame] | 244 | return cast<GlobalValue>(CE->getOperand(0)); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 247 | GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) { |
| 248 | SmallPtrSet<GlobalValue*, 3> Visited; |
Anton Korobeynikov | e846dd8 | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 249 | |
Anton Korobeynikov | 832b2a9 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 250 | // Check if we need to stop early. |
Duncan Sands | 86062af | 2009-01-08 20:55:49 +0000 | [diff] [blame] | 251 | if (stopOnWeak && mayBeOverridden()) |
Anton Korobeynikov | 832b2a9 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 252 | return this; |
| 253 | |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 254 | GlobalValue *GV = getAliasedGlobal(); |
Anton Korobeynikov | e846dd8 | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 255 | Visited.insert(GV); |
| 256 | |
Anton Korobeynikov | 832b2a9 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 257 | // Iterate over aliasing chain, stopping on weak alias if necessary. |
Peter Collingbourne | abd3796 | 2013-08-19 23:13:33 +0000 | [diff] [blame] | 258 | while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) { |
Duncan Sands | 86062af | 2009-01-08 20:55:49 +0000 | [diff] [blame] | 259 | if (stopOnWeak && GA->mayBeOverridden()) |
Anton Korobeynikov | 832b2a9 | 2008-09-09 18:23:48 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
Anton Korobeynikov | e846dd8 | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 262 | GV = GA->getAliasedGlobal(); |
| 263 | |
| 264 | if (!Visited.insert(GV)) |
Chris Lattner | 9854e1b | 2011-07-14 18:01:49 +0000 | [diff] [blame] | 265 | return 0; |
Anton Korobeynikov | e846dd8 | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return GV; |
| 269 | } |