KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > scalar > SmartLocalDefs


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.toolkits.scalar;
21 import soot.options.*;
22
23 import soot.jimple.*;
24 import soot.toolkits.graph.*;
25 import soot.*;
26 import soot.util.*;
27 import java.util.*;
28
29
30 /**
31  * Analysis that provides an implementation of the LocalDefs interface.
32  */

33 public class SmartLocalDefs implements LocalDefs
34 {
35     private final Map answer;
36
37     private final LiveLocals live;
38     private final Map localToDefs; // for each local, set of units
39
// where it's defined
40
private final UnitGraph graph;
41     private final LocalDefsAnalysis analysis;
42     private final Map unitToMask;
43     public SmartLocalDefs(UnitGraph g, LiveLocals live) {
44         this.live = live;
45         this.graph = g;
46
47         if(Options.v().time())
48             Timers.v().defsTimer.start();
49         
50         if(Options.v().verbose())
51             G.v().out.println("[" + g.getBody().getMethod().getName() +
52                                "] Constructing SmartLocalDefs...");
53
54         localToDefs = new HashMap();
55         unitToMask = new HashMap();
56         for( Iterator uIt = g.iterator(); uIt.hasNext(); ) {
57             final Unit u = (Unit) uIt.next();
58             Local l = localDef(u);
59             if( l == null ) continue;
60             HashSet s = defsOf(l);
61             s.add(u);
62         }
63
64         if(Options.v().verbose())
65             G.v().out.println("[" + g.getBody().getMethod().getName() +
66                                "] done localToDefs map..." );
67
68 outer:
69         for( Iterator uIt = g.iterator(); uIt.hasNext(); ) {
70             final Unit u = (Unit) uIt.next();
71             unitToMask.put(u, new HashSet(live.getLiveLocalsAfter(u)));
72         }
73
74         if(Options.v().verbose())
75             G.v().out.println("[" + g.getBody().getMethod().getName() +
76                                "] done unitToMask map..." );
77
78         analysis = new LocalDefsAnalysis(graph);
79
80         answer = new HashMap();
81         for( Iterator uIt = graph.iterator(); uIt.hasNext(); ) {
82             final Unit u = (Unit) uIt.next();
83             for( Iterator vbIt = u.getUseBoxes().iterator(); vbIt.hasNext(); ) {
84                 final ValueBox vb = (ValueBox) vbIt.next();
85                 Value v = vb.getValue();
86                 if( !(v instanceof Local) ) continue;
87                 HashSet analysisResult = (HashSet) analysis.getFlowBefore(u);
88                 ArrayList al = new ArrayList();
89                 for( Iterator unitIt = defsOf((Local)v).iterator(); unitIt.hasNext(); ) {
90                     final Unit unit = (Unit) unitIt.next();
91                     if(analysisResult.contains(unit)) al.add(unit);
92                 }
93                 answer.put(new Cons(u, v), al);
94             }
95         }
96         if(Options.v().time())
97             Timers.v().defsTimer.end();
98
99     if(Options.v().verbose())
100         G.v().out.println("[" + g.getBody().getMethod().getName() +
101                                "] SmartLocalDefs finished.");
102     }
103     private Local localDef(Unit u) {
104         List defBoxes = u.getDefBoxes();
105         if( defBoxes.size() == 0 ) return null;
106         if( defBoxes.size() != 1 ) throw new RuntimeException JavaDoc();
107         ValueBox vb = (ValueBox) defBoxes.get(0);
108         Value v = vb.getValue();
109         if( !(v instanceof Local) ) return null;
110         return (Local) v;
111     }
112     private HashSet defsOf( Local l ) {
113         HashSet ret = (HashSet)localToDefs.get(l);
114         if( ret == null ) localToDefs.put( l, ret = new HashSet() );
115         return ret;
116     }
117
118     class LocalDefsAnalysis extends ForwardFlowAnalysis {
119         LocalDefsAnalysis(UnitGraph g) {
120             super(g);
121             doAnalysis();
122         }
123         protected void merge(Object JavaDoc inoutO, Object JavaDoc inO) {
124             HashSet inout = (HashSet) inoutO;
125             HashSet in = (HashSet) inO;
126
127             inout.addAll(in);
128         }
129         protected void merge(Object JavaDoc in1, Object JavaDoc in2, Object JavaDoc out) {
130             HashSet inSet1 = (HashSet) in1;
131             HashSet inSet2 = (HashSet) in2;
132             HashSet outSet = (HashSet) out;
133
134             outSet.clear();
135             outSet.addAll(inSet1);
136             outSet.addAll(inSet2);
137         }
138         protected void flowThrough(Object JavaDoc inValue, Object JavaDoc unit, Object JavaDoc outValue) {
139             Unit u = (Unit) unit;
140             HashSet in = (HashSet) inValue;
141             HashSet out = (HashSet) outValue;
142             out.clear();
143             Set mask = (Set) unitToMask.get(u);
144             for( Iterator inUIt = in.iterator(); inUIt.hasNext(); ) {
145                 final Unit inU = (Unit) inUIt.next();
146                 if( mask.contains(localDef(inU)) ) out.add(inU);
147             }
148             Local l = localDef(u);
149             if( l != null ) {
150                 out.removeAll(defsOf(l));
151                 if(mask.contains(localDef(u))) out.add(u);
152             }
153         }
154     
155         protected void copy(Object JavaDoc source, Object JavaDoc dest) {
156             HashSet sourceSet = (HashSet) source;
157             HashSet destSet = (HashSet) dest;
158                 
159             destSet.clear();
160             destSet.addAll(sourceSet);
161         }
162
163         protected Object JavaDoc newInitialFlow() {
164             return new HashSet();
165         }
166
167         protected Object JavaDoc entryInitialFlow() {
168             return new HashSet();
169         }
170     }
171
172     public List getDefsOfAt(Local l, Unit s)
173     {
174         return (List) answer.get(new Cons(s, l));
175     }
176
177 }
178
179
Popular Tags