LLVM  mainline
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 #include "llvm/Transforms/IPO.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/IR/Constants.h"
00022 #include "llvm/IR/Instructions.h"
00023 #include "llvm/IR/Module.h"
00024 #include "llvm/Transforms/Utils/CtorUtils.h"
00025 #include "llvm/Transforms/Utils/GlobalStatus.h"
00026 #include "llvm/Pass.h"
00027 #include <unordered_map>
00028 using namespace llvm;
00029 
00030 #define DEBUG_TYPE "globaldce"
00031 
00032 STATISTIC(NumAliases  , "Number of global aliases removed");
00033 STATISTIC(NumFunctions, "Number of functions removed");
00034 STATISTIC(NumVariables, "Number of global variables removed");
00035 
00036 namespace {
00037   struct GlobalDCE : public ModulePass {
00038     static char ID; // Pass identification, replacement for typeid
00039     GlobalDCE() : ModulePass(ID) {
00040       initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
00041     }
00042 
00043     // run - Do the GlobalDCE pass on the specified module, optionally updating
00044     // the specified callgraph to reflect the changes.
00045     //
00046     bool runOnModule(Module &M) override;
00047 
00048   private:
00049     SmallPtrSet<GlobalValue*, 32> AliveGlobals;
00050     SmallPtrSet<Constant *, 8> SeenConstants;
00051     std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
00052 
00053     /// GlobalIsNeeded - mark the specific global value as needed, and
00054     /// recursively mark anything that it uses as also needed.
00055     void GlobalIsNeeded(GlobalValue *GV);
00056     void MarkUsedGlobalsAsNeeded(Constant *C);
00057 
00058     bool RemoveUnusedGlobalValue(GlobalValue &GV);
00059   };
00060 }
00061 
00062 /// Returns true if F contains only a single "ret" instruction.
00063 static bool isEmptyFunction(Function *F) {
00064   BasicBlock &Entry = F->getEntryBlock();
00065   if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
00066     return false;
00067   ReturnInst &RI = cast<ReturnInst>(Entry.front());
00068   return RI.getReturnValue() == nullptr;
00069 }
00070 
00071 char GlobalDCE::ID = 0;
00072 INITIALIZE_PASS(GlobalDCE, "globaldce",
00073                 "Dead Global Elimination", false, false)
00074 
00075 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
00076 
00077 bool GlobalDCE::runOnModule(Module &M) {
00078   bool Changed = false;
00079 
00080   // Remove empty functions from the global ctors list.
00081   Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
00082 
00083   // Collect the set of members for each comdat.
00084   for (Function &F : M)
00085     if (Comdat *C = F.getComdat())
00086       ComdatMembers.insert(std::make_pair(C, &F));
00087   for (GlobalVariable &GV : M.globals())
00088     if (Comdat *C = GV.getComdat())
00089       ComdatMembers.insert(std::make_pair(C, &GV));
00090   for (GlobalAlias &GA : M.aliases())
00091     if (Comdat *C = GA.getComdat())
00092       ComdatMembers.insert(std::make_pair(C, &GA));
00093 
00094   // Loop over the module, adding globals which are obviously necessary.
00095   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
00096     Changed |= RemoveUnusedGlobalValue(*I);
00097     // Functions with external linkage are needed if they have a body
00098     if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
00099       if (!I->isDiscardableIfUnused())
00100         GlobalIsNeeded(I);
00101     }
00102   }
00103 
00104   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00105        I != E; ++I) {
00106     Changed |= RemoveUnusedGlobalValue(*I);
00107     // Externally visible & appending globals are needed, if they have an
00108     // initializer.
00109     if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) {
00110       if (!I->isDiscardableIfUnused())
00111         GlobalIsNeeded(I);
00112     }
00113   }
00114 
00115   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
00116        I != E; ++I) {
00117     Changed |= RemoveUnusedGlobalValue(*I);
00118     // Externally visible aliases are needed.
00119     if (!I->isDiscardableIfUnused()) {
00120       GlobalIsNeeded(I);
00121     }
00122   }
00123 
00124   // Now that all globals which are needed are in the AliveGlobals set, we loop
00125   // through the program, deleting those which are not alive.
00126   //
00127 
00128   // The first pass is to drop initializers of global variables which are dead.
00129   std::vector<GlobalVariable*> DeadGlobalVars;   // Keep track of dead globals
00130   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
00131        I != E; ++I)
00132     if (!AliveGlobals.count(I)) {
00133       DeadGlobalVars.push_back(I);         // Keep track of dead globals
00134       if (I->hasInitializer()) {
00135         Constant *Init = I->getInitializer();
00136         I->setInitializer(nullptr);
00137         if (isSafeToDestroyConstant(Init))
00138           Init->destroyConstant();
00139       }
00140     }
00141 
00142   // The second pass drops the bodies of functions which are dead...
00143   std::vector<Function*> DeadFunctions;
00144   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
00145     if (!AliveGlobals.count(I)) {
00146       DeadFunctions.push_back(I);         // Keep track of dead globals
00147       if (!I->isDeclaration())
00148         I->deleteBody();
00149     }
00150 
00151   // The third pass drops targets of aliases which are dead...
00152   std::vector<GlobalAlias*> DeadAliases;
00153   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
00154        ++I)
00155     if (!AliveGlobals.count(I)) {
00156       DeadAliases.push_back(I);
00157       I->setAliasee(nullptr);
00158     }
00159 
00160   if (!DeadFunctions.empty()) {
00161     // Now that all interferences have been dropped, delete the actual objects
00162     // themselves.
00163     for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
00164       RemoveUnusedGlobalValue(*DeadFunctions[i]);
00165       M.getFunctionList().erase(DeadFunctions[i]);
00166     }
00167     NumFunctions += DeadFunctions.size();
00168     Changed = true;
00169   }
00170 
00171   if (!DeadGlobalVars.empty()) {
00172     for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
00173       RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
00174       M.getGlobalList().erase(DeadGlobalVars[i]);
00175     }
00176     NumVariables += DeadGlobalVars.size();
00177     Changed = true;
00178   }
00179 
00180   // Now delete any dead aliases.
00181   if (!DeadAliases.empty()) {
00182     for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
00183       RemoveUnusedGlobalValue(*DeadAliases[i]);
00184       M.getAliasList().erase(DeadAliases[i]);
00185     }
00186     NumAliases += DeadAliases.size();
00187     Changed = true;
00188   }
00189 
00190   // Make sure that all memory is released
00191   AliveGlobals.clear();
00192   SeenConstants.clear();
00193   ComdatMembers.clear();
00194 
00195   return Changed;
00196 }
00197 
00198 /// GlobalIsNeeded - the specific global value as needed, and
00199 /// recursively mark anything that it uses as also needed.
00200 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
00201   // If the global is already in the set, no need to reprocess it.
00202   if (!AliveGlobals.insert(G).second)
00203     return;
00204 
00205   if (Comdat *C = G->getComdat()) {
00206     for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
00207       GlobalIsNeeded(CM.second);
00208   }
00209 
00210   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
00211     // If this is a global variable, we must make sure to add any global values
00212     // referenced by the initializer to the alive set.
00213     if (GV->hasInitializer())
00214       MarkUsedGlobalsAsNeeded(GV->getInitializer());
00215   } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
00216     // The target of a global alias is needed.
00217     MarkUsedGlobalsAsNeeded(GA->getAliasee());
00218   } else {
00219     // Otherwise this must be a function object.  We have to scan the body of
00220     // the function looking for constants and global values which are used as
00221     // operands.  Any operands of these types must be processed to ensure that
00222     // any globals used will be marked as needed.
00223     Function *F = cast<Function>(G);
00224 
00225     if (F->hasPrefixData())
00226       MarkUsedGlobalsAsNeeded(F->getPrefixData());
00227 
00228     if (F->hasPrologueData())
00229       MarkUsedGlobalsAsNeeded(F->getPrologueData());
00230 
00231     if (F->hasPersonalityFn())
00232       MarkUsedGlobalsAsNeeded(F->getPersonalityFn());
00233 
00234     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00235       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
00236         for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U)
00237           if (GlobalValue *GV = dyn_cast<GlobalValue>(*U))
00238             GlobalIsNeeded(GV);
00239           else if (Constant *C = dyn_cast<Constant>(*U))
00240             MarkUsedGlobalsAsNeeded(C);
00241   }
00242 }
00243 
00244 void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
00245   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
00246     return GlobalIsNeeded(GV);
00247 
00248   // Loop over all of the operands of the constant, adding any globals they
00249   // use to the list of needed globals.
00250   for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
00251     // If we've already processed this constant there's no need to do it again.
00252     Constant *Op = dyn_cast<Constant>(*I);
00253     if (Op && SeenConstants.insert(Op).second)
00254       MarkUsedGlobalsAsNeeded(Op);
00255   }
00256 }
00257 
00258 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
00259 // GlobalValue, looking for the constant pointer ref that may be pointing to it.
00260 // If found, check to see if the constant pointer ref is safe to destroy, and if
00261 // so, nuke it.  This will reduce the reference count on the global value, which
00262 // might make it deader.
00263 //
00264 bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
00265   if (GV.use_empty()) return false;
00266   GV.removeDeadConstantUsers();
00267   return GV.use_empty();
00268 }