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 LocalSplitter
00075 {
00076
00077 public static void splitLocals(JimpleBody listBody)
00078 {
00079 StmtList stmtList = listBody.getStmtList();
00080 List webs = new ArrayList();
00081
00082 if(Main.isVerbose)
00083 System.out.println("[" + listBody.getMethod().getName() + "] Splitting locals...");
00084
00085 Map boxToSet = new HashMap(stmtList.size() * 2 + 1, 0.7f);
00086
00087 if(Main.isProfilingOptimization)
00088 Main.splitPhase1Timer.start();
00089
00090
00091 {
00092 List code = stmtList;
00093
00094 CompleteStmtGraph graph = new CompleteStmtGraph(stmtList);
00095
00096 LocalDefs localDefs;
00097
00098 if(Main.usePackedDefs)
00099 {
00100 localDefs = new SimpleLocalDefs(graph);
00101 }
00102 else {
00103 LiveLocals liveLocals;
00104
00105 if(Main.usePackedLive)
00106 liveLocals = new SimpleLiveLocals(graph);
00107 else
00108 liveLocals = new SparseLiveLocals(graph);
00109
00110 localDefs = new SparseLocalDefs(graph, liveLocals);
00111 }
00112
00113 LocalUses localUses = new SimpleLocalUses(graph, localDefs);
00114
00115 if(Main.isProfilingOptimization)
00116 Main.splitPhase1Timer.end();
00117
00118 if(Main.isProfilingOptimization)
00119 Main.splitPhase2Timer.start();
00120
00121 Set markedBoxes = new HashSet();
00122 Map boxToStmt = new HashMap(stmtList.size() * 2 + 1, 0.7f);
00123
00124 Iterator codeIt = stmtList.iterator();
00125
00126 while(codeIt.hasNext())
00127 {
00128 Stmt s = (Stmt) codeIt.next();
00129
00130 if(!(s instanceof DefinitionStmt))
00131 continue;
00132
00133 DefinitionStmt def = (DefinitionStmt) s;
00134
00135 if(def.getLeftOp() instanceof Local && !markedBoxes.contains(def.getLeftOpBox()))
00136 {
00137 LinkedList defsToVisit = new LinkedList();
00138 LinkedList boxesToVisit = new LinkedList();
00139
00140 List web = new ArrayList();
00141 webs.add(web);
00142
00143 defsToVisit.add(def);
00144 markedBoxes.add(def.getLeftOpBox());
00145
00146 while(!boxesToVisit.isEmpty() || !defsToVisit.isEmpty())
00147 {
00148 if(!defsToVisit.isEmpty())
00149 {
00150 DefinitionStmt d = (DefinitionStmt) defsToVisit.removeFirst();
00151
00152 web.add(d.getLeftOpBox());
00153
00154
00155 {
00156 List uses = localUses.getUsesOf(d);
00157 Iterator useIt = uses.iterator();
00158
00159 while(useIt.hasNext())
00160 {
00161 StmtValueBoxPair use = (StmtValueBoxPair) useIt.next();
00162
00163 if(!markedBoxes.contains(use.valueBox))
00164 {
00165 markedBoxes.add(use.valueBox);
00166 boxesToVisit.addLast(use.valueBox);
00167 boxToStmt.put(use.valueBox, use.stmt);
00168 }
00169 }
00170 }
00171 }
00172 else {
00173 ValueBox box = (ValueBox) boxesToVisit.removeFirst();
00174
00175 web.add(box);
00176
00177
00178 {
00179 List defs = localDefs.getDefsOfAt((Local) box.getValue(),
00180 (Stmt) boxToStmt.get(box));
00181 Iterator defIt = defs.iterator();
00182
00183 while(defIt.hasNext())
00184 {
00185 DefinitionStmt d = (DefinitionStmt) defIt.next();
00186
00187 if(!markedBoxes.contains(d.getLeftOpBox()))
00188 {
00189 markedBoxes.add(d.getLeftOpBox());
00190 defsToVisit.addLast(d);
00191 }
00192 }
00193 }
00194 }
00195 }
00196 }
00197 }
00198 }
00199
00200
00201 {
00202 Map localToUseCount = new HashMap(listBody.getLocalCount() * 2 + 1, 0.7f);
00203 Iterator webIt = webs.iterator();
00204
00205 while(webIt.hasNext())
00206 {
00207 List web = (List) webIt.next();
00208
00209 ValueBox rep = (ValueBox) web.get(0);
00210 Local desiredLocal = (Local) rep.getValue();
00211
00212 if(!localToUseCount.containsKey(desiredLocal))
00213 {
00214
00215
00216 localToUseCount.put(desiredLocal, new Integer(1));
00217 }
00218 else {
00219
00220
00221 int useCount = ((Integer) localToUseCount.get(desiredLocal)).intValue() + 1;
00222 localToUseCount.put(desiredLocal, new Integer(useCount));
00223
00224 Local local = new Local(desiredLocal.getName() + "$" + useCount, desiredLocal.getType());
00225
00226 listBody.addLocal(local);
00227
00228
00229 {
00230 Iterator j = web.iterator();
00231
00232 while(j.hasNext())
00233 {
00234 ValueBox box = (ValueBox) j.next();
00235
00236 box.setValue(local);
00237 }
00238 }
00239 }
00240 }
00241 }
00242
00243 if(Main.isProfilingOptimization)
00244 Main.splitPhase2Timer.end();
00245
00246 }
00247 }