KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > pre > UpSafetyAnalysis


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

20
21 /*
22  * Modified by the Sable Research Group and others 1997-2002.
23  * See the 'credits' file distributed with Soot for the complete list of
24  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
25  */

26
27
28 package soot.jimple.toolkits.scalar.pre;
29 import soot.*;
30 import soot.toolkits.scalar.*;
31 import soot.toolkits.graph.*;
32 import soot.jimple.toolkits.scalar.*;
33 import soot.jimple.*;
34 import java.util.*;
35 import soot.util.*;
36
37 /**
38  * Performs an UpSafe-analysis on the given graph.
39  * An expression is upsafe, if the computation already has been performed on
40  * every path from START to the given program-point.
41  */

42 public class UpSafetyAnalysis extends ForwardFlowAnalysis {
43   private SideEffectTester sideEffect;
44
45   private Map unitToGenerateMap;
46
47   private BoundedFlowSet set;
48
49   /**
50    * this constructor should not be used, and will throw a runtime-exception!
51    */

52   public UpSafetyAnalysis(DirectedGraph dg) {
53     /* we have to add super(dg). otherwise Javac complains. */
54     super(dg);
55     throw new RuntimeException JavaDoc("Don't use this Constructor!");
56   }
57
58   /**
59    * this constructor automaticly performs the UpSafety-analysis.<br>
60    * the result of the analysis is as usual in FlowBefore (getFlowBefore())
61    * and FlowAfter (getFlowAfter()).<br>
62    *
63    * @param dg a ExceptionalUnitGraph
64    * @param unitToGen the EquivalentValue of each unit.
65    * @param sideEffect the SideEffectTester that will be used to perform kills.
66    */

67   public UpSafetyAnalysis(DirectedGraph dg, Map unitToGen, SideEffectTester
68                           sideEffect) {
69     this(dg, unitToGen, sideEffect, new
70       ArrayPackedSet(new CollectionFlowUniverse(unitToGen.values())));
71   }
72
73   /**
74    * this constructor automaticly performs the UpSafety-analysis.<br>
75    * the result of the analysis is as usual in FlowBefore (getFlowBefore())
76    * and FlowAfter (getFlowAfter()).<br>
77    * As usually flowset-operations are more efficient if shared, this allows to
78    * share sets over several analyses.
79    *
80    * @param dg a ExceptionalUnitGraph
81    * @param unitToGen the EquivalentValue of each unit.
82    * @param sideEffect the SideEffectTester that will be used to perform kills.
83    * @param set a bounded flow-set.
84    */

85   public UpSafetyAnalysis(DirectedGraph dg, Map unitToGen, SideEffectTester
86               sideEffect, BoundedFlowSet set) {
87     super(dg);
88     this.sideEffect = sideEffect;
89     UnitGraph g = (UnitGraph)dg;
90     this.set = set;
91     unitToGenerateMap = unitToGen;
92     doAnalysis();
93   }
94
95   protected Object JavaDoc newInitialFlow() {
96     return set.topSet();
97   }
98
99   protected Object JavaDoc entryInitialFlow() {
100     return set.emptySet();
101   }
102
103   protected void flowThrough(Object JavaDoc inValue, Object JavaDoc unit, Object JavaDoc outValue) {
104     FlowSet in = (FlowSet) inValue, out = (FlowSet) outValue;
105
106     in.copy(out);
107
108     // Perform generation
109
Value add = (Value)unitToGenerateMap.get(unit);
110     if (add != null)
111       out.add(add, out);
112
113     { /* Perform kill */
114       Unit u = (Unit)unit;
115
116       Iterator outIt = ((FlowSet)out).iterator();
117
118       // iterate over things (avail) in out set.
119
while (outIt.hasNext()) {
120         EquivalentValue equiVal = (EquivalentValue)outIt.next();
121         Value avail = equiVal.getValue();
122         if (avail instanceof FieldRef) {
123           if (sideEffect.unitCanWriteTo(u, avail))
124             outIt.remove();
125         } else {
126           Iterator usesIt = avail.getUseBoxes().iterator();
127
128           // iterate over uses in each avail.
129
while (usesIt.hasNext()) {
130             Value use = ((ValueBox)usesIt.next()).getValue();
131             if (sideEffect.unitCanWriteTo(u, use)) {
132               outIt.remove();
133               break;
134             }
135           }
136         }
137       }
138     }
139   }
140
141   protected void merge(Object JavaDoc in1, Object JavaDoc in2, Object JavaDoc out) {
142     FlowSet inSet1 = (FlowSet) in1;
143     FlowSet inSet2 = (FlowSet) in2;
144
145     FlowSet outSet = (FlowSet) out;
146
147     inSet1.intersection(inSet2, outSet);
148   }
149
150   protected void copy(Object JavaDoc source, Object JavaDoc dest) {
151     FlowSet sourceSet = (FlowSet) source;
152     FlowSet destSet = (FlowSet) dest;
153
154     sourceSet.copy(destSet);
155   }
156 }
157
158
Popular Tags