From: jjenista Date: Mon, 4 May 2009 21:04:04 +0000 (+0000) Subject: Why did last checkin of VarSrcTokTable add carot-M to every line end but the whole... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f8e673b505f4575b6f01d9aadbdccead9ffd9422;p=IRC.git Why did last checkin of VarSrcTokTable add carot-M to every line end but the whole file didn't show up in the cvs diff--anyway, looks like everything is touched but actually just taking that line character out and adding section to assertConsistency that makes sure reference var sets are consistent as well. --- diff --git a/Robust/src/Analysis/MLP/VarSrcTokTable.java b/Robust/src/Analysis/MLP/VarSrcTokTable.java index 6191ce7a..f99627f5 100644 --- a/Robust/src/Analysis/MLP/VarSrcTokTable.java +++ b/Robust/src/Analysis/MLP/VarSrcTokTable.java @@ -1,701 +1,733 @@ -package Analysis.MLP; - -import IR.*; -import IR.Flat.*; -import java.util.*; -import java.io.*; - -// This class formerly had lazy consistency properties, but -// it is being changed so that the full set and the extra -// hash tables to access the full set efficiently by different -// elements will be consistent after EVERY operation. Also, -// a consistent assert method allows a debugger to ask whether -// an operation has produced an inconsistent VarSrcTokTable. -public class VarSrcTokTable { - - // a set of every token in the table - private HashSet trueSet; - - // these hashtables provide an efficient retreival from the true set - private Hashtable< TempDescriptor, Set > var2vst; - private Hashtable< FlatSESEEnterNode, Set > sese2vst; - private Hashtable< SVKey, Set > sv2vst; - - // maximum age from aging operation - private Integer MAX_AGE = new Integer( 2 ); - - - public VarSrcTokTable() { - trueSet = new HashSet(); - - sese2vst = new Hashtable< FlatSESEEnterNode, Set >(); - var2vst = new Hashtable< TempDescriptor, Set >(); - sv2vst = new Hashtable< SVKey, Set >(); - } - - - public VarSrcTokTable( VarSrcTokTable in ) { - trueSet = (HashSet) in.trueSet.clone(); - - Iterator itr; Set s; - - sese2vst = new Hashtable< FlatSESEEnterNode, Set >(); - itr = in.sese2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - sese2vst.put( sese, - (HashSet) (s1.clone()) ); - } - - var2vst = new Hashtable< TempDescriptor, Set >(); - itr = in.var2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - TempDescriptor var = (TempDescriptor) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - var2vst.put( var, - (HashSet) (s1.clone()) ); - } - - sv2vst = new Hashtable< SVKey, Set >(); - itr = in.sv2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - SVKey key = (SVKey) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - sv2vst.put( key, - (HashSet) (s1.clone()) ); - } - - assertConsistency(); - } - - - public void add( VariableSourceToken vst ) { - trueSet.add( vst ); - - Set s; - - s = sese2vst.get( vst.getSESE() ); - if( s == null ) { - s = new HashSet(); - } - s.add( vst ); - sese2vst.put( vst.getSESE(), s ); - - Iterator refVarItr = vst.getRefVars().iterator(); - while( refVarItr.hasNext() ) { - TempDescriptor refVar = refVarItr.next(); - s = var2vst.get( refVar ); - if( s == null ) { - s = new HashSet(); - } - s.add( vst ); - var2vst.put( refVar, s ); - - SVKey key = new SVKey( vst.getSESE(), refVar ); - s = sv2vst.get( key ); - if( s == null ) { - s = new HashSet(); - } - s.add( vst ); - sv2vst.put( key, s ); - } - - assertConsistency(); - } - - public void addAll( Set s ) { - Iterator itr = s.iterator(); - while( itr.hasNext() ) { - add( itr.next() ); - } - } - - - public Set get() { - return trueSet; - } - - public Set get( FlatSESEEnterNode sese ) { - Set s = sese2vst.get( sese ); - if( s == null ) { - s = new HashSet(); - sese2vst.put( sese, s ); - } - return s; - } - - public Set get( TempDescriptor var ) { - Set s = var2vst.get( var ); - if( s == null ) { - s = new HashSet(); - var2vst.put( var, s ); - } - return s; - } - - public Set get( FlatSESEEnterNode sese, - TempDescriptor refVar ) { - SVKey key = new SVKey( sese, refVar ); - Set s = sv2vst.get( key ); - if( s == null ) { - s = new HashSet(); - sv2vst.put( key, s ); - } - return s; - } - - public Set get( FlatSESEEnterNode sese, - Integer age ) { - Set s = sese2vst.get( sese ); - if( s == null ) { - s = new HashSet(); - sese2vst.put( sese, s ); - } - Iterator sItr = s.iterator(); - while( sItr.hasNext() ) { - VariableSourceToken vst = sItr.next(); - if( !vst.getAge().equals( age ) ) { - s.remove( vst ); - } - } - return s; - } - - - public void merge( VarSrcTokTable tableIn ) { - - if( tableIn == null ) { - return; - } - - // make a copy for modification to use in the merge - VarSrcTokTable table = new VarSrcTokTable( tableIn ); - - - trueSet.addAll( table.trueSet ); - - - Iterator itr; - - // merge sese2vst mappings - itr = this.sese2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); - Set s1 = (Set) me.getValue(); - Set s2 = table.sese2vst.get( sese ); - assert s1 != null; - - if( s2 != null ) { - s1.addAll( s2 ); - } - } - itr = table.sese2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); - Set s2 = (Set) me.getValue(); - Set s1 = this.sese2vst.get( sese ); - assert s2 != null; - - if( s1 == null ) { - this.sese2vst.put( sese, s2 ); - } - } - - // merge var2vst mappings - itr = this.var2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - TempDescriptor var = (TempDescriptor) me.getKey(); - Set s1 = (Set) me.getValue(); - Set s2 = table.var2vst.get( var ); - assert s1 != null; - - if( s2 != null ) { - s1.addAll( s2 ); - } - } - itr = table.var2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - TempDescriptor var = (TempDescriptor) me.getKey(); - Set s2 = (Set) me.getValue(); - Set s1 = this.var2vst.get( var ); - assert s2 != null; - - if( s1 == null ) { - this.var2vst.put( var, s2 ); - } - } - - // merge sv2vst mappings - itr = this.sv2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - SVKey key = (SVKey) me.getKey(); - Set s1 = (Set) me.getValue(); - Set s2 = table.sv2vst.get( key ); - assert s1 != null; - - if( s2 != null ) { - s1.addAll( s2 ); - } - } - itr = table.sv2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - SVKey key = (SVKey) me.getKey(); - Set s2 = (Set) me.getValue(); - Set s1 = this.sv2vst.get( key ); - assert s2 != null; - - if( s1 == null ) { - this.sv2vst.put( key, s2 ); - } - } - - assertConsistency(); - } - - - // remove operations must leave the trueSet - // and the hash maps consistent! - public void remove( VariableSourceToken vst ) { - trueSet.remove( vst ); - - Set s; - - s = get( vst.getSESE() ); - if( s != null ) { s.remove( vst ); } - - Iterator refVarItr = vst.getRefVars().iterator(); - while( refVarItr.hasNext() ) { - TempDescriptor refVar = refVarItr.next(); - - s = get( refVar ); - if( s != null ) { s.remove( vst ); } - - s = get( vst.getSESE(), refVar ); - if( s != null ) { s.remove( vst ); } - } - - assertConsistency(); - } - - public void remove( FlatSESEEnterNode sese ) { - Set s = sese2vst.get( sese ); - if( s == null ) { - return; - } - - Iterator itr = s.iterator(); - while( itr.hasNext() ) { - VariableSourceToken vst = itr.next(); - remove( vst ); - } - - sese2vst.remove( sese ); - - assertConsistency(); - } - - public void remove( TempDescriptor refVar ) { - Set s = var2vst.get( refVar ); - if( s == null ) { - return; - } - - Set forRemoval = new HashSet(); - - // iterate over tokens that this temp can reference, make a set - // of tokens that need this temp stripped out of them - Iterator itr = s.iterator(); - while( itr.hasNext() ) { - VariableSourceToken vst = itr.next(); - Set refVars = vst.getRefVars(); - assert refVars.contains( refVar ); - forRemoval.add( vst ); - } - - itr = forRemoval.iterator(); - while( itr.hasNext() ) { - // here's a token marked for removal, take it out as it is - // in this state - VariableSourceToken vst = itr.next(); - remove( vst ); - - // if there are other references to the token, alter the - // token and then readd it to this table, because it has - // a new hash value now - Set refVars = vst.getRefVars(); - refVars.remove( refVar ); - if( refVars.size() > 0 ) { - add( vst ); - } - } - - //var2vst.remove( var ); - - assertConsistency(); - } - - public void remove( FlatSESEEnterNode sese, - TempDescriptor var ) { - - SVKey key = new SVKey( sese, var ); - Set s = sv2vst.get( key ); - if( s == null ) { - return; - } - - Iterator itr = s.iterator(); - while( itr.hasNext() ) { - VariableSourceToken vst = itr.next(); - remove( vst ); - } - - sv2vst.remove( key ); - - assertConsistency(); - } - - - - // return a new table based on this one and - // age tokens with respect to SESE curr, where - // any curr tokens increase age by 1 - public VarSrcTokTable age( FlatSESEEnterNode curr ) { - - // create a table to modify as a copy of this - VarSrcTokTable out = new VarSrcTokTable( this ); - - Iterator itr = trueSet.iterator(); - while( itr.hasNext() ) { - VariableSourceToken vst = itr.next(); - - if( vst.getSESE().equals( curr ) ) { - - Integer newAge = vst.getAge()+1; - if( newAge > MAX_AGE ) { - newAge = MAX_AGE; - } - - out.remove( vst ); - - out.add( new VariableSourceToken( vst.getRefVars(), - curr, - newAge, - vst.getAddrVar() - ) - ); - } - } - - out.assertConsistency(); - return out; - } - - - // for the given SESE, change child tokens into this parent - public void remapChildTokens( FlatSESEEnterNode curr ) { - - Iterator childItr = curr.getChildren().iterator(); - if( childItr.hasNext() ) { - FlatSESEEnterNode child = childItr.next(); - - Iterator vstItr = get( child ).iterator(); - while( vstItr.hasNext() ) { - VariableSourceToken vst = vstItr.next(); - - remove( vst ); - - add( new VariableSourceToken( vst.getRefVars(), - curr, - new Integer( 0 ), - vst.getAddrVar() - ) - ); - } - } - - assertConsistency(); - } - - - // if we can get a value from the current SESE and the parent - // or a sibling, just getting from the current SESE suffices now - // return a set of temps that are virtually read - public Set removeParentAndSiblingTokens( FlatSESEEnterNode curr, - Set liveIn ) { - - HashSet virtualLiveIn = new HashSet(); - - FlatSESEEnterNode parent = curr.getParent(); - if( parent == null ) { - // have no parent or siblings - return virtualLiveIn; - } - - remove_A_if_B( parent, curr, liveIn, virtualLiveIn ); - - Iterator childItr = parent.getChildren().iterator(); - if( childItr.hasNext() ) { - FlatSESEEnterNode child = childItr.next(); - - if( !child.equals( curr ) ) { - remove_A_if_B( child, curr, liveIn, virtualLiveIn ); - } - } - - assertConsistency(); - return virtualLiveIn; - } - - // if B is also a source for some variable, remove all entries - // of A as a source for that variable: s is virtual reads - protected void remove_A_if_B( FlatSESEEnterNode a, - FlatSESEEnterNode b, - Set liveInCurrentSESE, - Set virtualLiveIn ) { - - Set forRemoval = new HashSet(); - - Iterator vstItr = get( a ).iterator(); - while( vstItr.hasNext() ) { - VariableSourceToken vst = vstItr.next(); - Iterator refVarItr = vst.getRefVars().iterator(); - while( refVarItr.hasNext() ) { - TempDescriptor refVar = refVarItr.next(); - Set bSet = get( b, refVar ); - - if( !bSet.isEmpty() ) { - forRemoval.add( vst ); - - // mark this variable as a virtual read as well - //if( liveInCurrentSESE.contains( varLive ) ) { ??????????? - virtualLiveIn.add( refVar ); - //} - } - } - } - - vstItr = forRemoval.iterator(); - while( vstItr.hasNext() ) { - VariableSourceToken vst = vstItr.next(); - remove( vst ); - } - } - - - public Set getStallSet( FlatSESEEnterNode curr ) { - - Set out = new HashSet(); - - Iterator cItr = curr.getChildren().iterator(); - while( cItr.hasNext() ) { - FlatSESEEnterNode child = cItr.next(); - out.addAll( get( child ) ); - } - - return out; - } - - - // use as an aid for debugging, where true-set is checked - // against the alternate mappings: assert that nothing is - // missing or extra in the alternates - public void assertConsistency() { - - Iterator itr; - Set s; - - Set trueSetByAlts = new HashSet(); - itr = sese2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - - // the trueSet should have all entries in s1 - assert trueSet.containsAll( s1 ); - - // s1 should not have anything that doesn't appear in trueset - Set sInt = (Set) s1.clone(); - sInt.removeAll( trueSet ); - - assert sInt.isEmpty(); - - // add s1 to a running union--at the end check if trueSet has extra - trueSetByAlts.addAll( s1 ); - } - // make sure trueSet isn't too big - assert trueSetByAlts.containsAll( trueSet ); - - - trueSetByAlts = new HashSet(); - itr = var2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - TempDescriptor var = (TempDescriptor) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - - // the trueSet should have all entries in s1 - assert trueSet.containsAll( s1 ); - - // s1 should not have anything that doesn't appear in trueset - Set sInt = (Set) s1.clone(); - sInt.removeAll( trueSet ); - - assert sInt.isEmpty(); - - // add s1 to a running union--at the end check if trueSet has extra - trueSetByAlts.addAll( s1 ); - } - // make sure trueSet isn't too big - assert trueSetByAlts.containsAll( trueSet ); - - - trueSetByAlts = new HashSet(); - itr = sv2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - SVKey key = (SVKey) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - - // the trueSet should have all entries in s1 - assert trueSet.containsAll( s1 ); - - // s1 should not have anything that doesn't appear in trueset - Set sInt = (Set) s1.clone(); - sInt.removeAll( trueSet ); - - assert sInt.isEmpty(); - - // add s1 to a running union--at the end check if trueSet has extra - trueSetByAlts.addAll( s1 ); - } - // make sure trueSet isn't too big - assert trueSetByAlts.containsAll( trueSet ); - } - - - public boolean equals( Object o ) { - if( o == null ) { - return false; - } - - if( !(o instanceof VarSrcTokTable) ) { - return false; - } - - VarSrcTokTable table = (VarSrcTokTable) o; - return trueSet.equals( table.trueSet ); - } - - public int hashCode() { - return trueSet.hashCode(); - } - - public Iterator iterator() { - return trueSet.iterator(); - } - - public String toString() { - return "trueSet ="+trueSet.toString(); - } - - public String toStringVerbose() { - return "trueSet ="+trueSet.toString()+"\n"+ - "sese2vst="+sese2vst.toString()+"\n"+ - "var2vst ="+var2vst.toString()+"\n"+ - "sv2vst ="+sv2vst.toString(); - } - - public String toStringPretty() { - String tokHighlighter = "o"; - - String str = "VarSrcTokTable\n"; - Iterator vstItr = trueSet.iterator(); - while( vstItr.hasNext() ) { - str += " "+tokHighlighter+" "+vstItr.next()+"\n"; - } - return str; - } - - public String toStringPrettyVerbose() { - String tokHighlighter = "o"; - - String str = "VarSrcTokTable\n"; - - Set s; - Iterator itr; - Iterator vstItr; - - str += " trueSet\n"; - vstItr = trueSet.iterator(); - while( vstItr.hasNext() ) { - str += " "+tokHighlighter+" "+vstItr.next()+"\n"; - } - - str += " sese2vst\n"; - itr = sese2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); - HashSet s1 = (HashSet) me.getValue(); - assert s1 != null; - - str += " "+sese.getPrettyIdentifier()+" -> \n"; - - vstItr = s1.iterator(); - while( vstItr.hasNext() ) { - str += " "+tokHighlighter+" "+vstItr.next()+"\n"; - } - } - - str += " var2vst\n"; - itr = var2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - TempDescriptor var = (TempDescriptor) me.getKey(); - Set s1 = (Set) me.getValue(); - assert s1 != null; - - str += " "+var+" -> \n"; - - vstItr = s1.iterator(); - while( vstItr.hasNext() ) { - str += " "+tokHighlighter+" "+vstItr.next()+"\n"; - } - } - - str += " sv2vst\n"; - itr = sv2vst.entrySet().iterator(); - while( itr.hasNext() ) { - Map.Entry me = (Map.Entry) itr.next(); - SVKey key = (SVKey) me.getKey(); - Set s1 = (Set) me.getValue(); - assert s1 != null; - - str += " "+key+" -> \n"; - - vstItr = s1.iterator(); - while( vstItr.hasNext() ) { - str += " "+tokHighlighter+" "+vstItr.next()+"\n"; - } - } - - return str; - } -} +package Analysis.MLP; + +import IR.*; +import IR.Flat.*; +import java.util.*; +import java.io.*; + +// This class formerly had lazy consistency properties, but +// it is being changed so that the full set and the extra +// hash tables to access the full set efficiently by different +// elements will be consistent after EVERY operation. Also, +// a consistent assert method allows a debugger to ask whether +// an operation has produced an inconsistent VarSrcTokTable. +public class VarSrcTokTable { + + // a set of every token in the table + private HashSet trueSet; + + // these hashtables provide an efficient retreival from the true set + private Hashtable< TempDescriptor, Set > var2vst; + private Hashtable< FlatSESEEnterNode, Set > sese2vst; + private Hashtable< SVKey, Set > sv2vst; + + // maximum age from aging operation + private Integer MAX_AGE = new Integer( 2 ); + + + public VarSrcTokTable() { + trueSet = new HashSet(); + + sese2vst = new Hashtable< FlatSESEEnterNode, Set >(); + var2vst = new Hashtable< TempDescriptor, Set >(); + sv2vst = new Hashtable< SVKey, Set >(); + } + + + public VarSrcTokTable( VarSrcTokTable in ) { + trueSet = (HashSet) in.trueSet.clone(); + + Iterator itr; Set s; + + sese2vst = new Hashtable< FlatSESEEnterNode, Set >(); + itr = in.sese2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + sese2vst.put( sese, + (HashSet) (s1.clone()) ); + } + + var2vst = new Hashtable< TempDescriptor, Set >(); + itr = in.var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor var = (TempDescriptor) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + var2vst.put( var, + (HashSet) (s1.clone()) ); + } + + sv2vst = new Hashtable< SVKey, Set >(); + itr = in.sv2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + SVKey key = (SVKey) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + sv2vst.put( key, + (HashSet) (s1.clone()) ); + } + + assertConsistency(); + } + + + public void add( VariableSourceToken vst ) { + + // make sure we aren't clobbering anything! + assert !trueSet.contains( vst ); + + trueSet.add( vst ); + + Set s; + + s = sese2vst.get( vst.getSESE() ); + if( s == null ) { + s = new HashSet(); + } + s.add( vst ); + sese2vst.put( vst.getSESE(), s ); + + Iterator refVarItr = vst.getRefVars().iterator(); + while( refVarItr.hasNext() ) { + TempDescriptor refVar = refVarItr.next(); + s = var2vst.get( refVar ); + if( s == null ) { + s = new HashSet(); + } + s.add( vst ); + var2vst.put( refVar, s ); + + SVKey key = new SVKey( vst.getSESE(), refVar ); + s = sv2vst.get( key ); + if( s == null ) { + s = new HashSet(); + } + s.add( vst ); + sv2vst.put( key, s ); + } + + assertConsistency(); + } + + public void addAll( Set s ) { + Iterator itr = s.iterator(); + while( itr.hasNext() ) { + add( itr.next() ); + } + } + + + public Set get() { + return trueSet; + } + + public Set get( FlatSESEEnterNode sese ) { + Set s = sese2vst.get( sese ); + if( s == null ) { + s = new HashSet(); + sese2vst.put( sese, s ); + } + return s; + } + + public Set get( TempDescriptor var ) { + Set s = var2vst.get( var ); + if( s == null ) { + s = new HashSet(); + var2vst.put( var, s ); + } + return s; + } + + public Set get( FlatSESEEnterNode sese, + TempDescriptor refVar ) { + SVKey key = new SVKey( sese, refVar ); + Set s = sv2vst.get( key ); + if( s == null ) { + s = new HashSet(); + sv2vst.put( key, s ); + } + return s; + } + + public Set get( FlatSESEEnterNode sese, + Integer age ) { + Set s = sese2vst.get( sese ); + if( s == null ) { + s = new HashSet(); + sese2vst.put( sese, s ); + } + Iterator sItr = s.iterator(); + while( sItr.hasNext() ) { + VariableSourceToken vst = sItr.next(); + if( !vst.getAge().equals( age ) ) { + s.remove( vst ); + } + } + return s; + } + + + public void merge( VarSrcTokTable tableIn ) { + + if( tableIn == null ) { + return; + } + + // make a copy for modification to use in the merge + VarSrcTokTable table = new VarSrcTokTable( tableIn ); + + + trueSet.addAll( table.trueSet ); + + + Iterator itr; + + // merge sese2vst mappings + itr = this.sese2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); + Set s1 = (Set) me.getValue(); + Set s2 = table.sese2vst.get( sese ); + assert s1 != null; + + if( s2 != null ) { + s1.addAll( s2 ); + } + } + itr = table.sese2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); + Set s2 = (Set) me.getValue(); + Set s1 = this.sese2vst.get( sese ); + assert s2 != null; + + if( s1 == null ) { + this.sese2vst.put( sese, s2 ); + } + } + + // merge var2vst mappings + itr = this.var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor var = (TempDescriptor) me.getKey(); + Set s1 = (Set) me.getValue(); + Set s2 = table.var2vst.get( var ); + assert s1 != null; + + if( s2 != null ) { + s1.addAll( s2 ); + } + } + itr = table.var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor var = (TempDescriptor) me.getKey(); + Set s2 = (Set) me.getValue(); + Set s1 = this.var2vst.get( var ); + assert s2 != null; + + if( s1 == null ) { + this.var2vst.put( var, s2 ); + } + } + + // merge sv2vst mappings + itr = this.sv2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + SVKey key = (SVKey) me.getKey(); + Set s1 = (Set) me.getValue(); + Set s2 = table.sv2vst.get( key ); + assert s1 != null; + + if( s2 != null ) { + s1.addAll( s2 ); + } + } + itr = table.sv2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + SVKey key = (SVKey) me.getKey(); + Set s2 = (Set) me.getValue(); + Set s1 = this.sv2vst.get( key ); + assert s2 != null; + + if( s1 == null ) { + this.sv2vst.put( key, s2 ); + } + } + + assertConsistency(); + } + + + // remove operations must leave the trueSet + // and the hash maps consistent! + public void remove( VariableSourceToken vst ) { + trueSet.remove( vst ); + + Set s; + + s = get( vst.getSESE() ); + if( s != null ) { s.remove( vst ); } + + Iterator refVarItr = vst.getRefVars().iterator(); + while( refVarItr.hasNext() ) { + TempDescriptor refVar = refVarItr.next(); + + s = get( refVar ); + if( s != null ) { s.remove( vst ); } + + s = get( vst.getSESE(), refVar ); + if( s != null ) { s.remove( vst ); } + } + + assertConsistency(); + } + + public void remove( FlatSESEEnterNode sese ) { + Set s = sese2vst.get( sese ); + if( s == null ) { + return; + } + + Iterator itr = s.iterator(); + while( itr.hasNext() ) { + VariableSourceToken vst = itr.next(); + remove( vst ); + } + + sese2vst.remove( sese ); + + assertConsistency(); + } + + public void remove( TempDescriptor refVar ) { + Set s = var2vst.get( refVar ); + if( s == null ) { + return; + } + + Set forRemoval = new HashSet(); + + // iterate over tokens that this temp can reference, make a set + // of tokens that need this temp stripped out of them + Iterator itr = s.iterator(); + while( itr.hasNext() ) { + VariableSourceToken vst = itr.next(); + Set refVars = vst.getRefVars(); + assert refVars.contains( refVar ); + forRemoval.add( vst ); + } + + itr = forRemoval.iterator(); + while( itr.hasNext() ) { + // here's a token marked for removal, take it out as it is + // in this state + VariableSourceToken vst = itr.next(); + remove( vst ); + + // if there are other references to the token, alter the + // token and then readd it to this table, because it has + // a new hash value now + Set refVars = vst.getRefVars(); + refVars.remove( refVar ); + if( refVars.size() > 0 ) { + add( vst ); + } + } + + //var2vst.remove( var ); + + assertConsistency(); + } + + public void remove( FlatSESEEnterNode sese, + TempDescriptor var ) { + + SVKey key = new SVKey( sese, var ); + Set s = sv2vst.get( key ); + if( s == null ) { + return; + } + + Iterator itr = s.iterator(); + while( itr.hasNext() ) { + VariableSourceToken vst = itr.next(); + remove( vst ); + } + + sv2vst.remove( key ); + + assertConsistency(); + } + + + + // return a new table based on this one and + // age tokens with respect to SESE curr, where + // any curr tokens increase age by 1 + public VarSrcTokTable age( FlatSESEEnterNode curr ) { + + // create a table to modify as a copy of this + VarSrcTokTable out = new VarSrcTokTable( this ); + + Iterator itr = trueSet.iterator(); + while( itr.hasNext() ) { + VariableSourceToken vst = itr.next(); + + if( vst.getSESE().equals( curr ) ) { + + Integer newAge = vst.getAge()+1; + if( newAge > MAX_AGE ) { + newAge = MAX_AGE; + } + + out.remove( vst ); + + out.add( new VariableSourceToken( vst.getRefVars(), + curr, + newAge, + vst.getAddrVar() + ) + ); + } + } + + out.assertConsistency(); + return out; + } + + + // for the given SESE, change child tokens into this parent + public void remapChildTokens( FlatSESEEnterNode curr ) { + + Iterator childItr = curr.getChildren().iterator(); + if( childItr.hasNext() ) { + FlatSESEEnterNode child = childItr.next(); + + Iterator vstItr = get( child ).iterator(); + while( vstItr.hasNext() ) { + VariableSourceToken vst = vstItr.next(); + + remove( vst ); + + add( new VariableSourceToken( vst.getRefVars(), + curr, + new Integer( 0 ), + vst.getAddrVar() + ) + ); + } + } + + assertConsistency(); + } + + + // if we can get a value from the current SESE and the parent + // or a sibling, just getting from the current SESE suffices now + // return a set of temps that are virtually read + public Set removeParentAndSiblingTokens( FlatSESEEnterNode curr, + Set liveIn ) { + + HashSet virtualLiveIn = new HashSet(); + + FlatSESEEnterNode parent = curr.getParent(); + if( parent == null ) { + // have no parent or siblings + return virtualLiveIn; + } + + remove_A_if_B( parent, curr, liveIn, virtualLiveIn ); + + Iterator childItr = parent.getChildren().iterator(); + if( childItr.hasNext() ) { + FlatSESEEnterNode child = childItr.next(); + + if( !child.equals( curr ) ) { + remove_A_if_B( child, curr, liveIn, virtualLiveIn ); + } + } + + assertConsistency(); + return virtualLiveIn; + } + + // if B is also a source for some variable, remove all entries + // of A as a source for that variable: s is virtual reads + protected void remove_A_if_B( FlatSESEEnterNode a, + FlatSESEEnterNode b, + Set liveInCurrentSESE, + Set virtualLiveIn ) { + + Set forRemoval = new HashSet(); + + Iterator vstItr = get( a ).iterator(); + while( vstItr.hasNext() ) { + VariableSourceToken vst = vstItr.next(); + Iterator refVarItr = vst.getRefVars().iterator(); + while( refVarItr.hasNext() ) { + TempDescriptor refVar = refVarItr.next(); + Set bSet = get( b, refVar ); + + if( !bSet.isEmpty() ) { + forRemoval.add( vst ); + + // mark this variable as a virtual read as well + virtualLiveIn.add( refVar ); + } + } + } + + vstItr = forRemoval.iterator(); + while( vstItr.hasNext() ) { + VariableSourceToken vst = vstItr.next(); + remove( vst ); + } + } + + + public Set getStallSet( FlatSESEEnterNode curr ) { + + Set out = new HashSet(); + + Iterator cItr = curr.getChildren().iterator(); + while( cItr.hasNext() ) { + FlatSESEEnterNode child = cItr.next(); + out.addAll( get( child ) ); + } + + return out; + } + + + // use as an aid for debugging, where true-set is checked + // against the alternate mappings: assert that nothing is + // missing or extra in the alternates + public void assertConsistency() { + + Iterator itr; + Set s; + + Set trueSetByAlts = new HashSet(); + itr = sese2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + + // the trueSet should have all entries in s1 + assert trueSet.containsAll( s1 ); + + // s1 should not have anything that doesn't appear in trueset + Set sInt = (Set) s1.clone(); + sInt.removeAll( trueSet ); + + assert sInt.isEmpty(); + + // add s1 to a running union--at the end check if trueSet has extra + trueSetByAlts.addAll( s1 ); + } + // make sure trueSet isn't too big + assert trueSetByAlts.containsAll( trueSet ); + + + trueSetByAlts = new HashSet(); + itr = var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor var = (TempDescriptor) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + + // the trueSet should have all entries in s1 + assert trueSet.containsAll( s1 ); + + // s1 should not have anything that doesn't appear in trueset + Set sInt = (Set) s1.clone(); + sInt.removeAll( trueSet ); + + assert sInt.isEmpty(); + + // add s1 to a running union--at the end check if trueSet has extra + trueSetByAlts.addAll( s1 ); + } + // make sure trueSet isn't too big + assert trueSetByAlts.containsAll( trueSet ); + + + trueSetByAlts = new HashSet(); + itr = sv2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + SVKey key = (SVKey) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + + // the trueSet should have all entries in s1 + assert trueSet.containsAll( s1 ); + + // s1 should not have anything that doesn't appear in trueset + Set sInt = (Set) s1.clone(); + sInt.removeAll( trueSet ); + + assert sInt.isEmpty(); + + // add s1 to a running union--at the end check if trueSet has extra + trueSetByAlts.addAll( s1 ); + } + // make sure trueSet isn't too big + assert trueSetByAlts.containsAll( trueSet ); + + + // also check that the reference var sets are consistent + Hashtable > vst2refVars = + new Hashtable >(); + itr = var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor refVar = (TempDescriptor) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + Iterator vstItr = s1.iterator(); + while( vstItr.hasNext() ) { + VariableSourceToken vst = vstItr.next(); + assert vst.getRefVars().contains( refVar ); + + Set refVarsPart = vst2refVars.get( vst ); + if( refVarsPart == null ) { + refVarsPart = new HashSet(); + } + refVarsPart.add( refVar ); + vst2refVars.put( vst, refVarsPart ); + } + } + itr = vst2refVars.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + VariableSourceToken vst = (VariableSourceToken) me.getKey(); + Set s1 = (Set) me.getValue(); + assert vst.getRefVars().equals( s1 ); + } + } + + + public boolean equals( Object o ) { + if( o == null ) { + return false; + } + + if( !(o instanceof VarSrcTokTable) ) { + return false; + } + + VarSrcTokTable table = (VarSrcTokTable) o; + return trueSet.equals( table.trueSet ); + } + + public int hashCode() { + return trueSet.hashCode(); + } + + public Iterator iterator() { + return trueSet.iterator(); + } + + public String toString() { + return "trueSet ="+trueSet.toString(); + } + + public String toStringVerbose() { + return "trueSet ="+trueSet.toString()+"\n"+ + "sese2vst="+sese2vst.toString()+"\n"+ + "var2vst ="+var2vst.toString()+"\n"+ + "sv2vst ="+sv2vst.toString(); + } + + public String toStringPretty() { + String tokHighlighter = "o"; + + String str = "VarSrcTokTable\n"; + Iterator vstItr = trueSet.iterator(); + while( vstItr.hasNext() ) { + str += " "+tokHighlighter+" "+vstItr.next()+"\n"; + } + return str; + } + + public String toStringPrettyVerbose() { + String tokHighlighter = "o"; + + String str = "VarSrcTokTable\n"; + + Set s; + Iterator itr; + Iterator vstItr; + + str += " trueSet\n"; + vstItr = trueSet.iterator(); + while( vstItr.hasNext() ) { + str += " "+tokHighlighter+" "+vstItr.next()+"\n"; + } + + str += " sese2vst\n"; + itr = sese2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + FlatSESEEnterNode sese = (FlatSESEEnterNode) me.getKey(); + HashSet s1 = (HashSet) me.getValue(); + assert s1 != null; + + str += " "+sese.getPrettyIdentifier()+" -> \n"; + + vstItr = s1.iterator(); + while( vstItr.hasNext() ) { + str += " "+tokHighlighter+" "+vstItr.next()+"\n"; + } + } + + str += " var2vst\n"; + itr = var2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + TempDescriptor var = (TempDescriptor) me.getKey(); + Set s1 = (Set) me.getValue(); + assert s1 != null; + + str += " "+var+" -> \n"; + + vstItr = s1.iterator(); + while( vstItr.hasNext() ) { + str += " "+tokHighlighter+" "+vstItr.next()+"\n"; + } + } + + str += " sv2vst\n"; + itr = sv2vst.entrySet().iterator(); + while( itr.hasNext() ) { + Map.Entry me = (Map.Entry) itr.next(); + SVKey key = (SVKey) me.getKey(); + Set s1 = (Set) me.getValue(); + assert s1 != null; + + str += " "+key+" -> \n"; + + vstItr = s1.iterator(); + while( vstItr.hasNext() ) { + str += " "+tokHighlighter+" "+vstItr.next()+"\n"; + } + } + + return str; + } +}