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