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  */
  23 package org.graalvm.compiler.virtual.phases.ea;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.EscapeAnalysisIterations;
  26 import static org.graalvm.compiler.core.common.GraalOptions.EscapeAnalyzeOnly;
  27 
  28 import java.util.Set;
  29 
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  33 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  34 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  35 import org.graalvm.compiler.options.Option;
  36 import org.graalvm.compiler.options.OptionType;
  37 import org.graalvm.compiler.options.OptionValue;
  38 import org.graalvm.compiler.phases.BasePhase;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.tiers.PhaseContext;
  41 
  42 public class PartialEscapePhase extends EffectsPhase<PhaseContext> {
  43 
  44     static class Options {
  45         //@formatter:off
  46         @Option(help = "", type = OptionType.Debug)
  47         public static final OptionValue<Boolean> OptEarlyReadElimination = new OptionValue<>(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) {
  55         this(iterative, Options.OptEarlyReadElimination.getValue(), canonicalizer, null);
  56     }
  57 
  58     public PartialEscapePhase(boolean iterative, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase) {
  59         this(iterative, Options.OptEarlyReadElimination.getValue(), canonicalizer, cleanupPhase);
  60     }
  61 
  62     public PartialEscapePhase(boolean iterative, boolean readElimination, CanonicalizerPhase canonicalizer, BasePhase<PhaseContext> cleanupPhase) {
  63         super(iterative ? EscapeAnalysisIterations.getValue() : 1, canonicalizer);
  64         this.readElimination = readElimination;
  65         this.cleanupPhase = cleanupPhase;
  66     }
  67 
  68     @Override
  69     protected void postIteration(StructuredGraph graph, PhaseContext context, Set<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())) {
  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 }