KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > vna > ValueNumberCache


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,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.vna;
21
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.bcel.generic.InstructionHandle;
25
26 import edu.umd.cs.findbugs.SystemProperties;
27 import edu.umd.cs.findbugs.util.Strings;
28
29 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
30
31 /**
32  * A cache mapping instructions and input values to the output values they
33  * produce. We must always produce the same output given identical
34  * input, or else value number analysis will not terminate.
35  *
36  * @author David Hovemeyer
37  * @see ValueNumberAnalysis
38  */

39 public class ValueNumberCache {
40     private static final boolean DEBUG = SystemProperties.getBoolean("vn.debug");
41
42     /**
43      * An entry in the cache.
44      * It represents an instruction with specific input values.
45      */

46     public static class Entry {
47         public final InstructionHandle handle;
48         public final ValueNumber[] inputValueList;
49         private int cachedHashCode;
50
51         @SuppressWarnings JavaDoc("EI2")
52         public Entry(InstructionHandle handle, ValueNumber[] inputValueList) {
53             this.handle = handle;
54             this.inputValueList = inputValueList;
55             this.cachedHashCode = 0;
56         }
57
58         @Override JavaDoc
59                  public boolean equals(Object JavaDoc o) {
60             if (!(o instanceof Entry))
61                 return false;
62             Entry other = (Entry) o;
63             if (handle.getPosition() != other.handle.getPosition())
64                 return false;
65             ValueNumber[] myList = inputValueList;
66             ValueNumber[] otherList = other.inputValueList;
67             if (myList.length != otherList.length)
68                 return false;
69             for (int i = 0; i < myList.length; ++i)
70                 if (!myList[i].equals(otherList[i]))
71                     return false;
72             return true;
73         }
74
75         @Override JavaDoc
76                  public int hashCode() {
77             if (cachedHashCode == 0) {
78                 int code = handle.getPosition();
79                 for (ValueNumber aInputValueList : inputValueList) {
80                     code *= 101;
81                     ValueNumber valueNumber = aInputValueList;
82                     code += valueNumber.hashCode();
83                 }
84                 cachedHashCode = code;
85             }
86             return cachedHashCode;
87         }
88
89         @Override JavaDoc
90                  public String JavaDoc toString() {
91             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92             buf.append(handle.toString());
93             for (ValueNumber aInputValueList : inputValueList) {
94                 buf.append(", ");
95                 buf.append(aInputValueList.toString());
96             }
97             return buf.toString();
98         }
99     }
100
101     /**
102      * Map of entries to output values.
103      */

104     private HashMap JavaDoc<Entry, ValueNumber[]> entryToOutputMap = new HashMap JavaDoc<Entry, ValueNumber[]>();
105
106     /**
107      * Look up cached output values for given entry.
108      *
109      * @param entry the entry
110      * @return the list of output values, or null if there is no matching entry
111      * in the cache
112      */

113     public ValueNumber[] lookupOutputValues(Entry entry) {
114         if (DEBUG) System.out.println("VN cache lookup: " + entry);
115         ValueNumber[] result = entryToOutputMap.get(entry);
116         if (DEBUG) System.out.println(" result ==> " + Strings.toString(result));
117         return result;
118     }
119
120     /**
121      * Add output values for given entry.
122      * Assumes that lookupOutputValues() has determined that the entry
123      * is not in the cache.
124      *
125      * @param entry the entry
126      * @param outputValueList the list of output values produced
127      * by the entry's instruction and input values
128      */

129     public void addOutputValues(Entry entry, ValueNumber[] outputValueList) {
130         ValueNumber[] old = entryToOutputMap.put(entry, outputValueList);
131         if (old != null)
132             throw new IllegalStateException JavaDoc("overwriting output values for entry!");
133     }
134
135 }
136
137 // vim:ts=4
138
Popular Tags