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) {
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()) {
|
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.EconomicSetNodeEventListener;
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) {
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 EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener();
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()) {
|