| 1 19 20 package edu.umd.cs.findbugs.ba.vna; 21 22 import java.util.HashMap ; 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 39 public class ValueNumberCache { 40 private static final boolean DEBUG = SystemProperties.getBoolean("vn.debug"); 41 42 46 public static class Entry { 47 public final InstructionHandle handle; 48 public final ValueNumber[] inputValueList; 49 private int cachedHashCode; 50 51 @SuppressWarnings ("EI2") 52 public Entry(InstructionHandle handle, ValueNumber[] inputValueList) { 53 this.handle = handle; 54 this.inputValueList = inputValueList; 55 this.cachedHashCode = 0; 56 } 57 58 @Override  59 public boolean equals(Object 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  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  90 public String toString() { 91 StringBuffer buf = new StringBuffer (); 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 104 private HashMap <Entry, ValueNumber[]> entryToOutputMap = new HashMap <Entry, ValueNumber[]>(); 105 106 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 129 public void addOutputValues(Entry entry, ValueNumber[] outputValueList) { 130 ValueNumber[] old = entryToOutputMap.put(entry, outputValueList); 131 if (old != null) 132 throw new IllegalStateException ("overwriting output values for entry!"); 133 } 134 135 } 136 137 | Popular Tags |