KickJava   Java API By Example, From Geeks To Geeks.

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


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
39
40
41
42 /**
43  * A BodyTransformer that attemps to minimize the number of local variables used in
44  * Body by 'reusing' them when possible. Implemented as a singleton.
45  * For example the code:
46  *
47  * for(int i; i < k; i++);
48  * for(int j; j < k; j++);
49  *
50  * would be transformed into:
51  * for(int i; i < k; i++);
52  * for(int i; i < k; i++);
53  *
54  * assuming to further conflicting uses of i and j.
55  *
56  * Note: LocalSplitter is corresponds to the inverse transformation.
57  *
58  * @see BodyTransformer
59  * @see Body
60  * @see LocalSplitter
61  */

62 public class LocalPacker extends BodyTransformer
63 {
64     public LocalPacker( Singletons.Global g ) {}
65     public static LocalPacker v() { return G.v().soot_toolkits_scalar_LocalPacker(); }
66
67     protected void internalTransform(Body body, String JavaDoc phaseName, Map options)
68     {
69         boolean isUnsplit = PhaseOptions.getBoolean(options, "unsplit-original-locals");
70         
71         if(Options.v().verbose())
72             G.v().out.println("[" + body.getMethod().getName() + "] Packing locals...");
73     
74         Map localToGroup = new DeterministicHashMap(body.getLocalCount() * 2 + 1, 0.7f);
75             // A group represents a bunch of locals which may potentially intefere with each other
76
// 2 separate groups can not possibly interfere with each other
77
// (coloring say ints and doubles)
78

79         Map groupToColorCount = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
80         Map localToColor = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
81         Map localToNewLocal;
82         
83         // Assign each local to a group, and set that group's color count to 0.
84
{
85             Iterator localIt = body.getLocals().iterator();
86
87             while(localIt.hasNext())
88             {
89                 Local l = (Local) localIt.next();
90                 Object JavaDoc g = l.getType();
91                 
92                 localToGroup.put(l, g);
93                 
94                 if(!groupToColorCount.containsKey(g))
95                 {
96                     groupToColorCount.put(g, new Integer JavaDoc(0));
97                 }
98             }
99         }
100
101         // Assign colors to the parameter locals.
102
{
103             Iterator codeIt = body.getUnits().iterator();
104
105             while(codeIt.hasNext())
106             {
107                 Unit s = (Unit) codeIt.next();
108
109                 if(s instanceof IdentityUnit &&
110                     ((IdentityUnit) s).getLeftOp() instanceof Local)
111                 {
112                     Local l = (Local) ((IdentityUnit) s).getLeftOp();
113                     
114                     Object JavaDoc group = localToGroup.get(l);
115                     int count = ((Integer JavaDoc) groupToColorCount.get(group)).intValue();
116                     
117                     localToColor.put(l, new Integer JavaDoc(count));
118                     
119                     count++;
120                     
121                     groupToColorCount.put(group, new Integer JavaDoc(count));
122                 }
123             }
124         }
125         
126         // Call the graph colorer.
127
if(isUnsplit)
128                 FastColorer.unsplitAssignColorsToLocals(body, localToGroup,
129                     localToColor, groupToColorCount);
130             else
131                 FastColorer.assignColorsToLocals(body, localToGroup,
132                     localToColor, groupToColorCount);
133
134                                     
135         // Map each local to a new local.
136
{
137             List originalLocals = new ArrayList();
138             localToNewLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
139             Map groupIntToLocal = new HashMap(body.getLocalCount() * 2 + 1, 0.7f);
140             
141             originalLocals.addAll(body.getLocals());
142             body.getLocals().clear();
143
144             Iterator localIt = originalLocals.iterator();
145
146             while(localIt.hasNext())
147             {
148                 Local original = (Local) localIt.next();
149                 
150                 Object JavaDoc group = localToGroup.get(original);
151                 int color = ((Integer JavaDoc) localToColor.get(original)).intValue();
152                 GroupIntPair pair = new GroupIntPair(group, color);
153                 
154                 Local newLocal;
155                 
156                 if(groupIntToLocal.containsKey(pair))
157                     newLocal = (Local) groupIntToLocal.get(pair);
158                 else {
159                     newLocal = (Local) original.clone();
160                     newLocal.setType((Type) group);
161
162             // Icky fix. But I guess it works. -PL
163
// It is no substitute for really understanding the
164
// problem, though. I'll leave that to someone
165
// who really understands the local naming stuff.
166
// Does such a person exist?
167

168             int signIndex = newLocal.getName().indexOf("#");
169                 
170             if(signIndex != -1)
171             newLocal.setName(newLocal.getName().substring(0, signIndex));
172                     
173                     groupIntToLocal.put(pair, newLocal);
174                     body.getLocals().add(newLocal);
175                 }
176                 
177                 localToNewLocal.put(original, newLocal);
178             }
179         }
180
181         
182         // Go through all valueBoxes of this method and perform changes
183
{
184             Iterator codeIt = body.getUnits().iterator();
185
186             while(codeIt.hasNext())
187             {
188                 Unit s = (Unit) codeIt.next();
189
190                 Iterator boxIt;
191                 boxIt = s.getUseBoxes().iterator();
192                 while(boxIt.hasNext())
193                 {
194                     ValueBox box = (ValueBox) boxIt.next();
195
196                     if(box.getValue() instanceof Local)
197                     {
198                         Local l = (Local) box.getValue();
199                         box.setValue((Local) localToNewLocal.get(l));
200                     }
201                 }
202                 boxIt = s.getDefBoxes().iterator();
203                 while(boxIt.hasNext())
204                 {
205                     ValueBox box = (ValueBox) boxIt.next();
206
207                     if(box.getValue() instanceof Local)
208                     {
209                         Local l = (Local) box.getValue();
210                         box.setValue((Local) localToNewLocal.get(l));
211                     }
212                 }
213             }
214         }
215     }
216 }
217
218
Popular Tags