KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > FastAvailableExpressions


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2000 Patrick Lam
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 package soot.jimple.toolkits.scalar;
28 import soot.options.*;
29 import soot.*;
30 import soot.toolkits.scalar.*;
31 import soot.toolkits.graph.*;
32 import soot.jimple.*;
33 import java.util.*;
34 import soot.util.*;
35
36 /** Provides an user-interface for the AvailableExpressionsAnalysis class.
37  * Returns, for each statement, the list of expressions available before and after it. */

38 public class FastAvailableExpressions implements AvailableExpressions
39 {
40     Map unitToPairsAfter;
41     Map unitToPairsBefore;
42     Map unitToEquivsAfter;
43     Map unitToEquivsBefore;
44
45     /** Wrapper for AvailableExpressionsAnalysis. */
46     public FastAvailableExpressions(Body b, SideEffectTester st)
47     {
48         if(Options.v().verbose())
49             G.v().out.println("[" + b.getMethod().getName() +
50                 "] Finding available expressions...");
51
52         FastAvailableExpressionsAnalysis analysis =
53             new FastAvailableExpressionsAnalysis(new ExceptionalUnitGraph(b),
54             b.getMethod(), st);
55
56         // Build unitToExprs map
57
{
58             unitToPairsAfter = new HashMap(b.getUnits().size() * 2 + 1, 0.7f);
59             unitToPairsBefore = new HashMap(b.getUnits().size() * 2 + 1, 0.7f);
60             unitToEquivsAfter = new HashMap(b.getUnits().size() * 2 + 1, 0.7f);
61             unitToEquivsBefore = new HashMap(b.getUnits().size() * 2 + 1, 0.7f);
62
63             Iterator unitIt = b.getUnits().iterator();
64
65             while(unitIt.hasNext())
66             {
67                 Unit s = (Unit) unitIt.next();
68  
69                 FlowSet set = (FlowSet) analysis.getFlowBefore(s);
70
71                 List pairsBefore = new ArrayList();
72                 List pairsAfter = new ArrayList();
73
74                 Chain equivsBefore = new HashChain();
75                 Chain equivsAfter = new HashChain();
76
77                 if (set instanceof ToppedSet &&
78                             ((ToppedSet)set).isTop())
79                     throw new RuntimeException JavaDoc("top! on "+s);
80
81                 List setAsList = set.toList();
82                 Iterator si = setAsList.iterator();
83                 while (si.hasNext())
84                 {
85                     Value v = (Value)si.next();
86                     Stmt containingStmt = (Stmt)analysis.rhsToContainingStmt.get(v);
87                     UnitValueBoxPair p = new UnitValueBoxPair
88                         (containingStmt, ((AssignStmt)containingStmt).getRightOpBox());
89                     pairsBefore.add(p);
90
91                     EquivalentValue ev = new EquivalentValue(v);
92                     if (!equivsBefore.contains(ev))
93                         equivsBefore.add(ev);
94                 }
95
96                 unitToPairsBefore.put(s, pairsBefore);
97                 unitToEquivsBefore.put(s, equivsBefore);
98  
99                 set = (FlowSet) analysis.getFlowAfter(s);
100                 setAsList = set.toList();
101                 si = setAsList.iterator();
102                 while (si.hasNext())
103                 {
104                     Value v = (Value)si.next();
105                     Stmt containingStmt = (Stmt)analysis.rhsToContainingStmt.get(v);
106                     UnitValueBoxPair p = new UnitValueBoxPair
107                         (containingStmt, ((AssignStmt)containingStmt).getRightOpBox());
108                     pairsAfter.add(p);
109
110                     EquivalentValue ev = new EquivalentValue(v);
111                     if (!equivsAfter.contains(ev))
112                         equivsAfter.add(ev);
113                 }
114
115                 unitToPairsAfter.put(s, pairsAfter);
116                 unitToEquivsAfter.put(s, equivsAfter);
117             }
118         }
119
120         if(Options.v().verbose())
121             G.v().out.println("[" + b.getMethod().getName() +
122                 "] Found available expressions...");
123     }
124
125     /** Returns a List containing the UnitValueBox pairs corresponding to expressions available before u. */
126     public List getAvailablePairsBefore(Unit u)
127     {
128         return (List)unitToPairsBefore.get(u);
129     }
130
131     /** Returns a Chain containing the EquivalentValue objects corresponding to expressions available before u. */
132     public Chain getAvailableEquivsBefore(Unit u)
133     {
134         return (Chain)unitToEquivsBefore.get(u);
135     }
136
137     /** Returns a List containing the EquivalentValue corresponding to expressions available after u. */
138     public List getAvailablePairsAfter(Unit u)
139     {
140         return (List)unitToPairsAfter.get(u);
141     }
142
143     /** Returns a List containing the UnitValueBox pairs corresponding to expressions available after u. */
144     public Chain getAvailableEquivsAfter(Unit u)
145     {
146         return (Chain)unitToEquivsAfter.get(u);
147     }
148 }
149
150
Popular Tags