1/*
2 * Copyright (c) 2013, 2013, 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 */
23package org.graalvm.compiler.phases.graph;
24
25import java.util.ListIterator;
26
27import org.graalvm.compiler.graph.Node;
28import org.graalvm.compiler.nodes.FixedNode;
29import org.graalvm.compiler.nodes.FixedWithNextNode;
30import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
31import org.graalvm.compiler.nodes.cfg.Block;
32
33/**
34 * Iterates over a list of nodes, which usually comes from
35 * {@link ScheduleResult#getBlockToNodesMap()}.
36 *
37 * While iterating, it is possible to {@link #insert(FixedNode, FixedWithNextNode) insert} and
38 * {@link #replaceCurrent(FixedWithNextNode) replace} nodes.
39 */
40public abstract class ScheduledNodeIterator {
41
42    private FixedWithNextNode lastFixed;
43    private FixedWithNextNode reconnect;
44    private ListIterator<Node> iterator;
45
46    public void processNodes(Block block, ScheduleResult schedule) {
47        lastFixed = block.getBeginNode();
48        assert lastFixed != null;
49        reconnect = null;
50        iterator = schedule.nodesFor(block).listIterator();
51
52        while (iterator.hasNext()) {
53            Node node = iterator.next();
54            if (!node.isAlive()) {
55                continue;
56            }
57            if (reconnect != null && node instanceof FixedNode) {
58                reconnect.setNext((FixedNode) node);
59                reconnect = null;
60            }
61            if (node instanceof FixedWithNextNode) {
62                lastFixed = (FixedWithNextNode) node;
63            }
64            processNode(node);
65        }
66        if (reconnect != null) {
67            assert block.getSuccessorCount() == 1;
68            reconnect.setNext(block.getFirstSuccessor().getBeginNode());
69        }
70    }
71
72    protected void insert(FixedNode start, FixedWithNextNode end) {
73        this.lastFixed.setNext(start);
74        this.lastFixed = end;
75        this.reconnect = end;
76    }
77
78    protected void replaceCurrent(FixedWithNextNode newNode) {
79        Node current = iterator.previous();
80        iterator.next(); // needed because of the previous() call
81        current.replaceAndDelete(newNode);
82        insert(newNode, newNode);
83        iterator.set(newNode);
84    }
85
86    protected abstract void processNode(Node node);
87}
88