For PR411:
[oota-llvm.git] / lib / VMCore / Globals.cpp
1 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
2 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the GlobalValue & GlobalVariable classes for the VMCore
11 // library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/LeakDetector.h"
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 //                            GlobalValue Class
23 //===----------------------------------------------------------------------===//
24
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
31
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
36     } else {
37       return false; // Non-constant usage;
38     }
39
40   C->destroyConstant();
41   return true;
42 }
43
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.
48 ///
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() {
53   while(!use_empty()) {
54     if (Constant* User = dyn_cast<Constant>(use_back())) {
55       if (!::removeDeadConstantUsers(User))
56         return; // Constant wasn't dead
57     } else {
58       return; // Non-constant usage;
59     }
60   }
61 }
62
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()!");
67   abort();
68 }
69 //===----------------------------------------------------------------------===//
70 // GlobalVariable Implementation
71 //===----------------------------------------------------------------------===//
72
73 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
74                                Constant *InitVal,
75                                const std::string &Name, Module *ParentModule)
76   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
77                 &Initializer, InitVal != 0, Link, Name),
78     isConstantGlobal(constant) {
79   if (InitVal) {
80     assert(InitVal->getType() == Ty &&
81            "Initializer should be the same type as the GlobalVariable!");
82     Initializer.init(InitVal, this);
83   } else {
84     Initializer.init(0, this);
85   }
86
87   LeakDetector::addGarbageObject(this);
88
89   if (ParentModule)
90     ParentModule->getGlobalList().push_back(this);
91 }
92
93 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
94                                Constant *InitVal,
95                                const std::string &Name, GlobalVariable *Before)
96   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
97                 &Initializer, InitVal != 0, Link, Name), 
98     isConstantGlobal(constant) {
99   if (InitVal) {
100     assert(InitVal->getType() == Ty &&
101            "Initializer should be the same type as the GlobalVariable!");
102     Initializer.init(InitVal, this);
103   } else {
104     Initializer.init(0, this);
105   }
106   
107   LeakDetector::addGarbageObject(this);
108   
109   if (Before)
110     Before->getParent()->getGlobalList().insert(Before, this);
111 }
112
113
114 void GlobalVariable::setParent(Module *parent) {
115   if (getParent())
116     LeakDetector::addGarbageObject(this);
117   Parent = parent;
118   if (getParent())
119     LeakDetector::removeGarbageObject(this);
120 }
121
122 void GlobalVariable::removeFromParent() {
123   getParent()->getGlobalList().remove(this);
124 }
125
126 void GlobalVariable::eraseFromParent() {
127   getParent()->getGlobalList().erase(this);
128 }
129
130 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
131                                                  Use *U) {
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");
136
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");
141
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");
145
146   // Okay, preconditions out of the way, replace the constant initializer.
147   this->setOperand(0, cast<Constant>(To));
148 }