29 import jdk.internal.vm.compiler.collections.EconomicSet; |
29 import jdk.internal.vm.compiler.collections.EconomicSet; |
30 import org.graalvm.compiler.core.common.util.CompilationAlarm; |
30 import org.graalvm.compiler.core.common.util.CompilationAlarm; |
31 import org.graalvm.compiler.debug.DebugContext; |
31 import org.graalvm.compiler.debug.DebugContext; |
32 import org.graalvm.compiler.graph.Graph.NodeEventScope; |
32 import org.graalvm.compiler.graph.Graph.NodeEventScope; |
33 import org.graalvm.compiler.graph.Node; |
33 import org.graalvm.compiler.graph.Node; |
34 import org.graalvm.compiler.graph.spi.Simplifiable; |
|
35 import org.graalvm.compiler.nodes.StructuredGraph; |
34 import org.graalvm.compiler.nodes.StructuredGraph; |
36 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; |
35 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; |
37 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; |
36 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; |
|
37 import org.graalvm.compiler.nodes.spi.CoreProviders; |
38 import org.graalvm.compiler.phases.BasePhase; |
38 import org.graalvm.compiler.phases.BasePhase; |
39 import org.graalvm.compiler.phases.common.CanonicalizerPhase; |
39 import org.graalvm.compiler.phases.common.CanonicalizerPhase; |
40 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; |
40 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; |
41 import org.graalvm.compiler.phases.common.util.EconomicSetNodeEventListener; |
41 import org.graalvm.compiler.phases.common.util.EconomicSetNodeEventListener; |
42 import org.graalvm.compiler.phases.graph.ReentrantBlockIterator; |
42 import org.graalvm.compiler.phases.graph.ReentrantBlockIterator; |
43 import org.graalvm.compiler.phases.schedule.SchedulePhase; |
43 import org.graalvm.compiler.phases.schedule.SchedulePhase; |
44 import org.graalvm.compiler.phases.tiers.PhaseContext; |
|
45 |
44 |
46 public abstract class EffectsPhase<PhaseContextT extends PhaseContext> extends BasePhase<PhaseContextT> { |
45 public abstract class EffectsPhase<CoreProvidersT extends CoreProviders> extends BasePhase<CoreProvidersT> { |
47 |
46 |
48 public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> { |
47 public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> { |
49 |
48 |
50 public abstract boolean hasChanged(); |
49 public abstract boolean hasChanged(); |
51 |
50 |
67 this.canonicalizer = canonicalizer; |
66 this.canonicalizer = canonicalizer; |
68 this.unscheduled = unscheduled; |
67 this.unscheduled = unscheduled; |
69 } |
68 } |
70 |
69 |
71 @Override |
70 @Override |
72 protected void run(StructuredGraph graph, PhaseContextT context) { |
71 protected void run(StructuredGraph graph, CoreProvidersT context) { |
73 runAnalysis(graph, context); |
72 runAnalysis(graph, context); |
74 } |
73 } |
75 |
74 |
76 @SuppressWarnings("try") |
75 @SuppressWarnings("try") |
77 public boolean runAnalysis(StructuredGraph graph, PhaseContextT context) { |
76 public boolean runAnalysis(StructuredGraph graph, CoreProvidersT context) { |
78 boolean changed = false; |
77 boolean changed = false; |
79 CompilationAlarm compilationAlarm = CompilationAlarm.current(); |
78 CompilationAlarm compilationAlarm = CompilationAlarm.current(); |
80 DebugContext debug = graph.getDebug(); |
79 DebugContext debug = graph.getDebug(); |
81 for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) { |
80 for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) { |
82 try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) { |
81 try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) { |
97 if (closure.needsApplyEffects()) { |
96 if (closure.needsApplyEffects()) { |
98 // apply the effects collected during this iteration |
97 // apply the effects collected during this iteration |
99 EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener(); |
98 EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener(); |
100 try (NodeEventScope nes = graph.trackNodeEvents(listener)) { |
99 try (NodeEventScope nes = graph.trackNodeEvents(listener)) { |
101 closure.applyEffects(); |
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); |
102 } |
107 } |
103 |
108 |
104 if (debug.isDumpEnabled(DebugContext.VERBOSE_LEVEL)) { |
109 postIteration(graph, context, listener.getNodes()); |
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 } |
110 } |
118 |
111 |
119 if (closure.hasChanged()) { |
112 if (closure.hasChanged()) { |
120 changed = true; |
113 changed = true; |
121 } else { |
114 } else { |
127 } |
120 } |
128 } |
121 } |
129 return changed; |
122 return changed; |
130 } |
123 } |
131 |
124 |
132 protected void postIteration(final StructuredGraph graph, final PhaseContextT context, EconomicSet<Node> changedNodes) { |
125 protected void postIteration(final StructuredGraph graph, final CoreProvidersT context, EconomicSet<Node> changedNodes) { |
133 if (canonicalizer != null) { |
126 if (canonicalizer != null) { |
134 canonicalizer.applyIncremental(graph, context, changedNodes); |
127 canonicalizer.applyIncremental(graph, context, changedNodes); |
135 } |
128 } |
136 } |
129 } |
137 |
130 |
138 protected abstract Closure<?> createEffectsClosure(PhaseContextT context, ScheduleResult schedule, ControlFlowGraph cfg); |
131 protected abstract Closure<?> createEffectsClosure(CoreProvidersT context, ScheduleResult schedule, ControlFlowGraph cfg); |
139 } |
132 } |