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