LLVM API Documentation

Globals.cpp

Go to the documentation of this file.
00001 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
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/GlobalVariable.h"
00016 #include "llvm/DerivedTypes.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 *InitVal,
00076                                const std::string &Name, Module *ParentModule)
00077   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal,
00078                 &Initializer, InitVal != 0, Link, Name),
00079     isConstantGlobal(constant) {
00080   if (InitVal) {
00081     assert(InitVal->getType() == Ty &&
00082            "Initializer should be the same type as the GlobalVariable!");
00083     Initializer.init(InitVal, this);
00084   } else {
00085     Initializer.init(0, this);
00086   }
00087 
00088   LeakDetector::addGarbageObject(this);
00089 
00090   if (ParentModule)
00091     ParentModule->getGlobalList().push_back(this);
00092 }
00093 
00094 void GlobalVariable::setParent(Module *parent) {
00095   if (getParent())
00096     LeakDetector::addGarbageObject(this);
00097   Parent = parent;
00098   if (getParent())
00099     LeakDetector::removeGarbageObject(this);
00100 }
00101 
00102 void GlobalVariable::removeFromParent() {
00103   getParent()->getGlobalList().remove(this);
00104 }
00105 
00106 void GlobalVariable::eraseFromParent() {
00107   getParent()->getGlobalList().erase(this);
00108 }
00109 
00110 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
00111                                                  Use *U) {
00112   // If you call this, then you better know this GVar has a constant
00113   // initializer worth replacing. Enforce that here.
00114   assert(getNumOperands() == 1 &&
00115          "Attempt to replace uses of Constants on a GVar with no initializer");
00116 
00117   // And, since you know it has an initializer, the From value better be
00118   // the initializer :)
00119   assert(getOperand(0) == From &&
00120          "Attempt to replace wrong constant initializer in GVar");
00121 
00122   // And, you better have a constant for the replacement value
00123   assert(isa<Constant>(To) &&
00124          "Attempt to replace GVar initializer with non-constant");
00125 
00126   // Okay, preconditions out of the way, replace the constant initializer.
00127   this->setOperand(0, cast<Constant>(To));
00128 }