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