1 //===- ADCE.cpp - Code to perform agressive dead code elimination ---------===//
3 // This file implements "agressive" dead code elimination. ADCE is DCe where
4 // values are assumed to be dead until proven otherwise. This is similar to
5 // SCCP, except applied to the liveness of values.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Optimizations/DCE.h"
10 #include "llvm/Instruction.h"
11 #include "llvm/Type.h"
14 #include "llvm/Assembly/Writer.h"
16 //===----------------------------------------------------------------------===//
19 // This class does all of the work of Agressive Dead Code Elimination.
20 // It's public interface consists of a constructor and a doADCE() method.
23 Method *M; // The method that we are working on...
24 vector<Instruction*> WorkList; // Instructions that just became live
25 set<Instruction*> LiveSet; // The set of live instructions
27 //===--------------------------------------------------------------------===//
28 // The public interface for this class
31 // ADCE Ctor - Save the method to operate on...
32 inline ADCE(Method *m) : M(m) {}
34 // doADCE() - Run the Agressive Dead Code Elimination algorithm, returning
35 // true if the method was modified.
38 //===--------------------------------------------------------------------===//
39 // The implementation of this class
42 inline void markInstructionLive(Instruction *I) {
43 if (LiveSet.count(I)) return;
44 cerr << "Insn Live: " << I;
46 WorkList.push_back(I);
54 // doADCE() - Run the Agressive Dead Code Elimination algorithm, returning
55 // true if the method was modified.
58 // Iterate over all of the instructions in the method, eliminating trivially
59 // dead instructions, and marking instructions live that are known to be
62 for (Method::inst_iterator II = M->inst_begin(); II != M->inst_end(); ) {
64 switch (I->getInstType()) {
65 case Instruction::Call:
66 case Instruction::Store:
67 markInstructionLive(I);
70 if (I->getType() == Type::VoidTy) {
71 markInstructionLive(I); // Catches terminators and friends
73 if (I->use_size() == 0) { // Check to see if anything is trivially dead
74 // Remove the instruction from it's basic block...
75 BasicBlock *BB = I->getParent();
76 delete BB->getInstList().remove(II.getInstructionIterator());
78 // Make sure to sync up the iterator again...
79 II.resyncInstructionIterator();
80 continue; // Don't increment the iterator past the current slot
85 ++II; // Increment the iterator
89 cerr << "Processing work list\n";
91 // Process the work list of instructions that just became live... if they
92 // became live, then that means that all of their operands are neccesary as
93 // well... make them live as well.
95 while (!WorkList.empty()) {
96 Instruction *I = WorkList.back();
99 for (unsigned op = 0; Value *Op = I->getOperand(op); ++op) {
100 Instruction *Operand = Op->castInstruction();
101 if (Operand) markInstructionLive(Operand);
105 // After the worklist is processed, loop through the instructions again,
106 // removing any that are not live... by the definition of the LiveSet.
108 for (Method::inst_iterator II = M->inst_begin(); II != M->inst_end(); ) {
109 Instruction *I = *II;
110 if (!LiveSet.count(I)) {
111 cerr << "Instruction Dead: " << I;
114 ++II; // Increment the iterator
121 // DoADCE - Execute the Agressive Dead Code Elimination Algorithm
123 bool opt::DoADCE(Method *M) {