1 /*
2  * Copyright (c) 2011, 2020, 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 import org.graalvm.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
45 
46 public abstract class EffectsPhase<CoreProvidersT extends CoreProviders> extends BasePhase<CoreProvidersT> {
47 
48     public abstract static class Closure<T> extends ReentrantBlockIterator.BlockIteratorClosure<T> {
49 
hasChanged()50         public abstract boolean hasChanged();
51 
needsApplyEffects()52         public abstract boolean needsApplyEffects();
53 
applyEffects()54         public abstract void applyEffects();
55     }
56 
57     private final int maxIterations;
58     protected final CanonicalizerPhase canonicalizer;
59     private final boolean unscheduled;
60     private final SchedulePhase.SchedulingStrategy strategy;
61 
EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer)62     protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer) {
63         this(maxIterations, canonicalizer, false, SchedulingStrategy.EARLIEST);
64     }
65 
EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled, SchedulingStrategy strategy)66     protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled, SchedulingStrategy strategy) {
67         this.strategy = strategy;
68         this.maxIterations = maxIterations;
69         this.canonicalizer = canonicalizer;
70         this.unscheduled = unscheduled;
71     }
72 
EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled)73     protected EffectsPhase(int maxIterations, CanonicalizerPhase canonicalizer, boolean unscheduled) {
74         this(maxIterations, canonicalizer, unscheduled, unscheduled ? null : SchedulingStrategy.EARLIEST);
75     }
76 
77     @Override
run(StructuredGraph graph, CoreProvidersT context)78     protected void run(StructuredGraph graph, CoreProvidersT context) {
79         runAnalysis(graph, context);
80     }
81 
82     @SuppressWarnings("try")
runAnalysis(StructuredGraph graph, CoreProvidersT context)83     public boolean runAnalysis(StructuredGraph graph, CoreProvidersT context) {
84         assert unscheduled || strategy != null;
85         boolean changed = false;
86         CompilationAlarm compilationAlarm = CompilationAlarm.current();
87         DebugContext debug = graph.getDebug();
88         for (int iteration = 0; iteration < maxIterations && !compilationAlarm.hasExpired(); iteration++) {
89             try (DebugContext.Scope s = debug.scope(debug.areScopesEnabled() ? "iteration " + iteration : null)) {
90                 ScheduleResult schedule;
91                 ControlFlowGraph cfg;
92                 if (unscheduled) {
93                     schedule = null;
94                     cfg = ControlFlowGraph.compute(graph, true, true, false, false);
95                 } else {
96                     new SchedulePhase(strategy).apply(graph, false);
97                     schedule = graph.getLastSchedule();
98                     cfg = schedule.getCFG();
99                 }
100                 try (DebugContext.Scope scheduleScope = debug.scope("EffectsPhaseWithSchedule", schedule)) {
101                     Closure<?> closure = createEffectsClosure(context, schedule, cfg);
102                     ReentrantBlockIterator.apply(closure, cfg.getStartBlock());
103 
104                     if (closure.needsApplyEffects()) {
105                         // apply the effects collected during this iteration
106                         EconomicSetNodeEventListener listener = new EconomicSetNodeEventListener();
107                         try (NodeEventScope nes = graph.trackNodeEvents(listener)) {
108                             closure.applyEffects();
109 
110                             if (debug.isDumpEnabled(DebugContext.VERBOSE_LEVEL)) {
111                                 debug.dump(DebugContext.VERBOSE_LEVEL, graph, "%s iteration", getName());
112                             }
113 
114                             new DeadCodeEliminationPhase(Required).apply(graph);
115                         }
116 
117                         postIteration(graph, context, listener.getNodes());
118                     }
119 
120                     if (closure.hasChanged()) {
121                         changed = true;
122                     } else {
123                         break;
124                     }
125                 } catch (Throwable t) {
126                     throw debug.handle(t);
127                 }
128             }
129         }
130         return changed;
131     }
132 
postIteration(final StructuredGraph graph, final CoreProvidersT context, EconomicSet<Node> changedNodes)133     protected void postIteration(final StructuredGraph graph, final CoreProvidersT context, EconomicSet<Node> changedNodes) {
134         if (canonicalizer != null) {
135             canonicalizer.applyIncremental(graph, context, changedNodes);
136         }
137     }
138 
createEffectsClosure(CoreProvidersT context, ScheduleResult schedule, ControlFlowGraph cfg)139     protected abstract Closure<?> createEffectsClosure(CoreProvidersT context, ScheduleResult schedule, ControlFlowGraph cfg);
140 }
141