LLVM API Documentation
00001 //===-- Globals.cpp - Implement the Global object classes -----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the GlobalValue & GlobalVariable classes for the VMCore 00011 // library. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/DerivedTypes.h" 00016 #include "llvm/GlobalVariable.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/SymbolTable.h" 00019 #include "llvm/Support/LeakDetector.h" 00020 using namespace llvm; 00021 00022 //===----------------------------------------------------------------------===// 00023 // GlobalValue Class 00024 //===----------------------------------------------------------------------===// 00025 00026 /// This could be named "SafeToDestroyGlobalValue". It just makes sure that 00027 /// there are no non-constant uses of this GlobalValue. If there aren't then 00028 /// this and the transitive closure of the constants can be deleted. See the 00029 /// destructor for details. 00030 static bool removeDeadConstantUsers(Constant* C) { 00031 if (isa<GlobalValue>(C)) return false; // Cannot remove this 00032 00033 while (!C->use_empty()) 00034 if (Constant *User = dyn_cast<Constant>(C->use_back())) { 00035 if (!removeDeadConstantUsers(User)) 00036 return false; // Constant wasn't dead 00037 } else { 00038 return false; // Non-constant usage; 00039 } 00040 00041 C->destroyConstant(); 00042 return true; 00043 } 00044 00045 /// removeDeadConstantUsers - If there are any dead constant users dangling 00046 /// off of this global value, remove them. This method is useful for clients 00047 /// that want to check to see if a global is unused, but don't want to deal 00048 /// with potentially dead constants hanging off of the globals. 00049 /// 00050 /// This function returns true if the global value is now dead. If all 00051 /// users of this global are not dead, this method may return false and 00052 /// leave some of them around. 00053 void GlobalValue::removeDeadConstantUsers() { 00054 while(!use_empty()) { 00055 if (Constant* User = dyn_cast<Constant>(use_back())) { 00056 if (!::removeDeadConstantUsers(User)) 00057 return; // Constant wasn't dead 00058 } else { 00059 return; // Non-constant usage; 00060 } 00061 } 00062 } 00063 00064 /// Override destroyConstant to make sure it doesn't get called on 00065 /// GlobalValue's because they shouldn't be treated like other constants. 00066 void GlobalValue::destroyConstant() { 00067 assert(0 && "You can't GV->destroyConstant()!"); 00068 abort(); 00069 } 00070 //===----------------------------------------------------------------------===// 00071 // GlobalVariable Implementation 00072 //===----------------------------------------------------------------------===// 00073 00074 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, 00075 Constant *Initializer, 00076 const std::string &Name, Module *ParentModule) 00077 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name), 00078 isConstantGlobal(constant) { 00079 if (Initializer) { 00080 assert(Initializer->getType() == Ty && 00081 "Initializer should be the same type as the GlobalVariable!"); 00082 Operands.push_back(Use((Value*)Initializer, this)); 00083 } 00084 00085 LeakDetector::addGarbageObject(this); 00086 00087 if (ParentModule) 00088 ParentModule->getGlobalList().push_back(this); 00089 } 00090 00091 void GlobalVariable::setParent(Module *parent) { 00092 if (getParent()) 00093 LeakDetector::addGarbageObject(this); 00094 Parent = parent; 00095 if (getParent()) 00096 LeakDetector::removeGarbageObject(this); 00097 } 00098 00099 // Specialize setName to take care of symbol table majik 00100 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) { 00101 Module *P; 00102 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) && 00103 "Invalid symtab argument!"); 00104 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this); 00105 Value::setName(name); 00106 if (P && hasName()) P->getSymbolTable().insert(this); 00107 } 00108 00109 void GlobalVariable::removeFromParent() { 00110 getParent()->getGlobalList().remove(this); 00111 } 00112 00113 void GlobalVariable::eraseFromParent() { 00114 getParent()->getGlobalList().erase(this); 00115 } 00116 00117 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, 00118 bool DisableChecking) { 00119 // If you call this, then you better know this GVar has a constant 00120 // initializer worth replacing. Enforce that here. 00121 assert(getNumOperands() == 1 && 00122 "Attempt to replace uses of Constants on a GVar with no initializer"); 00123 00124 // And, since you know it has an initializer, the From value better be 00125 // the initializer :) 00126 assert(getOperand(0) == From && 00127 "Attempt to replace wrong constant initializer in GVar"); 00128 00129 // And, you better have a constant for the replacement value 00130 assert(isa<Constant>(To) && 00131 "Attempt to replace GVar initializer with non-constant"); 00132 00133 // Okay, preconditions out of the way, replace the constant initializer. 00134 this->setOperand(0, cast<Constant>(To)); 00135 } 00136 00137 // vim: sw=2 ai 00138