/*
 * Copyright (c) 2011, 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;

import java.util.regex.*;

import com.oracle.graal.debug.*;
import com.oracle.graal.debug.Debug.Scope;
import com.oracle.graal.debug.internal.*;
import com.oracle.graal.nodes.*;

/**
 * Base class for all compiler phases. Subclasses should be stateless. There will be one global
 * instance for each compiler phase that is shared for all compilations. VM-, target- and
 * compilation-specific data can be passed with a context object.
 */
public abstract class BasePhase<C> {

    private final String name;

    private final DebugTimer phaseTimer;
    private final DebugMetric phaseMetric;

    private static final Pattern NAME_PATTERN = Pattern.compile("[A-Z][A-Za-z0-9]+");

    private static boolean checkName(String name) {
        assert NAME_PATTERN.matcher(name).matches() : "illegal phase name: " + name;
        return true;
    }

    protected BasePhase() {
        String nm = this.getClass().getSimpleName();
        if (nm.endsWith("Phase")) {
            name = nm.substring(0, nm.length() - "Phase".length());
        } else {
            name = nm;
        }
        assert checkName(name);
        phaseTimer = Debug.timer("PhaseTime_" + name);
        phaseMetric = Debug.metric("PhaseCount_" + name);
    }

    protected BasePhase(String name) {
        assert checkName(name);
        this.name = name;
        phaseTimer = Debug.timer("PhaseTime_" + name);
        phaseMetric = Debug.metric("PhaseCount_" + name);
    }

    protected String getDetailedName() {
        return getName();
    }

    public final void apply(final StructuredGraph graph, final C context) {
        apply(graph, context, true);
    }

    public final void apply(final StructuredGraph graph, final C context, final boolean dumpGraph) {
        try (TimerCloseable a = phaseTimer.start(); Scope s = Debug.scope(name, this)) {
            BasePhase.this.run(graph, context);
            phaseMetric.increment();
            if (dumpGraph) {
                Debug.dump(graph, "After phase %s", name);
            }
            assert graph.verify();
        } catch (Throwable t) {
            throw Debug.handle(t);
        }
    }

    public final String getName() {
        return name;
    }

    protected abstract void run(StructuredGraph graph, C context);
}

@@ -1569,28 +1569,718 @@
     
-private final String
+/**%0A     * Phase name lazily computed from the phase class.%0A     */%0A    class Name extends LazyName %7B%0A%0A        @Override%0A        public String createString() %7B%0A            String s = BasePhase.this.getClass().getSimpleName();%0A            return s.substring(0, s.length() - %22Phase%22.length());%0A        %7D%0A    %7D%0A%0A    /**%0A     * Lazily computed debug value name composed of a prefix and a phase's name.%0A     */%0A    class DebugValueName extends LazyName %7B%0A        final String prefix;%0A%0A        public DebugValueName(String prefix) %7B%0A            this.prefix = prefix;%0A        %7D%0A%0A        @Override%0A        public String createString() %7B%0A            return prefix + name;%0A        %7D%0A    %7D%0A%0A    private final CharSequence
  nam
@@ -2660,206 +2660,26 @@
     
-String nm = this.getClass().getSimpleName();%0A        if (nm.endsWith(%22Phase%22)) %7B%0A            name = nm.substring(0, nm.length() - %22Phase%22.length());%0A        %7D else %7B%0A            name = nm;%0A        %7D
+name = new Name();
 %0A   
@@ -2696,32 +2696,43 @@
 t checkName(name
+.toString()
 );%0A        phase
@@ -2743,32 +2743,51 @@
 r = Debug.timer(
+new DebugValueName(
 %22PhaseTime_%22 + n
@@ -2774,39 +2774,33 @@
 ame(%22PhaseTime_%22
- + name
+)
 );%0A        phase
@@ -2813,32 +2813,51 @@
  = Debug.metric(
+new DebugValueName(
 %22PhaseCount_%22 + 
@@ -2845,39 +2845,33 @@
 me(%22PhaseCount_%22
- + name
+)
 );%0A    %7D%0A%0A    pr
@@ -2994,16 +2994,35 @@
 g.timer(
+new DebugValueName(
 %22PhaseTi
@@ -3025,23 +3025,17 @@
 seTime_%22
- + name
+)
 );%0A     
@@ -3064,16 +3064,35 @@
 .metric(
+new DebugValueName(
 %22PhaseCo
@@ -3096,23 +3096,17 @@
 eCount_%22
- + name
+)
 );%0A    %7D
@@ -3121,22 +3121,28 @@
 otected 
-String
+CharSequence
  getDeta
@@ -3820,22 +3820,28 @@
 c final 
-String
+CharSequence
  getName

use lazy names in BasePhase