1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the GlobalValue & GlobalVariable classes for the VMCore
13 //===----------------------------------------------------------------------===//
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/LeakDetector.h"
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 /// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
26 /// it. This involves recursively eliminating any dead users of the
28 static bool removeDeadUsersOfConstant(Constant *C) {
29 if (isa<GlobalValue>(C)) return false; // Cannot remove this
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
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.
46 void GlobalValue::removeDeadConstantUsers() {
47 Value::use_iterator I = use_begin(), E = use_end();
48 Value::use_iterator LastNonDeadUser = 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.
57 // If the constant was dead, then the iterator is invalidated.
58 if (LastNonDeadUser == E) {
73 /// Override destroyConstant to make sure it doesn't get called on
74 /// GlobalValue's because they shouldn't be treated like other constants.
75 void GlobalValue::destroyConstant() {
76 assert(0 && "You can't GV->destroyConstant()!");
79 //===----------------------------------------------------------------------===//
80 // GlobalVariable Implementation
81 //===----------------------------------------------------------------------===//
83 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
85 const std::string &Name, Module *ParentModule)
86 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
87 &Initializer, InitVal != 0, Link, Name),
88 isConstantGlobal(constant) {
90 assert(InitVal->getType() == Ty &&
91 "Initializer should be the same type as the GlobalVariable!");
92 Initializer.init(InitVal, this);
94 Initializer.init(0, this);
97 LeakDetector::addGarbageObject(this);
100 ParentModule->getGlobalList().push_back(this);
103 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
105 const std::string &Name, GlobalVariable *Before)
106 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
107 &Initializer, InitVal != 0, Link, Name),
108 isConstantGlobal(constant) {
110 assert(InitVal->getType() == Ty &&
111 "Initializer should be the same type as the GlobalVariable!");
112 Initializer.init(InitVal, this);
114 Initializer.init(0, this);
117 LeakDetector::addGarbageObject(this);
120 Before->getParent()->getGlobalList().insert(Before, this);
124 void GlobalVariable::setParent(Module *parent) {
126 LeakDetector::addGarbageObject(this);
129 LeakDetector::removeGarbageObject(this);
132 void GlobalVariable::removeFromParent() {
133 getParent()->getGlobalList().remove(this);
136 void GlobalVariable::eraseFromParent() {
137 getParent()->getGlobalList().erase(this);
140 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
142 // If you call this, then you better know this GVar has a constant
143 // initializer worth replacing. Enforce that here.
144 assert(getNumOperands() == 1 &&
145 "Attempt to replace uses of Constants on a GVar with no initializer");
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");
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");
156 // Okay, preconditions out of the way, replace the constant initializer.
157 this->setOperand(0, cast<Constant>(To));