1 19 20 25 26 27 28 29 30 31 package soot.jimple.toolkits.scalar; 32 import soot.options.*; 33 34 import soot.*; 35 import soot.jimple.*; 36 import soot.toolkits.scalar.*; 37 import soot.toolkits.graph.*; 38 import soot.util.*; 39 import java.util.*; 40 import soot.options.CPOptions; 41 42 public class CopyPropagator extends BodyTransformer 43 { 44 public CopyPropagator( Singletons.Global g ) {} 45 public static CopyPropagator v() { return G.v().soot_jimple_toolkits_scalar_CopyPropagator(); } 46 47 59 protected void internalTransform(Body b, String phaseName, Map opts) 60 { 61 CPOptions options = new CPOptions( opts ); 62 StmtBody stmtBody = (StmtBody)b; 63 int fastCopyPropagationCount = 0; 64 int slowCopyPropagationCount = 0; 65 66 if(Options.v().verbose()) 67 G.v().out.println("[" + stmtBody.getMethod().getName() + 68 "] Propagating copies..."); 69 70 if(Options.v().time()) 71 Timers.v().propagatorTimer.start(); 72 73 Chain units = stmtBody.getUnits(); 74 75 Map localToDefCount = new HashMap(); 76 77 { 79 Iterator stmtIt = units.iterator(); 80 81 while(stmtIt.hasNext()) 82 { 83 Stmt s = (Stmt) stmtIt.next(); 84 85 if(s instanceof DefinitionStmt && 86 ((DefinitionStmt) s).getLeftOp() instanceof Local) 87 { 88 Local l = (Local) ((DefinitionStmt) s).getLeftOp(); 89 90 if(!localToDefCount.containsKey(l)) 91 localToDefCount.put(l, new Integer (1)); 92 else 93 localToDefCount.put(l, new Integer (((Integer ) localToDefCount.get(l)).intValue() + 1)); 94 } 95 96 } 97 } 98 99 101 ExceptionalUnitGraph graph = new ExceptionalUnitGraph(stmtBody); 102 103 LocalDefs localDefs; 104 105 localDefs = new SmartLocalDefs(graph, new SimpleLiveLocals(graph)); 106 107 LocalUses localUses = new SimpleLocalUses(graph, localDefs); 108 109 { 111 Iterator stmtIt = (new PseudoTopologicalOrderer()).newList(graph).iterator(); 112 113 while(stmtIt.hasNext()) 114 { 115 Stmt stmt = (Stmt) stmtIt.next(); 116 Iterator useBoxIt = stmt.getUseBoxes().iterator(); 117 118 while(useBoxIt.hasNext()) 119 { 120 ValueBox useBox = (ValueBox) useBoxIt.next(); 121 122 if(useBox.getValue() instanceof Local) 123 { 124 Local l = (Local) useBox.getValue(); 125 126 if(options.only_regular_locals() && l.getName().startsWith("$")) 127 continue; 128 129 if(options.only_stack_locals() && !l.getName().startsWith("$")) 130 continue; 131 132 List defsOfUse = localDefs.getDefsOfAt(l, stmt); 133 134 if(defsOfUse.size() == 1) 135 { 136 DefinitionStmt def = (DefinitionStmt) defsOfUse.get(0); 137 138 if(def.getRightOp() instanceof Local) 139 { 140 Local m = (Local) def.getRightOp(); 141 142 if(l != m) 143 { 144 Object dcObj = localToDefCount.get(m); 145 146 if (dcObj == null) 147 throw new RuntimeException ("Variable " + m + " used without definition!"); 148 149 int defCount = ((Integer )dcObj).intValue(); 150 151 if(defCount == 0) 152 throw new RuntimeException ("Variable " + m + " used without definition!"); 153 else if(defCount == 1) 154 { 155 useBox.setValue(m); 156 fastCopyPropagationCount++; 157 continue; 158 } 159 160 List path = graph.getExtendedBasicBlockPathBetween(def, stmt); 161 162 if(path == null) 163 { 164 continue; 166 } 167 168 Iterator pathIt = path.iterator(); 169 170 pathIt.next(); 172 173 { 175 boolean isRedefined = false; 176 177 while(pathIt.hasNext()) 178 { 179 Stmt s = (Stmt) pathIt.next(); 180 181 if(stmt == s) 182 { 183 186 break; 187 } 188 if(s instanceof DefinitionStmt) 189 { 190 if(((DefinitionStmt) s).getLeftOp() == m) 191 { 192 isRedefined = true; 193 break; 194 } 195 } 196 } 197 198 if(isRedefined) 199 continue; 200 201 } 202 203 useBox.setValue(m); 204 slowCopyPropagationCount++; 205 } 206 } 207 } 208 } 209 210 } 211 } 212 } 213 214 215 if(Options.v().verbose()) 216 G.v().out.println("[" + stmtBody.getMethod().getName() + 217 "] Propagated: " + 218 fastCopyPropagationCount + " fast copies " + 219 slowCopyPropagationCount + " slow copies"); 220 221 if(Options.v().time()) 222 Timers.v().propagatorTimer.end(); 223 224 } 225 226 } 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | Popular Tags |