PartialEscapePhase.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2011, 2016, 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.virtual.phases.ea;
24
25import static org.graalvm.compiler.core.common.GraalOptions.EscapeAnalysisIterations;
26import static org.graalvm.compiler.core.common.GraalOptions.EscapeAnalyzeOnly;
27
28import org.graalvm.compiler.graph.Node;
29import org.graalvm.compiler.nodes.StructuredGraph;
30import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
31import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
32import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
33import org.graalvm.compiler.options.Option;
34import org.graalvm.compiler.options.OptionKey;
35import org.graalvm.compiler.options.OptionType;
36import org.graalvm.compiler.options.OptionValues;
37import org.graalvm.compiler.phases.BasePhase;
38import org.graalvm.compiler.phases.common.CanonicalizerPhase;
39import org.graalvm.compiler.phases.tiers.PhaseContext;
40import org.graalvm.util.EconomicSet;
41
42public class PartialEscapePhase extends EffectsPhase<PhaseContext> {
43
44    static class Options {
45        //@formatter:off
46        @Option(help = "", type = OptionType.Debug)
47        public static final OptionKey<Boolean> OptEarlyReadElimination = new OptionKey<>(true);
48        //@formatter:on
49    }
50
51    private final boolean readElimination;
52    private final BasePhase<PhaseContext> cleanupPhase;
53
54    public PartialEscapePhase(boolean iterative, CanonicalizerPhase canonicalizer, OptionValues options) {
55        this(iterative, Options.OptEarlyReadElimination.getValue(options), canonicalizer, null, options);
56    }
57
58    public PartialEscapePhase(boolean iterative, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase, OptionValues options) {
59        this(iterative, Options.OptEarlyReadElimination.getValue(options), canonicalizer, cleanupPhase, options);
60    }
61
62    public PartialEscapePhase(boolean iterative, boolean readElimination, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase, OptionValues options) {
63        super(iterative ? EscapeAnalysisIterations.getValue(options) : 1, canonicalizer);
64        this.readElimination = readElimination;
65        this.cleanupPhase = cleanupPhase;
66    }
67
68    @Override
69    protected void postIteration(StructuredGraph graph, PhaseContext context, EconomicSet<Node> changedNodes) {
70        super.postIteration(graph, context, changedNodes);
71        if (cleanupPhase != null) {
72            cleanupPhase.apply(graph, context);
73        }
74    }
75
76    @Override
77    protected void run(StructuredGraph graph, PhaseContext context) {
78        if (VirtualUtil.matches(graph, EscapeAnalyzeOnly.getValue(graph.getOptions()))) {
79            if (readElimination || graph.hasVirtualizableAllocation()) {
80                runAnalysis(graph, context);
81            }
82        }
83    }
84
85    @Override
86    protected Closure<?> createEffectsClosure(PhaseContext context, ScheduleResult schedule, ControlFlowGraph cfg) {
87        for (VirtualObjectNode virtual : cfg.graph.getNodes(VirtualObjectNode.TYPE)) {
88            virtual.resetObjectId();
89        }
90        assert schedule != null;
91        if (readElimination) {
92            return new PEReadEliminationClosure(schedule, context.getMetaAccess(), context.getConstantReflection(), context.getConstantFieldProvider(), context.getLowerer());
93        } else {
94            return new PartialEscapeClosure.Final(schedule, context.getMetaAccess(), context.getConstantReflection(), context.getConstantFieldProvider(), context.getLowerer());
95        }
96    }
97
98    @Override
99    public boolean checkContract() {
100        return false;
101    }
102
103}
104