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.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.nodes.StructuredGraph; 35 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; 36 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 37 import org.graalvm.compiler.nodes.spi.CoreProviders; 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.EconomicSetNodeEventListener; 42 import org.graalvm.compiler.phases.graph.ReentrantBlockIterator; 43 import org.graalvm.compiler.phases.schedule.SchedulePhase; 44 45 public abstract class EffectsPhase<CoreProvidersT extends CoreProviders> extends BasePhase<CoreProvidersT> { 46 47 public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> { 48 49 public abstract boolean hasChanged(); 50 51 public abstract boolean needsApplyEffects(); 52 53 public abstract void applyEffects(); 54 } 55 56 private final int maxIterations; 57 protected final CanonicalizerPhase canonicalizer; 58 private final boolean unscheduled; 59 60 protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer) { 61 this(maxIterations, canonicalizer, false); 62 } 63 64 protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled) { 65 this.maxIterations = maxIterations; 66 this.canonicalizer = canonicalizer; 67 this.unscheduled = unscheduled; 68 } 69 70 @Override 71 protected void run(StructuredGraph graph, CoreProvidersT context) { 72 runAnalysis(graph, context); 73 } 74 75 @SuppressWarnings("try") 76 public boolean runAnalysis(StructuredGraph graph, CoreProvidersT context) { 77 boolean changed = false; 78 CompilationAlarm compilationAlarm = CompilationAlarm.current(); 79 DebugContext debug = graph.getDebug(); 80 for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) { 81 try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "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 (DebugContext.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 EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener(); 99 try (NodeEventScope nes = graph.trackNodeEvents(listener)) { 100 closure.applyEffects(); 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 109 postIteration(graph, context, listener.getNodes()); 110 } 111 112 if (closure.hasChanged()) { 113 changed = true; 114 } else { 115 break; 116 } 117 } catch (Throwable t) { 118 throw debug.handle(t); 119 } 120 } 121 } 122 return changed; 123 } 124 125 protected void postIteration(final StructuredGraph graph, final CoreProvidersT context, EconomicSet<Node> changedNodes) { 126 if (canonicalizer != null) { 127 canonicalizer.applyIncremental(graph, context, changedNodes); 128 } 129 } 130 131 protected abstract Closure<?> createEffectsClosure(CoreProvidersT context, ScheduleResult schedule, ControlFlowGraph cfg); 132 }