view graal/GraalCompiler/src/com/sun/c1x/gen/PhiSimplifier.java @ 2808:189ffb7d1d84

enable PhiSimplifier, schedule values used in a merge's stateBefore in the dominator
author Lukas Stadler <lukas.stadler@jku.at>
date Mon, 30 May 2011 13:42:23 +0200
parents 6d14aa4fbf90
children 015be60afcf3
line wrap: on
line source

/*
 * Copyright (c) 2009, 2011, 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.sun.c1x.gen;

import com.oracle.graal.graph.*;
import com.sun.c1x.graph.*;
import com.sun.c1x.ir.*;

/**
 * The {@code PhiSimplifier} class is a helper class that can reduce phi instructions.
 */
public final class PhiSimplifier {

    public PhiSimplifier(IR ir) {
        for (Node n : ir.compilation.graph.getNodes()) {
            if (n instanceof Phi) {
                simplify((Phi) n);
            }
        }
    }

    private Value simplify(Value x) {
        if (x == null || !(x instanceof Phi)) {
            return x;
        }
        Phi phi = (Phi) x;

        if (phi.valueCount() == 1 && !phi.checkFlag(Value.Flag.PhiCannotSimplify)) {
            return (Value) phi.replace(phi.valueAt(0));
        }

        if (phi.checkFlag(Value.Flag.PhiCannotSimplify)) {
            // already tried, cannot simplify this phi
            return phi;
        } else if (phi.checkFlag(Value.Flag.PhiVisited)) {
            // break cycles in phis
            return phi;
        } else if (phi.isIllegal()) {
            // don't bother with illegals
            return phi;
        } else {
            // attempt to simplify the phi by recursively simplifying its operands
            phi.setFlag(Value.Flag.PhiVisited);
            Value phiSubst = null;
            int max = phi.valueCount();
            boolean cannotSimplify = false;
            for (int i = 0; i < max; i++) {
                Value oldInstr = phi.valueAt(i);

                if (oldInstr == null || oldInstr.isIllegal() || oldInstr.isDeadPhi()) {
                    // if one operand is illegal, make the entire phi illegal
                    phi.makeDead();
                    phi.clearFlag(Value.Flag.PhiVisited);
                    return phi;
                }

                Value newInstr = simplify(oldInstr);

                if (newInstr == null || newInstr.isIllegal() || newInstr.isDeadPhi()) {
                    // if the subst instruction is illegal, make the entire phi illegal
                    phi.makeDead();
                    phi.clearFlag(Value.Flag.PhiVisited);
                    return phi;
                }

                // attempt to simplify this operand
                if (!cannotSimplify) {

                    if (newInstr != phi && newInstr != phiSubst) {
                        if (phiSubst == null) {
                            phiSubst = newInstr;
                            continue;
                        }
                        // this phi cannot be simplified
                        cannotSimplify = true;
                    }
                }
            }
            if (cannotSimplify) {
                phi.setFlag(Value.Flag.PhiCannotSimplify);
                phi.clearFlag(Value.Flag.PhiVisited);
                return phi;
            }

            // successfully simplified the phi
            assert phiSubst != null : "illegal phi function";
            phi.clearFlag(Value.Flag.PhiVisited);

            phi.replace(phiSubst);
//            System.out.printf("replaced phi with %d inputs\n", max);

            return phiSubst;
        }
    }
}