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