View Javadoc

1   /*
2    * Created on Jul 11, 2004
3    * 
4    * TODO To change the template for this generated file go to Window -
5    * Preferences - Java - Code Style - Code Templates
6    */
7   package net.sf.bddbddb.dataflow;
8   
9   import java.util.ListIterator;
10  import net.sf.bddbddb.IterationList;
11  import net.sf.bddbddb.Relation;
12  import net.sf.bddbddb.dataflow.Liveness.LivenessFact;
13  import net.sf.bddbddb.ir.IR;
14  import net.sf.bddbddb.ir.Operation;
15  
16  /***
17   * @author mcarbin
18   * 
19   * To change the template for this generated type comment go to Window -
20   * Preferences - Java - Code Style - Code Templates
21   */
22  public class DeadCode implements IRPass {
23      public boolean TRACE = false;
24      IR ir;
25      Liveness liveness;
26  
27      public DeadCode(IR ir) {
28          this.ir = ir;
29          liveness = new Liveness(ir);
30      }
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see net.sf.bddbddb.dataflow.IRPass#run()
36       */
37      public boolean run() {
38          long time = System.currentTimeMillis();
39          IterationList list = ir.graph.getIterationList();
40          DataflowSolver solver = new DataflowSolver();
41          solver.solve(liveness, list);
42          boolean result = deadCodeElimination(list);
43          System.out.println(((System.currentTimeMillis() - time) / 1000.) + "s");
44          return result;
45      }
46  
47      boolean deadCodeElimination(IterationList list) {
48          boolean changed = false;
49          for (ListIterator it = list.iterator(); it.hasNext();) {
50              Object o = it.next();
51              if (o instanceof Operation) {
52                  Operation op = (Operation) o;
53                  Relation dest = op.getRelationDest();
54                  if (dest != null) {
55                      LivenessFact fact = liveness.getOut(op);
56                      if (!fact.isAlive(dest)) {
57                          if (TRACE) System.out.println(dest + " is dead after " + op + " , removing operation");
58                          it.remove();
59                          changed = true;
60                      }
61                  }
62              } else {
63                  boolean b = deadCodeElimination((IterationList) o);
64                  if (!changed) changed = b;
65              }
66          }
67          return changed;
68      }
69  }