KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31 package soot.toolkits.scalar;
32 import soot.options.*;
33
34 import soot.*;
35 import soot.util.*;
36 import java.util.*;
37 import soot.jimple.*;
38 import soot.toolkits.graph.*;
39
40
41 /**
42  * Analysis that provides an implementation of the LiveLocals interface.
43  */

44 public class SimpleLiveLocals implements LiveLocals
45 {
46     Map unitToLocalsAfter;
47     Map unitToLocalsBefore;
48
49
50
51     /**
52      * Computes the analysis given a UnitGraph computed from a
53      * method body. It is recommended that a ExceptionalUnitGraph (or
54      * similar) be provided for correct results in the case of
55      * exceptional control flow.
56      *
57      * @param g a graph on which to compute the analysis.
58      *
59      * @see ExceptionalUnitGraph
60      */

61     public SimpleLiveLocals(UnitGraph graph)
62     {
63         if(Options.v().time())
64             Timers.v().liveTimer.start();
65         
66         if(Options.v().verbose())
67             G.v().out.println("[" + graph.getBody().getMethod().getName() +
68                 "] Constructing SimpleLiveLocals...");
69
70                         
71         SimpleLiveLocalsAnalysis analysis = new SimpleLiveLocalsAnalysis(graph);
72
73         if(Options.v().time())
74                 Timers.v().livePostTimer.start();
75
76         // Build unitToLocals map
77
{
78             unitToLocalsAfter = new HashMap(graph.size() * 2 + 1, 0.7f);
79             unitToLocalsBefore = new HashMap(graph.size() * 2 + 1, 0.7f);
80
81             Iterator unitIt = graph.iterator();
82
83             while(unitIt.hasNext())
84             {
85                 Unit s = (Unit) unitIt.next();
86  
87                 FlowSet set = (FlowSet) analysis.getFlowBefore(s);
88                 unitToLocalsBefore.put(s, Collections.unmodifiableList(set.toList()));
89                 
90                 set = (FlowSet) analysis.getFlowAfter(s);
91                 unitToLocalsAfter.put(s, Collections.unmodifiableList(set.toList()));
92             }
93         }
94         
95         if(Options.v().time())
96             Timers.v().livePostTimer.end();
97         
98         if(Options.v().time())
99             Timers.v().liveTimer.end();
100     }
101
102     public List getLiveLocalsAfter(Unit s)
103     {
104         return (List) unitToLocalsAfter.get(s);
105     }
106     
107     public List getLiveLocalsBefore(Unit s)
108     {
109         return (List) unitToLocalsBefore.get(s);
110     }
111 }
112
113 class SimpleLiveLocalsAnalysis extends BackwardFlowAnalysis
114 {
115     FlowSet emptySet;
116     Map unitToGenerateSet;
117     Map unitToKillSet;
118
119     SimpleLiveLocalsAnalysis(UnitGraph g)
120     {
121         super(g);
122
123         if(Options.v().time())
124             Timers.v().liveSetupTimer.start();
125
126         emptySet = new ArraySparseSet();
127
128         // Create kill sets.
129
{
130             unitToKillSet = new HashMap(g.size() * 2 + 1, 0.7f);
131
132             Iterator unitIt = g.iterator();
133
134             while(unitIt.hasNext())
135             {
136                 Unit s = (Unit) unitIt.next();
137
138                 FlowSet killSet = (FlowSet) emptySet.clone();
139
140                 Iterator boxIt = s.getDefBoxes().iterator();
141
142                 while(boxIt.hasNext())
143                 {
144                     ValueBox box = (ValueBox) boxIt.next();
145
146                     if(box.getValue() instanceof Local)
147                         killSet.add(box.getValue(), killSet);
148                 }
149
150                     unitToKillSet.put(s, killSet);
151             }
152         }
153
154         // Create generate sets
155
{
156             unitToGenerateSet = new HashMap(g.size() * 2 + 1, 0.7f);
157
158             Iterator unitIt = g.iterator();
159
160             while(unitIt.hasNext())
161             {
162                 Unit s = (Unit) unitIt.next();
163
164                 FlowSet genSet = (FlowSet) emptySet.clone();
165
166                 Iterator boxIt = s.getUseBoxes().iterator();
167
168                 while(boxIt.hasNext())
169                 {
170                     ValueBox box = (ValueBox) boxIt.next();
171
172                     if(box.getValue() instanceof Local)
173                         genSet.add(box.getValue(), genSet);
174                 }
175
176                 unitToGenerateSet.put(s, genSet);
177             }
178         }
179
180         if(Options.v().time())
181             Timers.v().liveSetupTimer.end();
182
183         if(Options.v().time())
184             Timers.v().liveAnalysisTimer.start();
185
186         doAnalysis();
187         
188         if(Options.v().time())
189             Timers.v().liveAnalysisTimer.end();
190
191     }
192
193     protected Object JavaDoc newInitialFlow()
194     {
195         return emptySet.clone();
196     }
197
198     protected Object JavaDoc entryInitialFlow()
199     {
200         return emptySet.clone();
201     }
202         
203     protected void flowThrough(Object JavaDoc inValue, Object JavaDoc unit, Object JavaDoc outValue)
204     {
205         FlowSet in = (FlowSet) inValue, out = (FlowSet) outValue;
206
207         // Perform kill
208
in.difference((FlowSet) unitToKillSet.get(unit), out);
209
210         // Perform generation
211
out.union((FlowSet) unitToGenerateSet.get(unit), out);
212     }
213
214     protected void merge(Object JavaDoc in1, Object JavaDoc in2, Object JavaDoc out)
215     {
216         FlowSet inSet1 = (FlowSet) in1,
217             inSet2 = (FlowSet) in2;
218
219         FlowSet outSet = (FlowSet) out;
220
221         inSet1.union(inSet2, outSet);
222     }
223     
224     protected void copy(Object JavaDoc source, Object JavaDoc dest)
225     {
226         FlowSet sourceSet = (FlowSet) source,
227             destSet = (FlowSet) dest;
228             
229         sourceSet.copy(destSet);
230     }
231 }
232
Popular Tags