00001 package ca.mcgill.sable.soot.jimple;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 import ca.mcgill.sable.soot.*;
00072 import ca.mcgill.sable.util.*;
00073
00074 public class ConstantAndCopyPropagator
00075 {
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 public static void propagateConstantsAndCopies(StmtBody stmtBody)
00087 {
00088 int fastCopyPropagationCount = 0;
00089 int slowCopyPropagationCount = 0;
00090 int constantPropagationCount = 0;
00091
00092 if(Main.isVerbose)
00093 System.out.println("[" + stmtBody.getMethod().getName() +
00094 "] Propagating constants and copies...");
00095
00096 if(Main.isProfilingOptimization)
00097 Main.propagatorTimer.start();
00098
00099 StmtList stmtList = stmtBody.getStmtList();
00100
00101 Map localToDefCount = new HashMap();
00102
00103
00104 {
00105 Iterator stmtIt = stmtList.iterator();
00106
00107 while(stmtIt.hasNext())
00108 {
00109 Stmt s = (Stmt) stmtIt.next();
00110
00111 if(s instanceof DefinitionStmt &&
00112 ((DefinitionStmt) s).getLeftOp() instanceof Local)
00113 {
00114 Local l = (Local) ((DefinitionStmt) s).getLeftOp();
00115
00116 if(!localToDefCount.containsKey(l))
00117 localToDefCount.put(l, new Integer(1));
00118 else
00119 localToDefCount.put(l, new Integer(((Integer) localToDefCount.get(l)).intValue() + 1));
00120 }
00121
00122 }
00123 }
00124
00125
00126
00127 CompleteStmtGraph graph = new CompleteStmtGraph(stmtList);
00128
00129 LocalDefs localDefs;
00130
00131 if(Main.usePackedDefs)
00132 {
00133 localDefs = new SimpleLocalDefs(graph);
00134 }
00135 else {
00136 LiveLocals liveLocals;
00137
00138 if(Main.usePackedLive)
00139 liveLocals = new SimpleLiveLocals(graph);
00140 else
00141 liveLocals = new SparseLiveLocals(graph);
00142
00143 localDefs = new SparseLocalDefs(graph, liveLocals);
00144 }
00145
00146
00147 LocalUses localUses = new SimpleLocalUses(graph, localDefs);
00148
00149
00150 {
00151 Iterator stmtIt = graph.pseudoTopologicalOrderIterator();
00152
00153 while(stmtIt.hasNext())
00154 {
00155 Stmt stmt = (Stmt) stmtIt.next();
00156 Iterator useBoxIt = stmt.getUseBoxes().iterator();
00157
00158 while(useBoxIt.hasNext())
00159 {
00160 ValueBox useBox = (ValueBox) useBoxIt.next();
00161
00162 if(useBox.getValue() instanceof Local)
00163 {
00164 Local l = (Local) useBox.getValue();
00165
00166 List defsOfUse = localDefs.getDefsOfAt(l, stmt);
00167
00168 if(defsOfUse.size() == 1)
00169 {
00170 DefinitionStmt def = (DefinitionStmt) defsOfUse.get(0);
00171
00172 if(def.getRightOp() instanceof Constant)
00173 {
00174 if(useBox.canContainValue(def.getRightOp()))
00175 {
00176
00177
00178
00179 useBox.setValue(def.getRightOp());
00180 constantPropagationCount++;
00181 }
00182 }
00183 else if(def.getRightOp() instanceof Local)
00184 {
00185 Local m = (Local) def.getRightOp();
00186
00187 if(l != m)
00188 {
00189 int defCount = ((Integer) localToDefCount.get(m)).intValue();
00190
00191 if(defCount == 0)
00192 throw new RuntimeException("Variable " + m + " used without definition!");
00193 else if(defCount == 1)
00194 {
00195 useBox.setValue(m);
00196 fastCopyPropagationCount++;
00197 continue;
00198 }
00199
00200 List path = graph.getExtendedBasicBlockPathBetween(def, stmt);
00201
00202 if(path == null)
00203 {
00204
00205 continue;
00206 }
00207
00208 Iterator pathIt = path.iterator();
00209
00210
00211 pathIt.next();
00212
00213
00214 {
00215 boolean isRedefined = false;
00216
00217 while(pathIt.hasNext())
00218 {
00219 Stmt s = (Stmt) pathIt.next();
00220
00221 if(stmt == s)
00222 {
00223
00224
00225
00226 break;
00227 }
00228 if(s instanceof DefinitionStmt)
00229 {
00230 if(((DefinitionStmt) s).getLeftOp() == m)
00231 {
00232 isRedefined = true;
00233 break;
00234 }
00235 }
00236 }
00237
00238 if(isRedefined)
00239 continue;
00240
00241 }
00242
00243 useBox.setValue(m);
00244 slowCopyPropagationCount++;
00245 }
00246 }
00247 }
00248 }
00249
00250 }
00251 }
00252 }
00253
00254
00255 if(Main.isVerbose)
00256 System.out.println("[" + stmtBody.getMethod().getName() +
00257 "] Propagated: " + constantPropagationCount + " constants " +
00258 fastCopyPropagationCount + " fast copies " +
00259 slowCopyPropagationCount + " slow copies");
00260
00261 if(Main.isProfilingOptimization)
00262 Main.propagatorTimer.end();
00263
00264 }
00265 }