1 19 20 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 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 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 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 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 } 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 (!(stmt instanceof GotoStmt)) 139 return stmt; 140 141 stmtMap.put(stmt, stmt); 143 144 target = (Stmt)((GotoStmt)stmt).getTarget(); 145 146 if (stmtMap.containsKey(target)) { 148 finalTarget = (Stmt)stmtMap.get(target); 150 if (finalTarget == target) 151 finalTarget = null; 153 } 154 else 155 finalTarget = getFinalTarget(target); 156 157 stmtMap.put(stmt, finalTarget); 158 return finalTarget; 159 } 161 } 163 164 165 166 167 | Popular Tags |