1/* 2 * Copyright (c) 2011, 2012, 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 */ 23package org.graalvm.compiler.virtual.phases.ea; 24 25import static org.graalvm.compiler.debug.Debug.isEnabled; 26import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required; 27 28import java.util.Set; 29 30import org.graalvm.compiler.core.common.util.CompilationAlarm; 31import org.graalvm.compiler.debug.Debug; 32import org.graalvm.compiler.debug.Debug.Scope; 33import org.graalvm.compiler.graph.Graph.NodeEventScope; 34import org.graalvm.compiler.graph.Node; 35import org.graalvm.compiler.graph.spi.Simplifiable; 36import org.graalvm.compiler.nodes.StructuredGraph; 37import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; 38import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 39import org.graalvm.compiler.phases.BasePhase; 40import org.graalvm.compiler.phases.common.CanonicalizerPhase; 41import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 42import org.graalvm.compiler.phases.common.util.HashSetNodeEventListener; 43import org.graalvm.compiler.phases.graph.ReentrantBlockIterator; 44import org.graalvm.compiler.phases.schedule.SchedulePhase; 45import org.graalvm.compiler.phases.tiers.PhaseContext; 46 47public abstract class EffectsPhase<PhaseContextT extends PhaseContext> extends BasePhase<PhaseContextT> { 48 49 public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> { 50 51 public abstract boolean hasChanged(); 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, PhaseContextT context) { 72 runAnalysis(graph, context); 73 } 74 75 @SuppressWarnings("try") 76 public boolean runAnalysis(final StructuredGraph graph, final PhaseContextT context) { 77 boolean changed = false; 78 boolean stop = false; 79 for (int iteration = 0; !stop && iteration < maxIterations && !CompilationAlarm.hasExpired(); iteration++) { 80 try (Scope s = Debug.scope(isEnabled() ? "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 (Scope scheduleScope = Debug.scope("EffectsPhaseWithSchedule", schedule)) { 92 Closure<?> closure = createEffectsClosure(context, schedule, cfg); 93 ReentrantBlockIterator.apply(closure, cfg.getStartBlock()); 94 95 if (closure.hasChanged()) { 96 changed = true; 97 } else { 98 stop = true; 99 } 100 101 // apply the effects collected during this iteration 102 HashSetNodeEventListener listener = new HashSetNodeEventListener(); 103 try (NodeEventScope nes = graph.trackNodeEvents(listener)) { 104 closure.applyEffects(); 105 } 106 107 if (Debug.isDumpEnabled(Debug.INFO_LOG_LEVEL)) { 108 Debug.dump(Debug.INFO_LOG_LEVEL, graph, "%s iteration", getName()); 109 } 110 111 new DeadCodeEliminationPhase(Required).apply(graph); 112 113 Set<Node> changedNodes = listener.getNodes(); 114 for (Node node : graph.getNodes()) { 115 if (node instanceof Simplifiable) { 116 changedNodes.add(node); 117 } 118 } 119 postIteration(graph, context, changedNodes); 120 } catch (Throwable t) { 121 throw Debug.handle(t); 122 } 123 } 124 } 125 return changed; 126 } 127 128 protected void postIteration(final StructuredGraph graph, final PhaseContextT context, Set<Node> changedNodes) { 129 if (canonicalizer != null) { 130 canonicalizer.applyIncremental(graph, context, changedNodes); 131 } 132 } 133 134 protected abstract Closure<?> createEffectsClosure(PhaseContextT context, ScheduleResult schedule, ControlFlowGraph cfg); 135} 136