Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 1 | //===-- Globals.cpp - Implement the Global object classes -----------------===// |
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 | |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/GlobalVariable.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/SymbolTable.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 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 26 | /// This could be named "SafeToDestroyGlobalValue". It just makes sure that |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 27 | /// there are no non-constant uses of this GlobalValue. If there aren't then |
| 28 | /// this and the transitive closure of the constants can be deleted. See the |
| 29 | /// destructor for details. |
Chris Lattner | 0145eb7 | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 30 | static bool removeDeadConstantUsers(Constant* C) { |
| 31 | if (isa<GlobalValue>(C)) return false; // Cannot remove this |
| 32 | |
| 33 | while (!C->use_empty()) |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 34 | if (Constant *User = dyn_cast<Constant>(C->use_back())) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 35 | if (!removeDeadConstantUsers(User)) |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 36 | return false; // Constant wasn't dead |
| 37 | } else { |
| 38 | return false; // Non-constant usage; |
| 39 | } |
Chris Lattner | 0145eb7 | 2004-07-18 08:12:57 +0000 | [diff] [blame] | 40 | |
| 41 | C->destroyConstant(); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 42 | return true; |
| 43 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 44 | |
| 45 | /// removeDeadConstantUsers - If there are any dead constant users dangling |
| 46 | /// off of this global value, remove them. This method is useful for clients |
| 47 | /// that want to check to see if a global is unused, but don't want to deal |
| 48 | /// with potentially dead constants hanging off of the globals. |
| 49 | /// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 50 | /// This function returns true if the global value is now dead. If all |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 51 | /// users of this global are not dead, this method may return false and |
| 52 | /// leave some of them around. |
Chris Lattner | 2730e6a | 2004-07-19 00:55:35 +0000 | [diff] [blame] | 53 | void GlobalValue::removeDeadConstantUsers() { |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 54 | while(!use_empty()) { |
| 55 | if (Constant* User = dyn_cast<Constant>(use_back())) { |
| 56 | if (!::removeDeadConstantUsers(User)) |
Chris Lattner | 2730e6a | 2004-07-19 00:55:35 +0000 | [diff] [blame] | 57 | return; // Constant wasn't dead |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 58 | } else { |
Chris Lattner | 2730e6a | 2004-07-19 00:55:35 +0000 | [diff] [blame] | 59 | return; // Non-constant usage; |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 64 | /// Override destroyConstant to make sure it doesn't get called on |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 65 | /// GlobalValue's because they shouldn't be treated like other constants. |
| 66 | void GlobalValue::destroyConstant() { |
| 67 | assert(0 && "You can't GV->destroyConstant()!"); |
| 68 | abort(); |
| 69 | } |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | // GlobalVariable Implementation |
| 72 | //===----------------------------------------------------------------------===// |
| 73 | |
| 74 | GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 75 | Constant *InitVal, |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 76 | const std::string &Name, Module *ParentModule) |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 77 | : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, |
| 78 | &Initializer, InitVal != 0, Link, Name), |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 79 | isConstantGlobal(constant) { |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 80 | if (InitVal) { |
| 81 | assert(InitVal->getType() == Ty && |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 82 | "Initializer should be the same type as the GlobalVariable!"); |
Chris Lattner | 5d1bc2c | 2005-01-29 00:35:33 +0000 | [diff] [blame] | 83 | Initializer.init(InitVal, this); |
| 84 | } else { |
| 85 | Initializer.init(0, this); |
Alkis Evlogimenos | f45cc7a | 2004-08-05 11:28:34 +0000 | [diff] [blame] | 86 | } |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 87 | |
| 88 | LeakDetector::addGarbageObject(this); |
| 89 | |
| 90 | if (ParentModule) |
| 91 | ParentModule->getGlobalList().push_back(this); |
| 92 | } |
| 93 | |
| 94 | void GlobalVariable::setParent(Module *parent) { |
| 95 | if (getParent()) |
| 96 | LeakDetector::addGarbageObject(this); |
| 97 | Parent = parent; |
| 98 | if (getParent()) |
| 99 | LeakDetector::removeGarbageObject(this); |
| 100 | } |
| 101 | |
Chris Lattner | 02a71e7 | 2004-10-11 22:21:39 +0000 | [diff] [blame] | 102 | void GlobalVariable::removeFromParent() { |
| 103 | getParent()->getGlobalList().remove(this); |
| 104 | } |
| 105 | |
| 106 | void GlobalVariable::eraseFromParent() { |
| 107 | getParent()->getGlobalList().erase(this); |
| 108 | } |
| 109 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 110 | void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, |
Chris Lattner | 7a1450d | 2005-10-04 18:13:04 +0000 | [diff] [blame^] | 111 | Use *U) { |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 112 | // If you call this, then you better know this GVar has a constant |
| 113 | // initializer worth replacing. Enforce that here. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 114 | assert(getNumOperands() == 1 && |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 115 | "Attempt to replace uses of Constants on a GVar with no initializer"); |
| 116 | |
| 117 | // And, since you know it has an initializer, the From value better be |
| 118 | // the initializer :) |
| 119 | assert(getOperand(0) == From && |
| 120 | "Attempt to replace wrong constant initializer in GVar"); |
| 121 | |
| 122 | // And, you better have a constant for the replacement value |
| 123 | assert(isa<Constant>(To) && |
| 124 | "Attempt to replace GVar initializer with non-constant"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 125 | |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 126 | // Okay, preconditions out of the way, replace the constant initializer. |
Chris Lattner | 37b570a | 2004-08-04 02:27:17 +0000 | [diff] [blame] | 127 | this->setOperand(0, cast<Constant>(To)); |
Reid Spencer | 3d169b1 | 2004-07-18 00:06:26 +0000 | [diff] [blame] | 128 | } |