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