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"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 /// This could be named "SafeToDestroyGlobalValue". It just makes sure that
28 /// there are no non-constant uses of this GlobalValue. If there aren't then
29 /// this and the transitive closure of the constants can be deleted. See the
30 /// destructor for details.
32 bool removeDeadConstantUsers(Constant* C) {
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;
41 if (!isa<GlobalValue>(C))
47 /// removeDeadConstantUsers - If there are any dead constant users dangling
48 /// off of this global value, remove them. This method is useful for clients
49 /// that want to check to see if a global is unused, but don't want to deal
50 /// with potentially dead constants hanging off of the globals.
52 /// This function returns true if the global value is now dead. If all
53 /// users of this global are not dead, this method may return false and
54 /// leave some of them around.
55 bool GlobalValue::removeDeadConstantUsers() {
57 if (Constant* User = dyn_cast<Constant>(use_back())) {
58 if (!::removeDeadConstantUsers(User))
59 return false; // Constant wasn't dead
61 return false; // Non-constant usage;
67 /// This virtual destructor is responsible for deleting any transitively dead
68 /// Constants that are using the GlobalValue.
69 GlobalValue::~GlobalValue() {
70 // Its an error to attempt destruction with non-constant uses remaining.
71 bool okay_to_destruct = removeDeadConstantUsers();
72 assert(okay_to_destruct &&
73 "Can't destroy GlobalValue with non-constant uses.");
76 /// Override destroyConstant to make sure it doesn't get called on
77 /// GlobalValue's because they shouldn't be treated like other constants.
78 void GlobalValue::destroyConstant() {
79 assert(0 && "You can't GV->destroyConstant()!");
82 //===----------------------------------------------------------------------===//
83 // GlobalVariable Implementation
84 //===----------------------------------------------------------------------===//
86 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
87 Constant *Initializer,
88 const std::string &Name, Module *ParentModule)
89 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
90 isConstantGlobal(constant) {
91 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
93 LeakDetector::addGarbageObject(this);
96 ParentModule->getGlobalList().push_back(this);
99 void GlobalVariable::setParent(Module *parent) {
101 LeakDetector::addGarbageObject(this);
104 LeakDetector::removeGarbageObject(this);
107 // Specialize setName to take care of symbol table majik
108 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
110 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
111 "Invalid symtab argument!");
112 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
113 Value::setName(name);
114 if (P && hasName()) P->getSymbolTable().insert(this);
117 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
118 bool DisableChecking )
120 // If you call this, then you better know this GVar has a constant
121 // initializer worth replacing. Enforce that here.
122 assert(getNumOperands() == 1 &&
123 "Attempt to replace uses of Constants on a GVar with no initializer");
125 // And, since you know it has an initializer, the From value better be
126 // the initializer :)
127 assert(getOperand(0) == From &&
128 "Attempt to replace wrong constant initializer in GVar");
130 // And, you better have a constant for the replacement value
131 assert(isa<Constant>(To) &&
132 "Attempt to replace GVar initializer with non-constant");
134 // Okay, preconditions out of the way, replace the constant initializer.
135 this->setOperand(0,To);