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.toolkits.scalar.*; 34 import soot.jimple.*; 35 import java.io.*; 36 import java.util.*; 37 import soot.toolkits.graph.*; 38 39 42 public class ConstantPropagatorAndFolder extends BodyTransformer 43 { 44 public ConstantPropagatorAndFolder( Singletons.Global g ) {} 45 public static ConstantPropagatorAndFolder v() { return G.v().soot_jimple_toolkits_scalar_ConstantPropagatorAndFolder(); } 46 47 protected void internalTransform(Body b, String phaseName, Map options) 48 { 49 StmtBody stmtBody = (StmtBody)b; 50 int numFolded = 0; 51 int numPropagated = 0; 52 53 if (Options.v().verbose()) 54 G.v().out.println("[" + stmtBody.getMethod().getName() + 55 "] Propagating and folding constants..."); 56 57 Chain units = stmtBody.getUnits(); 58 ExceptionalUnitGraph unitGraph = new ExceptionalUnitGraph(stmtBody); 59 LocalDefs localDefs; 60 61 localDefs = new SmartLocalDefs(unitGraph, new SimpleLiveLocals(unitGraph)); 62 63 Iterator stmtIt = (new PseudoTopologicalOrderer()).newList(unitGraph).iterator(); 65 66 while (stmtIt.hasNext()) { 68 Stmt stmt = (Stmt) stmtIt.next(); 69 70 Iterator useBoxIt = stmt.getUseBoxes().iterator(); 72 ValueBox useBox; 73 74 while (useBoxIt.hasNext()) { 75 useBox = (ValueBox) useBoxIt.next(); 76 if (useBox.getValue() instanceof Local) { 77 Local local = (Local) useBox.getValue(); 78 List defsOfUse = localDefs.getDefsOfAt(local, stmt); 79 if (defsOfUse.size() == 1) { 80 DefinitionStmt defStmt = 81 (DefinitionStmt) defsOfUse.get(0); 82 if (defStmt.getRightOp() instanceof NumericConstant) { 83 if (useBox.canContainValue(defStmt.getRightOp())) { 84 useBox.setValue(defStmt.getRightOp()); 85 numPropagated++; 86 } 87 } 88 } 89 } 90 } 91 92 useBoxIt = stmt.getUseBoxes().iterator(); 94 95 while (useBoxIt.hasNext()) { 96 useBox = (ValueBox) useBoxIt.next(); 97 Value value = useBox.getValue(); 98 if (!(value instanceof Constant)) { 99 if (Evaluator.isValueConstantValued(value)) { 100 Value constValue = 101 Evaluator.getConstantValueOf(value); 102 if (useBox.canContainValue(constValue)) { 103 useBox.setValue(constValue); 104 numFolded++; 105 } 106 } 107 } 108 } 109 } 110 111 if (Options.v().verbose()) 112 G.v().out.println("[" + stmtBody.getMethod().getName() + 113 "] Propagated: " + numPropagated + ", Folded: " + numFolded); 114 115 } 117 } 118 119 120 121 122 123 | Popular Tags |