01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 1999 Phong Co
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * 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: package soot.jimple.toolkits.scalar;
27:
28: import soot.options.*;
29:
30: import soot.util.*;
31: import soot.*;
32: import soot.jimple.*;
33: import java.util.*;
34:
35: public class ConditionalBranchFolder extends BodyTransformer {
36: public ConditionalBranchFolder(Singletons.Global g) {
37: }
38:
39: public static ConditionalBranchFolder v() {
40: return G.v()
41: .soot_jimple_toolkits_scalar_ConditionalBranchFolder();
42: }
43:
44: protected void internalTransform(Body body, String phaseName,
45: Map options) {
46: StmtBody stmtBody = (StmtBody) body;
47:
48: int numTrue = 0, numFalse = 0;
49:
50: if (Options.v().verbose())
51: G.v().out.println("[" + stmtBody.getMethod().getName()
52: + "] Folding conditional branches...");
53:
54: Chain units = stmtBody.getUnits();
55: ArrayList<Unit> unitList = new ArrayList<Unit>();
56: unitList.addAll(units);
57:
58: Iterator<Unit> stmtIt = unitList.iterator();
59: while (stmtIt.hasNext()) {
60: Stmt stmt = (Stmt) stmtIt.next();
61: if (stmt instanceof IfStmt) {
62: // check for constant-valued conditions
63: Value cond = ((IfStmt) stmt).getCondition();
64: if (Evaluator.isValueConstantValued(cond)) {
65: cond = Evaluator.getConstantValueOf(cond);
66:
67: if (((IntConstant) cond).value == 1) {
68: // if condition always true, convert if to goto
69: Stmt newStmt = Jimple.v().newGotoStmt(
70: ((IfStmt) stmt).getTarget());
71:
72: units.insertAfter(newStmt, stmt);
73:
74: numTrue++;
75: } else
76: numFalse++;
77:
78: // remove if
79: units.remove(stmt);
80: }
81: }
82: }
83:
84: if (Options.v().verbose())
85: G.v().out.println("[" + stmtBody.getMethod().getName()
86: + "] Folded " + numTrue + " true, " + numFalse
87: + " conditional branches");
88:
89: } // foldBranches
90:
91: } // BranchFolder
|