1) allow to set the maximum threshold for the liveness analysis. if threashold is...
[IRC.git] / Robust / src / Analysis / Loops / UseDef.java
index b72a05a911c915c4cf6545c0e924c619b067c3c3..b58c1a4ce81153c29e0c41a592a74389384dc55a 100644 (file)
@@ -3,18 +3,43 @@ package Analysis.Loops;
 import IR.Flat.*;
 import java.util.HashSet;
 import java.util.Hashtable;
+import java.util.Set;
+import java.util.Iterator;
+import Analysis.Liveness;
 
-public class UseDef{
+public class UseDef {
   Hashtable<TempFlatPair, Set<FlatNode>> defs;
   Hashtable<TempFlatPair, Set<FlatNode>> uses;
 
+  public UseDef() {
+  }
+
   public UseDef(FlatMethod fm) {
     analyze(fm);
   }
 
+  /* Return FlatNodes that define Temp */
+  public Set<FlatNode> defMap(FlatNode fn, TempDescriptor t) {
+    Set<FlatNode> s=defs.get(new TempFlatPair(t,fn));
+    if (s==null)
+      return new HashSet<FlatNode>();
+    else
+      return s;
+  }
+
+  /* Return FlatNodes that use Temp */
+  public Set<FlatNode> useMap(FlatNode fn, TempDescriptor t) {
+    Set<FlatNode> s=uses.get(new TempFlatPair(t,fn));
+    if (s==null)
+      return new HashSet<FlatNode>();
+    else
+      return s;
+  }
+
   public void analyze(FlatMethod fm) {
     Hashtable<FlatNode, Set<TempFlatPair>> tmp=new Hashtable<FlatNode, Set<TempFlatPair>>();
     HashSet<FlatNode> toanalyze=new HashSet<FlatNode>();
+    Hashtable<FlatNode, Set<TempDescriptor>> livemap=Liveness.computeLiveTemps(fm,-1);
     toanalyze.addAll(fm.getNodeSet());
     while(!toanalyze.isEmpty()) {
       FlatNode fn=toanalyze.iterator().next();
@@ -22,53 +47,58 @@ public class UseDef{
 
       toanalyze.remove(fn);
       HashSet<TempFlatPair> s=new HashSet<TempFlatPair>();
-      for(int i=0;i<fn.numPrev();i++) {
-       FlatNode prev=fn.getPrev(i);
-       HashSet<TempFlatPair> prevs=tmp.get(prev);
-       nexttfp:
-       for(Iterator<TempFlatPair> tfit=prevs.iterator();tfit.hasNext();) {
-         TempFlatPair tfp=tfit.next();
-         for(int j=0;j<fnwrites.length;j++) {
-           if (tfp.t==fnwrites[j])
-             continue nexttfp;
-         }
-         s.add(tfp);
-       }
-       for(int j=0;j<fnwrites.length;j++) {
-         TempFlatPair tfp=new TempFlatPair(fnwrites[j], fn);
-         s.add(tfp);
-       }
+      Set<TempDescriptor> liveset=livemap.get(fn);
+      for(int i=0; i<fn.numPrev(); i++) {
+        FlatNode prev=fn.getPrev(i);
+        Set<TempFlatPair> prevs=tmp.get(prev);
+        if (prevs!=null) {
+nexttfp:
+          for(Iterator<TempFlatPair> tfit=prevs.iterator(); tfit.hasNext(); ) {
+            TempFlatPair tfp=tfit.next();
+            if (!liveset.contains(tfp.t))
+              continue;
+            for(int j=0; j<fnwrites.length; j++) {
+              if (tfp.t==fnwrites[j])
+                continue nexttfp;
+            }
+            s.add(tfp);
+          }
+        }
+        for(int j=0; j<fnwrites.length; j++) {
+          TempFlatPair tfp=new TempFlatPair(fnwrites[j], fn);
+          s.add(tfp);
+        }
       }
       if (!tmp.containsKey(fn)||
-         !tmp.get(fn).equals(s)) {
-       tmp.put(fn,s);
-       for(int i=0;i<fn.numNext();i++)
-         toanalyze.put(fn.getNext(i));
+          !tmp.get(fn).equals(s)) {
+        tmp.put(fn,s);
+        for(int i=0; i<fn.numNext(); i++)
+          toanalyze.add(fn.getNext(i));
       }
     }
     Set<FlatNode> fset=fm.getNodeSet();
     defs=new Hashtable<TempFlatPair, Set<FlatNode>>();
     uses=new Hashtable<TempFlatPair, Set<FlatNode>>();
-    for(Iterator<FlatNode> fnit=fset.iterator();fnit.hasNext();) {
+    for(Iterator<FlatNode> fnit=fset.iterator(); fnit.hasNext(); ) {
       FlatNode fn=fnit.next();
       TempDescriptor[] fnreads=fn.readsTemps();
       Set<TempFlatPair> tfpset=tmp.get(fn);
-      
-      for(int i=0;i<fnreads.length;i++) {
-       TempDescriptor readt=fnreads[i];
-       for(Iterator<TempFlatPair> tfpit=tfpset.iterator();tfpit.hasNext();) {
-         TempFlatPair tfp=tfpit.next();
-         if (tfp.t==readt) {
-           //have use
-           if (!uses.containsKey(tfp))
-             uses.put(tfp,new HashSet<FlatNode>());
-           uses.get(tfp).add(fn);
-           TempFlatPair readtfp=new TempFlatPair(readt,fn);
-           if (!defs.containsKey(readtfp))
-             defs.put(readtfp,new HashSet<FlatNode>());
-           defs.get(readtfp).add(tfp.f);
-         }
-       }
+
+      for(int i=0; i<fnreads.length; i++) {
+        TempDescriptor readt=fnreads[i];
+        for(Iterator<TempFlatPair> tfpit=tfpset.iterator(); tfpit.hasNext(); ) {
+          TempFlatPair tfp=tfpit.next();
+          if (tfp.t==readt) {
+            //have use
+            if (!uses.containsKey(tfp))
+              uses.put(tfp,new HashSet<FlatNode>());
+            uses.get(tfp).add(fn);
+            TempFlatPair readtfp=new TempFlatPair(readt,fn);
+            if (!defs.containsKey(readtfp))
+              defs.put(readtfp,new HashSet<FlatNode>());
+            defs.get(readtfp).add(tfp.f);
+          }
+        }
       }
     }
   }