/*
 * Copyright (c) 2013, 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.phases.common;

import java.util.*;

import com.oracle.graal.graph.Graph.Mark;
import com.oracle.graal.graph.Graph.NodeChangedListener;
import com.oracle.graal.graph.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.phases.*;
import com.oracle.graal.phases.tiers.*;

/**
 * A phase suite that applies {@linkplain CanonicalizerPhase canonicalization} to a graph after all
 * phases in the suite have been applied if any of the phases changed the graph.
 */
public class IncrementalCanonicalizerPhase<C extends PhaseContext> extends PhaseSuite<C> {

    private final CanonicalizerPhase canonicalizer;

    public IncrementalCanonicalizerPhase(CanonicalizerPhase canonicalizer) {
        this.canonicalizer = canonicalizer;
    }

    @Override
    protected void run(StructuredGraph graph, C context) {
        Mark newNodesMark = graph.getMark();
        final Set<Node> changedNodes = new HashSet<>();

        NodeChangedListener listener = new NodeChangedListener() {

            @Override
            public void nodeChanged(Node node) {
                changedNodes.add(node);
            }
        };
        graph.trackInputChange(listener);
        graph.trackUsagesDroppedZero(listener);

        super.run(graph, context);

        graph.stopTrackingInputChange();
        graph.stopTrackingUsagesDroppedZero();

        if (graph.getMark() != newNodesMark || !changedNodes.isEmpty()) {
            canonicalizer.applyIncremental(graph, context, changedNodes, newNodesMark, false);
        }
    }
}

@@ -1089,29 +1089,8 @@
 n;%0A%0A
-import java.util.*;%0A%0A
 impo
@@ -1131,98 +1131,8 @@
 rk;%0A
-import com.oracle.graal.graph.Graph.NodeChangedListener;%0Aimport com.oracle.graal.graph.*;%0A
 impo
@@ -1190,24 +1190,70 @@
 l.phases.*;%0A
+import com.oracle.graal.phases.common.util.*;%0A
 import com.o
@@ -1865,73 +1865,24 @@
 ();%0A
- 
+%0A
        
-final Set%3CNode%3E changedNodes = new HashSet%3C%3E();%0A%0A        
+ HashSet
 Node
@@ -1879,33 +1879,32 @@
 ashSetNodeChange
-d
 Listener listene
@@ -1911,16 +1911,23 @@
 r = new 
+HashSet
 NodeChan
@@ -1928,17 +1928,16 @@
 deChange
-d
 Listener
@@ -1942,146 +1942,8 @@
 er()
- %7B%0A%0A            @Override%0A            public void nodeChanged(Node node) %7B%0A                changedNodes.add(node);%0A            %7D%0A        %7D
 ;%0A  
@@ -2204,17 +2204,29 @@
 ark %7C%7C !
-c
+listener.getC
 hangedNo
@@ -2228,16 +2228,18 @@
 gedNodes
+()
 .isEmpty
@@ -2303,17 +2303,29 @@
 ontext, 
-c
+listener.getC
 hangedNo
@@ -2327,16 +2327,18 @@
 gedNodes
+()
 , newNod

use HashSetNodeChangeListener