1 package Analysis.Loops;
4 import java.util.HashSet;
5 import java.util.Iterator;
7 public class DeadCode {
10 public void optimize(FlatMethod fm) {
11 UseDef ud=new UseDef(fm);
12 HashSet useful=new HashSet();
17 for(Iterator<FlatNode> it=fm.getNodeSet().iterator();it.hasNext();) {
18 FlatNode fn=it.next();
21 case FKind.FlatFieldNode:
22 case FKind.FlatSetFieldNode:
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:
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)) {
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)) {
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)) {
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);
81 for(int j=0;j<nprev.numNext();j++) {
82 if (nprev.getNext(j)==fn) {
83 nprev.setnext(j, fnnext);
84 fnnext.addPrev(nprev);
88 //fix up prev edge of fnnext
89 fnnext.removePrev(fn);
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())