primitives passed to a child and then all available stuff copied back at one stall...
[IRC.git] / Robust / src / Analysis / MLP / MLPAnalysis.java
index 1848d470889da562caeb06d55573e385cff91273..752de79ade0509f330246c71a3634b20e7418279 100644 (file)
@@ -12,15 +12,12 @@ 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;
@@ -30,15 +27,25 @@ public class MLPAnalysis {
   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 );
-    assert cp != null;
     return cp;
   }
 
@@ -55,6 +62,7 @@ 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>();
@@ -65,16 +73,13 @@ public class MLPAnalysis {
     notAvailableResults  = new Hashtable< FlatNode, Set<TempDescriptor>      >();
     codePlans            = new Hashtable< FlatNode, CodePlan                 >();
 
-
-    // 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 );
-
     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
@@ -137,12 +142,12 @@ public class MLPAnalysis {
 
     if( state.MLPDEBUG ) {      
       System.out.println( "" );
-      //printSESEHierarchy();
-      //printSESELiveness();
-      //System.out.println( fmMain.printMethod( livenessRootView ) );
-      //System.out.println( fmMain.printMethod( variableResults ) );
-      //System.out.println( fmMain.printMethod( notAvailableResults ) );
-      //System.out.println( "CODE PLANS\n"+fmMain.printMethod( codePlans ) );
+      //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 ) );
     }
 
 
@@ -164,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() ) {
@@ -201,11 +205,15 @@ public class MLPAnalysis {
       FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
 
       allSESEs.add( fsen );
-      fsen.setEnclosingFlatMeth( fm );
+      fsen.setfmEnclosing( fm );
+      fsen.setmdEnclosing( fm.getMethod() );
+      fsen.setcdEnclosing( fm.getMethod().getClassDesc() );
+
+      if( !seseStack.empty() ) {
+       seseStack.peek().addChild( fsen );
+       fsen.setParent( seseStack.peek() );
+      }
 
-      assert !seseStack.empty();
-      seseStack.peek().addChild( fsen );
-      fsen.setParent( seseStack.peek() );
       seseStack.push( fsen );
     } break;
 
@@ -217,9 +225,9 @@ 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;
       
@@ -237,7 +245,7 @@ public class MLPAnalysis {
     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() ) {
@@ -284,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
@@ -316,7 +324,7 @@ public class MLPAnalysis {
     Iterator<FlatSESEEnterNode> childItr = fsen.getChildren().iterator();
     while( childItr.hasNext() ) {
       FlatSESEEnterNode fsenChild = childItr.next();
-      livenessAnalysisBackward( fsenChild, false, liveout, null);
+      livenessAnalysisBackward( fsenChild, false, liveout, null );
     }
   }
 
@@ -420,14 +428,16 @@ public class MLPAnalysis {
       VarSrcTokTable prev = variableResults.get( fn );
 
       // 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 );          
        VarSrcTokTable incoming = variableResults.get( nn );
-       inUnion.merge( incoming );
+       curr.merge( incoming );
       }
 
-      VarSrcTokTable curr = variable_nodeActions( fn, inUnion, seseStack.peek() );     
+      if( !seseStack.empty() ) {
+       variable_nodeActions( fn, curr, seseStack.peek() );
+      }
 
       // if a new result, schedule forward nodes for analysis
       if( !curr.equals( prev ) ) {       
@@ -441,9 +451,9 @@ 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: {
@@ -552,8 +562,6 @@ public class MLPAnalysis {
     } break;
 
     } // end switch
-
-    return vstTable;
   }
 
 
@@ -571,16 +579,18 @@ public class MLPAnalysis {
 
       Set<TempDescriptor> prev = notAvailableResults.get( fn );
 
-      Set<TempDescriptor> inUnion = new HashSet<TempDescriptor>();      
+      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 ) {
-          inUnion.addAll( notAvailIn );
+          curr.addAll( notAvailIn );
         }
       }
 
-      Set<TempDescriptor> curr = notAvailable_nodeActions( fn, inUnion, seseStack.peek() );     
+      if( !seseStack.empty() ) {
+       notAvailable_nodeActions( fn, curr, seseStack.peek() );     
+      }
 
       // if a new result, schedule forward nodes for analysis
       if( !curr.equals( prev ) ) {
@@ -594,9 +604,9 @@ public class MLPAnalysis {
     }
   }
 
-  private Set<TempDescriptor> notAvailable_nodeActions( FlatNode fn, 
-                                                       Set<TempDescriptor> notAvailSet,
-                                                       FlatSESEEnterNode currentSESE ) {
+  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
@@ -695,8 +705,6 @@ public class MLPAnalysis {
     } break;
 
     } // end switch
-
-    return notAvailSet;
   }
 
 
@@ -737,7 +745,9 @@ public class MLPAnalysis {
         }
       }
 
-      computeStalls_nodeActions( fn, dotSTtable, dotSTnotAvailSet, seseStack.peek() );
+      if( !seseStack.empty() ) {
+       computeStalls_nodeActions( fn, dotSTtable, dotSTnotAvailSet, seseStack.peek() );
+      }
 
       for( int i = 0; i < fn.numNext(); i++ ) {
        FlatNode nn = fn.getNext( i );
@@ -760,7 +770,6 @@ public class MLPAnalysis {
 
     case FKind.FlatSESEEnterNode: {
       FlatSESEEnterNode fsen = (FlatSESEEnterNode) fn;
-      plan.setSESEtoIssue( fsen );
     } break;
 
     case FKind.FlatSESEExitNode: {
@@ -783,8 +792,16 @@ public class MLPAnalysis {
     // note that FlatOpNode's that aren't ASSIGN
     // fall through to this default case
     default: {          
+
       // decide if we must stall for variables dereferenced at this node
-      Set<VariableSourceToken> stallSet = vstTable.getStallSet( currentSESE );
+      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;
+      }
 
       TempDescriptor[] readarray = fn.readsTemps();
       for( int i = 0; i < readarray.length; i++ ) {
@@ -796,81 +813,93 @@ public class MLPAnalysis {
          continue;
        }
 
-        Set<VariableSourceToken> readSet = vstTable.get( readtmp );
+       // Two cases:
+        Set<VariableSourceToken> srcs = vstTable.get( readtmp );
+       assert !srcs.isEmpty();
 
-       //Two cases:
+       // 1) Multiple token/age pairs or unknown age: Stall for
+       // dynamic name only.
+       if( srcs.size() > 1 || 
+           srcs.iterator().next().getAge() == maxSESEage ) {
 
-       //1) Multiple token/age pairs or unknown age: Stall for
-       //dynamic name only.
-       
+         // 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.
-
-       //VarSrcTokTable table = variableResults.get( fn );
-       //Set<VariableSourceToken> srcs = table.get( rTemp );
+       // 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();                       
 
-       //XXXXXXXXXX: Note: We have to generate code to do these
-       //copies in the codeplan.  Note we should only copy the
-       //variables that are live!
+         Iterator<VariableSourceToken> availItr = 
+           vstTable.get( vst.getSESE(), vst.getAge() ).iterator();
 
-       /*
-       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() );
-         }
-       }
-       */
 
+           // 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 notAvailSet.containsAll( writeSet );
+       // 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
 
-        /*
-        for( Iterator<VariableSourceToken> readit = readSet.iterator(); 
-             readit.hasNext(); ) {
-          VariableSourceToken vst = readit.next();
-          if( stallSet.contains( vst ) ) {
-            if( before == null ) {
-              before = "**STALL for:";
-            }
-            before += "("+vst+" "+readtmp+")";     
-          }
-        }
-        */
       }      
     } break;
 
     } // end switch
 
 
+    // 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 );
-      assert nextVstTable != null;
-      static2dynamicSet.addAll( vstTable.getStatic2DynamicSet( nextVstTable ) );
-    }
-    /*
-    Iterator<VariableSourceToken> vstItr = static2dynamicSet.iterator();
-    while( vstItr.hasNext() ) {
-      VariableSourceToken vst = vstItr.next();
-      if( after == null ) {
-       after = "** Write dynamic: ";
+      // 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 ) );
       }
-      after += "("+vst+")";
     }
-    */
+
+    if( !static2dynamicSet.isEmpty() ) {
+      plan.setWriteToDynamicSrc( static2dynamicSet );
+    }
 
     codePlans.put( fn, plan );
   }