| 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 | // |
| Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the GlobalValue & GlobalVariable classes for the IR |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/GlobalValue.h" |
| Anton Korobeynikov | da7db7d | 2008-03-11 22:28:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
| Chandler Carruth | 9fb823b | 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" |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Operator.h" |
| Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // GlobalValue Class |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 30 | bool GlobalValue::isMaterializable() const { |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 31 | if (const Function *F = dyn_cast<Function>(this)) |
| 32 | return F->isMaterializable(); |
| 33 | return false; |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 34 | } |
| 35 | bool GlobalValue::isDematerializable() const { |
| Nick Lewycky | 780d2fe | 2010-02-15 21:27:20 +0000 | [diff] [blame] | 36 | return getParent() && getParent()->isDematerializable(this); |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 37 | } |
| Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 38 | std::error_code GlobalValue::materialize() { |
| 39 | return getParent()->materialize(this); |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 40 | } |
| Eric Christopher | 97cb565 | 2015-05-15 18:20:14 +0000 | [diff] [blame] | 41 | void GlobalValue::dematerialize() { |
| 42 | getParent()->dematerialize(this); |
| Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 45 | /// Override destroyConstant to make sure it doesn't get called on |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 46 | /// GlobalValue's because they shouldn't be treated like other constants. |
| Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 47 | void GlobalValue::destroyConstant() { |
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 48 | llvm_unreachable("You can't GV->destroyConstant()!"); |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 49 | } |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 50 | |
| 51 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 52 | /// create a GlobalValue) from the GlobalValue Src to this one. |
| 53 | void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 54 | setVisibility(Src->getVisibility()); |
| Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 55 | setUnnamedAddr(Src->hasUnnamedAddr()); |
| Rafael Espindola | 75ec01f | 2014-02-13 05:11:35 +0000 | [diff] [blame] | 56 | setDLLStorageClass(Src->getDLLStorageClass()); |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 59 | unsigned GlobalValue::getAlignment() const { |
| 60 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 61 | // In general we cannot compute this at the IR level, but we try. |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 62 | if (const GlobalObject *GO = GA->getBaseObject()) |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 63 | return GO->getAlignment(); |
| 64 | |
| 65 | // FIXME: we should also be able to handle: |
| 66 | // Alias = Global + Offset |
| 67 | // Alias = Absolute |
| 68 | return 0; |
| 69 | } |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 70 | return cast<GlobalObject>(this)->getAlignment(); |
| Rafael Espindola | 52dc5d8 | 2014-05-06 16:48:58 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 73 | void GlobalObject::setAlignment(unsigned Align) { |
| Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 74 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 75 | assert(Align <= MaximumAlignment && |
| 76 | "Alignment is greater than MaximumAlignment!"); |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 77 | unsigned AlignmentData = Log2_32(Align) + 1; |
| 78 | unsigned OldData = getGlobalValueSubClassData(); |
| 79 | setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); |
| Dan Gohman | 390914c | 2010-07-28 20:56:48 +0000 | [diff] [blame] | 80 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 81 | } |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 82 | |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 83 | unsigned GlobalObject::getGlobalObjectSubClassData() const { |
| 84 | unsigned ValueData = getGlobalValueSubClassData(); |
| 85 | return ValueData >> AlignmentBits; |
| 86 | } |
| 87 | |
| 88 | void GlobalObject::setGlobalObjectSubClassData(unsigned Val) { |
| 89 | unsigned OldData = getGlobalValueSubClassData(); |
| 90 | setGlobalValueSubClassData((OldData & AlignmentMask) | |
| 91 | (Val << AlignmentBits)); |
| 92 | assert(getGlobalObjectSubClassData() == Val && "representation error"); |
| 93 | } |
| 94 | |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 95 | void GlobalObject::copyAttributesFrom(const GlobalValue *Src) { |
| 96 | const auto *GV = cast<GlobalObject>(Src); |
| 97 | GlobalValue::copyAttributesFrom(GV); |
| 98 | setAlignment(GV->getAlignment()); |
| 99 | setSection(GV->getSection()); |
| 100 | } |
| 101 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 102 | const char *GlobalValue::getSection() const { |
| 103 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 104 | // In general we cannot compute this at the IR level, but we try. |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 105 | if (const GlobalObject *GO = GA->getBaseObject()) |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 106 | return GO->getSection(); |
| 107 | return ""; |
| 108 | } |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 109 | return cast<GlobalObject>(this)->getSection(); |
| Rafael Espindola | 8d8f100 | 2014-05-06 22:44:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 112 | Comdat *GlobalValue::getComdat() { |
| 113 | if (auto *GA = dyn_cast<GlobalAlias>(this)) { |
| 114 | // In general we cannot compute this at the IR level, but we try. |
| 115 | if (const GlobalObject *GO = GA->getBaseObject()) |
| 116 | return const_cast<GlobalObject *>(GO)->getComdat(); |
| 117 | return nullptr; |
| 118 | } |
| 119 | return cast<GlobalObject>(this)->getComdat(); |
| 120 | } |
| 121 | |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 122 | void GlobalObject::setSection(StringRef S) { Section = S; } |
| Rafael Espindola | 8fbbfbb | 2014-05-06 14:59:14 +0000 | [diff] [blame] | 123 | |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 124 | bool GlobalValue::isDeclaration() const { |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 125 | // Globals are definitions if they have an initializer. |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 126 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) |
| 127 | return GV->getNumOperands() == 0; |
| 128 | |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 129 | // Functions are definitions if they have a body. |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 130 | if (const Function *F = dyn_cast<Function>(this)) |
| Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 131 | return F->empty() && !F->isMaterializable(); |
| Chris Lattner | 8561721 | 2011-07-14 18:12:44 +0000 | [diff] [blame] | 132 | |
| Chris Lattner | 81210d2 | 2011-07-14 20:22:18 +0000 | [diff] [blame] | 133 | // Aliases are always definitions. |
| 134 | assert(isa<GlobalAlias>(this)); |
| Chris Lattner | 923053a | 2011-07-14 18:10:41 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| Rafael Espindola | a03c599 | 2014-05-09 14:31:07 +0000 | [diff] [blame] | 137 | |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
| 139 | // GlobalVariable Implementation |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | |
| Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 142 | GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 143 | Constant *InitVal, const Twine &Name, |
| 144 | ThreadLocalMode TLMode, unsigned AddressSpace, |
| Michael Gottesman | 4c4ffd7 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 145 | bool isExternallyInitialized) |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 146 | : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, |
| 147 | OperandTraits<GlobalVariable>::op_begin(this), |
| 148 | InitVal != nullptr, Link, Name), |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 149 | isConstantGlobal(constant), |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 150 | isExternallyInitializedConstant(isExternallyInitialized) { |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 151 | setThreadLocalMode(TLMode); |
| Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 152 | if (InitVal) { |
| 153 | assert(InitVal->getType() == Ty && |
| Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 154 | "Initializer should be the same type as the GlobalVariable!"); |
| Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 155 | Op<0>() = InitVal; |
| Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 156 | } |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 159 | GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, |
| Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 160 | LinkageTypes Link, Constant *InitVal, |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 161 | const Twine &Name, GlobalVariable *Before, |
| 162 | ThreadLocalMode TLMode, unsigned AddressSpace, |
| Michael Gottesman | 4c4ffd7 | 2013-02-03 21:54:38 +0000 | [diff] [blame] | 163 | bool isExternallyInitialized) |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 164 | : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, |
| 165 | OperandTraits<GlobalVariable>::op_begin(this), |
| 166 | InitVal != nullptr, Link, Name), |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 167 | isConstantGlobal(constant), |
| Rafael Espindola | b2ea339 | 2014-05-09 15:49:02 +0000 | [diff] [blame] | 168 | isExternallyInitializedConstant(isExternallyInitialized) { |
| Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 169 | setThreadLocalMode(TLMode); |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 170 | if (InitVal) { |
| 171 | assert(InitVal->getType() == Ty && |
| 172 | "Initializer should be the same type as the GlobalVariable!"); |
| Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 173 | Op<0>() = InitVal; |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 174 | } |
| Rafael Espindola | a03c599 | 2014-05-09 14:31:07 +0000 | [diff] [blame] | 175 | |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 176 | if (Before) |
| 177 | Before->getParent()->getGlobalList().insert(Before, this); |
| Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 178 | else |
| 179 | M.getGlobalList().push_back(this); |
| Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 182 | void GlobalVariable::setParent(Module *parent) { |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 183 | Parent = parent; |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 186 | void GlobalVariable::removeFromParent() { |
| 187 | getParent()->getGlobalList().remove(this); |
| 188 | } |
| 189 | |
| 190 | void GlobalVariable::eraseFromParent() { |
| 191 | getParent()->getGlobalList().erase(this); |
| 192 | } |
| 193 | |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 194 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
| Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 195 | Use *U) { |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 196 | // If you call this, then you better know this GVar has a constant |
| 197 | // initializer worth replacing. Enforce that here. |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 198 | assert(getNumOperands() == 1 && |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 199 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 200 | |
| 201 | // And, since you know it has an initializer, the From value better be |
| 202 | // the initializer :) |
| 203 | assert(getOperand(0) == From && |
| 204 | "Attempt to replace wrong constant initializer in GVar"); |
| 205 | |
| 206 | // And, you better have a constant for the replacement value |
| 207 | assert(isa<Constant>(To) && |
| 208 | "Attempt to replace GVar initializer with non-constant"); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 209 | |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 210 | // Okay, preconditions out of the way, replace the constant initializer. |
| Chris Lattner | 37b570a | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 211 | this->setOperand(0, cast<Constant>(To)); |
| Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 212 | } |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 213 | |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 214 | void GlobalVariable::setInitializer(Constant *InitVal) { |
| Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 215 | if (!InitVal) { |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 216 | if (hasInitializer()) { |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame^] | 217 | // Note, the num operands is used to compute the offset of the operand, so |
| 218 | // the order here matters. Clearing the operand then clearing the num |
| 219 | // operands ensures we have the correct offset to the operand. |
| Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 220 | Op<0>().set(nullptr); |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame^] | 221 | setGlobalVariableNumOperands(0); |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 222 | } |
| 223 | } else { |
| 224 | assert(InitVal->getType() == getType()->getElementType() && |
| 225 | "Initializer type must match GlobalVariable type"); |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame^] | 226 | // Note, the num operands is used to compute the offset of the operand, so |
| 227 | // the order here matters. We need to set num operands to 1 first so that |
| 228 | // we get the correct offset to the first operand when we set it. |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 229 | if (!hasInitializer()) |
| Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame^] | 230 | setGlobalVariableNumOperands(1); |
| Jeffrey Yasskin | 7c57c41 | 2009-11-17 00:43:13 +0000 | [diff] [blame] | 231 | Op<0>().set(InitVal); |
| 232 | } |
| 233 | } |
| 234 | |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 235 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 236 | /// create a GlobalVariable) from the GlobalVariable Src to this one. |
| 237 | void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) { |
| 238 | assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!"); |
| Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 239 | GlobalObject::copyAttributesFrom(Src); |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 240 | const GlobalVariable *SrcVar = cast<GlobalVariable>(Src); |
| Hans Wennborg | 4a4be11 | 2014-02-10 17:13:56 +0000 | [diff] [blame] | 241 | setThreadLocalMode(SrcVar->getThreadLocalMode()); |
| Rafael Espindola | 75b809c | 2014-11-10 18:41:59 +0000 | [diff] [blame] | 242 | setExternallyInitialized(SrcVar->isExternallyInitialized()); |
| Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 246 | //===----------------------------------------------------------------------===// |
| 247 | // GlobalAlias Implementation |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | |
| David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 250 | GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name, |
| 251 | Constant *Aliasee, Module *ParentModule) |
| 252 | : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { |
| Rafael Espindola | 4fe0094 | 2014-05-16 13:34:04 +0000 | [diff] [blame] | 253 | Op<0>() = Aliasee; |
| Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 254 | |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 255 | if (ParentModule) |
| 256 | ParentModule->getAliasList().push_back(this); |
| 257 | } |
| 258 | |
| David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 259 | GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link, |
| 260 | const Twine &Name, Constant *Aliasee, |
| 261 | Module *ParentModule) { |
| 262 | return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 263 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 264 | |
| David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 265 | GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage, |
| 266 | const Twine &Name, Module *Parent) { |
| 267 | return create(Ty, Linkage, Name, nullptr, Parent); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 268 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 269 | |
| David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 270 | GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage, |
| 271 | const Twine &Name, GlobalValue *Aliasee) { |
| 272 | return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent()); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 273 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 274 | |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 275 | GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name, |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 276 | GlobalValue *Aliasee) { |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 277 | PointerType *PTy = Aliasee->getType(); |
| David Blaikie | f64246b | 2015-04-29 21:22:39 +0000 | [diff] [blame] | 278 | return create(PTy, Link, Name, Aliasee); |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 281 | GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { |
| Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 282 | return create(Aliasee->getLinkage(), Name, Aliasee); |
| 283 | } |
| Rafael Espindola | 8370565 | 2014-05-17 19:57:46 +0000 | [diff] [blame] | 284 | |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 285 | void GlobalAlias::setParent(Module *parent) { |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 286 | Parent = parent; |
| Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void GlobalAlias::removeFromParent() { |
| 290 | getParent()->getAliasList().remove(this); |
| 291 | } |
| 292 | |
| 293 | void GlobalAlias::eraseFromParent() { |
| 294 | getParent()->getAliasList().erase(this); |
| 295 | } |
| 296 | |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 297 | void GlobalAlias::setAliasee(Constant *Aliasee) { |
| Patrik Hagglund | 3154171 | 2014-06-04 11:21:11 +0000 | [diff] [blame] | 298 | assert((!Aliasee || Aliasee->getType() == getType()) && |
| Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 299 | "Alias and aliasee types should match!"); |
| 300 | setOperand(0, Aliasee); |
| 301 | } |