1 /*
   2  * Copyright (c) 2011, 2017, 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.phases.common.DeadCodeEliminationPhase.Optionality.Required;
  28 
  29 import jdk.internal.vm.compiler.collections.EconomicSet;
  30 import org.graalvm.compiler.core.common.util.CompilationAlarm;
  31 import org.graalvm.compiler.debug.DebugContext;
  32 import org.graalvm.compiler.graph.Graph.NodeEventScope;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.spi.Simplifiable;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  37 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  38 import org.graalvm.compiler.phases.BasePhase;
  39 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  40 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  41 import org.graalvm.compiler.phases.common.util.HashSetNodeEventListener;
  42 import org.graalvm.compiler.phases.graph.ReentrantBlockIterator;
  43 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  44 import org.graalvm.compiler.phases.tiers.PhaseContext;
  45 
  46 public abstract class EffectsPhase<PhaseContextT extends PhaseContext> extends BasePhase<PhaseContextT> {
  47 
  48     public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> {
  49 
  50         public abstract boolean hasChanged();
  51 
  52         public abstract boolean needsApplyEffects();
  53 
  54         public abstract void applyEffects();
  55     }
  56 
  57     private final int maxIterations;
  58     protected final CanonicalizerPhase canonicalizer;
  59     private final boolean unscheduled;
  60 
  61     protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer) {
  62         this(maxIterations, canonicalizer, false);
  63     }
  64 
  65     protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled) {
  66         this.maxIterations = maxIterations;
  67         this.canonicalizer = canonicalizer;
  68         this.unscheduled = unscheduled;
  69     }
  70 
  71     @Override
  72     protected void run(StructuredGraph graph, PhaseContextT context) {
  73         runAnalysis(graph, context);
  74     }
  75 
  76     @SuppressWarnings("try")
  77     public boolean runAnalysis(StructuredGraph graph, PhaseContextT context) {
  78         boolean changed = false;
  79         CompilationAlarm compilationAlarm = CompilationAlarm.current();
  80         DebugContext debug = graph.getDebug();
  81         for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) {
  82             try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) {
  83                 ScheduleResult schedule;
  84                 ControlFlowGraph cfg;
  85                 if (unscheduled) {
  86                     schedule = null;
  87                     cfg = ControlFlowGraph.compute(graph, true, true, false, false);
  88                 } else {
  89                     new SchedulePhase(SchedulePhase.SchedulingStrategy.EARLIEST).apply(graph, false);
  90                     schedule = graph.getLastSchedule();
  91                     cfg = schedule.getCFG();
  92                 }
  93                 try (DebugContext.Scope scheduleScope = debug.scope("EffectsPhaseWithSchedule", schedule)) {
  94                     Closure<?> closure = createEffectsClosure(context, schedule, cfg);
  95                     ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
  96 
  97                     if (closure.needsApplyEffects()) {
  98                         // apply the effects collected during this iteration
  99                         HashSetNodeEventListener listener = new HashSetNodeEventListener();
 100                         try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
 101                             closure.applyEffects();
 102                         }
 103 
 104                         if (debug.isDumpEnabled(DebugContext.VERBOSE_LEVEL)) {
 105                             debug.dump(DebugContext.VERBOSE_LEVEL, graph, "%s iteration", getName());
 106                         }
 107 
 108                         new DeadCodeEliminationPhase(Required).apply(graph);
 109 
 110                         EconomicSet<Node> changedNodes = listener.getNodes();
 111                         for (Node node : graph.getNodes()) {
 112                             if (node instanceof Simplifiable) {
 113                                 changedNodes.add(node);
 114                             }
 115                         }
 116                         postIteration(graph, context, changedNodes);
 117                     }
 118 
 119                     if (closure.hasChanged()) {
 120                         changed = true;
 121                     } else {
 122                         break;
 123                     }
 124                 } catch (Throwable t) {
 125                     throw debug.handle(t);
 126                 }
 127             }
 128         }
 129         return changed;
 130     }
 131 
 132     protected void postIteration(final StructuredGraph graph, final PhaseContextT context, EconomicSet<Node> changedNodes) {
 133         if (canonicalizer != null) {
 134             canonicalizer.applyIncremental(graph, context, changedNodes);
 135         }
 136     }
 137 
 138     protected abstract Closure<?> createEffectsClosure(PhaseContextT context, ScheduleResult schedule, ControlFlowGraph cfg);
 139 }