changeset 5435:1d63466ba795

Add distinct filter
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 23 May 2012 10:09:39 +0200
parents 86478955e54c
children 16c27447923c
files graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/FloatingReadPhase.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/DistinctPredicatedProxyNodeIterator.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/FilteredNodeIterable.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/NodeIterable.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/PredicatedProxyNodeIterator.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/ValueAnchorNode.java
diffstat 6 files changed, 87 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/FloatingReadPhase.java	Tue May 22 14:34:18 2012 +0200
+++ b/graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/FloatingReadPhase.java	Wed May 23 10:09:39 2012 +0200
@@ -158,7 +158,7 @@
                 if (anchor == null) {
                     anchor = graph.add(new ValueAnchorNode());
                 }
-                anchor.addAnchoredValue(guard);
+                anchor.addAnchoredNode(guard);
             }
             if (anchor != null) {
                 graph.addAfterFixed(readNode, anchor);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/DistinctPredicatedProxyNodeIterator.java	Wed May 23 10:09:39 2012 +0200
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.graph.iterators;
+
+import java.util.*;
+
+import com.oracle.graal.graph.*;
+
+
+public class DistinctPredicatedProxyNodeIterator<T extends Node> extends PredicatedProxyNodeIterator<T> {
+    private NodeBitMap visited;
+
+    public DistinctPredicatedProxyNodeIterator(NodePredicate until, Iterator<T> iterator, NodePredicate predicate) {
+        super(until, iterator, predicate);
+    }
+
+    @Override
+    protected void forward() {
+        if (current == null) {
+            super.forward();
+            while (!accept(current)) {
+                current = null;
+                super.forward();
+            }
+        }
+    }
+
+    private boolean accept(T n) {
+        if (n == null) {
+            return true;
+        }
+        if (visited == null) {
+            visited = n.graph().createNodeBitMap(true);
+        }
+        boolean accept = !visited.isMarked(n);
+        visited.mark(n);
+        return accept;
+    }
+}
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/FilteredNodeIterable.java	Tue May 22 14:34:18 2012 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/FilteredNodeIterable.java	Wed May 23 10:09:39 2012 +0200
@@ -30,6 +30,7 @@
     private final NodeIterable<T> nodeIterable;
     private NodePredicate predicate = NodePredicates.alwaysTrue();
     private NodePredicate until = NodePredicates.isNull();
+    private boolean distinct;
     public FilteredNodeIterable(NodeIterable<T> nodeIterable) {
         this.nodeIterable = nodeIterable;
     }
@@ -52,9 +53,23 @@
         return this;
     }
     @Override
+    public FilteredNodeIterable<T> nonNull() {
+        this.predicate = this.predicate.or(NodePredicates.isNotNull());
+        return this;
+    }
+    @Override
+    public FilteredNodeIterable<T> distinct() {
+        distinct = true;
+        return this;
+    }
+    @Override
     public Iterator<T> iterator() {
         final Iterator<T> iterator = nodeIterable.iterator();
-        return new PredicatedProxyNodeIterator<>(until, iterator, predicate);
+        if (distinct) {
+            return new DistinctPredicatedProxyNodeIterator<>(until, iterator, predicate);
+        } else {
+            return new PredicatedProxyNodeIterator<>(until, iterator, predicate);
+        }
     }
 
     @SuppressWarnings("unchecked")
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/NodeIterable.java	Tue May 22 14:34:18 2012 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/NodeIterable.java	Wed May 23 10:09:39 2012 +0200
@@ -46,6 +46,9 @@
     public FilteredNodeIterable<T> nonNull() {
         return new FilteredNodeIterable<>(this).and(NodePredicates.isNotNull());
     }
+    public FilteredNodeIterable<T> distinct() {
+        return new FilteredNodeIterable<>(this).distinct();
+    }
     public List<T> snapshot() {
         ArrayList<T> list = new ArrayList<>();
         for (T n : this) {
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/PredicatedProxyNodeIterator.java	Tue May 22 14:34:18 2012 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/PredicatedProxyNodeIterator.java	Wed May 23 10:09:39 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
 
 import com.oracle.graal.graph.*;
 
-public final class PredicatedProxyNodeIterator<T extends Node> extends NodeIterator<T> {
+public class PredicatedProxyNodeIterator<T extends Node> extends NodeIterator<T> {
     private final Iterator<T> iterator;
     private final NodePredicate predicate;
     private final NodePredicate until;
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/ValueAnchorNode.java	Tue May 22 14:34:18 2012 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/ValueAnchorNode.java	Wed May 23 10:09:39 2012 +0200
@@ -44,8 +44,10 @@
         // Nothing to emit, since this node is used for structural purposes only.
     }
 
-    public void addAnchoredValue(ValueNode value) {
-        this.dependencies().add(value);
+    public void addAnchoredNode(Node value) {
+        if (!this.dependencies().contains(value)) {
+            this.dependencies().add(value);
+        }
     }
 
     @Override
@@ -53,8 +55,8 @@
         if (this.predecessor() instanceof ValueAnchorNode) {
             // transfer values and remove
             ValueAnchorNode previousAnchor = (ValueAnchorNode) this.predecessor();
-            for (Node node : dependencies().nonNull()) {
-                previousAnchor.dependencies().add(node);
+            for (Node node : dependencies().nonNull().distinct()) {
+                previousAnchor.addAnchoredNode(node);
             }
             return null;
         }