1 //===-- Globals.cpp - Implement the Global object classes -----------------===//
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/DerivedTypes.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/Module.h"
18 #include "llvm/SymbolTable.h"
19 #include "Support/LeakDetector.h"
22 //===----------------------------------------------------------------------===//
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
38 return false; // Non-constant usage;
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.
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 bool GlobalValue::removeDeadConstantUsers() {
55 if (Constant* User = dyn_cast<Constant>(use_back())) {
56 if (!::removeDeadConstantUsers(User))
57 return false; // Constant wasn't dead
59 return false; // Non-constant usage;
65 /// This virtual destructor is responsible for deleting any transitively dead
66 /// Constants that are using the GlobalValue.
67 GlobalValue::~GlobalValue() {
68 // Its an error to attempt destruction with non-constant uses remaining.
69 bool okay_to_destruct = removeDeadConstantUsers();
70 assert(okay_to_destruct &&
71 "Can't destroy GlobalValue with non-constant uses.");
74 /// Override destroyConstant to make sure it doesn't get called on
75 /// GlobalValue's because they shouldn't be treated like other constants.
76 void GlobalValue::destroyConstant() {
77 assert(0 && "You can't GV->destroyConstant()!");
80 //===----------------------------------------------------------------------===//
81 // GlobalVariable Implementation
82 //===----------------------------------------------------------------------===//
84 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
85 Constant *Initializer,
86 const std::string &Name, Module *ParentModule)
87 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
88 isConstantGlobal(constant) {
89 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
91 LeakDetector::addGarbageObject(this);
94 ParentModule->getGlobalList().push_back(this);
97 void GlobalVariable::setParent(Module *parent) {
99 LeakDetector::addGarbageObject(this);
102 LeakDetector::removeGarbageObject(this);
105 // Specialize setName to take care of symbol table majik
106 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
108 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
109 "Invalid symtab argument!");
110 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
111 Value::setName(name);
112 if (P && hasName()) P->getSymbolTable().insert(this);
115 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
116 bool DisableChecking )
118 // If you call this, then you better know this GVar has a constant
119 // initializer worth replacing. Enforce that here.
120 assert(getNumOperands() == 1 &&
121 "Attempt to replace uses of Constants on a GVar with no initializer");
123 // And, since you know it has an initializer, the From value better be
124 // the initializer :)
125 assert(getOperand(0) == From &&
126 "Attempt to replace wrong constant initializer in GVar");
128 // And, you better have a constant for the replacement value
129 assert(isa<Constant>(To) &&
130 "Attempt to replace GVar initializer with non-constant");
132 // Okay, preconditions out of the way, replace the constant initializer.
133 this->setOperand(0,To);