primitives passed to a child and then all available stuff copied back at one stall...
[IRC.git] / Robust / src / Analysis / MLP / MLPAnalysis.java
index c4961b631e18020d71738a9d9d57bce7f2f965c8..752de79ade0509f330246c71a3634b20e7418279 100644 (file)
@@ -12,19 +12,42 @@ import java.io.*;
 public class MLPAnalysis {
 
   // data from the compiler
-  private State state;
-  private TypeUtil typeUtil;
-  private CallGraph callGraph;
+  private State             state;
+  private TypeUtil          typeUtil;
+  private CallGraph         callGraph;
   private OwnershipAnalysis ownAnalysis;
 
-  private SESENode          rootTree;
-  private FlatSESEEnterNode rootSESE;
-  private FlatSESEExitNode  rootExit;
+  private FlatSESEEnterNode      rootSESE;  
+  private Set<FlatSESEEnterNode> allSESEs;
 
   private Hashtable< FlatNode, Stack<FlatSESEEnterNode> > seseStacks;
+  private Hashtable< FlatNode, Set<TempDescriptor>      > livenessRootView;
   private Hashtable< FlatNode, Set<TempDescriptor>      > livenessVirtualReads;
   private Hashtable< FlatNode, VarSrcTokTable           > variableResults;
-  private Hashtable< FlatNode, String                   > codePlan;
+  private Hashtable< FlatNode, Set<TempDescriptor>      > notAvailableResults;
+  private Hashtable< FlatNode, CodePlan                 > codePlans;
+
+  private static int maxSESEage = -1;
+
+
+  // use these methods in BuildCode to have access to analysis results
+  public FlatSESEEnterNode getRootSESE() {
+    return rootSESE;
+  }
+
+  public Set<FlatSESEEnterNode> getAllSESEs() {
+    return allSESEs;
+  }
+
+  public int getMaxSESEage() {
+    return maxSESEage;
+  }
+
+  // may be null
+  public CodePlan getCodePlan( FlatNode fn ) {
+    CodePlan cp = codePlans.get( fn );
+    return cp;
+  }
 
 
   public MLPAnalysis( State             state,
@@ -39,23 +62,24 @@ public class MLPAnalysis {
     this.typeUtil    = tu;
     this.callGraph   = callGraph;
     this.ownAnalysis = ownAnalysis;
+    this.maxSESEage  = state.MLP_MAXSESEAGE;
 
     // initialize analysis data structures
+    allSESEs = new HashSet<FlatSESEEnterNode>();
+
     seseStacks           = new Hashtable< FlatNode, Stack<FlatSESEEnterNode> >();
     livenessVirtualReads = new Hashtable< FlatNode, Set<TempDescriptor>      >();
     variableResults      = new Hashtable< FlatNode, VarSrcTokTable           >();
-    codePlan             = new Hashtable< FlatNode, String                   >();
-
-
-    // build an implicit root SESE to wrap contents of main method
-    rootTree = new SESENode( "root" );
-    rootSESE = new FlatSESEEnterNode( rootTree );
-    rootExit = new FlatSESEExitNode ( rootTree );
-    rootSESE.setFlatExit ( rootExit );
-    rootExit.setFlatEnter( rootSESE );
+    notAvailableResults  = new Hashtable< FlatNode, Set<TempDescriptor>      >();
+    codePlans            = new Hashtable< FlatNode, CodePlan                 >();
 
     FlatMethod fmMain = state.getMethodFlat( tu.getMain() );
 
+    rootSESE = (FlatSESEEnterNode) fmMain.getNext(0);    
+    rootSESE.setfmEnclosing( fmMain );
+    rootSESE.setmdEnclosing( fmMain.getMethod() );
+    rootSESE.setcdEnclosing( fmMain.getMethod().getClassDesc() );
+
 
     // 1st pass
     // run analysis on each method that is actually called
@@ -93,6 +117,18 @@ public class MLPAnalysis {
     livenessAnalysisBackward( rootSESE, true, null, fmMain.getFlatExit() );        
 
 
+    // 5th pass
+    methItr = ownAnalysis.descriptorsToAnalyze.iterator();
+    while( methItr.hasNext() ) {
+      Descriptor d  = methItr.next();      
+      FlatMethod fm = state.getMethodFlat( d );
+
+      // compute what is not available at every program
+      // point, in a forward fixed-point pass
+      notAvailableForward( fm );
+    }
+
+
     // 5th pass
     methItr = ownAnalysis.descriptorsToAnalyze.iterator();
     while( methItr.hasNext() ) {
@@ -104,6 +140,17 @@ public class MLPAnalysis {
     }
 
 
+    if( state.MLPDEBUG ) {      
+      System.out.println( "" );
+      //System.out.println( "\nSESE Hierarchy\n--------------\n" ); printSESEHierarchy();
+      //System.out.println( "\nSESE Liveness\n-------------\n" ); printSESELiveness();
+      System.out.println( "\nLiveness Root View\n------------------\n"+fmMain.printMethod( livenessRootView ) );
+      System.out.println( "\nVariable Results\n----------------\n"+fmMain.printMethod( variableResults ) );
+      //System.out.println( "\nNot Available Results\n---------------------\n"+fmMain.printMethod( notAvailableResults ) );
+      System.out.println( "\nCode Plans\n----------\n"+fmMain.printMethod( codePlans ) );
+    }
+
+
     double timeEndAnalysis = (double) System.nanoTime();
     double dt = (timeEndAnalysis - timeStartAnalysis)/(Math.pow( 10.0, 9.0 ) );
     String treport = String.format( "The mlp analysis took %.3f sec.", dt );
@@ -122,7 +169,6 @@ public class MLPAnalysis {
     Set<FlatNode> visited = new HashSet<FlatNode>();    
 
     Stack<FlatSESEEnterNode> seseStackFirst = new Stack<FlatSESEEnterNode>();
-    seseStackFirst.push( rootSESE );
     seseStacks.put( fm, seseStackFirst );
 
     while( !flatNodesToVisit.isEmpty() ) {
@@ -135,7 +181,7 @@ public class MLPAnalysis {
       flatNodesToVisit.remove( fn );
       visited.add( fn );      
 
-      buildForest_nodeActions( fn, seseStack );
+      buildForest_nodeActions( fn, seseStack, fm );
 
       for( int i = 0; i < fn.numNext(); i++ ) {
        FlatNode nn = fn.getNext( i );
@@ -148,21 +194,26 @@ public class MLPAnalysis {
        }
       }
     }      
-
-    if( state.MLPDEBUG ) { 
-      printSESEForest();
-    }
   }
 
   private void buildForest_nodeActions( FlatNode fn,                                                      
-                                       Stack<FlatSESEEnterNode> seseStack ) {
+                                       Stack<FlatSESEEnterNode> seseStack,
+                                       FlatMethod fm ) {
     switch( fn.kind() ) {
 
     case FKind.FlatSESEEnterNode: {
-      FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;      
-      assert !seseStack.empty();
-      seseStack.peek().addChild( fsen );
-      fsen.setParent( seseStack.peek() );
+      FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
+
+      allSESEs.add( fsen );
+      fsen.setfmEnclosing( fm );
+      fsen.setmdEnclosing( fm.getMethod() );
+      fsen.setcdEnclosing( fm.getMethod().getClassDesc() );
+
+      if( !seseStack.empty() ) {
+       seseStack.peek().addChild( fsen );
+       fsen.setParent( seseStack.peek() );
+      }
+
       seseStack.push( fsen );
     } break;
 
@@ -174,37 +225,41 @@ public class MLPAnalysis {
 
     case FKind.FlatReturnNode: {
       FlatReturnNode frn = (FlatReturnNode) fn;
-      if( !seseStack.empty() && 
-         !seseStack.peek().equals( rootSESE ) ) {
-       throw new Error( "Error: return statement enclosed within "+seseStack.peek() );
+      if( !seseStack.empty() ) {
+       throw new Error( "Error: return statement enclosed within SESE "+
+                        seseStack.peek().getPrettyIdentifier() );
       }
     } break;
       
     }
   }
 
-  private void printSESEForest() {
+  private void printSESEHierarchy() {
     // our forest is actually a tree now that
     // there is an implicit root SESE
-    printSESETree( rootSESE, 0 );
+    printSESEHierarchyTree( rootSESE, 0 );
     System.out.println( "" );
   }
 
-  private void printSESETree( FlatSESEEnterNode fsen, int depth ) {
+  private void printSESEHierarchyTree( FlatSESEEnterNode fsen, int depth ) {
     for( int i = 0; i < depth; ++i ) {
       System.out.print( "  " );
     }
-    System.out.println( fsen.getPrettyIdentifier() );
+    System.out.println( "- "+fsen.getPrettyIdentifier() );
 
     Iterator<FlatSESEEnterNode> childItr = fsen.getChildren().iterator();
     while( childItr.hasNext() ) {
       FlatSESEEnterNode fsenChild = childItr.next();
-      printSESETree( fsenChild, depth + 1 );
+      printSESEHierarchyTree( fsenChild, depth + 1 );
     }
   }
 
 
-    private void livenessAnalysisBackward( FlatSESEEnterNode fsen, boolean toplevel, Hashtable<FlatSESEExitNode, Set<TempDescriptor>> liveout, FlatExit fexit) {
+  private void livenessAnalysisBackward( FlatSESEEnterNode fsen, 
+                                         boolean toplevel, 
+                                         Hashtable< FlatSESEExitNode, Set<TempDescriptor> > liveout, 
+                                         FlatExit fexit ) {
+
     // start from an SESE exit, visit nodes in reverse up to
     // SESE enter in a fixed-point scheme, where children SESEs
     // should already be analyzed and therefore can be skipped 
@@ -237,11 +292,11 @@ public class MLPAnalysis {
           u.addAll( s );
         }
       }
-
+      
       Set<TempDescriptor> curr = liveness_nodeActions( fn, u, fsen, toplevel, liveout);
 
       // if a new result, schedule backward nodes for analysis
-      if(!curr.equals(prev)) {
+      if( !curr.equals( prev ) ) {
        livenessResults.put( fn, curr );
 
        // don't flow backwards past current SESE enter
@@ -259,24 +314,17 @@ public class MLPAnalysis {
       fsen.addInVarSet( s );
     }
     
-    if( state.MLPDEBUG ) { 
-      System.out.println( "SESE "+fsen.getPrettyIdentifier()+" has in-set:" );
-      Iterator<TempDescriptor> tItr = fsen.getInVarSet().iterator();
-      while( tItr.hasNext() ) {
-       System.out.println( "  "+tItr.next() );
-      }
-      System.out.println( "and out-set:" );
-      tItr = fsen.getOutVarSet().iterator();
-      while( tItr.hasNext() ) {
-       System.out.println( "  "+tItr.next() );
-      }
-      System.out.println( "" );
+    // remember liveness per node from the root view as the
+    // global liveness of variables for later passes to use
+    if( toplevel == true ) {
+      livenessRootView = livenessResults;
     }
+
     // post-order traversal, so do children first
     Iterator<FlatSESEEnterNode> childItr = fsen.getChildren().iterator();
     while( childItr.hasNext() ) {
       FlatSESEEnterNode fsenChild = childItr.next();
-      livenessAnalysisBackward( fsenChild, false, liveout, null);
+      livenessAnalysisBackward( fsenChild, false, liveout, null );
     }
   }
 
@@ -284,7 +332,7 @@ public class MLPAnalysis {
                                                     Set<TempDescriptor> liveIn,
                                                     FlatSESEEnterNode currentSESE,
                                                    boolean toplevel,
-                                                   Hashtable<FlatSESEExitNode, Set<TempDescriptor>> liveout) {
+                                                   Hashtable< FlatSESEExitNode, Set<TempDescriptor> > liveout ) {
 
     switch( fn.kind() ) {
 
@@ -297,21 +345,21 @@ public class MLPAnalysis {
          liveout.get(exitn).addAll(liveIn);
       }
       // no break, sese exits should also execute default actions
-
       
     default: {
       // handle effects of statement in reverse, writes then reads
       TempDescriptor [] writeTemps = fn.writesTemps();
       for( int i = 0; i < writeTemps.length; ++i ) {
        liveIn.remove( writeTemps[i] );
+
        if (!toplevel) {
-           FlatSESEExitNode exitnode=currentSESE.getFlatExit();
-           Set<TempDescriptor> livetemps=liveout.get(exitnode);
-           if (livetemps.contains(writeTemps[i])) {
-             //write to a live out temp...
-             //need to put in SESE liveout set
-               currentSESE.addOutVar(writeTemps[i]);
-           }
+          FlatSESEExitNode exitnode=currentSESE.getFlatExit();
+          Set<TempDescriptor> livetemps=liveout.get(exitnode);
+          if (livetemps.contains(writeTemps[i])) {
+            //write to a live out temp...
+            //need to put in SESE liveout set
+            currentSESE.addOutVar(writeTemps[i]);
+          }
        }
       }
 
@@ -324,7 +372,8 @@ public class MLPAnalysis {
       if( virtualReadTemps != null ) {
        Iterator<TempDescriptor> vrItr = virtualReadTemps.iterator();
        while( vrItr.hasNext() ) {
-         liveIn.add( vrItr.next() );
+          TempDescriptor vrt = vrItr.next();
+         liveIn.add( vrt );
        }
       }
     } break;
@@ -334,6 +383,35 @@ public class MLPAnalysis {
     return liveIn;
   }
 
+  private void printSESELiveness() {
+    // our forest is actually a tree now that
+    // there is an implicit root SESE
+    printSESELivenessTree( rootSESE );
+    System.out.println( "" );
+  }
+
+  private void printSESELivenessTree( FlatSESEEnterNode fsen ) {
+
+    System.out.println( "SESE "+fsen.getPrettyIdentifier()+" has in-set:" );
+    Iterator<TempDescriptor> tItr = fsen.getInVarSet().iterator();
+    while( tItr.hasNext() ) {
+      System.out.println( "  "+tItr.next() );
+    }
+    System.out.println( "and out-set:" );
+    tItr = fsen.getOutVarSet().iterator();
+    while( tItr.hasNext() ) {
+      System.out.println( "  "+tItr.next() );
+    }
+    System.out.println( "" );
+
+
+    Iterator<FlatSESEEnterNode> childItr = fsen.getChildren().iterator();
+    while( childItr.hasNext() ) {
+      FlatSESEEnterNode fsenChild = childItr.next();
+      printSESELivenessTree( fsenChild );
+    }
+  }
+
 
   private void variableAnalysisForward( FlatMethod fm ) {
 
@@ -349,36 +427,20 @@ public class MLPAnalysis {
 
       VarSrcTokTable prev = variableResults.get( fn );
 
-      // to stay sane
-      if( state.MLPDEBUG ) { 
-       if( prev != null ) {
-         prev.assertConsistency();
-       }
-      }
-
       // merge sets from control flow joins
-      VarSrcTokTable inUnion = new VarSrcTokTable();
+      VarSrcTokTable curr = new VarSrcTokTable();
       for( int i = 0; i < fn.numPrev(); i++ ) {
-       FlatNode nn = fn.getPrev( i );
-       
-       inUnion.merge( variableResults.get( nn ) );
+       FlatNode nn = fn.getPrev( i );          
+       VarSrcTokTable incoming = variableResults.get( nn );
+       curr.merge( incoming );
       }
 
-      // check merge results before sending
-      if( state.MLPDEBUG ) { 
-       inUnion.assertConsistency();
-      }
-
-      VarSrcTokTable curr = variable_nodeActions( fn, inUnion, seseStack.peek() );
-      
-      // a sanity check after table operations before we proceed
-      if( state.MLPDEBUG ) { 
-       curr.assertConsistency();
+      if( !seseStack.empty() ) {
+       variable_nodeActions( fn, curr, seseStack.peek() );
       }
 
       // if a new result, schedule forward nodes for analysis
-      if( !curr.equals( prev ) ) {
-       
+      if( !curr.equals( prev ) ) {       
        variableResults.put( fn, curr );
 
        for( int i = 0; i < fn.numNext(); i++ ) {
@@ -389,15 +451,16 @@ public class MLPAnalysis {
     }
   }
 
-  private VarSrcTokTable variable_nodeActions( FlatNode fn, 
-                                              VarSrcTokTable vstTable,
-                                              FlatSESEEnterNode currentSESE ) {
+  private void variable_nodeActions( FlatNode fn, 
+                                    VarSrcTokTable vstTable,
+                                    FlatSESEEnterNode currentSESE ) {
     switch( fn.kind() ) {
 
     case FKind.FlatSESEEnterNode: {
       FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
       assert fsen.equals( currentSESE );
       vstTable.age( currentSESE );
+      vstTable.assertConsistency();
     } break;
 
     case FKind.FlatSESEExitNode: {
@@ -413,6 +476,7 @@ public class MLPAnalysis {
         virLiveIn.addAll( virLiveInOld );
       }
       livenessVirtualReads.put( fn, virLiveIn );
+      vstTable.assertConsistency();
     } break;
 
     case FKind.FlatOpNode: {
@@ -424,33 +488,41 @@ public class MLPAnalysis {
 
        vstTable.remove( lhs );
 
+        Set<VariableSourceToken> forAddition = new HashSet<VariableSourceToken>();
+
        Iterator<VariableSourceToken> itr = vstTable.get( rhs ).iterator();
        while( itr.hasNext() ) {
          VariableSourceToken vst = itr.next();
 
+          HashSet<TempDescriptor> ts = new HashSet<TempDescriptor>();
+          ts.add( lhs );
+
           // if this is from a child, keep the source information
           if( currentSESE.getChildren().contains( vst.getSESE() ) ) {    
-            vstTable.add( new VariableSourceToken( lhs,
-                                                   vst.getSESE(),
-                                                   vst.getAge(),
-                                                   vst.getVarSrc()
-                                                   )
-                          );
+            forAddition.add( new VariableSourceToken( ts,
+                                                      vst.getSESE(),
+                                                      vst.getAge(),
+                                                      vst.getAddrVar()
+                                                      )
+                             );
 
           // otherwise, it's our or an ancestor's token so we
           // can assume we have everything we need
           } else {
-            vstTable.add( new VariableSourceToken( lhs,
-                                                   currentSESE,
-                                                   new Integer( 0 ),
-                                                   lhs
-                                                   )
-                          );
+            forAddition.add( new VariableSourceToken( ts,
+                                                      currentSESE,
+                                                      new Integer( 0 ),
+                                                      lhs
+                                                      )
+                             );
           }
        }
 
+        vstTable.addAll( forAddition );
+
        // only break if this is an ASSIGN op node,
        // otherwise fall through to default case
+       vstTable.assertConsistency();
        break;
       }
     }
@@ -460,22 +532,179 @@ public class MLPAnalysis {
     default: {
       TempDescriptor [] writeTemps = fn.writesTemps();
       if( writeTemps.length > 0 ) {
-       assert writeTemps.length == 1;
+
+
+        // for now, when writeTemps > 1, make sure
+        // its a call node, programmer enforce only
+        // doing stuff like calling a print routine
+       //assert writeTemps.length == 1;
+        if( writeTemps.length > 1 ) {
+          assert fn.kind() == FKind.FlatCall ||
+                 fn.kind() == FKind.FlatMethod;
+          break;
+        }
+
 
        vstTable.remove( writeTemps[0] );
 
-       vstTable.add( new VariableSourceToken( writeTemps[0],
+        HashSet<TempDescriptor> ts = new HashSet<TempDescriptor>();
+        ts.add( writeTemps[0] );
+
+       vstTable.add( new VariableSourceToken( ts,
                                               currentSESE,
                                               new Integer( 0 ),
                                               writeTemps[0]
                                             )
                      );
       }      
+
+      vstTable.assertConsistency();
     } break;
 
     } // end switch
+  }
+
+
+  private void notAvailableForward( FlatMethod fm ) {
+
+    Set<FlatNode> flatNodesToVisit = new HashSet<FlatNode>();
+    flatNodesToVisit.add( fm );         
+
+    while( !flatNodesToVisit.isEmpty() ) {
+      FlatNode fn = (FlatNode) flatNodesToVisit.iterator().next();
+      flatNodesToVisit.remove( fn );      
+
+      Stack<FlatSESEEnterNode> seseStack = seseStacks.get( fn );
+      assert seseStack != null;      
+
+      Set<TempDescriptor> prev = notAvailableResults.get( fn );
+
+      Set<TempDescriptor> curr = new HashSet<TempDescriptor>();      
+      for( int i = 0; i < fn.numPrev(); i++ ) {
+       FlatNode nn = fn.getPrev( i );       
+       Set<TempDescriptor> notAvailIn = notAvailableResults.get( nn );
+        if( notAvailIn != null ) {
+          curr.addAll( notAvailIn );
+        }
+      }
+
+      if( !seseStack.empty() ) {
+       notAvailable_nodeActions( fn, curr, seseStack.peek() );     
+      }
+
+      // if a new result, schedule forward nodes for analysis
+      if( !curr.equals( prev ) ) {
+       notAvailableResults.put( fn, curr );
 
-    return vstTable;
+       for( int i = 0; i < fn.numNext(); i++ ) {
+         FlatNode nn = fn.getNext( i );         
+         flatNodesToVisit.add( nn );    
+       }
+      }
+    }
+  }
+
+  private void notAvailable_nodeActions( FlatNode fn, 
+                                        Set<TempDescriptor> notAvailSet,
+                                        FlatSESEEnterNode currentSESE ) {
+
+    // any temps that are removed from the not available set
+    // at this node should be marked in this node's code plan
+    // as temps to be grabbed at runtime!
+
+    switch( fn.kind() ) {
+
+    case FKind.FlatSESEEnterNode: {
+      FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
+      assert fsen.equals( currentSESE );
+      notAvailSet.clear();
+    } break;
+
+    case FKind.FlatSESEExitNode: {
+      FlatSESEExitNode  fsexn = (FlatSESEExitNode)  fn;
+      FlatSESEEnterNode fsen  = fsexn.getFlatEnter();
+      assert currentSESE.getChildren().contains( fsen );
+
+      Set<TempDescriptor> liveTemps = livenessRootView.get( fn );
+      assert liveTemps != null;
+
+      VarSrcTokTable vstTable = variableResults.get( fn );
+      assert vstTable != null;
+
+      Set<TempDescriptor> notAvailAtEnter = notAvailableResults.get( fsen );
+      assert notAvailAtEnter != null;
+
+      Iterator<TempDescriptor> tdItr = liveTemps.iterator();
+      while( tdItr.hasNext() ) {
+       TempDescriptor td = tdItr.next();
+
+       if( vstTable.get( fsen, td ).size() > 0 ) {
+         // there is at least one child token for this variable
+         notAvailSet.add( td );
+         continue;
+       }
+
+       if( notAvailAtEnter.contains( td ) ) {
+         // wasn't available at enter, not available now
+         notAvailSet.add( td );
+         continue;
+       }
+      }
+    } break;
+
+    case FKind.FlatOpNode: {
+      FlatOpNode fon = (FlatOpNode) fn;
+
+      if( fon.getOp().getOp() == Operation.ASSIGN ) {
+       TempDescriptor lhs = fon.getDest();
+       TempDescriptor rhs = fon.getLeft();
+
+       // copy makes lhs same availability as rhs
+       if( notAvailSet.contains( rhs ) ) {
+         notAvailSet.add( lhs );
+       } else {
+         notAvailSet.remove( lhs );
+       }
+
+       // only break if this is an ASSIGN op node,
+       // otherwise fall through to default case
+       break;
+      }
+    }
+
+    // note that FlatOpNode's that aren't ASSIGN
+    // fall through to this default case
+    default: {
+      TempDescriptor [] writeTemps = fn.writesTemps();
+      for( int i = 0; i < writeTemps.length; i++ ) {
+        TempDescriptor wTemp = writeTemps[i];
+        notAvailSet.remove( wTemp );
+      }
+      TempDescriptor [] readTemps = fn.readsTemps();
+      for( int i = 0; i < readTemps.length; i++ ) {
+        TempDescriptor rTemp = readTemps[i];
+        notAvailSet.remove( rTemp );
+
+       // if this variable has exactly one source, mark everything
+       // else from that source as available as well
+       VarSrcTokTable table = variableResults.get( fn );
+       Set<VariableSourceToken> srcs = table.get( rTemp );
+
+       if( srcs.size() == 1 ) {
+         VariableSourceToken vst = srcs.iterator().next();
+         
+         Iterator<VariableSourceToken> availItr = table.get( vst.getSESE(), 
+                                                             vst.getAge()
+                                                           ).iterator();
+         while( availItr.hasNext() ) {
+           VariableSourceToken vstAlsoAvail = availItr.next();
+           notAvailSet.removeAll( vstAlsoAvail.getRefVars() );
+         }
+       }
+      }
+    } break;
+
+    } // end switch
   }
 
 
@@ -500,13 +729,25 @@ public class MLPAnalysis {
 
       // use incoming results as "dot statement" or just
       // before the current statement
-      VarSrcTokTable dotST = new VarSrcTokTable();
+      VarSrcTokTable dotSTtable = new VarSrcTokTable();
       for( int i = 0; i < fn.numPrev(); i++ ) {
        FlatNode nn = fn.getPrev( i );
-       dotST.merge( variableResults.get( nn ) );
+       dotSTtable.merge( variableResults.get( nn ) );
       }
 
-      computeStalls_nodeActions( fn, dotST, seseStack.peek() );
+      // find dt-st notAvailableSet also
+      Set<TempDescriptor> dotSTnotAvailSet = new HashSet<TempDescriptor>();      
+      for( int i = 0; i < fn.numPrev(); i++ ) {
+       FlatNode nn = fn.getPrev( i );       
+       Set<TempDescriptor> notAvailIn = notAvailableResults.get( nn );
+        if( notAvailIn != null ) {
+         dotSTnotAvailSet.addAll( notAvailIn );
+        }
+      }
+
+      if( !seseStack.empty() ) {
+       computeStalls_nodeActions( fn, dotSTtable, dotSTnotAvailSet, seseStack.peek() );
+      }
 
       for( int i = 0; i < fn.numNext(); i++ ) {
        FlatNode nn = fn.getNext( i );
@@ -515,51 +756,151 @@ public class MLPAnalysis {
          flatNodesToVisit.add( nn );
        }
       }
-    }      
-
-    if( state.MLPDEBUG ) { 
-      System.out.println( fm.printMethod( codePlan ) );
     }
   }
 
   private void computeStalls_nodeActions( FlatNode fn,
                                           VarSrcTokTable vstTable,
+                                         Set<TempDescriptor> notAvailSet,
                                           FlatSESEEnterNode currentSESE ) {
-    String s = "no op";
+    CodePlan plan = new CodePlan();
+
 
     switch( fn.kind() ) {
 
     case FKind.FlatSESEEnterNode: {
-      FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;      
+      FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
     } break;
 
     case FKind.FlatSESEExitNode: {
       FlatSESEExitNode fsexn = (FlatSESEExitNode) fn;
     } break;
 
+    case FKind.FlatOpNode: {
+      FlatOpNode fon = (FlatOpNode) fn;
+
+      if( fon.getOp().getOp() == Operation.ASSIGN ) {
+       // if this is an op node, don't stall, copy
+       // source and delay until we need to use value
+
+       // only break if this is an ASSIGN op node,
+       // otherwise fall through to default case
+       break;
+      }
+    }
+
+    // note that FlatOpNode's that aren't ASSIGN
+    // fall through to this default case
     default: {          
-      Set<VariableSourceToken> stallSet = vstTable.getStallSet( currentSESE );           
-      Set<TempDescriptor>      liveIn   = currentSESE.getInVarSet();
-
-      if( liveIn != null ) {
-       stallSet.retainAll( liveIn );
-      } else {
-       // there is nothing live, clear all
-       stallSet.clear();
+
+      // decide if we must stall for variables dereferenced at this node
+      Set<VariableSourceToken> potentialStallSet = 
+       vstTable.getChildrenVSTs( currentSESE );
+
+      // a node with no live set has nothing to stall for
+      Set<TempDescriptor> liveSet = livenessRootView.get( fn );
+      if( liveSet == null ) {
+       break;
       }
 
-      if( !stallSet.isEmpty() ) {
-       s = "STALL for:";
-       Iterator<VariableSourceToken> itr = stallSet.iterator();
-       while( itr.hasNext() ) {
-         VariableSourceToken vst = itr.next();
-         s += "  "+vst.getVarLive()+",";
-       }       
+      TempDescriptor[] readarray = fn.readsTemps();
+      for( int i = 0; i < readarray.length; i++ ) {
+        TempDescriptor readtmp = readarray[i];
+
+       // ignore temps that are definitely available 
+       // when considering to stall on it
+       if( !notAvailSet.contains( readtmp ) ) {
+         continue;
+       }
+
+       // Two cases:
+        Set<VariableSourceToken> srcs = vstTable.get( readtmp );
+       assert !srcs.isEmpty();
+
+       // 1) Multiple token/age pairs or unknown age: Stall for
+       // dynamic name only.
+       if( srcs.size() > 1 || 
+           srcs.iterator().next().getAge() == maxSESEage ) {
+
+         // identify that this is a stall, and allocate an integer
+         // pointer in the generated code that keeps a pointer to
+         // the source SESE and the address of where to get this thing
+         // --then the stall is just wait for that, and copy the
+         // one thing because we're not sure if we can copy other stuff
+
+         // NEEDS WORK!
+         
+         
+
+       // 2) Single token/age pair: Stall for token/age pair, and copy
+       // all live variables with same token/age pair at the same
+       // time.  This is the same stuff that the notavaialable analysis 
+       // marks as now available.        
+       } else {          
+         VariableSourceToken vst = srcs.iterator().next();                       
+
+         Iterator<VariableSourceToken> availItr = 
+           vstTable.get( vst.getSESE(), vst.getAge() ).iterator();
+
+         while( availItr.hasNext() ) {
+           VariableSourceToken vstAlsoAvail = availItr.next();
+
+           // only grab additional stuff that is live
+           Set<TempDescriptor> copySet = new HashSet<TempDescriptor>();
+
+           Iterator<TempDescriptor> refVarItr = vstAlsoAvail.getRefVars().iterator();
+           while( refVarItr.hasNext() ) {
+             TempDescriptor refVar = refVarItr.next();
+             if( liveSet.contains( refVar ) ) {
+               copySet.add( refVar );
+             }
+           }
+
+           if( !copySet.isEmpty() ) {
+             plan.addStall2CopySet( vstAlsoAvail, copySet );
+           }
+         }                      
+       }
+
+       // assert that everything being stalled for is in the
+       // "not available" set coming into this flat node and
+       // that every VST identified is in the possible "stall set"
+       // that represents VST's from children SESE's
+
       }      
     } break;
 
     } // end switch
 
-    codePlan.put( fn, s );
+
+    // identify sese-age pairs that are statically useful
+    // and should have an associated SESE variable in code
+    Set<VariableSourceToken> staticSet = vstTable.getStaticSet();
+    Iterator<VariableSourceToken> vstItr = staticSet.iterator();
+    while( vstItr.hasNext() ) {
+      VariableSourceToken vst = vstItr.next();
+      currentSESE.addNeededStaticName( 
+        new SESEandAgePair( vst.getSESE(), vst.getAge() ) 
+                                    );
+    }
+
+    // if any variable at this node has a static source (exactly one sese)
+    // but goes to a dynamic source at a next node, write its dynamic addr      
+    Set<VariableSourceToken> static2dynamicSet = new HashSet<VariableSourceToken>();
+    for( int i = 0; i < fn.numNext(); i++ ) {
+      FlatNode nn = fn.getNext( i );
+      VarSrcTokTable nextVstTable = variableResults.get( nn );
+      // the table can be null if it is one of the few IR nodes
+      // completely outside of the root SESE scope
+      if( nextVstTable != null ) {
+       static2dynamicSet.addAll( vstTable.getStatic2DynamicSet( nextVstTable ) );
+      }
+    }
+
+    if( !static2dynamicSet.isEmpty() ) {
+      plan.setWriteToDynamicSrc( static2dynamicSet );
+    }
+
+    codePlans.put( fn, plan );
   }
 }