1 19 20 25 26 27 package soot.jimple.toolkits.scalar.pre; 28 import soot.options.*; 29 import soot.jimple.toolkits.graph.*; 30 import soot.jimple.toolkits.scalar.*; 31 import soot.*; 32 import soot.toolkits.scalar.*; 33 import soot.toolkits.graph.*; 34 import soot.jimple.*; 35 import java.util.*; 36 import soot.util.*; 37 import soot.jimple.toolkits.pointer.PASideEffectTester; 38 import soot.options.BCMOptions; 39 40 56 public class BusyCodeMotion extends BodyTransformer { 57 public BusyCodeMotion( Singletons.Global g ) {} 58 public static BusyCodeMotion v() { return G.v().soot_jimple_toolkits_scalar_pre_BusyCodeMotion(); } 59 60 private static final String PREFIX = "$bcm"; 61 62 65 protected void internalTransform(Body b, String phaseName, Map opts) { 66 BCMOptions options = new BCMOptions( opts ); 67 int counter = 0; 68 HashMap expToHelper = new HashMap(); 69 Chain unitChain = b.getUnits(); 70 71 if(Options.v().verbose()) 72 G.v().out.println("[" + b.getMethod().getName() + 73 "] performing Busy Code Motion..."); 74 75 CriticalEdgeRemover.v().transform(b, phaseName + ".cer"); 76 77 UnitGraph graph = new BriefUnitGraph(b); 78 79 80 Map unitToEquivRhs = new UnitMap(b, graph.size() + 1, 0.7f) { 81 protected Object mapTo(Unit unit) { 82 Value tmp = SootFilter.noInvokeRhs(unit); 83 Value tmp2 = SootFilter.binop(tmp); 84 if (tmp2 == null) tmp2 = SootFilter.concreteRef(tmp); 85 return SootFilter.equiVal(tmp2); 86 } 87 }; 88 89 90 Map unitToNoExceptionEquivRhs = new UnitMap(b, graph.size() + 1, 0.7f) { 91 protected Object mapTo(Unit unit) { 92 Value tmp = SootFilter.binopRhs(unit); 93 tmp = SootFilter.noExceptionThrowing(tmp); 94 return SootFilter.equiVal(tmp); 95 } 96 }; 97 98 99 SideEffectTester sideEffect; 100 if( Scene.v().hasCallGraph() && !options.naive_side_effect() ) { 101 sideEffect = new PASideEffectTester(); 102 } else { 103 sideEffect = new NaiveSideEffectTester(); 104 } 105 sideEffect.newMethod( b.getMethod() ); 106 UpSafetyAnalysis upSafe = new UpSafetyAnalysis(graph, unitToEquivRhs, 107 sideEffect ); 108 DownSafetyAnalysis downSafe = new DownSafetyAnalysis(graph, 109 unitToNoExceptionEquivRhs, sideEffect ); 110 EarliestnessComputation earliest = new EarliestnessComputation(graph, 111 upSafe, downSafe, sideEffect ); 112 113 LocalCreation localCreation = new LocalCreation(b.getLocals(), PREFIX); 114 115 Iterator unitIt = unitChain.snapshotIterator(); 116 117 { 118 while (unitIt.hasNext()) { 119 Unit currentUnit = (Unit)unitIt.next(); 120 Iterator earliestIt = 121 ((FlowSet)earliest.getFlowBefore(currentUnit)).iterator(); 122 while (earliestIt.hasNext()) { 123 EquivalentValue equiVal = (EquivalentValue)earliestIt.next(); 124 Value exp = equiVal.getValue(); 125 126 Local helper = (Local)expToHelper.get(equiVal); 127 if (helper == null) { 128 helper = localCreation.newLocal(equiVal.getType()); 129 expToHelper.put(equiVal, helper); 130 } 131 132 133 Value insertValue = Jimple.cloneIfNecessary(equiVal.getValue()); 134 Unit firstComp = Jimple.v().newAssignStmt(helper, insertValue); 135 unitChain.insertBefore(firstComp, currentUnit); 136 } 137 } 138 } 139 140 { 141 unitIt = unitChain.iterator(); 142 while (unitIt.hasNext()) { 143 Unit currentUnit = (Unit)unitIt.next(); 144 EquivalentValue rhs = (EquivalentValue)unitToEquivRhs.get(currentUnit); 145 if (rhs != null) { 146 Local helper = (Local)expToHelper.get(rhs); 147 if (helper != null) 148 ((AssignStmt)currentUnit).setRightOp(helper); 149 } 150 } 151 } 152 if(Options.v().verbose()) 153 G.v().out.println("[" + b.getMethod().getName() + 154 "] Busy Code Motion done!"); 155 } 156 } 157 | Popular Tags |