Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / VMCore / Globals.cpp
blob3ea8265ebad9c8ec2e27fe13d11ecc7309927519
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/Support/LeakDetector.h"
20 using namespace llvm;
22 //===----------------------------------------------------------------------===//
23 // GlobalValue Class
24 //===----------------------------------------------------------------------===//
26 /// This could be named "SafeToDestroyGlobalValue". It just makes sure that
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.
30 static bool removeDeadConstantUsers(Constant* C) {
31 if (isa<GlobalValue>(C)) return false; // Cannot remove this
33 while (!C->use_empty())
34 if (Constant *User = dyn_cast<Constant>(C->use_back())) {
35 if (!removeDeadConstantUsers(User))
36 return false; // Constant wasn't dead
37 } else {
38 return false; // Non-constant usage;
41 C->destroyConstant();
42 return true;
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 ///
50 /// This function returns true if the global value is now dead. If all
51 /// users of this global are not dead, this method may return false and
52 /// leave some of them around.
53 void GlobalValue::removeDeadConstantUsers() {
54 while(!use_empty()) {
55 if (Constant* User = dyn_cast<Constant>(use_back())) {
56 if (!::removeDeadConstantUsers(User))
57 return; // Constant wasn't dead
58 } else {
59 return; // Non-constant usage;
64 /// Override destroyConstant to make sure it doesn't get called on
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();
70 //===----------------------------------------------------------------------===//
71 // GlobalVariable Implementation
72 //===----------------------------------------------------------------------===//
74 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
75 Constant *InitVal,
76 const std::string &Name, Module *ParentModule)
77 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
78 &Initializer, InitVal != 0, Link, Name),
79 isConstantGlobal(constant) {
80 if (InitVal) {
81 assert(InitVal->getType() == Ty &&
82 "Initializer should be the same type as the GlobalVariable!");
83 Initializer.init(InitVal, this);
84 } else {
85 Initializer.init(0, this);
88 LeakDetector::addGarbageObject(this);
90 if (ParentModule)
91 ParentModule->getGlobalList().push_back(this);
94 void GlobalVariable::setParent(Module *parent) {
95 if (getParent())
96 LeakDetector::addGarbageObject(this);
97 Parent = parent;
98 if (getParent())
99 LeakDetector::removeGarbageObject(this);
102 void GlobalVariable::removeFromParent() {
103 getParent()->getGlobalList().remove(this);
106 void GlobalVariable::eraseFromParent() {
107 getParent()->getGlobalList().erase(this);
110 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
111 Use *U) {
112 // If you call this, then you better know this GVar has a constant
113 // initializer worth replacing. Enforce that here.
114 assert(getNumOperands() == 1 &&
115 "Attempt to replace uses of Constants on a GVar with no initializer");
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");
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");
126 // Okay, preconditions out of the way, replace the constant initializer.
127 this->setOperand(0, cast<Constant>(To));