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 | // |
| 10 | // This file implements the GlobalValue & GlobalVariable classes for the VMCore |
| 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 16 | #include "llvm/GlobalVariable.h" |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalAlias.h" |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/LeakDetector.h" |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // GlobalValue Class |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | 00f23ec | 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 | da6cdfa | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 31 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
| 32 | |
Chris Lattner | 00f23ec | 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 | da6cdfa | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 39 | |
| 40 | C->destroyConstant(); |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 41 | return true; |
| 42 | } |
Reid Spencer | e253cf6 | 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 | f8083b7 | 2004-07-19 00:55:35 +0000 | [diff] [blame] | 48 | void GlobalValue::removeDeadConstantUsers() { |
Chris Lattner | 00f23ec | 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 | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 68 | } else { |
Chris Lattner | 00f23ec | 2007-02-26 05:02:39 +0000 | [diff] [blame] | 69 | LastNonDeadUser = I; |
| 70 | ++I; |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 75 | /// Override destroyConstant to make sure it doesn't get called on |
Reid Spencer | e253cf6 | 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 | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 81 | |
Reid Spencer | e253cf6 | 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 | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 87 | Constant *InitVal, const std::string &Name, |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 88 | Module *ParentModule, bool ThreadLocal, |
| 89 | unsigned AddressSpace) |
| 90 | : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 91 | &Initializer, InitVal != 0, Link, Name), |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 92 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 93 | if (InitVal) { |
| 94 | assert(InitVal->getType() == Ty && |
Alkis Evlogimenos | 8243976 | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 95 | "Initializer should be the same type as the GlobalVariable!"); |
Chris Lattner | 96d83f6 | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 96 | Initializer.init(InitVal, this); |
| 97 | } else { |
| 98 | Initializer.init(0, this); |
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); |
| 102 | |
| 103 | if (ParentModule) |
| 104 | ParentModule->getGlobalList().push_back(this); |
| 105 | } |
| 106 | |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 107 | GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 108 | Constant *InitVal, const std::string &Name, |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 109 | GlobalVariable *Before, bool ThreadLocal, |
| 110 | unsigned AddressSpace) |
| 111 | : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 112 | &Initializer, InitVal != 0, Link, Name), |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 113 | isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { |
Chris Lattner | adc9546 | 2006-09-30 21:31:26 +0000 | [diff] [blame] | 114 | if (InitVal) { |
| 115 | assert(InitVal->getType() == Ty && |
| 116 | "Initializer should be the same type as the GlobalVariable!"); |
| 117 | Initializer.init(InitVal, this); |
| 118 | } else { |
| 119 | Initializer.init(0, this); |
| 120 | } |
| 121 | |
| 122 | LeakDetector::addGarbageObject(this); |
| 123 | |
| 124 | if (Before) |
| 125 | Before->getParent()->getGlobalList().insert(Before, this); |
| 126 | } |
| 127 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 128 | void GlobalVariable::setParent(Module *parent) { |
| 129 | if (getParent()) |
| 130 | LeakDetector::addGarbageObject(this); |
| 131 | Parent = parent; |
| 132 | if (getParent()) |
| 133 | LeakDetector::removeGarbageObject(this); |
| 134 | } |
| 135 | |
Chris Lattner | 4b83380 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 136 | void GlobalVariable::removeFromParent() { |
| 137 | getParent()->getGlobalList().remove(this); |
| 138 | } |
| 139 | |
| 140 | void GlobalVariable::eraseFromParent() { |
| 141 | getParent()->getGlobalList().erase(this); |
| 142 | } |
| 143 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 144 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | d0ff1ad | 2005-10-04 18:13:04 +0000 | [diff] [blame] | 145 | Use *U) { |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 146 | // If you call this, then you better know this GVar has a constant |
| 147 | // initializer worth replacing. Enforce that here. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 148 | assert(getNumOperands() == 1 && |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 149 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 150 | |
| 151 | // And, since you know it has an initializer, the From value better be |
| 152 | // the initializer :) |
| 153 | assert(getOperand(0) == From && |
| 154 | "Attempt to replace wrong constant initializer in GVar"); |
| 155 | |
| 156 | // And, you better have a constant for the replacement value |
| 157 | assert(isa<Constant>(To) && |
| 158 | "Attempt to replace GVar initializer with non-constant"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 159 | |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 160 | // Okay, preconditions out of the way, replace the constant initializer. |
Chris Lattner | 07d7c9d | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 161 | this->setOperand(0, cast<Constant>(To)); |
Reid Spencer | e253cf6 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 162 | } |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 163 | |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | // GlobalAlias Implementation |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | |
| 168 | GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link, |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 169 | const std::string &Name, Constant* aliasee, |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 170 | Module *ParentModule) |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 171 | : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) { |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 172 | LeakDetector::addGarbageObject(this); |
| 173 | |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 174 | if (aliasee) |
| 175 | assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); |
| 176 | Aliasee.init(aliasee, this); |
| 177 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 178 | if (ParentModule) |
| 179 | ParentModule->getAliasList().push_back(this); |
| 180 | } |
| 181 | |
| 182 | void GlobalAlias::setParent(Module *parent) { |
| 183 | if (getParent()) |
| 184 | LeakDetector::addGarbageObject(this); |
| 185 | Parent = parent; |
| 186 | if (getParent()) |
| 187 | LeakDetector::removeGarbageObject(this); |
| 188 | } |
| 189 | |
| 190 | void GlobalAlias::removeFromParent() { |
| 191 | getParent()->getAliasList().remove(this); |
| 192 | } |
| 193 | |
| 194 | void GlobalAlias::eraseFromParent() { |
| 195 | getParent()->getAliasList().erase(this); |
| 196 | } |
| 197 | |
| 198 | bool GlobalAlias::isDeclaration() const { |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 199 | const GlobalValue* AV = getAliasedGlobal(); |
| 200 | if (AV) |
| 201 | return AV->isDeclaration(); |
| 202 | else |
| 203 | return false; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 206 | void GlobalAlias::setAliasee(Constant *Aliasee) |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 207 | { |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 208 | if (Aliasee) |
| 209 | assert(Aliasee->getType() == getType() && |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 210 | "Alias and aliasee types should match!"); |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 211 | |
| 212 | setOperand(0, Aliasee); |
| 213 | } |
| 214 | |
Chris Lattner | 2bf6e6a | 2007-05-05 23:49:02 +0000 | [diff] [blame] | 215 | const GlobalValue *GlobalAlias::getAliasedGlobal() const { |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 216 | const Constant *C = getAliasee(); |
| 217 | if (C) { |
| 218 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 219 | return GV; |
| 220 | else { |
| 221 | const ConstantExpr *CE = 0; |
Anton Korobeynikov | bb1f97c | 2007-04-30 10:28:40 +0000 | [diff] [blame] | 222 | if ((CE = dyn_cast<ConstantExpr>(C)) && |
Chris Lattner | 2bf6e6a | 2007-05-05 23:49:02 +0000 | [diff] [blame] | 223 | (CE->getOpcode() == Instruction::BitCast || |
| 224 | CE->getOpcode() == Instruction::GetElementPtr)) |
| 225 | return dyn_cast<GlobalValue>(CE->getOperand(0)); |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 226 | else |
| 227 | assert(0 && "Unsupported aliasee"); |
| 228 | } |
Jeff Cohen | b4dbd9e | 2007-05-03 22:09:21 +0000 | [diff] [blame] | 229 | } |
| 230 | return 0; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 231 | } |
| 232 | |