43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.virtual.phases.ea;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
|
|
28 |
|
49873
|
29 |
import jdk.internal.vm.compiler.collections.EconomicSet;
|
43972
|
30 |
import org.graalvm.compiler.core.common.util.CompilationAlarm;
|
46640
|
31 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
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;
|
55509
|
37 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
43972
|
38 |
import org.graalvm.compiler.phases.BasePhase;
|
|
39 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
40 |
import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
|
52578
|
41 |
import org.graalvm.compiler.phases.common.util.EconomicSetNodeEventListener;
|
43972
|
42 |
import org.graalvm.compiler.phases.graph.ReentrantBlockIterator;
|
|
43 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
|
44 |
|
55509
|
45 |
public abstract class EffectsPhase<CoreProvidersT extends CoreProviders> extends BasePhase<CoreProvidersT> {
|
43972
|
46 |
|
|
47 |
public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> {
|
|
48 |
|
|
49 |
public abstract boolean hasChanged();
|
|
50 |
|
46344
|
51 |
public abstract boolean needsApplyEffects();
|
|
52 |
|
43972
|
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
|
55509
|
71 |
protected void run(StructuredGraph graph, CoreProvidersT context) {
|
43972
|
72 |
runAnalysis(graph, context);
|
|
73 |
}
|
|
74 |
|
|
75 |
@SuppressWarnings("try")
|
55509
|
76 |
public boolean runAnalysis(StructuredGraph graph, CoreProvidersT context) {
|
43972
|
77 |
boolean changed = false;
|
46344
|
78 |
CompilationAlarm compilationAlarm = CompilationAlarm.current();
|
46640
|
79 |
DebugContext debug = graph.getDebug();
|
46344
|
80 |
for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) {
|
46640
|
81 |
try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) {
|
43972
|
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 |
}
|
46640
|
92 |
try (DebugContext.Scope scheduleScope = debug.scope("EffectsPhaseWithSchedule", schedule)) {
|
43972
|
93 |
Closure<?> closure = createEffectsClosure(context, schedule, cfg);
|
|
94 |
ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
|
|
95 |
|
46344
|
96 |
if (closure.needsApplyEffects()) {
|
|
97 |
// apply the effects collected during this iteration
|
52578
|
98 |
EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener();
|
46344
|
99 |
try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
|
|
100 |
closure.applyEffects();
|
|
101 |
|
55509
|
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);
|
46344
|
107 |
}
|
|
108 |
|
55509
|
109 |
postIteration(graph, context, listener.getNodes());
|
46344
|
110 |
}
|
|
111 |
|
43972
|
112 |
if (closure.hasChanged()) {
|
|
113 |
changed = true;
|
|
114 |
} else {
|
46344
|
115 |
break;
|
43972
|
116 |
}
|
|
117 |
} catch (Throwable t) {
|
46640
|
118 |
throw debug.handle(t);
|
43972
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
return changed;
|
|
123 |
}
|
|
124 |
|
55509
|
125 |
protected void postIteration(final StructuredGraph graph, final CoreProvidersT context, EconomicSet<Node> changedNodes) {
|
43972
|
126 |
if (canonicalizer != null) {
|
|
127 |
canonicalizer.applyIncremental(graph, context, changedNodes);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
55509
|
131 |
protected abstract Closure<?> createEffectsClosure(CoreProvidersT context, ScheduleResult schedule, ControlFlowGraph cfg);
|
43972
|
132 |
}
|