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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/LeakDetector.h" |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // GlobalValue Class |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | dd89d9c | 2007-02-26 05:02:39 +0000 | [diff] [blame] | 27 | /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove |
| 28 | /// it. This involves recursively eliminating any dead users of the |
| 29 | /// constantexpr. |
| 30 | static bool removeDeadUsersOfConstant(Constant *C) { |
Chris Lattner | 0145eb7 | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 31 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
| 32 | |
Chris Lattner | dd89d9c | 2007-02-26 05:02:39 +0000 | [diff] [blame] | 33 | while (!C->use_empty()) { |
| 34 | Constant *User = dyn_cast<Constant>(C->use_back()); |
| 35 | if (!User) return false; // Non-constant usage; |
| 36 | if (!removeDeadUsersOfConstant(User)) |
| 37 | return false; // Constant wasn't dead |
| 38 | } |
Chris Lattner | 0145eb7 | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 39 | |
| 40 | C->destroyConstant(); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 41 | return true; |
| 42 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 43 | |
| 44 | /// removeDeadConstantUsers - If there are any dead constant users dangling |
| 45 | /// off of this global value, remove them. This method is useful for clients |
| 46 | /// that want to check to see if a global is unused, but don't want to deal |
| 47 | /// with potentially dead constants hanging off of the globals. |
Chris Lattner | 2730e6a | 2004-07-19 00:55:35 +0000 | [diff] [blame] | 48 | void GlobalValue::removeDeadConstantUsers() { |
Chris Lattner | dd89d9c | 2007-02-26 05:02:39 +0000 | [diff] [blame] | 49 | Value::use_iterator I = use_begin(), E = use_end(); |
| 50 | Value::use_iterator LastNonDeadUser = E; |
| 51 | while (I != E) { |
| 52 | if (Constant *User = dyn_cast<Constant>(*I)) { |
| 53 | if (!removeDeadUsersOfConstant(User)) { |
| 54 | // If the constant wasn't dead, remember that this was the last live use |
| 55 | // and move on to the next constant. |
| 56 | LastNonDeadUser = I; |
| 57 | ++I; |
| 58 | } else { |
| 59 | // If the constant was dead, then the iterator is invalidated. |
| 60 | if (LastNonDeadUser == E) { |
| 61 | I = use_begin(); |
| 62 | if (I == E) break; |
| 63 | } else { |
| 64 | I = LastNonDeadUser; |
| 65 | ++I; |
| 66 | } |
| 67 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 68 | } else { |
Chris Lattner | dd89d9c | 2007-02-26 05:02:39 +0000 | [diff] [blame] | 69 | LastNonDeadUser = I; |
| 70 | ++I; |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 75 | /// Override destroyConstant to make sure it doesn't get called on |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 76 | /// GlobalValue's because they shouldn't be treated like other constants. |
| 77 | void GlobalValue::destroyConstant() { |
| 78 | assert(0 && "You can't GV->destroyConstant()!"); |
| 79 | abort(); |
| 80 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 81 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | // GlobalVariable Implementation |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 87 | Constant *InitVal, const std::string &Name, |
| 88 | Module *ParentModule, bool ThreadLocal) |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 89 | : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, |
| 90 | &Initializer, InitVal != 0, Link, Name), |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 91 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 92 | if (InitVal) { |
| 93 | assert(InitVal->getType() == Ty && |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 94 | "Initializer should be the same type as the GlobalVariable!"); |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 95 | Initializer.init(InitVal, this); |
| 96 | } else { |
| 97 | Initializer.init(0, this); |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 98 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 99 | |
| 100 | LeakDetector::addGarbageObject(this); |
| 101 | |
| 102 | if (ParentModule) |
| 103 | ParentModule->getGlobalList().push_back(this); |
| 104 | } |
| 105 | |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 106 | GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 107 | Constant *InitVal, const std::string &Name, |
| 108 | GlobalVariable *Before, bool ThreadLocal) |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 109 | : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, |
| 110 | &Initializer, InitVal != 0, Link, Name), |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 111 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | 87732cf | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 112 | if (InitVal) { |
| 113 | assert(InitVal->getType() == Ty && |
| 114 | "Initializer should be the same type as the GlobalVariable!"); |
| 115 | Initializer.init(InitVal, this); |
| 116 | } else { |
| 117 | Initializer.init(0, this); |
| 118 | } |
| 119 | |
| 120 | LeakDetector::addGarbageObject(this); |
| 121 | |
| 122 | if (Before) |
| 123 | Before->getParent()->getGlobalList().insert(Before, this); |
| 124 | } |
| 125 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 126 | void GlobalVariable::setParent(Module *parent) { |
| 127 | if (getParent()) |
| 128 | LeakDetector::addGarbageObject(this); |
| 129 | Parent = parent; |
| 130 | if (getParent()) |
| 131 | LeakDetector::removeGarbageObject(this); |
| 132 | } |
| 133 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 134 | void GlobalVariable::removeFromParent() { |
| 135 | getParent()->getGlobalList().remove(this); |
| 136 | } |
| 137 | |
| 138 | void GlobalVariable::eraseFromParent() { |
| 139 | getParent()->getGlobalList().erase(this); |
| 140 | } |
| 141 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 142 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 143 | Use *U) { |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 144 | // If you call this, then you better know this GVar has a constant |
| 145 | // initializer worth replacing. Enforce that here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 146 | assert(getNumOperands() == 1 && |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 147 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 148 | |
| 149 | // And, since you know it has an initializer, the From value better be |
| 150 | // the initializer :) |
| 151 | assert(getOperand(0) == From && |
| 152 | "Attempt to replace wrong constant initializer in GVar"); |
| 153 | |
| 154 | // And, you better have a constant for the replacement value |
| 155 | assert(isa<Constant>(To) && |
| 156 | "Attempt to replace GVar initializer with non-constant"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 157 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 158 | // Okay, preconditions out of the way, replace the constant initializer. |
Chris Lattner | 37b570a | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 159 | this->setOperand(0, cast<Constant>(To)); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 160 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // GlobalAlias Implementation |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link, |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 167 | const std::string &Name, Constant* aliasee, |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 168 | Module *ParentModule) |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 169 | : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) { |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 170 | LeakDetector::addGarbageObject(this); |
| 171 | |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 172 | if (aliasee) |
| 173 | assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); |
| 174 | Aliasee.init(aliasee, this); |
| 175 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 176 | if (ParentModule) |
| 177 | ParentModule->getAliasList().push_back(this); |
| 178 | } |
| 179 | |
| 180 | void GlobalAlias::setParent(Module *parent) { |
| 181 | if (getParent()) |
| 182 | LeakDetector::addGarbageObject(this); |
| 183 | Parent = parent; |
| 184 | if (getParent()) |
| 185 | LeakDetector::removeGarbageObject(this); |
| 186 | } |
| 187 | |
| 188 | void GlobalAlias::removeFromParent() { |
| 189 | getParent()->getAliasList().remove(this); |
| 190 | } |
| 191 | |
| 192 | void GlobalAlias::eraseFromParent() { |
| 193 | getParent()->getAliasList().erase(this); |
| 194 | } |
| 195 | |
| 196 | bool GlobalAlias::isDeclaration() const { |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 197 | const GlobalValue* AV = getAliasedGlobal(); |
| 198 | if (AV) |
| 199 | return AV->isDeclaration(); |
| 200 | else |
| 201 | return false; |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 204 | void GlobalAlias::setAliasee(Constant *Aliasee) |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 205 | { |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 206 | if (Aliasee) |
| 207 | assert(Aliasee->getType() == getType() && |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 208 | "Alias and aliasee types should match!"); |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 209 | |
| 210 | setOperand(0, Aliasee); |
| 211 | } |
| 212 | |
| 213 | const GlobalValue *GlobalAlias::getAliasedGlobal() const { |
| 214 | const Constant *C = getAliasee(); |
| 215 | if (C) { |
| 216 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 217 | return GV; |
| 218 | else { |
| 219 | const ConstantExpr *CE = 0; |
Anton Korobeynikov | a30bc8f | 2007-04-30 10:28:40 +0000 | [diff] [blame] | 220 | if ((CE = dyn_cast<ConstantExpr>(C)) && |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 221 | (CE->getOpcode() == Instruction::BitCast)) |
| 222 | return cast<GlobalValue>(CE->getOperand(0)); |
| 223 | else |
| 224 | assert(0 && "Unsupported aliasee"); |
| 225 | } |
Jeff Cohen | ee7bf76 | 2007-05-03 22:09:21 +0000 | [diff] [blame^] | 226 | } |
| 227 | return 0; |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 228 | } |
| 229 | |