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
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 import ca.mcgill.sable.soot.*;
00082 import ca.mcgill.sable.util.*;
00083
00084 class LocalDefsFlowAnalysis extends ForwardFlowAnalysis
00085 {
00086 FlowSet emptySet;
00087 Map localToPreserveSet;
00088 Map localToIntPair;
00089
00090 public LocalDefsFlowAnalysis(StmtGraph g)
00091 {
00092 super(g);
00093
00094 Object[] defs;
00095 FlowUniverse defUniverse;
00096
00097 if(Main.isProfilingOptimization)
00098 Main.defsSetupTimer.start();
00099
00100
00101 {
00102 Map localToDefList = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00103
00104
00105 {
00106 Iterator localIt = g.getBody().getLocals().iterator();
00107
00108 while(localIt.hasNext())
00109 {
00110 Local l = (Local) localIt.next();
00111
00112 localToDefList.put(l, new ArrayList());
00113 }
00114 }
00115
00116
00117 {
00118 Iterator it = g.iterator();
00119
00120 while(it.hasNext())
00121 {
00122 Stmt s = (Stmt) it.next();
00123
00124 if(s instanceof DefinitionStmt)
00125 {
00126 DefinitionStmt d = (DefinitionStmt) s;
00127
00128 if(d.getLeftOp() instanceof Local)
00129 ((List) localToDefList.get(d.getLeftOp())).add(d);
00130
00131 }
00132 }
00133 }
00134
00135
00136 {
00137 Iterator it = localToDefList.keySet().iterator();
00138 List defList = new LinkedList();
00139
00140 int startPos = 0;
00141
00142 localToIntPair = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00143
00144
00145 {
00146 while(it.hasNext())
00147 {
00148 Local l = (Local) it.next();
00149 Iterator jt = ((List) localToDefList.get(l)).iterator();
00150
00151 int endPos = startPos - 1;
00152
00153 while(jt.hasNext())
00154 {
00155 defList.add(jt.next());
00156 endPos++;
00157 }
00158
00159 localToIntPair.put(l, new IntPair(startPos, endPos));
00160
00161
00162
00163 startPos = endPos + 1;
00164 }
00165 }
00166
00167 defs = defList.toArray();
00168 defUniverse = new FlowUniverse(defs);
00169 }
00170 }
00171
00172 emptySet = new ArrayPackedSet(defUniverse);
00173
00174
00175 {
00176 Map localToKillSet = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00177 localToPreserveSet = new HashMap(g.getBody().getLocalCount() * 2 + 1, 0.7f);
00178
00179 List locals = g.getBody().getLocals();
00180
00181
00182 {
00183 Iterator localIt = locals.iterator();
00184
00185 while(localIt.hasNext())
00186 {
00187 Local l = (Local) localIt.next();
00188
00189 localToKillSet.put(l, emptySet.clone());
00190 }
00191 }
00192
00193
00194 for(int i = 0; i < defs.length; i++)
00195 {
00196 DefinitionStmt d = (DefinitionStmt) defs[i];
00197
00198 if(d.getLeftOp() instanceof Local)
00199 {
00200 BoundedFlowSet killSet = (BoundedFlowSet) localToKillSet.get(d.getLeftOp());
00201
00202 killSet.add(d, killSet);
00203 }
00204 }
00205
00206
00207 {
00208 Iterator localIt = locals.iterator();
00209
00210 while(localIt.hasNext())
00211 {
00212 Local l = (Local) localIt.next();
00213
00214 BoundedFlowSet killSet = (BoundedFlowSet) localToKillSet.get(l);
00215
00216 killSet.complement(killSet);
00217
00218 localToPreserveSet.put(l, killSet);
00219 }
00220 }
00221 }
00222
00223 if(Main.isProfilingOptimization)
00224 Main.defsSetupTimer.end();
00225
00226 if(Main.isProfilingOptimization)
00227 Main.defsAnalysisTimer.start();
00228
00229 doAnalysis();
00230
00231 if(Main.isProfilingOptimization)
00232 Main.defsAnalysisTimer.end();
00233 }
00234 protected void copy(Object source, Object dest)
00235 {
00236 FlowSet sourceSet = (FlowSet) source,
00237 destSet = (FlowSet) dest;
00238
00239 sourceSet.copy(destSet);
00240 }
00241 protected void flowThrough(Object inValue, Stmt stmt, Object outValue)
00242 {
00243 FlowSet in = (FlowSet) inValue, out = (FlowSet) outValue;
00244
00245 if(stmt instanceof DefinitionStmt)
00246 {
00247 DefinitionStmt d = (DefinitionStmt) stmt;
00248
00249 if(!(d.getLeftOp() instanceof Local))
00250 {
00251 in.copy(out);
00252 return;
00253 }
00254
00255
00256 Local local = (Local) d.getLeftOp();
00257
00258
00259 in.intersection((FlowSet) localToPreserveSet.get(local), out);
00260
00261
00262 out.add(d, out);
00263
00264 }
00265 else
00266 in.copy(out);
00267 }
00268 protected void merge(Object in1, Object in2, Object out)
00269 {
00270 FlowSet inSet1 = (FlowSet) in1,
00271 inSet2 = (FlowSet) in2;
00272
00273 FlowSet outSet = (FlowSet) out;
00274
00275 inSet1.union(inSet2, outSet);
00276 }
00277 protected Object newInitialFlow()
00278 {
00279 return emptySet.clone();
00280 }
00281 }