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