KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > LiveLocalStoreAnalysis


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004, University of Maryland
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 Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.BitSet JavaDoc;
23
24 import org.apache.bcel.classfile.Method;
25 import org.apache.bcel.generic.IINC;
26 import org.apache.bcel.generic.IndexedInstruction;
27 import org.apache.bcel.generic.Instruction;
28 import org.apache.bcel.generic.InstructionHandle;
29 import org.apache.bcel.generic.LoadInstruction;
30 import org.apache.bcel.generic.LocalVariableInstruction;
31 import org.apache.bcel.generic.MethodGen;
32 import org.apache.bcel.generic.RET;
33 import org.apache.bcel.generic.StoreInstruction;
34
35 /**
36  * Dataflow analysis to find live stores of locals.
37  * This is just a backward analysis to see which loads
38  * reach stores of the same local.
39  *
40  * <p> This analysis also computes which stores that were
41  * killed by a subsequent store on any subsequent reachable path.
42  * (The FindDeadLocalStores detector uses this information
43  * to reduce false positives.)
44  *
45  * @author David Hovemeyer
46  */

47 public class LiveLocalStoreAnalysis extends BackwardDataflowAnalysis<BitSet JavaDoc>
48         implements Debug {
49     private int topBit ;
50     private int killedByStoreOffset;
51
52     public LiveLocalStoreAnalysis(MethodGen methodGen, ReverseDepthFirstSearch rdfs, DepthFirstSearch dfs) {
53         super(rdfs, dfs);
54         this.topBit = methodGen.getMaxLocals() * 2;
55         this.killedByStoreOffset = methodGen.getMaxLocals();
56     }
57
58     public BitSet JavaDoc createFact() {
59         return new BitSet JavaDoc();
60     }
61
62     public void copy(BitSet JavaDoc source, BitSet JavaDoc dest) {
63         dest.clear();
64         dest.or(source);
65     }
66
67     public void initEntryFact(BitSet JavaDoc result) throws DataflowAnalysisException {
68         result.clear();
69     }
70
71     public void initResultFact(BitSet JavaDoc result) {
72         makeFactTop(result);
73     }
74
75     public void makeFactTop(BitSet JavaDoc fact) {
76         fact.clear();
77         fact.set(topBit);
78     }
79
80     public boolean same(BitSet JavaDoc fact1, BitSet JavaDoc fact2) {
81         return fact1.equals(fact2);
82     }
83
84     public void meetInto(BitSet JavaDoc fact, Edge edge, BitSet JavaDoc result) throws DataflowAnalysisException {
85         isFactValid(fact);
86         isFactValid(result);
87
88         if (isTop(fact)) {
89             // Nothing to do, result stays the same
90
} else if (isTop(result)) {
91             // Result is top, so it takes the value of fact
92
copy(fact, result);
93         } else {
94             // Meet is union
95
result.or(fact);
96         }
97
98         isFactValid(result);
99     }
100
101     @Override JavaDoc
102          public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, BitSet JavaDoc fact)
103         throws DataflowAnalysisException {
104         isFactValid(fact);
105
106         Instruction ins = handle.getInstruction();
107
108         if (ins instanceof StoreInstruction || ins instanceof IINC) {
109             // Local is stored: any live stores on paths leading
110
// to this instruction are now dead
111

112             LocalVariableInstruction store = (LocalVariableInstruction) ins;
113             int local = store.getIndex();
114             fact.clear(local);
115             fact.set(local + killedByStoreOffset);
116         }
117
118         if (ins instanceof LoadInstruction || ins instanceof IINC || ins instanceof RET) {
119             // Local is loaded: it will be live on any path leading
120
// to this instruction
121

122             IndexedInstruction load = (IndexedInstruction) ins;
123             int local = load.getIndex();
124             fact.set(local);
125             fact.clear(local + killedByStoreOffset);
126         }
127
128         isFactValid(fact);
129     }
130
131     @Override JavaDoc
132          public boolean isFactValid(BitSet JavaDoc fact) {
133         if (VERIFY_INTEGRITY) {
134             if (isTop(fact) && fact.nextSetBit(0) < topBit)
135                 throw new IllegalStateException JavaDoc();
136         }
137         return !isTop(fact);
138     }
139
140     @Override JavaDoc
141          public String JavaDoc factToString(BitSet JavaDoc fact) {
142         if (isTop(fact))
143             return "[TOP]";
144         else
145             return fact.toString();
146     }
147
148     /**
149      * Return whether or not given fact is the special TOP value.
150      */

151     public boolean isTop(BitSet JavaDoc fact) {
152         return fact.get(topBit);
153     }
154
155     /**
156      * Return whether or not a store of given local is alive.
157      *
158      * @param fact a dataflow fact created by this analysis
159      * @param local the local
160      */

161     public boolean isStoreAlive(BitSet JavaDoc fact, int local) {
162         return fact.get(local);
163     }
164
165     /**
166      * Return whether or not a store of given local was killed
167      * by a subsequent (dominated) store.
168      */

169     public boolean killedByStore(BitSet JavaDoc fact, int local) {
170         return fact.get(local + killedByStoreOffset);
171     }
172
173     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
174         if (argv.length != 1) {
175             System.err.println("Usage: " + LiveLocalStoreAnalysis.class.getName() +
176                 " <classfile>");
177             System.exit(1);
178         }
179
180         String JavaDoc filename = argv[0];
181
182         DataflowTestDriver<BitSet JavaDoc,LiveLocalStoreAnalysis> driver =
183             new DataflowTestDriver<BitSet JavaDoc, LiveLocalStoreAnalysis>() {
184
185             @Override JavaDoc
186                          public Dataflow<BitSet JavaDoc, LiveLocalStoreAnalysis> createDataflow(ClassContext classContext, Method method)
187                     throws CFGBuilderException, DataflowAnalysisException {
188                 return classContext.getLiveLocalStoreDataflow(method);
189             }
190         };
191
192         driver.execute(filename);
193     }
194 }
195
196 // vim:ts=4
197
Popular Tags