lots of bugs
[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)) {
76         //We have a useless node
77         FlatNode fnnext=fn.getNext(0);
78
79         for(int i=0;i<fn.numPrev();i++) {
80           FlatNode nprev=fn.getPrev(i);
81               
82           for(int j=0;j<nprev.numNext();j++) {
83             if (nprev.getNext(j)==fn) {
84               nprev.setnext(j, fnnext);
85               fnnext.addPrev(nprev);
86             }
87           }
88         }
89         //fix up prev edge of fnnext
90         fnnext.removePrev(fn);
91       }
92     }
93   }
94 }