KickJava   Java API By Example, From Geeks To Geeks.

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


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 DownSafe-analysis on the given graph.
39  * An expression is downsafe, if the computation will occur on every path from
40  * the current point down to the END.
41  */

42 public class DownSafetyAnalysis extends BackwardFlowAnalysis {
43   private SideEffectTester sideEffect = null;
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 DownSafetyAnalysis(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 DownSafety-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 performs kills.
66    */

67   public DownSafetyAnalysis(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 DownSafety-analysis.<br>
75    * the result of the analysis is as usual in FlowBefore (getFlowBefore())
76    * and FlowAfter (getFlowAfter()).<br>
77    * as sets-operations are usually more efficient, if the original set comes
78    * from the same source, this allows to share sets.
79    *
80    * @param dg a ExceptionalUnitGraph.
81    * @param unitToGen the equivalentValue of each unit.
82    * @param sideEffect the SideEffectTester that performs kills.
83    * @param BoundedFlowSet the shared set.
84    */

85   public DownSafetyAnalysis(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 kill */
109       Unit u = (Unit)unit;
110
111       Iterator outIt = ((FlowSet)out).iterator();
112       // iterate over things (avail) in out set.
113
while (outIt.hasNext()) {
114         EquivalentValue equiVal = (EquivalentValue)outIt.next();
115         Value avail = equiVal.getValue();
116         if (avail instanceof FieldRef) {
117           if (sideEffect.unitCanWriteTo(u, avail))
118             outIt.remove();
119         } else {
120           Iterator usesIt = avail.getUseBoxes().iterator();
121
122           // iterate over uses in each avail.
123
while (usesIt.hasNext()) {
124             Value use = ((ValueBox)usesIt.next()).getValue();
125             if (sideEffect.unitCanWriteTo(u, use)) {
126               outIt.remove();
127               break;
128             }
129           }
130         }
131       }
132     }
133
134     // Perform generation
135
Value add = (Value)unitToGenerateMap.get(unit);
136     if (add != null)
137       out.add(add, out);
138   }
139
140   protected void merge(Object JavaDoc in1, Object JavaDoc in2, Object JavaDoc out) {
141     FlowSet inSet1 = (FlowSet) in1;
142     FlowSet inSet2 = (FlowSet) in2;
143
144     FlowSet outSet = (FlowSet) out;
145
146     inSet1.intersection(inSet2, outSet);
147   }
148
149   protected void copy(Object JavaDoc source, Object JavaDoc dest) {
150     FlowSet sourceSet = (FlowSet) source;
151     FlowSet destSet = (FlowSet) dest;
152
153     sourceSet.copy(destSet);
154   }
155 }
156
157
Popular Tags