Home | History | Annotate | Download | only in IPO
      1 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This transform is designed to eliminate unreachable internal globals from the
     11 // program.  It uses an aggressive algorithm, searching out globals that are
     12 // known to be alive.  After it finds all of the globals which are needed, it
     13 // deletes whatever is left over.  This allows it to delete recursive chunks of
     14 // the program which are unreachable.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #include "llvm/Transforms/IPO.h"
     19 #include "llvm/ADT/SmallPtrSet.h"
     20 #include "llvm/ADT/Statistic.h"
     21 #include "llvm/IR/Constants.h"
     22 #include "llvm/IR/Instructions.h"
     23 #include "llvm/IR/Module.h"
     24 #include "llvm/Transforms/Utils/CtorUtils.h"
     25 #include "llvm/Transforms/Utils/GlobalStatus.h"
     26 #include "llvm/Pass.h"
     27 #include <unordered_map>
     28 using namespace llvm;
     29 
     30 #define DEBUG_TYPE "globaldce"
     31 
     32 STATISTIC(NumAliases  , "Number of global aliases removed");
     33 STATISTIC(NumFunctions, "Number of functions removed");
     34 STATISTIC(NumVariables, "Number of global variables removed");
     35 
     36 namespace {
     37   struct GlobalDCE : public ModulePass {
     38     static char ID; // Pass identification, replacement for typeid
     39     GlobalDCE() : ModulePass(ID) {
     40       initializeGlobalDCEPass(*PassRegistry::getPassRegistry());
     41     }
     42 
     43     // run - Do the GlobalDCE pass on the specified module, optionally updating
     44     // the specified callgraph to reflect the changes.
     45     //
     46     bool runOnModule(Module &M) override;
     47 
     48   private:
     49     SmallPtrSet<GlobalValue*, 32> AliveGlobals;
     50     SmallPtrSet<Constant *, 8> SeenConstants;
     51     std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
     52 
     53     /// GlobalIsNeeded - mark the specific global value as needed, and
     54     /// recursively mark anything that it uses as also needed.
     55     void GlobalIsNeeded(GlobalValue *GV);
     56     void MarkUsedGlobalsAsNeeded(Constant *C);
     57 
     58     bool RemoveUnusedGlobalValue(GlobalValue &GV);
     59   };
     60 }
     61 
     62 /// Returns true if F contains only a single "ret" instruction.
     63 static bool isEmptyFunction(Function *F) {
     64   BasicBlock &Entry = F->getEntryBlock();
     65   if (Entry.size() != 1 || !isa<ReturnInst>(Entry.front()))
     66     return false;
     67   ReturnInst &RI = cast<ReturnInst>(Entry.front());
     68   return RI.getReturnValue() == nullptr;
     69 }
     70 
     71 char GlobalDCE::ID = 0;
     72 INITIALIZE_PASS(GlobalDCE, "globaldce",
     73                 "Dead Global Elimination", false, false)
     74 
     75 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
     76 
     77 bool GlobalDCE::runOnModule(Module &M) {
     78   bool Changed = false;
     79 
     80   // Remove empty functions from the global ctors list.
     81   Changed |= optimizeGlobalCtorsList(M, isEmptyFunction);
     82 
     83   // Collect the set of members for each comdat.
     84   for (Function &F : M)
     85     if (Comdat *C = F.getComdat())
     86       ComdatMembers.insert(std::make_pair(C, &F));
     87   for (GlobalVariable &GV : M.globals())
     88     if (Comdat *C = GV.getComdat())
     89       ComdatMembers.insert(std::make_pair(C, &GV));
     90   for (GlobalAlias &GA : M.aliases())
     91     if (Comdat *C = GA.getComdat())
     92       ComdatMembers.insert(std::make_pair(C, &GA));
     93 
     94   // Loop over the module, adding globals which are obviously necessary.
     95   for (Function &F : M) {
     96     Changed |= RemoveUnusedGlobalValue(F);
     97     // Functions with external linkage are needed if they have a body
     98     if (!F.isDeclaration() && !F.hasAvailableExternallyLinkage())
     99       if (!F.isDiscardableIfUnused())
    100         GlobalIsNeeded(&F);
    101   }
    102 
    103   for (GlobalVariable &GV : M.globals()) {
    104     Changed |= RemoveUnusedGlobalValue(GV);
    105     // Externally visible & appending globals are needed, if they have an
    106     // initializer.
    107     if (!GV.isDeclaration() && !GV.hasAvailableExternallyLinkage())
    108       if (!GV.isDiscardableIfUnused())
    109         GlobalIsNeeded(&GV);
    110   }
    111 
    112   for (GlobalAlias &GA : M.aliases()) {
    113     Changed |= RemoveUnusedGlobalValue(GA);
    114     // Externally visible aliases are needed.
    115     if (!GA.isDiscardableIfUnused())
    116       GlobalIsNeeded(&GA);
    117   }
    118 
    119   // Now that all globals which are needed are in the AliveGlobals set, we loop
    120   // through the program, deleting those which are not alive.
    121   //
    122 
    123   // The first pass is to drop initializers of global variables which are dead.
    124   std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals
    125   for (GlobalVariable &GV : M.globals())
    126     if (!AliveGlobals.count(&GV)) {
    127       DeadGlobalVars.push_back(&GV);         // Keep track of dead globals
    128       if (GV.hasInitializer()) {
    129         Constant *Init = GV.getInitializer();
    130         GV.setInitializer(nullptr);
    131         if (isSafeToDestroyConstant(Init))
    132           Init->destroyConstant();
    133       }
    134     }
    135 
    136   // The second pass drops the bodies of functions which are dead...
    137   std::vector<Function *> DeadFunctions;
    138   for (Function &F : M)
    139     if (!AliveGlobals.count(&F)) {
    140       DeadFunctions.push_back(&F);         // Keep track of dead globals
    141       if (!F.isDeclaration())
    142         F.deleteBody();
    143     }
    144 
    145   // The third pass drops targets of aliases which are dead...
    146   std::vector<GlobalAlias*> DeadAliases;
    147   for (GlobalAlias &GA : M.aliases())
    148     if (!AliveGlobals.count(&GA)) {
    149       DeadAliases.push_back(&GA);
    150       GA.setAliasee(nullptr);
    151     }
    152 
    153   if (!DeadFunctions.empty()) {
    154     // Now that all interferences have been dropped, delete the actual objects
    155     // themselves.
    156     for (Function *F : DeadFunctions) {
    157       RemoveUnusedGlobalValue(*F);
    158       M.getFunctionList().erase(F);
    159     }
    160     NumFunctions += DeadFunctions.size();
    161     Changed = true;
    162   }
    163 
    164   if (!DeadGlobalVars.empty()) {
    165     for (GlobalVariable *GV : DeadGlobalVars) {
    166       RemoveUnusedGlobalValue(*GV);
    167       M.getGlobalList().erase(GV);
    168     }
    169     NumVariables += DeadGlobalVars.size();
    170     Changed = true;
    171   }
    172 
    173   // Now delete any dead aliases.
    174   if (!DeadAliases.empty()) {
    175     for (GlobalAlias *GA : DeadAliases) {
    176       RemoveUnusedGlobalValue(*GA);
    177       M.getAliasList().erase(GA);
    178     }
    179     NumAliases += DeadAliases.size();
    180     Changed = true;
    181   }
    182 
    183   // Make sure that all memory is released
    184   AliveGlobals.clear();
    185   SeenConstants.clear();
    186   ComdatMembers.clear();
    187 
    188   return Changed;
    189 }
    190 
    191 /// GlobalIsNeeded - the specific global value as needed, and
    192 /// recursively mark anything that it uses as also needed.
    193 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
    194   // If the global is already in the set, no need to reprocess it.
    195   if (!AliveGlobals.insert(G).second)
    196     return;
    197 
    198   if (Comdat *C = G->getComdat()) {
    199     for (auto &&CM : make_range(ComdatMembers.equal_range(C)))
    200       GlobalIsNeeded(CM.second);
    201   }
    202 
    203   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) {
    204     // If this is a global variable, we must make sure to add any global values
    205     // referenced by the initializer to the alive set.
    206     if (GV->hasInitializer())
    207       MarkUsedGlobalsAsNeeded(GV->getInitializer());
    208   } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
    209     // The target of a global alias is needed.
    210     MarkUsedGlobalsAsNeeded(GA->getAliasee());
    211   } else {
    212     // Otherwise this must be a function object.  We have to scan the body of
    213     // the function looking for constants and global values which are used as
    214     // operands.  Any operands of these types must be processed to ensure that
    215     // any globals used will be marked as needed.
    216     Function *F = cast<Function>(G);
    217 
    218     for (Use &U : F->operands())
    219       MarkUsedGlobalsAsNeeded(cast<Constant>(U.get()));
    220 
    221     for (BasicBlock &BB : *F)
    222       for (Instruction &I : BB)
    223         for (Use &U : I.operands())
    224           if (GlobalValue *GV = dyn_cast<GlobalValue>(U))
    225             GlobalIsNeeded(GV);
    226           else if (Constant *C = dyn_cast<Constant>(U))
    227             MarkUsedGlobalsAsNeeded(C);
    228   }
    229 }
    230 
    231 void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) {
    232   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
    233     return GlobalIsNeeded(GV);
    234 
    235   // Loop over all of the operands of the constant, adding any globals they
    236   // use to the list of needed globals.
    237   for (Use &U : C->operands()) {
    238     // If we've already processed this constant there's no need to do it again.
    239     Constant *Op = dyn_cast<Constant>(U);
    240     if (Op && SeenConstants.insert(Op).second)
    241       MarkUsedGlobalsAsNeeded(Op);
    242   }
    243 }
    244 
    245 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified
    246 // GlobalValue, looking for the constant pointer ref that may be pointing to it.
    247 // If found, check to see if the constant pointer ref is safe to destroy, and if
    248 // so, nuke it.  This will reduce the reference count on the global value, which
    249 // might make it deader.
    250 //
    251 bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) {
    252   if (GV.use_empty())
    253     return false;
    254   GV.removeDeadConstantUsers();
    255   return GV.use_empty();
    256 }
    257