lots of bugs
authorbdemsky <bdemsky>
Fri, 3 Apr 2009 09:06:12 +0000 (09:06 +0000)
committerbdemsky <bdemsky>
Fri, 3 Apr 2009 09:06:12 +0000 (09:06 +0000)
Robust/src/Analysis/Loops/DeadCode.java
Robust/src/Analysis/Loops/DomTree.java
Robust/src/Analysis/Loops/LoopFinder.java
Robust/src/Analysis/Loops/LoopInvariant.java
Robust/src/Analysis/Loops/LoopOptimize.java
Robust/src/Analysis/Loops/localCSE.java

index ebbba25404176bef349664dd4bf94ae439226198..4df9eb4841f54fc3febfe0b52b1706ea71719771 100644 (file)
@@ -29,6 +29,7 @@ public class DeadCode {
        case FKind.FlatFlagActionNode:
        case FKind.FlatCheckNode:
        case FKind.FlatBackEdge:
+       case FKind.FlatExit:
        case FKind.FlatTagDeclaration:
        case FKind.FlatMethod:
        case FKind.FlatAtomicEnterNode:
@@ -74,8 +75,10 @@ public class DeadCode {
       if (!useful.contains(fn)) {
        //We have a useless node
        FlatNode fnnext=fn.getNext(0);
+
        for(int i=0;i<fn.numPrev();i++) {
          FlatNode nprev=fn.getPrev(i);
+             
          for(int j=0;j<nprev.numNext();j++) {
            if (nprev.getNext(j)==fn) {
              nprev.setnext(j, fnnext);
index 758876ac7504993efadb28194bbafbe1c032cafe..0eb8390188c05558f1a20c59cb11b1e15989b9ce 100644 (file)
@@ -28,13 +28,15 @@ public class DomTree {
 
   public void analyze(FlatMethod fm, boolean postdominator) {
     domtable=new Hashtable<FlatNode, FlatNode>();
+    childtree=new Hashtable<FlatNode, Set<FlatNode>>();
     if (postdominator) {
       Set<FlatNode> fnodes=fm.getNodeSet();
       Vector<FlatNode> v=new Vector<FlatNode>();
       for(Iterator<FlatNode> fit=fnodes.iterator();fit.hasNext();) {
        FlatNode fn=fit.next();
-       if (fn.numNext()==0)
+       if (fn.numNext()==0) {
          v.add(fn);
+       }
       }
       FlatNode[] fnarray=new FlatNode[v.size()];
       for(int i=0;i<v.size();i++) {
@@ -60,8 +62,14 @@ public class DomTree {
        FlatNode fn=vec.elementAt(i);
        FlatNode dom=null;
        for(int j=0;j<(postdominator?fn.numNext():fn.numPrev());j++) {
-         FlatNode ndom=domtable.get(postdominator?fn.getNext(i):fn.getPrev(i));
-         dom=intersect(dom,ndom);
+         FlatNode np=postdominator?fn.getNext(j):fn.getPrev(j);
+         FlatNode ndom=domtable.get(np);
+         if (ndom!=null) {
+           if (dom==null)
+             dom=np;
+           else
+             dom=intersect(dom,np);
+         }
        }
        if (!domtable.containsKey(fn)||
            !domtable.get(fn).equals(dom)) {
index c677972084694179ae37f4f095328e58c56f68d4..56098e310b5389331f01f4f823df61b2b93a7242 100755 (executable)
@@ -15,7 +15,7 @@ import java.util.Iterator;
  * <code>LoopFinder</code> implements Dominator Tree Loop detection.
  * 
  * @author  Brian Demsky <bdemsky@mit.edu>
- * @version $Id: LoopFinder.java,v 1.2 2009/03/27 09:02:25 bdemsky Exp $
+ * @version $Id: LoopFinder.java,v 1.3 2009/04/03 09:06:12 bdemsky Exp $
  */
 
 public class LoopFinder implements Loops {
@@ -154,6 +154,7 @@ public class LoopFinder implements Loops {
       //nested loop tree
       setofloops=new HashSet();
 
+
       //Find loops
       findloopheaders(hc);
       
@@ -266,7 +267,15 @@ public class LoopFinder implements Loops {
            root.entries.add(current_node);
            
            //See if those we dominate are backedges
-           stk.addAll(dominator.children(current_node));
+           Set<FlatNode> children=dominator.children(current_node);
+
+           if (children!=null) {
+             for(Iterator<FlatNode> it=children.iterator();it.hasNext();) {
+               FlatNode fn=it.next();
+               if (fn!=current_node)
+                 stk.push(fn);
+             }
+           }
        }
     }
 
index 0e007e1fa3950a7a02c1384acc0509cd94496211..2afbc5b2fe5cbe73ee5973663eee1824b0a9a034 100644 (file)
@@ -17,16 +17,17 @@ public class LoopInvariant {
   }
   LoopFinder loops;
   DomTree posttree;
-  Hashtable<Loops, Vector<FlatNode>> table;
+  Hashtable<FlatNode, Vector<FlatNode>> table;
   Set<FlatNode> hoisted;
   UseDef usedef;
   TypeUtil typeutil;
   Set tounroll;
+  Loops root;
 
   public void analyze(FlatMethod fm) {
     loops=new LoopFinder(fm);
-    Loops root=loops.getRootloop(fm);
-    table=new Hashtable<Loops, Vector<FlatNode>>();
+    root=loops.getRootloop(fm);
+    table=new Hashtable<FlatNode, Vector<FlatNode>>();
     hoisted=new HashSet<FlatNode>();
     usedef=new UseDef(fm);
     posttree=new DomTree(fm,true);
@@ -35,9 +36,9 @@ public class LoopInvariant {
   }
 
   public void recurse(Loops parent) {
-    processLoop(parent, parent.nestedLoops().size()==0);
     for(Iterator lpit=parent.nestedLoops().iterator();lpit.hasNext();) {
       Loops child=(Loops)lpit.next();
+      processLoop(child, child.nestedLoops().size()==0);
       recurse(child);
     }
   }
@@ -48,6 +49,9 @@ public class LoopInvariant {
     Set elements=l.loopIncElements();
     Set toprocess=l.loopIncElements();
     toprocess.removeAll(hoisted);
+    Set entrances=l.loopEntrances();
+    assert entrances.size()==1;
+    FlatNode entrance=(FlatNode)entrances.iterator().next();
 
     HashSet<FieldDescriptor> fields=new HashSet<FieldDescriptor>();
     HashSet<TypeDescriptor> types=new HashSet<TypeDescriptor>();
@@ -76,7 +80,7 @@ public class LoopInvariant {
     HashSet dominatorset=unsafe?null:computeAlways(l);
 
     /* Compute loop invariants */
-    table.put(l, new Vector<FlatNode>());
+    table.put(entrance, new Vector<FlatNode>());
     while(changed) {
       changed=false;
       nextfn:
@@ -98,7 +102,8 @@ public class LoopInvariant {
          break;
 
        case FKind.FlatElementNode:
-         if (unsafe||!dominatorset.contains(fn)||
+         if (unsafe||dominatorset==null||
+             !dominatorset.contains(fn)||
              checkNode(fn,elements))
            continue nextfn;
          TypeDescriptor td=((FlatElementNode)fn).getSrc().getType();
@@ -114,7 +119,8 @@ public class LoopInvariant {
          break;
 
        case FKind.FlatFieldNode:
-         if (unsafe||!dominatorset.contains(fn)||
+         if (unsafe||dominatorset==null||
+             !dominatorset.contains(fn)||
              fields.contains(((FlatFieldNode)fn).getField())||
              checkNode(fn,elements)) {
            continue nextfn;
@@ -128,7 +134,7 @@ public class LoopInvariant {
        }
        //mark to hoist
        hoisted.add(fn);
-       table.get(l).add(fn);
+       table.get(entrance).add(fn);
       }
     }
   }
index 2e5845065a7a17d86c6cee3165299c7d499d53a4..e875918950177744cfdade17b7cee65dff397deb 100644 (file)
@@ -18,13 +18,13 @@ public class LoopOptimize {
     dooptimize(fm);
   } 
   private void dooptimize(FlatMethod fm) {
-    Loops root=loopinv.loops.getRootloop(fm);
+    Loops root=loopinv.root;
     recurse(root);
   }
   private void recurse(Loops parent) {
-    processLoop(parent);
     for(Iterator lpit=parent.nestedLoops().iterator();lpit.hasNext();) {
       Loops child=(Loops)lpit.next();
+      processLoop(child);
       recurse(child);
     }
   }
@@ -36,21 +36,33 @@ public class LoopOptimize {
     }
   }
   public void hoistOps(Loops l) {
-    Vector<FlatNode> tohoist=loopinv.table.get(l);
+    Set entrances=l.loopEntrances();
+    assert entrances.size()==1;
+    FlatNode entrance=(FlatNode)entrances.iterator().next();
+    Vector<FlatNode> tohoist=loopinv.table.get(entrance);
     Set lelements=l.loopIncElements();
     TempMap t=new TempMap();
+    TempMap tnone=new TempMap();
     FlatNode first=null;
     FlatNode last=null;
+    if (tohoist.size()==0)
+      return;
+
     for(int i=0;i<tohoist.size();i++) {
       FlatNode fn=tohoist.elementAt(i);
       TempDescriptor[] writes=fn.writesTemps();
+      FlatNode fnnew=fn.clone(tnone);
+
+      fnnew.rewriteUse(t);
+
       for(int j=0;j<writes.length;j++) {
-       if (writes[j]!=null&&!t.maps(writes[j])) {
+       if (writes[j]!=null) {
          TempDescriptor cp=writes[j].createNew();
          t.addPair(writes[j],cp);
        }
       }
-      FlatNode fnnew=fn.clone(t);
+      fnnew.rewriteDef(t);
+
       if (first==null)
        first=fnnew;
       else
@@ -58,18 +70,23 @@ public class LoopOptimize {
       last=fnnew;
       /* Splice out old node */
       if (writes.length==1) {
-       FlatOpNode fon=new FlatOpNode(t.tempMap(writes[0]),writes[0], null, new Operation(Operation.ASSIGN));
+       FlatOpNode fon=new FlatOpNode(writes[0], t.tempMap(writes[0]), null, new Operation(Operation.ASSIGN));
        fn.replace(fon);
+       if (fn==entrance)
+         entrance=fon;
       } else if (writes.length>1) {
        throw new Error();
       }
     }
     /* The chain is built at this point. */
     
-    assert l.loopEntrances().size()==1;
-    FlatNode entrance=(FlatNode)l.loopEntrances().iterator().next();
+    FlatNode[] prevarray=new FlatNode[entrance.numPrev()];
     for(int i=0;i<entrance.numPrev();i++) {
-      FlatNode prev=entrance.getPrev(i);
+      prevarray[i]=entrance.getPrev(i);
+    }
+    for(int i=0;i<prevarray.length;i++) {
+      FlatNode prev=prevarray[i];
+
       if (!lelements.contains(prev)) {
        //need to fix this edge
        for(int j=0;j<prev.numNext();j++) {
index 1678837c3ebb18b10a2712a01e90b1015be089d9..0d1b4e0df4dc0db85ee5899d75f7bdb2065c34fb 100644 (file)
@@ -197,8 +197,10 @@ public class localCSE {
   public void kill(Hashtable<LocalExpression, Group> tab, TempDescriptor t) {
     LocalExpression e=new LocalExpression(t);
     Group g=tab.get(e);
-    tab.remove(e);
-    g.set.remove(e);
+    if (g!=null) {
+      tab.remove(e);
+      g.set.remove(e);
+    }
   }
 }
 
@@ -220,7 +222,7 @@ class Group {
 
 class LocalExpression {
   Operation op;
-  Object o;
+  Object obj;
   Group a;
   Group b;
   TempDescriptor t;
@@ -230,7 +232,7 @@ class LocalExpression {
     this.t=t;
   }
   LocalExpression(Object o) {
-    this.o=o;
+    this.obj=o;
   }
   LocalExpression(Group a, Group b, Operation op) {
     this.a=a;
@@ -254,30 +256,32 @@ class LocalExpression {
       h^=t.hashCode();
     if (a!=null)
       h^=a.hashCode();
-    if (o!=null)
-      h^=o.hashCode();
     if (op!=null)
       h^=op.getOp();
     if (b!=null)
       h^=b.hashCode();
     if (f!=null)
       h^=f.hashCode();
+    if (obj!=null)
+      h^=obj.hashCode();
     return h;
   }
+  public static boolean equiv(Object a, Object b) {
+    if (a!=null)
+      return a.equals(b);
+    else
+      return b==null;
+  }
+
   public boolean equals(Object o) {
     LocalExpression e=(LocalExpression)o;
-    if (a!=e.a||f!=e.f||b!=e.b)
+    if (!(equiv(a,e.a)&&equiv(f,e.f)&&equiv(b,e.b)&&
+         equiv(td,e.td)&&equiv(this.obj,e.obj)))
       return false;
-    if (td!=null) {
-      if (!td.equals(e.td))
-       return false;
-    }
-    if (o!=null) {
-      if (e.o==null)
-       return false;
-      return o.equals(e.o);
-    } else if (op!=null)
+    if (op!=null)
       return op.getOp()==e.op.getOp();
+    else if (e.op!=null)
+      return false;
     return true;
   }
 }
\ No newline at end of file