LLVM API Documentation

DCE.cpp
Go to the documentation of this file.
00001 //===- DCE.cpp - Code to perform dead code elimination --------------------===//
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 file implements dead inst elimination and dead code elimination.
00011 //
00012 // Dead Inst Elimination performs a single pass over the function removing
00013 // instructions that are obviously dead.  Dead Code Elimination is similar, but
00014 // it rechecks instructions that were used by removed instructions to see if
00015 // they are newly dead.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Transforms/Scalar.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/IR/InstIterator.h"
00022 #include "llvm/IR/Instruction.h"
00023 #include "llvm/Pass.h"
00024 #include "llvm/Target/TargetLibraryInfo.h"
00025 #include "llvm/Transforms/Utils/Local.h"
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "dce"
00029 
00030 STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
00031 STATISTIC(DCEEliminated, "Number of insts removed");
00032 
00033 namespace {
00034   //===--------------------------------------------------------------------===//
00035   // DeadInstElimination pass implementation
00036   //
00037   struct DeadInstElimination : public BasicBlockPass {
00038     static char ID; // Pass identification, replacement for typeid
00039     DeadInstElimination() : BasicBlockPass(ID) {
00040       initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
00041     }
00042     bool runOnBasicBlock(BasicBlock &BB) override {
00043       if (skipOptnoneFunction(BB))
00044         return false;
00045       TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
00046       bool Changed = false;
00047       for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
00048         Instruction *Inst = DI++;
00049         if (isInstructionTriviallyDead(Inst, TLI)) {
00050           Inst->eraseFromParent();
00051           Changed = true;
00052           ++DIEEliminated;
00053         }
00054       }
00055       return Changed;
00056     }
00057 
00058     void getAnalysisUsage(AnalysisUsage &AU) const override {
00059       AU.setPreservesCFG();
00060     }
00061   };
00062 }
00063 
00064 char DeadInstElimination::ID = 0;
00065 INITIALIZE_PASS(DeadInstElimination, "die",
00066                 "Dead Instruction Elimination", false, false)
00067 
00068 Pass *llvm::createDeadInstEliminationPass() {
00069   return new DeadInstElimination();
00070 }
00071 
00072 
00073 namespace {
00074   //===--------------------------------------------------------------------===//
00075   // DeadCodeElimination pass implementation
00076   //
00077   struct DCE : public FunctionPass {
00078     static char ID; // Pass identification, replacement for typeid
00079     DCE() : FunctionPass(ID) {
00080       initializeDCEPass(*PassRegistry::getPassRegistry());
00081     }
00082 
00083     bool runOnFunction(Function &F) override;
00084 
00085     void getAnalysisUsage(AnalysisUsage &AU) const override {
00086       AU.setPreservesCFG();
00087     }
00088  };
00089 }
00090 
00091 char DCE::ID = 0;
00092 INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
00093 
00094 bool DCE::runOnFunction(Function &F) {
00095   if (skipOptnoneFunction(F))
00096     return false;
00097 
00098   TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
00099 
00100   // Start out with all of the instructions in the worklist...
00101   std::vector<Instruction*> WorkList;
00102   for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
00103     WorkList.push_back(&*i);
00104 
00105   // Loop over the worklist finding instructions that are dead.  If they are
00106   // dead make them drop all of their uses, making other instructions
00107   // potentially dead, and work until the worklist is empty.
00108   //
00109   bool MadeChange = false;
00110   while (!WorkList.empty()) {
00111     Instruction *I = WorkList.back();
00112     WorkList.pop_back();
00113 
00114     if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
00115       // Loop over all of the values that the instruction uses, if there are
00116       // instructions being used, add them to the worklist, because they might
00117       // go dead after this one is removed.
00118       //
00119       for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
00120         if (Instruction *Used = dyn_cast<Instruction>(*OI))
00121           WorkList.push_back(Used);
00122 
00123       // Remove the instruction.
00124       I->eraseFromParent();
00125 
00126       // Remove the instruction from the worklist if it still exists in it.
00127       WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
00128                      WorkList.end());
00129 
00130       MadeChange = true;
00131       ++DCEEliminated;
00132     }
00133   }
00134   return MadeChange;
00135 }
00136 
00137 FunctionPass *llvm::createDeadCodeEliminationPass() {
00138   return new DCE();
00139 }
00140