1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  29 
  30 import java.util.Iterator;
  31 
  32 import org.junit.Assert;
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.graph.Node;
  36 import org.graalvm.compiler.graph.NodeClass;
  37 import org.graalvm.compiler.graph.NodeInputList;
  38 import org.graalvm.compiler.graph.NodeSuccessorList;
  39 import org.graalvm.compiler.graph.Position;
  40 import org.graalvm.compiler.graph.iterators.NodeIterable;
  41 import org.graalvm.compiler.nodeinfo.NodeInfo;
  42 import org.graalvm.compiler.nodes.ConstantNode;
  43 import org.graalvm.compiler.nodes.EndNode;
  44 import org.graalvm.compiler.nodes.ValueNode;
  45 import org.graalvm.compiler.nodes.calc.FloatingNode;
  46 
  47 public class NodePosIteratorTest extends GraalCompilerTest {
  48 
  49     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  50     static final class TestNode extends Node {
  51         public static final NodeClass<TestNode> TYPE = NodeClass.create(TestNode.class);
  52         @Successor Node s1;
  53         @Successor Node s2;
  54         @Successor NodeSuccessorList<Node> stail;
  55 
  56         @Input NodeInputList<ValueNode> itail;
  57         @Input ConstantNode i1;
  58         @Input FloatingNode i2;
  59 
  60         protected TestNode() {
  61             super(TYPE);
  62         }
  63 
  64     }
  65 
  66     @Test
  67     public void testInputs() {
  68         TestNode n = new TestNode();
  69 
  70         ConstantNode i1 = ConstantNode.forInt(1);
  71         ConstantNode i2 = ConstantNode.forDouble(1.0d);
  72         ConstantNode i3 = ConstantNode.forInt(4);
  73         ConstantNode i4 = ConstantNode.forInt(14);
  74         n.itail = new NodeInputList<>(n, new ValueNode[]{i3, i4});
  75         n.i1 = i1;
  76         n.i2 = i2;
  77 
  78         NodeIterable<Node> inputs = n.inputs();
  79 
  80         Iterator<Node> iterator = inputs.iterator();
  81         Assert.assertTrue(iterator.hasNext());
  82         Assert.assertTrue(iterator.hasNext());
  83         Assert.assertEquals(iterator.next(), i1);
  84         Assert.assertTrue(iterator.hasNext());
  85         Assert.assertTrue(iterator.hasNext());
  86         Assert.assertEquals(iterator.next(), i2);
  87         Assert.assertTrue(iterator.hasNext());
  88         Assert.assertTrue(iterator.hasNext());
  89         Assert.assertEquals(iterator.next(), i3);
  90         Assert.assertTrue(iterator.hasNext());
  91         Assert.assertTrue(iterator.hasNext());
  92         Assert.assertEquals(iterator.next(), i4);
  93         Assert.assertFalse(iterator.hasNext());
  94         Assert.assertFalse(iterator.hasNext());
  95 
  96         Iterator<Position> positionIterator = n.inputPositions().iterator();
  97         Assert.assertTrue(positionIterator.hasNext());
  98         Assert.assertTrue(positionIterator.hasNext());
  99         Assert.assertEquals("ConstantNode:i1", positionIterator.next().toString());
 100         Assert.assertTrue(positionIterator.hasNext());
 101         Assert.assertTrue(positionIterator.hasNext());
 102         Assert.assertEquals("FloatingNode:i2", positionIterator.next().toString());
 103         Assert.assertTrue(positionIterator.hasNext());
 104         Assert.assertTrue(positionIterator.hasNext());
 105         Assert.assertEquals("NodeInputList:itail[0]", positionIterator.next().toString());
 106         Assert.assertTrue(positionIterator.hasNext());
 107         Assert.assertTrue(positionIterator.hasNext());
 108         Assert.assertEquals("NodeInputList:itail[1]", positionIterator.next().toString());
 109         Assert.assertFalse(positionIterator.hasNext());
 110         Assert.assertFalse(positionIterator.hasNext());
 111 
 112         iterator = inputs.iterator();
 113         n.i1 = i4;
 114         Assert.assertTrue(iterator.hasNext());
 115         Assert.assertEquals(iterator.next(), i4);
 116         n.i2 = i1;
 117         Assert.assertTrue(iterator.hasNext());
 118         Assert.assertEquals(iterator.next(), i1);
 119         Assert.assertTrue(iterator.hasNext());
 120         Assert.assertEquals(iterator.next(), i3);
 121         n.itail.initialize(1, i4);
 122         Assert.assertTrue(iterator.hasNext());
 123         Assert.assertEquals(iterator.next(), i4);
 124         Assert.assertFalse(iterator.hasNext());
 125 
 126         iterator = inputs.iterator();
 127         n.i1 = null;
 128         n.i2 = i2;
 129         n.itail.initialize(0, null);
 130         n.itail.initialize(1, i4);
 131         Assert.assertTrue(iterator.hasNext());
 132         Assert.assertEquals(iterator.next(), i2);
 133         Assert.assertTrue(iterator.hasNext());
 134         Assert.assertEquals(iterator.next(), i4);
 135         Assert.assertFalse(iterator.hasNext());
 136     }
 137 
 138     @Test
 139     public void testSuccessors() {
 140         TestNode n = new TestNode();
 141         EndNode s1 = new EndNode();
 142         EndNode s2 = new EndNode();
 143         EndNode s3 = new EndNode();
 144         EndNode s4 = new EndNode();
 145         n.s1 = s1;
 146         n.s2 = s2;
 147         n.stail = new NodeSuccessorList<>(n, new Node[]{s3, s4});
 148 
 149         NodeIterable<Node> successors = n.successors();
 150         Iterator<Node> iterator = successors.iterator();
 151         Assert.assertTrue(iterator.hasNext());
 152         Assert.assertTrue(iterator.hasNext());
 153         Assert.assertEquals(iterator.next(), s1);
 154         Assert.assertTrue(iterator.hasNext());
 155         Assert.assertTrue(iterator.hasNext());
 156         Assert.assertEquals(iterator.next(), s2);
 157         Assert.assertTrue(iterator.hasNext());
 158         Assert.assertTrue(iterator.hasNext());
 159         Assert.assertEquals(iterator.next(), s3);
 160         Assert.assertTrue(iterator.hasNext());
 161         Assert.assertTrue(iterator.hasNext());
 162         Assert.assertEquals(iterator.next(), s4);
 163         Assert.assertFalse(iterator.hasNext());
 164         Assert.assertFalse(iterator.hasNext());
 165 
 166         Iterator<Position> positionIterator = n.successorPositions().iterator();
 167         Assert.assertTrue(positionIterator.hasNext());
 168         Assert.assertTrue(positionIterator.hasNext());
 169         Assert.assertEquals(Node.class.getSimpleName() + ":s1", positionIterator.next().toString());
 170         Assert.assertTrue(positionIterator.hasNext());
 171         Assert.assertTrue(positionIterator.hasNext());
 172         Assert.assertEquals(Node.class.getSimpleName() + ":s2", positionIterator.next().toString());
 173         Assert.assertTrue(positionIterator.hasNext());
 174         Assert.assertTrue(positionIterator.hasNext());
 175         Assert.assertEquals(NodeSuccessorList.class.getSimpleName() + ":stail[0]", positionIterator.next().toString());
 176         Assert.assertTrue(positionIterator.hasNext());
 177         Assert.assertTrue(positionIterator.hasNext());
 178         Assert.assertEquals(NodeSuccessorList.class.getSimpleName() + ":stail[1]", positionIterator.next().toString());
 179         Assert.assertFalse(positionIterator.hasNext());
 180         Assert.assertFalse(positionIterator.hasNext());
 181 
 182         iterator = successors.iterator();
 183         n.s1 = s4;
 184         Assert.assertTrue(iterator.hasNext());
 185         Assert.assertEquals(iterator.next(), s4);
 186         n.s2 = s1;
 187         Assert.assertTrue(iterator.hasNext());
 188         Assert.assertEquals(iterator.next(), s1);
 189         Assert.assertTrue(iterator.hasNext());
 190         Assert.assertEquals(iterator.next(), s3);
 191         n.stail.initialize(1, s4);
 192         Assert.assertTrue(iterator.hasNext());
 193         Assert.assertEquals(iterator.next(), s4);
 194         Assert.assertFalse(iterator.hasNext());
 195 
 196         iterator = successors.iterator();
 197         n.s1 = null;
 198         n.s2 = s2;
 199         n.stail.initialize(0, null);
 200         n.stail.initialize(1, s4);
 201         Assert.assertTrue(iterator.hasNext());
 202         Assert.assertEquals(iterator.next(), s2);
 203         Assert.assertTrue(iterator.hasNext());
 204         Assert.assertEquals(iterator.next(), s4);
 205         Assert.assertFalse(iterator.hasNext());
 206     }
 207 }