LLVM API Documentation

GlobalDCE.cpp

Go to the documentation of this file.
00001 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This transform is designed to eliminate unreachable internal globals from the
00011 // program.  It uses an aggressive algorithm, searching out globals that are
00012 // known to be alive.  After it finds all of the globals which are needed, it
00013 // deletes whatever is left over.  This allows it to delete recursive chunks of
00014 // the program which are unreachable.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #define DEBUG_TYPE "globaldce"
00019 #include "llvm/Transforms/IPO.h"
00020 #include "llvm/Constants.h"
00021 #include "llvm/Module.h"
00022 #include "llvm/Pass.h"
00023 #include "llvm/ADT/Statistic.h"
00024 #include "llvm/Support/Compiler.h"
00025 #include <set>
00026 using namespace llvm;
00027 
00028 STATISTIC(NumAliases  , "Number of global aliases removed");
00029 STATISTIC(NumFunctions, "Number of functions removed");
00030 STATISTIC(NumVariables, "Number of global variables removed");
00031 
00032 namespace {
00033   struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
00034     static char ID; // Pass identification, replacement for typeid
00035     GlobalDCE() : ModulePass(&ID) {}
00036 
00037     // run - Do the GlobalDCE pass on the specified module, optionally updating
00038     // the specified callgraph to reflect the changes.
00039     //
00040     bool runOnModule(Module &M);
00041 
00042   private:
00043     std::set<GlobalValue*> AliveGlobals;
00044 
00045     /// GlobalIsNeeded - mark the specific global value as needed, and
00046     /// recursively mark anything that it uses as also needed.
00047     void GlobalIsNeeded(GlobalValue *GV);
00048     void MarkUsedGlobalsAsNeeded(Constant *C);
00049 
00050     bool RemoveUnusedGlobalValue(GlobalValue &GV);
00051   };
00052 }
00053 
00054 char GlobalDCE::ID = 0;
00055 static RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
00056 
00057 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
00058 
00059 bool GlobalDCE::runOnModule(Module &M) {
00060   bool Changed = false;
00061   
00062   // Loop over the module, adding globals which are obviously necessary.
00063   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
00064     Changed |= RemoveUnusedGlobalValue(*I);
00065     // Functions with external linkage are needed if they have a body
00066     if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
00067         !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
00068       GlobalIsNeeded(I);
00069   }
00070 
00071   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00072        I != E; ++I) {
00073     Changed |= RemoveUnusedGlobalValue(*I);
00074     // Externally visible & appending globals are needed, if they have an
00075     // initializer.
00076     if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
00077         !I->isDeclaration() && !I->hasAvailableExternallyLinkage())
00078       GlobalIsNeeded(I);
00079   }
00080 
00081   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
00082        I != E; ++I) {
00083     Changed |= RemoveUnusedGlobalValue(*I);
00084     // Externally visible aliases are needed.
00085     if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
00086       GlobalIsNeeded(I);
00087   }
00088 
00089   // Now that all globals which are needed are in the AliveGlobals set, we loop
00090   // through the program, deleting those which are not alive.
00091   //
00092 
00093   // The first pass is to drop initializers of global variables which are dead.
00094   std::vector<GlobalVariable*> DeadGlobalVars;   // Keep track of dead globals
00095   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
00096     if (!AliveGlobals.count(I)) {
00097       DeadGlobalVars.push_back(I);         // Keep track of dead globals
00098       I->setInitializer(0);
00099     }
00100 
00101   // The second pass drops the bodies of functions which are dead...
00102   std::vector<Function*> DeadFunctions;
00103   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00104     if (!AliveGlobals.count(I)) {
00105       DeadFunctions.push_back(I);         // Keep track of dead globals
00106       if (!I->isDeclaration())
00107         I->deleteBody();
00108     }
00109 
00110   // The third pass drops targets of aliases which are dead...
00111   std::vector<GlobalAlias*> DeadAliases;
00112   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
00113        ++I)
00114     if (!AliveGlobals.count(I)) {
00115       DeadAliases.push_back(I);
00116       I->setAliasee(0);
00117     }
00118 
00119   if (!DeadFunctions.empty()) {
00120     // Now that all interferences have been dropped, delete the actual objects
00121     // themselves.
00122     for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
00123       RemoveUnusedGlobalValue(*DeadFunctions[i]);
00124       M.getFunctionList().erase(DeadFunctions[i]);
00125     }
00126     NumFunctions += DeadFunctions.size();
00127     Changed = true;
00128   }
00129 
00130   if (!DeadGlobalVars.empty()) {
00131     for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
00132       RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
00133       M.getGlobalList().erase(DeadGlobalVars[i]);
00134     }
00135     NumVariables += DeadGlobalVars.size();
00136     Changed = true;
00137   }
00138 
00139   // Now delete any dead aliases.
00140   if (!DeadAliases.empty()) {
00141     for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
00142       RemoveUnusedGlobalValue(*DeadAliases[i]);
00143       M.getAliasList().erase(DeadAliases[i]);
00144     }
00145     NumAliases += DeadAliases.size();
00146     Changed = true;
00147   }
00148 
00149   // Make sure that all memory is released
00150   AliveGlobals.clear();
00151 
00152   // Remove dead metadata.
00153   Changed |= M.getContext().RemoveDeadMetadata();
00154   return Changed;
00155 }
00156 
00157 /// GlobalIsNeeded - the specific global value as needed, and
00158 /// recursively mark anything that it uses as also needed.
00159 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
00160   std::set<GlobalValue*>::iterator I = AliveGlobals.find(G);
00161 
00162   // If the global is already in the set, no need to reprocess it.
00163   if (I != AliveGlobals.end()) return;
00164 
00165   // Otherwise insert it now, so we do not infinitely recurse
00166   AliveGlobals.insert(I, G);
00167 
00168   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
00169     // If this is a global variable, we must make sure to add any global values
00170     // referenced by the initializer to the alive set.
00171     if (GV->hasInitializer())
00172       MarkUsedGlobalsAsNeeded(GV->getInitializer());
00173   } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
00174     // The target of a global alias is needed.
00175     MarkUsedGlobalsAsNeeded(GA->getAliasee());
00176   } else {
00177     // Otherwise this must be a function object.  We have to scan the body of
00178     // the function looking for constants and global values which are used as
00179     // operands.  Any operands of these types must be processed to ensure that
00180     // any globals used will be marked as needed.
00181     Function *F = cast<Function>(G);
00182     // For all basic blocks...
00183     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00184       // For all instructions...
00185       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
00186         // For all operands...
00187         for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
00188           if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
00189             GlobalIsNeeded(GV);
00190           else if (Constant *C = dyn_cast<Constant>(*U))
00191             MarkUsedGlobalsAsNeeded(C);
00192   }
00193 }
00194 
00195 void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
00196   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
00197     GlobalIsNeeded(GV);
00198   else {
00199     // Loop over all of the operands of the constant, adding any globals they
00200     // use to the list of needed globals.
00201     for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I)
00202       MarkUsedGlobalsAsNeeded(cast<Constant>(*I));
00203   }
00204 }
00205 
00206 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
00207 // GlobalValue, looking for the constant pointer ref that may be pointing to it.
00208 // If found, check to see if the constant pointer ref is safe to destroy, and if
00209 // so, nuke it.  This will reduce the reference count on the global value, which
00210 // might make it deader.
00211 //
00212 bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
00213   if (GV.use_empty()) return false;
00214   GV.removeDeadConstantUsers();
00215   return GV.use_empty();
00216 }