001/*
002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.virtual.phases.ea;
024
025import static com.oracle.graal.compiler.common.GraalOptions.*;
026import static com.oracle.graal.virtual.phases.ea.PartialEscapePhase.Options.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.options.*;
031
032import com.oracle.graal.graph.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.cfg.*;
035import com.oracle.graal.nodes.spi.*;
036import com.oracle.graal.nodes.virtual.*;
037import com.oracle.graal.phases.*;
038import com.oracle.graal.phases.common.*;
039import com.oracle.graal.phases.schedule.*;
040import com.oracle.graal.phases.tiers.*;
041
042public class PartialEscapePhase extends EffectsPhase<PhaseContext> {
043
044    static class Options {
045        //@formatter:off
046        @Option(help = "", type = OptionType.Debug)
047        public static final OptionValue<Boolean> OptEarlyReadElimination = new OptionValue<>(true);
048        //@formatter:on
049    }
050
051    private final boolean readElimination;
052    private final BasePhase<PhaseContext> cleanupPhase;
053
054    public PartialEscapePhase(boolean iterative, CanonicalizerPhase canonicalizer) {
055        this(iterative, OptEarlyReadElimination.getValue(), canonicalizer, null);
056    }
057
058    public PartialEscapePhase(boolean iterative, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase) {
059        this(iterative, OptEarlyReadElimination.getValue(), canonicalizer, cleanupPhase);
060    }
061
062    public PartialEscapePhase(boolean iterative, boolean readElimination, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase) {
063        super(iterative ? EscapeAnalysisIterations.getValue() : 1, canonicalizer);
064        this.readElimination = readElimination;
065        this.cleanupPhase = cleanupPhase;
066    }
067
068    @Override
069    protected void postIteration(StructuredGraph graph, PhaseContext context, Set<Node> changedNodes) {
070        super.postIteration(graph, context, changedNodes);
071        if (cleanupPhase != null) {
072            cleanupPhase.apply(graph, context);
073        }
074    }
075
076    @Override
077    protected void run(StructuredGraph graph, PhaseContext context) {
078        if (VirtualUtil.matches(graph, EscapeAnalyzeOnly.getValue())) {
079            if (readElimination || graph.getNodes().filterInterface(VirtualizableAllocation.class).isNotEmpty()) {
080                runAnalysis(graph, context);
081            }
082        }
083    }
084
085    @Override
086    protected Closure<?> createEffectsClosure(PhaseContext context, SchedulePhase schedule, ControlFlowGraph cfg) {
087        for (VirtualObjectNode virtual : cfg.graph.getNodes(VirtualObjectNode.TYPE)) {
088            virtual.resetObjectId();
089        }
090        assert schedule != null;
091        if (readElimination) {
092            return new PEReadEliminationClosure(schedule, context.getMetaAccess(), context.getConstantReflection());
093        } else {
094            return new PartialEscapeClosure.Final(schedule, context.getMetaAccess(), context.getConstantReflection());
095        }
096    }
097}