KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > annotation > nullcheck > NullCheckEliminator


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Ganesh Sittampalam
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.jimple.toolkits.annotation.nullcheck;
21
22 import java.util.*;
23 import soot.*;
24 import soot.jimple.*;
25 import soot.toolkits.graph.UnitGraph;
26 import soot.toolkits.graph.ExceptionalUnitGraph;
27 import soot.toolkits.scalar.FlowSet;
28 import soot.util.Chain;
29
30 public class NullCheckEliminator extends BodyTransformer {
31
32     public static class AnalysisFactory {
33     public BranchedRefVarsAnalysis newAnalysis(UnitGraph g) {
34         return new BranchedRefVarsAnalysis(g);
35     }
36     }
37
38     private AnalysisFactory analysisFactory;
39
40     public NullCheckEliminator() {
41     this(new AnalysisFactory());
42     }
43
44     public NullCheckEliminator(AnalysisFactory f) {
45     this.analysisFactory=f;
46     }
47
48     public void internalTransform(Body body, String JavaDoc phaseName, Map options) {
49
50     // really, the analysis should be able to use its own results to determine
51
// that some branches are dead, but since it doesn't we just iterate.
52
boolean changed;
53     int i=0;
54     do {
55         changed=false;
56
57         BranchedRefVarsAnalysis analysis=analysisFactory.newAnalysis(new ExceptionalUnitGraph(body));
58
59         Chain units=body.getUnits();
60         Stmt s;
61         for(s=(Stmt) units.getFirst();s!=null;s=(Stmt) units.getSuccOf(s)) {
62         if(!(s instanceof IfStmt)) continue;
63         IfStmt is=(IfStmt) s;
64         Value c=is.getCondition();
65         if(!(c instanceof EqExpr || c instanceof NeExpr)) continue;
66         BinopExpr e=(BinopExpr) c;
67         Value v=null;
68         if(e.getOp1() instanceof NullConstant) v=e.getOp2();
69         if(e.getOp2() instanceof NullConstant) v=e.getOp1();
70         if(v==null) continue;
71         int res=analysis.anyRefInfo(v,(FlowSet) analysis.getFlowBefore(s));
72         int elim=0; // -1 => condition is false, 1 => condition is true
73
if(res==BranchedRefVarsAnalysis.kNonNull) elim=c instanceof EqExpr ? -1 : 1;
74         if(res==BranchedRefVarsAnalysis.kNull) elim=c instanceof EqExpr ? 1 : -1;
75         Stmt newstmt=null;
76         if(elim==-1) newstmt=Jimple.v().newNopStmt();
77         if(elim==1) newstmt=Jimple.v().newGotoStmt(is.getTarget());
78         if(newstmt!=null) {
79             units.swapWith(s,newstmt);
80             s=newstmt;
81             changed=true;
82         }
83         }
84     } while(changed);
85     }
86
87 }
88
Popular Tags