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 /// This could be named "SafeToDestroyGlobalValue". It just makes sure that
26 /// there are no non-constant uses of this GlobalValue. If there aren't then
27 /// this and the transitive closure of the constants can be deleted. See the
28 /// destructor for details.
29 static bool removeDeadConstantUsers(Constant* C) {
30 if (isa<GlobalValue>(C)) return false; // Cannot remove this
32 while (!C->use_empty())
33 if (Constant *User = dyn_cast<Constant>(C->use_back())) {
34 if (!removeDeadConstantUsers(User))
35 return false; // Constant wasn't dead
37 return false; // Non-constant usage;
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.
49 /// This function returns true if the global value is now dead. If all
50 /// users of this global are not dead, this method may return false and
51 /// leave some of them around.
52 void GlobalValue::removeDeadConstantUsers() {
54 if (Constant* User = dyn_cast<Constant>(use_back())) {
55 if (!::removeDeadConstantUsers(User))
56 return; // Constant wasn't dead
58 return; // Non-constant usage;
63 /// Override destroyConstant to make sure it doesn't get called on
64 /// GlobalValue's because they shouldn't be treated like other constants.
65 void GlobalValue::destroyConstant() {
66 assert(0 && "You can't GV->destroyConstant()!");
69 //===----------------------------------------------------------------------===//
70 // GlobalVariable Implementation
71 //===----------------------------------------------------------------------===//
73 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
75 const std::string &Name, Module *ParentModule)
76 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
77 &Initializer, InitVal != 0, Link, Name),
78 isConstantGlobal(constant) {
80 assert(InitVal->getType() == Ty &&
81 "Initializer should be the same type as the GlobalVariable!");
82 Initializer.init(InitVal, this);
84 Initializer.init(0, this);
87 LeakDetector::addGarbageObject(this);
90 ParentModule->getGlobalList().push_back(this);
93 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
95 const std::string &Name, GlobalVariable *Before)
96 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
97 &Initializer, InitVal != 0, Link, Name),
98 isConstantGlobal(constant) {
100 assert(InitVal->getType() == Ty &&
101 "Initializer should be the same type as the GlobalVariable!");
102 Initializer.init(InitVal, this);
104 Initializer.init(0, this);
107 LeakDetector::addGarbageObject(this);
110 Before->getParent()->getGlobalList().insert(Before, this);
114 void GlobalVariable::setParent(Module *parent) {
116 LeakDetector::addGarbageObject(this);
119 LeakDetector::removeGarbageObject(this);
122 void GlobalVariable::removeFromParent() {
123 getParent()->getGlobalList().remove(this);
126 void GlobalVariable::eraseFromParent() {
127 getParent()->getGlobalList().erase(this);
130 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
132 // If you call this, then you better know this GVar has a constant
133 // initializer worth replacing. Enforce that here.
134 assert(getNumOperands() == 1 &&
135 "Attempt to replace uses of Constants on a GVar with no initializer");
137 // And, since you know it has an initializer, the From value better be
138 // the initializer :)
139 assert(getOperand(0) == From &&
140 "Attempt to replace wrong constant initializer in GVar");
142 // And, you better have a constant for the replacement value
143 assert(isa<Constant>(To) &&
144 "Attempt to replace GVar initializer with non-constant");
146 // Okay, preconditions out of the way, replace the constant initializer.
147 this->setOperand(0, cast<Constant>(To));