1 package Analysis.Disjoint;
9 public class ReachState extends Canonical {
11 private HashSet<ReachTuple> tokenTuples;
15 tokenTuples = new HashSet<ReachTuple>();
18 public ReachState(ReachTuple tt) {
24 public ReachState(ReachState tts) {
26 // okay to clone, ReachTuple and ReachState should be canonical
27 tokenTuples = (HashSet<ReachTuple>)tts.tokenTuples.clone();
31 public ReachState makeCanonical() {
32 return (ReachState) Canonical.makeCanonical(this);
35 public Iterator iterator() {
36 return tokenTuples.iterator();
39 public boolean isEmpty() {
40 return tokenTuples.isEmpty();
43 public boolean isSubset(ReachState ttsIn) {
45 return ttsIn.tokenTuples.containsAll(this.tokenTuples);
48 public boolean containsTuple(ReachTuple tt) {
50 return tokenTuples.contains(tt);
53 public boolean containsBoth(ReachTuple tt1, ReachTuple tt2) {
54 return containsTuple(tt1) && containsTuple(tt2);
57 public boolean containsWithZeroes(ReachState tts) {
60 // first establish that every token tuple from tts is
62 Iterator<ReachTuple> ttItrIn = tts.iterator();
63 while( ttItrIn.hasNext() ) {
64 ReachTuple ttIn = ttItrIn.next();
65 ReachTuple ttThis = this.containsToken(ttIn.getToken() );
67 if( ttThis == null ) {
72 // then establish that anything in this set that is
73 // not in tts is a zero-arity token tuple, which is okay
74 Iterator<ReachTuple> ttItrThis = this.iterator();
75 while( ttItrThis.hasNext() ) {
76 ReachTuple ttThis = ttItrThis.next();
77 ReachTuple ttIn = tts.containsToken(ttThis.getToken() );
80 ttThis.getArity() != ReachTuple.ARITY_ZEROORMORE ) {
85 // if so this set contains tts with zeroes
89 public ReachState union(ReachTuple ttIn) {
91 ReachOperation ro=new ReachOperation(this, ttIn);
92 if (unionhash.containsKey(ro))
93 return (ReachState) unionhash.get(ro).c;
95 ReachState ttsOut = new ReachState(this);
96 ttsOut.tokenTuples.add(ttIn);
97 ro.c=ttsOut=ttsOut.makeCanonical();
103 public ReachState union(ReachState ttsIn) {
104 assert ttsIn != null;
105 ReachOperation ro=new ReachOperation(this, ttsIn);
106 if (unionhash.containsKey(ro)) {
107 return (ReachState) unionhash.get(ro).c;
109 ReachState ttsOut = new ReachState(this);
110 ttsOut.tokenTuples.addAll(ttsIn.tokenTuples);
111 ro.c=ttsOut=ttsOut.makeCanonical();
112 unionhash.put(ro,ro);
118 public ReachState unionUpArity(ReachState ttsIn) {
119 assert ttsIn != null;
120 ReachState ttsOut = new ReachState();
122 Iterator<ReachTuple> ttItr = this.iterator();
123 while( ttItr.hasNext() ) {
124 ReachTuple ttThis = ttItr.next();
125 ReachTuple ttIn = ttsIn.containsToken(ttThis.getToken() );
128 ttsOut.tokenTuples.add(ttThis.unionArity(ttIn) );
130 ttsOut.tokenTuples.add(ttThis);
134 ttItr = ttsIn.iterator();
135 while( ttItr.hasNext() ) {
136 ReachTuple ttIn = ttItr.next();
137 ReachTuple ttThis = ttsOut.containsToken(ttIn.getToken() );
139 if( ttThis == null ) {
140 ttsOut.tokenTuples.add(ttIn);
144 return ttsOut.makeCanonical();
148 public ReachState add(ReachTuple tt) {
150 return this.union(tt);
154 public ReachState remove(ReachTuple tt) {
156 ReachState ttsOut = new ReachState(this);
157 ttsOut.tokenTuples.remove(tt);
158 return ttsOut.makeCanonical();
162 public boolean equals(Object o) {
167 if( !(o instanceof ReachState) ) {
171 ReachState tts = (ReachState) o;
172 return tokenTuples.equals(tts.tokenTuples);
175 boolean hashcodecomputed=false;
179 public int hashCode() {
180 if (hashcodecomputed)
183 ourhashcode=tokenTuples.hashCode();
184 hashcodecomputed=true;
190 // this should be a hash table so we can do this by key
191 public ReachTuple containsToken(Integer token) {
192 assert token != null;
194 Iterator itr = tokenTuples.iterator();
195 while( itr.hasNext() ) {
196 ReachTuple tt = (ReachTuple) itr.next();
197 if( token.equals(tt.getToken() ) ) {
205 public ReachState ageTokens(AllocSite as) {
208 ReachState ttsOut = new ReachState();
210 ReachTuple ttSummary = null;
211 ReachTuple ttOldest = null;
213 Iterator itrT = this.iterator();
214 while( itrT.hasNext() ) {
215 ReachTuple tt = (ReachTuple) itrT.next();
217 Integer token = tt.getToken();
218 int age = as.getAgeCategory(token);
220 // tokens not associated with
221 // the site should be left alone
222 if( age == AllocSite.AGE_notInThisSite ) {
223 ttsOut.tokenTuples.add(tt);
225 } else if( age == AllocSite.AGE_summary ) {
226 // remember the summary tuple, but don't add it
227 // we may combine it with the oldest tuple
230 } else if( age == AllocSite.AGE_oldest ) {
231 // found an oldest token, again just remember
236 assert age == AllocSite.AGE_in_I;
238 Integer I = as.getAge(token);
241 // otherwise, we change this token to the
243 Integer tokenToChangeTo = as.getIthOldest(I + 1);
244 ReachTuple ttAged = tt.changeTokenTo(tokenToChangeTo);
245 ttsOut.tokenTuples.add(ttAged);
249 // there are four cases to consider here
250 // 1. we found a summary tuple and no oldest tuple
251 // Here we just pass the summary unchanged
252 // 2. we found an oldest tuple, no summary
253 // Make a new, arity-one summary tuple
254 // 3. we found both a summary and an oldest
255 // Merge them by arity
256 // 4. (not handled) we found neither, do nothing
257 if ( ttSummary != null && ttOldest == null ) {
258 ttsOut.tokenTuples.add(ttSummary);
260 } else if( ttSummary == null && ttOldest != null ) {
261 ttsOut.tokenTuples.add(new ReachTuple(as.getSummary(),
267 } else if( ttSummary != null && ttOldest != null ) {
268 ttsOut.tokenTuples.add(ttSummary.unionArity(new ReachTuple(as.getSummary(),
276 return ttsOut.makeCanonical();
280 public ReachState unshadowTokens(AllocSite as) {
283 ReachState ttsOut = new ReachState();
285 ReachTuple ttSummary = null;
286 ReachTuple ttShadowSummary = null;
288 Iterator itrT = this.iterator();
289 while( itrT.hasNext() ) {
290 ReachTuple tt = (ReachTuple) itrT.next();
292 Integer token = tt.getToken();
293 int shadowAge = as.getShadowAgeCategory(token);
295 if( shadowAge == AllocSite.AGE_summary ) {
296 // remember the summary tuple, but don't add it
297 // we may combine it with the oldest tuple
300 } else if( shadowAge == AllocSite.SHADOWAGE_notInThisSite ) {
301 ttsOut.tokenTuples.add(tt);
303 } else if( shadowAge == AllocSite.SHADOWAGE_summary ) {
304 // found the shadow summary token, again just remember
306 ttShadowSummary = tt;
308 } else if( shadowAge == AllocSite.SHADOWAGE_oldest ) {
309 Integer tokenToChangeTo = as.getOldest();
310 ReachTuple ttNormal = tt.changeTokenTo(tokenToChangeTo);
311 ttsOut.tokenTuples.add(ttNormal);
314 assert shadowAge == AllocSite.SHADOWAGE_in_I;
316 Integer I = as.getShadowAge(token);
319 Integer tokenToChangeTo = as.getIthOldest(-I);
320 ReachTuple ttNormal = tt.changeTokenTo(tokenToChangeTo);
321 ttsOut.tokenTuples.add(ttNormal);
325 if ( ttSummary != null && ttShadowSummary == null ) {
326 ttsOut.tokenTuples.add(ttSummary);
328 } else if( ttSummary == null && ttShadowSummary != null ) {
329 ttsOut.tokenTuples.add(new ReachTuple(as.getSummary(),
331 ttShadowSummary.getArity()
335 } else if( ttSummary != null && ttShadowSummary != null ) {
336 ttsOut.tokenTuples.add(ttSummary.unionArity(new ReachTuple(as.getSummary(),
338 ttShadowSummary.getArity()
344 return ttsOut.makeCanonical();
348 public ReachState toShadowTokens(AllocSite as) {
351 ReachState ttsOut = new ReachState().makeCanonical();
353 Iterator itrT = this.iterator();
354 while( itrT.hasNext() ) {
355 ReachTuple tt = (ReachTuple) itrT.next();
357 Integer token = tt.getToken();
358 int age = as.getAgeCategory(token);
360 // summary tokens and tokens not associated with
361 // the site should be left alone
362 if( age == AllocSite.AGE_notInThisSite ) {
363 ttsOut = ttsOut.union(tt);
365 } else if( age == AllocSite.AGE_summary ) {
366 ttsOut = ttsOut.union(tt.changeTokenTo(as.getSummaryShadow() ));
368 } else if( age == AllocSite.AGE_oldest ) {
369 ttsOut = ttsOut.union(tt.changeTokenTo(as.getOldestShadow() ));
372 assert age == AllocSite.AGE_in_I;
374 Integer I = as.getAge(token);
377 ttsOut = ttsOut.union(tt.changeTokenTo(as.getIthOldestShadow(I) ));
381 return ttsOut.makeCanonical();
385 public ReachSet rewriteToken(ReachTuple tokenToRewrite,
386 ReachSet replacements,
387 boolean makeChangeSet,
388 Hashtable<ReachState, HashSet<ReachState> > forChangeSet) {
390 ReachSet rsOut = new ReachSet().makeCanonical();
392 if( !tokenTuples.contains(tokenToRewrite) ) {
393 rsOut = rsOut.add(this);
396 ReachState ttsMinusToken = new ReachState(this);
397 ttsMinusToken.tokenTuples.remove(tokenToRewrite);
399 Iterator<ReachState> replaceItr = replacements.iterator();
400 while( replaceItr.hasNext() ) {
401 ReachState replacement = replaceItr.next();
402 ReachState replaced = new ReachState(ttsMinusToken).makeCanonical();
403 replaced = replaced.unionUpArity(replacement);
404 rsOut = rsOut.add(replaced);
406 if( makeChangeSet ) {
407 assert forChangeSet != null;
409 if( forChangeSet.get(this) == null ) {
410 forChangeSet.put(this, new HashSet<ReachState>() );
413 forChangeSet.get(this).add(replaced);
418 return rsOut.makeCanonical();
422 public ReachState makeArityZeroOrMore() {
423 ReachState ttsOut = new ReachState().makeCanonical();
425 Iterator<ReachTuple> itrThis = this.iterator();
426 while( itrThis.hasNext() ) {
427 ReachTuple tt = itrThis.next();
429 ttsOut = ttsOut.union(new ReachTuple(tt.getToken(),
431 ReachTuple.ARITY_ZEROORMORE
436 return ttsOut.makeCanonical();
439 public String toString() {
440 return tokenTuples.toString();