1 19 20 25 26 27 package soot.jimple.toolkits.scalar; 28 import soot.options.*; 29 import soot.options.*; 30 import soot.*; 31 import soot.toolkits.scalar.*; 32 import soot.toolkits.graph.*; 33 import soot.jimple.*; 34 import java.util.*; 35 import soot.util.*; 36 import soot.jimple.toolkits.pointer.PASideEffectTester; 37 import soot.tagkit.*; 38 39 50 51 public class CommonSubexpressionEliminator extends BodyTransformer 52 { 53 public CommonSubexpressionEliminator( Singletons.Global g ) {} 54 public static CommonSubexpressionEliminator v() { return G.v().soot_jimple_toolkits_scalar_CommonSubexpressionEliminator(); } 55 56 57 protected void internalTransform(Body b, String phaseName, Map options) 58 { 59 int counter = 0; 60 61 Iterator localsIt = b.getLocals().iterator(); 63 HashSet localNames = new HashSet(b.getLocals().size()); 64 while (localsIt.hasNext()) 65 { 66 localNames.add(((Local)localsIt.next()).getName()); 67 } 68 69 SideEffectTester sideEffect; 70 if( Scene.v().hasCallGraph() 71 && !PhaseOptions.getBoolean( options, "naive-side-effect" ) ) { 72 sideEffect = new PASideEffectTester(); 73 } else { 74 sideEffect = new NaiveSideEffectTester(); 75 } 76 sideEffect.newMethod( b.getMethod() ); 77 78 if(Options.v().verbose()) 79 G.v().out.println("[" + b.getMethod().getName() + 80 "] Eliminating common subexpressions " + 81 (sideEffect instanceof NaiveSideEffectTester ? 82 "(naively)" : "") + 83 "..."); 84 85 AvailableExpressions ae = new FastAvailableExpressions(b, sideEffect); 87 88 Chain units = b.getUnits(); 89 Iterator unitsIt = units.snapshotIterator(); 90 while (unitsIt.hasNext()) 91 { 92 Stmt s = (Stmt) unitsIt.next(); 93 94 if (s instanceof AssignStmt) 95 { 96 Chain availExprs = ae.getAvailableEquivsBefore(s); 97 Value v = (Value)((AssignStmt)s).getRightOp(); 99 EquivalentValue ev = new EquivalentValue(v); 100 101 if (availExprs.contains(ev)) 102 { 103 List availPairs = ae.getAvailablePairsBefore(s); 105 Iterator availIt = availPairs.iterator(); 107 while (availIt.hasNext()) 108 { 109 UnitValueBoxPair up = (UnitValueBoxPair)availIt.next(); 110 if (up.getValueBox().getValue().equivTo(v)) 111 { 112 String newName = "$cseTmp"+counter; 115 counter++; 116 117 while (localNames.contains(newName)) 118 { 119 newName = "$cseTmp"+counter; 120 counter++; 121 } 122 123 Local l = Jimple.v().newLocal(newName, Type.toMachineType(v.getType())); 124 125 b.getLocals().add(l); 126 127 AssignStmt origCalc = (AssignStmt)up.getUnit(); 129 Value origLHS = origCalc.getLeftOp(); 130 131 origCalc.setLeftOp(l); 132 133 Unit copier = Jimple.v().newAssignStmt(origLHS, l); 134 units.insertAfter(copier, origCalc); 135 136 ((AssignStmt)s).setRightOp(l); 137 copier.addTag(new StringTag("Common sub-expression")); 138 s.addTag(new StringTag("Common sub-expression")); 139 } 142 } 143 } 144 } 145 } 146 if(Options.v().verbose()) 147 G.v().out.println("[" + b.getMethod().getName() + 148 "] Eliminating common subexpressions done!"); 149 } 150 } 151 | Popular Tags |