KickJava   Java API By Example, From Geeks To Geeks.

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


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.toolkits.graph.*;
36 import soot.util.*;
37 import java.util.*;
38
39
40 /**
41  * Analysis that implements the LocalUses interface.
42  * Uses for a Local defined at a given Unit are returned as
43  * a list of UnitValueBoxPairs each containing a Unit that use the
44  * local and the Local itself wrapped in a ValueBox.
45  */

46 public class SimpleLocalUses implements LocalUses
47 {
48     Map unitToUses;
49
50     /**
51      * Construct the analysis from a UnitGraph representation
52      * of a method body and a LocalDefs interface. This supposes that
53      * a LocalDefs analysis must have been computed prior.
54      *
55      * <p> Note: If you do not already have a UnitGraph, it may be
56      * cheaper to use the constructor which only requires a Body.
57      */

58     public SimpleLocalUses(UnitGraph graph, LocalDefs localDefs)
59     {
60         this(graph.getBody(), localDefs);
61     }
62
63     /**
64      * Construct the analysis from a method body and a LocalDefs
65      * interface. This supposes that a LocalDefs analysis must have
66      * been computed prior.
67      */

68     public SimpleLocalUses(Body body, LocalDefs localDefs)
69     {
70         if(Options.v().time())
71            Timers.v().usesTimer.start();
72     
73         if(Options.v().time())
74            Timers.v().usePhase1Timer.start();
75         
76         if(Options.v().verbose())
77             G.v().out.println("[" + body.getMethod().getName() +
78                 "] Constructing SimpleLocalUses...");
79     
80     Chain units = body.getUnits();
81     
82         unitToUses = new HashMap(units.size() * 2 + 1, 0.7f);
83     
84         // Initialize this map to empty sets
85
{
86             Iterator it = units.iterator();
87
88             while(it.hasNext())
89             {
90                 Unit s = (Unit) it.next();
91                 unitToUses.put(s, new ArrayList());
92             }
93         }
94
95         if(Options.v().time())
96            Timers.v().usePhase1Timer.end();
97     
98         if(Options.v().time())
99            Timers.v().usePhase2Timer.start();
100     
101         // Traverse units and associate uses with definitions
102
{
103             Iterator it = units.iterator();
104
105             while(it.hasNext())
106             {
107                 Unit s = (Unit) it.next();
108
109                 Iterator boxIt = s.getUseBoxes().iterator();
110
111                 while(boxIt.hasNext())
112                 {
113                     ValueBox useBox = (ValueBox) boxIt.next();
114
115                     if(useBox.getValue() instanceof Local)
116                     {
117                         // Add this statement to the uses of the definition of the local
118

119                         Local l = (Local) useBox.getValue();
120
121                         List possibleDefs = localDefs.getDefsOfAt(l, s);
122                         Iterator defIt = possibleDefs.iterator();
123
124                         while(defIt.hasNext())
125                         {
126                             List useList = (List) unitToUses.get(defIt.next());
127                             useList.add(new UnitValueBoxPair(s, useBox));
128                         }
129                     }
130                 }
131             }
132         }
133
134         if(Options.v().time())
135            Timers.v().usePhase2Timer.end();
136     
137         if(Options.v().time())
138            Timers.v().usePhase3Timer.start();
139     
140         // Store the map as a bunch of unmodifiable lists.
141
{
142             Iterator it = units.iterator();
143             
144             while(it.hasNext())
145             {
146                 Unit s = (Unit) it.next();
147
148                 unitToUses.put(s, Collections.unmodifiableList(((List) unitToUses.get(s))));
149             }
150             
151         }
152         
153         if(Options.v().time())
154            Timers.v().usePhase3Timer.end();
155     
156         if(Options.v().time())
157             Timers.v().usesTimer.end();
158
159         if(Options.v().verbose())
160             G.v().out.println("[" + body.getMethod().getName() +
161                 "] finished SimpleLocalUses...");
162     }
163
164     /**
165      * Uses for a Local defined at a given Unit are returned as
166      * a list of UnitValueBoxPairs each containing a Unit that use the
167      * local and the Local itself wrapped in a ValueBox.
168      * @param s a unit that we want to query for the uses of the Local it (may) define.
169      * @return a UnitValueBoxPair of the Units that use the Local.
170      */

171     public List getUsesOf(Unit s)
172     {
173         List l = (List) unitToUses.get(s);
174
175         return l;
176     }
177 }
178
Popular Tags