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 package org.graalvm.compiler.virtual.phases.ea;
  24 
  25 import static org.graalvm.compiler.debug.Debug.isEnabled;
  26 import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
  27 
  28 import org.graalvm.compiler.core.common.util.CompilationAlarm;
  29 import org.graalvm.compiler.debug.Debug;
  30 import org.graalvm.compiler.debug.Debug.Scope;
  31 import org.graalvm.compiler.graph.Graph.NodeEventScope;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.graph.spi.Simplifiable;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
  36 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  37 import org.graalvm.compiler.phases.BasePhase;
  38 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  39 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  40 import org.graalvm.compiler.phases.common.util.HashSetNodeEventListener;
  41 import org.graalvm.compiler.phases.graph.ReentrantBlockIterator;
  42 import org.graalvm.compiler.phases.schedule.SchedulePhase;
  43 import org.graalvm.compiler.phases.tiers.PhaseContext;
  44 import org.graalvm.util.EconomicSet;
  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         for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) {
  81             try (Scope s = Debug.scope(isEnabled() ? "iteration " + iteration : null)) {
  82                 ScheduleResult schedule;
  83                 ControlFlowGraph cfg;
  84                 if (unscheduled) {
  85                     schedule = null;
  86                     cfg = ControlFlowGraph.compute(graph, true, true, false, false);
  87                 } else {
  88                     new SchedulePhase(SchedulePhase.SchedulingStrategy.EARLIEST).apply(graph, false);
  89                     schedule = graph.getLastSchedule();
  90                     cfg = schedule.getCFG();
  91                 }
  92                 try (Scope scheduleScope = Debug.scope("EffectsPhaseWithSchedule", schedule)) {
  93                     Closure<?> closure = createEffectsClosure(context, schedule, cfg);
  94                     ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
  95 
  96                     if (closure.needsApplyEffects()) {
  97                         // apply the effects collected during this iteration
  98                         HashSetNodeEventListener listener = new HashSetNodeEventListener();
  99                         try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
 100                             closure.applyEffects();
 101                         }
 102 
 103                         if (Debug.isDumpEnabled(Debug.INFO_LEVEL)) {
 104                             Debug.dump(Debug.DETAILED_LEVEL, graph, "%s iteration", getName());
 105                         }
 106 
 107                         new DeadCodeEliminationPhase(Required).apply(graph);
 108 
 109                         EconomicSet<Node> changedNodes = listener.getNodes();
 110                         for (Node node : graph.getNodes()) {
 111                             if (node instanceof Simplifiable) {
 112                                 changedNodes.add(node);
 113                             }
 114                         }
 115                         postIteration(graph, context, changedNodes);
 116                     }
 117 
 118                     if (closure.hasChanged()) {
 119                         changed = true;
 120                     } else {
 121                         break;
 122                     }
 123                 } catch (Throwable t) {
 124                     throw Debug.handle(t);
 125                 }
 126             }
 127         }
 128         return changed;
 129     }
 130 
 131     protected void postIteration(final StructuredGraph graph, final PhaseContextT context, EconomicSet<Node> changedNodes) {
 132         if (canonicalizer != null) {
 133             canonicalizer.applyIncremental(graph, context, changedNodes);
 134         }
 135     }
 136 
 137     protected abstract Closure<?> createEffectsClosure(PhaseContextT context, ScheduleResult schedule, ControlFlowGraph cfg);
 138 }