new write barrier class
[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.FlatExit:
33         case FKind.FlatTagDeclaration:
34         case FKind.FlatMethod:
35         case FKind.FlatAtomicEnterNode:
36         case FKind.FlatAtomicExitNode:
37         case FKind.FlatPrefetchNode:
38         case FKind.FlatSESEEnterNode:
39         case FKind.FlatSESEExitNode:
40           if (!useful.contains(fn)) {
41             useful.add(fn);
42             changed=true;
43           }       
44           break;
45         case FKind.FlatOpNode:
46           FlatOpNode fon=(FlatOpNode)fn;
47           if (fon.getOp().getOp()==Operation.DIV||
48               fon.getOp().getOp()==Operation.MOD) {
49             if (!useful.contains(fn)) {
50               useful.add(fn);
51               changed=true;
52             }
53             break;
54           }
55         default:
56           TempDescriptor[] writes=fn.writesTemps();
57           if (!useful.contains(fn))
58             for(int i=0;i<writes.length;i++) {
59               for(Iterator<FlatNode> uit=ud.useMap(fn,writes[i]).iterator();uit.hasNext();) {
60                 FlatNode ufn=uit.next();
61                 if (useful.contains(ufn)) {
62                   //we are useful
63                   useful.add(fn);
64                   changed=true;
65                   continue nextfn;
66                 }
67               }
68             }
69         }
70       }
71     }
72     //get rid of useless nodes
73     for(Iterator<FlatNode> it=fm.getNodeSet().iterator();it.hasNext();) {
74       FlatNode fn=it.next();
75       if (!useful.contains(fn)||isuseless(fn)) {
76         //We have a useless node
77         FlatNode fnnext=fn.getNext(0);
78         for(int i=0;i<fn.numPrev();i++) {
79           FlatNode nprev=fn.getPrev(i);
80               
81           for(int j=0;j<nprev.numNext();j++) {
82             if (nprev.getNext(j)==fn) {
83               nprev.setnext(j, fnnext);
84               fnnext.addPrev(nprev);
85             }
86           }
87         }
88         //fix up prev edge of fnnext
89         fnnext.removePrev(fn);
90       }
91     }
92   }
93   public boolean isuseless(FlatNode fn) {
94     if (fn.kind()==FKind.FlatOpNode) {
95       FlatOpNode fon=(FlatOpNode)fn;
96       if (fon.getOp().getOp()==Operation.ASSIGN&&fon.getLeft()==fon.getDest())
97         return true;
98     }
99     return false;
100   }
101
102 }