KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > UnconditionalBranchFolder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Phong Co
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28 package soot.jimple.toolkits.scalar;
29 import soot.options.*;
30
31 import soot.util.*;
32 import soot.*;
33 import soot.jimple.*;
34 import java.io.*;
35 import java.util.*;
36
37
38 public class UnconditionalBranchFolder extends BodyTransformer
39 {
40     public UnconditionalBranchFolder( Singletons.Global g ) {}
41     public static UnconditionalBranchFolder v() { return G.v().soot_jimple_toolkits_scalar_UnconditionalBranchFolder(); }
42
43     static final int JUMPOPT_TYPES = 6;
44     int numFound[], numFixed[];
45
46     HashMap stmtMap;
47     
48     protected void internalTransform(Body b, String JavaDoc phaseName, Map options)
49     {
50         StmtBody body = (StmtBody)b;
51
52         if (Options.v().verbose())
53             G.v().out.println("[" + body.getMethod().getName() + "] Folding unconditional branches...");
54
55
56         // allocate counters once only
57
if (numFound == null) {
58             numFound = new int[JUMPOPT_TYPES+1];
59             numFixed = new int[JUMPOPT_TYPES+1];
60         }
61
62         for (int i = 0; i <= JUMPOPT_TYPES; i++) {
63             numFound[i] = 0;
64             numFixed[i] = 0;
65         }
66
67         Chain units = body.getUnits();
68         stmtMap = new HashMap();
69
70         // find goto and if-goto statements
71
Iterator stmtIt = units.iterator();
72         Stmt stmt, target, newTarget;
73         while (stmtIt.hasNext()) {
74             stmt = (Stmt)stmtIt.next();
75             if (stmt instanceof GotoStmt) {
76
77                 target = (Stmt)((GotoStmt)stmt).getTarget();
78
79                 if (stmtIt.hasNext()) {
80                     // check for goto -> next statement
81
if (units.getSuccOf(stmt) == target)
82                     {
83                         stmtIt.remove();
84                         updateCounters(6, true);
85                     }
86                 }
87
88                 if (target instanceof GotoStmt) {
89                     newTarget = getFinalTarget(target);
90                     if (newTarget == null)
91                         newTarget = stmt;
92                     ((GotoStmt)stmt).setTarget(newTarget);
93                     updateCounters(1, true);
94                 }
95                 else if (target instanceof IfStmt) {
96                     updateCounters(3, false);
97                 }
98             }
99             else if (stmt instanceof IfStmt) {
100                 target = (Stmt)((IfStmt)stmt).getTarget();
101
102                 if (target instanceof GotoStmt) {
103                     newTarget = getFinalTarget(target);
104                     if (newTarget == null)
105                         newTarget = stmt;
106                     ((IfStmt)stmt).setTarget((Unit)newTarget);
107                     updateCounters(2, true);
108                 }
109                 else if (target instanceof IfStmt) {
110                     updateCounters(4, false);
111                 }
112             }
113         }
114         if (Options.v().verbose())
115             G.v().out.println("[" + body.getMethod().getName() + "] " + numFixed[0] + " of " +
116                                 numFound[0] + " branches folded.");
117              
118                                
119     } // optimizeJumps
120

121     private void updateCounters(int type, boolean fixed) {
122
123         if ((type < 0) || (type > JUMPOPT_TYPES))
124             return;
125
126         numFound[0]++;
127         numFound[type]++;
128         if (fixed) {
129             numFixed[0]++;
130             numFixed[type]++;
131         }
132     }
133         
134     private Stmt getFinalTarget(Stmt stmt) {
135         Stmt finalTarget=null, target;
136         
137         // if not a goto, this is the final target
138
if (!(stmt instanceof GotoStmt))
139             return stmt;
140
141         // first map this statement to itself, so we can detect cycles
142
stmtMap.put(stmt, stmt);
143
144         target = (Stmt)((GotoStmt)stmt).getTarget();
145
146         // check if target is in statement map
147
if (stmtMap.containsKey(target)) {
148             // see if it maps to itself
149
finalTarget = (Stmt)stmtMap.get(target);
150             if (finalTarget == target)
151                 // this is part of a cycle
152
finalTarget = null;
153         }
154         else
155             finalTarget = getFinalTarget(target);
156             
157         stmtMap.put(stmt, finalTarget);
158         return finalTarget;
159     } // getFinalTarget
160

161 } // JumpOptimizer
162

163
164
165
166
167
Popular Tags