a whole bunch of optimizations...should be useful for transactions
[IRC.git] / Robust / src / Analysis / Loops / DeadCode.java
1 package Analysis.Loops;
2 import IR.Flat.*;
3 import IR.Operation;
4 import java.util.HashSet;
5 import java.util.Iterator;
6
7 public class DeadCode {
8   public DeadCode() {
9   }
10   public void optimize(FlatMethod fm) {
11     UseDef ud=new UseDef(fm);
12     HashSet useful=new HashSet();
13     boolean changed=true;
14     while(changed) {
15       changed=false;
16       nextfn:
17       for(Iterator<FlatNode> it=fm.getNodeSet().iterator();it.hasNext();) {
18         FlatNode fn=it.next();
19         switch(fn.kind()) {
20         case FKind.FlatCall:
21         case FKind.FlatFieldNode:
22         case FKind.FlatSetFieldNode:
23         case FKind.FlatNew:
24         case FKind.FlatCastNode:
25         case FKind.FlatReturnNode:
26         case FKind.FlatCondBranch:
27         case FKind.FlatSetElementNode:
28         case FKind.FlatElementNode:
29         case FKind.FlatFlagActionNode:
30         case FKind.FlatCheckNode:
31         case FKind.FlatBackEdge:
32         case FKind.FlatTagDeclaration:
33         case FKind.FlatMethod:
34         case FKind.FlatAtomicEnterNode:
35         case FKind.FlatAtomicExitNode:
36         case FKind.FlatPrefetchNode:
37         case FKind.FlatSESEEnterNode:
38         case FKind.FlatSESEExitNode:
39           if (!useful.contains(fn)) {
40             useful.add(fn);
41             changed=true;
42           }       
43           break;
44         case FKind.FlatOpNode:
45           FlatOpNode fon=(FlatOpNode)fn;
46           if (fon.getOp().getOp()==Operation.DIV||
47               fon.getOp().getOp()==Operation.MOD) {
48             if (!useful.contains(fn)) {
49               useful.add(fn);
50               changed=true;
51             }
52             break;
53           }
54         default:
55           TempDescriptor[] writes=fn.writesTemps();
56           if (!useful.contains(fn))
57             for(int i=0;i<writes.length;i++) {
58               for(Iterator<FlatNode> uit=ud.useMap(fn,writes[i]).iterator();uit.hasNext();) {
59                 FlatNode ufn=uit.next();
60                 if (useful.contains(ufn)) {
61                   //we are useful
62                   useful.add(fn);
63                   changed=true;
64                   continue nextfn;
65                 }
66               }
67             }
68         }
69       }
70     }
71     //get rid of useless nodes
72     for(Iterator<FlatNode> it=fm.getNodeSet().iterator();it.hasNext();) {
73       FlatNode fn=it.next();
74       if (!useful.contains(fn)) {
75         //We have a useless node
76         FlatNode fnnext=fn.getNext(0);
77         for(int i=0;i<fn.numPrev();i++) {
78           FlatNode nprev=fn.getPrev(i);
79           for(int j=0;j<nprev.numNext();j++) {
80             if (nprev.getNext(j)==fn) {
81               nprev.setnext(j, fnnext);
82               fnnext.addPrev(nprev);
83             }
84           }
85         }
86         //fix up prev edge of fnnext
87         fnnext.removePrev(fn);
88       }
89     }
90   }
91 }