LLVM API Documentation

ADCE.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 the Aggressive Dead Code Elimination pass.  This pass
00011 // optimistically assumes that all instructions are dead until proven otherwise,
00012 // allowing it to eliminate dead computations that other DCE passes do not
00013 // catch, particularly involving loop computations.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/Transforms/Scalar.h"
00018 #include "llvm/ADT/DepthFirstIterator.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/IR/BasicBlock.h"
00023 #include "llvm/IR/CFG.h"
00024 #include "llvm/IR/InstIterator.h"
00025 #include "llvm/IR/Instructions.h"
00026 #include "llvm/IR/IntrinsicInst.h"
00027 #include "llvm/Pass.h"
00028 using namespace llvm;
00029 
00030 #define DEBUG_TYPE "adce"
00031 
00032 STATISTIC(NumRemoved, "Number of instructions removed");
00033 
00034 namespace {
00035   struct ADCE : public FunctionPass {
00036     static char ID; // Pass identification, replacement for typeid
00037     ADCE() : FunctionPass(ID) {
00038       initializeADCEPass(*PassRegistry::getPassRegistry());
00039     }
00040 
00041     bool runOnFunction(Function& F) override;
00042 
00043     void getAnalysisUsage(AnalysisUsage& AU) const override {
00044       AU.setPreservesCFG();
00045     }
00046 
00047   };
00048 }
00049 
00050 char ADCE::ID = 0;
00051 INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
00052 
00053 bool ADCE::runOnFunction(Function& F) {
00054   if (skipOptnoneFunction(F))
00055     return false;
00056 
00057   SmallPtrSet<Instruction*, 128> alive;
00058   SmallVector<Instruction*, 128> worklist;
00059 
00060   // Collect the set of "root" instructions that are known live.
00061   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
00062     if (isa<TerminatorInst>(I.getInstructionIterator()) ||
00063         isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
00064         isa<LandingPadInst>(I.getInstructionIterator()) ||
00065         I->mayHaveSideEffects()) {
00066       alive.insert(I.getInstructionIterator());
00067       worklist.push_back(I.getInstructionIterator());
00068     }
00069 
00070   // Propagate liveness backwards to operands.
00071   while (!worklist.empty()) {
00072     Instruction* curr = worklist.pop_back_val();
00073     for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
00074          OI != OE; ++OI)
00075       if (Instruction* Inst = dyn_cast<Instruction>(OI))
00076         if (alive.insert(Inst))
00077           worklist.push_back(Inst);
00078   }
00079 
00080   // The inverse of the live set is the dead set.  These are those instructions
00081   // which have no side effects and do not influence the control flow or return
00082   // value of the function, and may therefore be deleted safely.
00083   // NOTE: We reuse the worklist vector here for memory efficiency.
00084   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
00085     if (!alive.count(I.getInstructionIterator())) {
00086       worklist.push_back(I.getInstructionIterator());
00087       I->dropAllReferences();
00088     }
00089 
00090   for (SmallVectorImpl<Instruction *>::iterator I = worklist.begin(),
00091        E = worklist.end(); I != E; ++I) {
00092     ++NumRemoved;
00093     (*I)->eraseFromParent();
00094   }
00095 
00096   return !worklist.empty();
00097 }
00098 
00099 FunctionPass *llvm::createAggressiveDCEPass() {
00100   return new ADCE();
00101 }