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