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 package org.graalvm.compiler.lir.alloc.trace.lsra;
24
25 import static jdk.vm.ci.code.ValueUtil.isRegister;
26 import static org.graalvm.compiler.core.common.GraalOptions.DetailedAsserts;
27 import static org.graalvm.compiler.lir.LIRValueUtil.asVariable;
28 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
29 import static org.graalvm.compiler.lir.LIRValueUtil.isVariable;
30
31 import java.util.ArrayList;
32
33 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
34 import org.graalvm.compiler.core.common.alloc.Trace;
35 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
36 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
37 import org.graalvm.compiler.debug.DebugContext;
38 import org.graalvm.compiler.debug.Indent;
39 import org.graalvm.compiler.lir.LIRInsertionBuffer;
40 import org.graalvm.compiler.lir.LIRInstruction;
41 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
42 import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
43 import org.graalvm.compiler.lir.StandardOp.MoveOp;
44 import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
45 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceInterval.SpillState;
46 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.IntervalPredicate;
47 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.TraceLinearScan;
48 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
49 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory;
50
51 import jdk.vm.ci.code.TargetDescription;
52 import jdk.vm.ci.meta.AllocatableValue;
53
54 final class TraceLinearScanEliminateSpillMovePhase extends TraceLinearScanAllocationPhase {
55
56 private static final IntervalPredicate spilledIntervals = new TraceLinearScanPhase.IntervalPredicate() {
67 boolean shouldEliminateSpillMoves = shouldEliminateSpillMoves(traceBuilderResult, allocator);
68 eliminateSpillMoves(allocator, shouldEliminateSpillMoves, traceBuilderResult, lirGenRes);
69 }
70
71 private static boolean shouldEliminateSpillMoves(TraceBuilderResult traceBuilderResult, TraceLinearScan allocator) {
72 return !traceBuilderResult.incomingSideEdges(traceBuilderResult.getTraceForBlock(allocator.blockAt(0)));
73 }
74
75 // called once before assignment of register numbers
76 @SuppressWarnings("try")
77 private static void eliminateSpillMoves(TraceLinearScan allocator, boolean shouldEliminateSpillMoves, TraceBuilderResult traceBuilderResult, LIRGenerationResult res) {
78 DebugContext debug = allocator.getDebug();
79 try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves: Trace%d", traceBuilderResult.getTraceForBlock(allocator.blockAt(0)).getId())) {
80 allocator.sortIntervalsBySpillPos();
81
82 /*
83 * collect all intervals that must be stored after their definition. The list is sorted
84 * by Interval.spillDefinitionPos.
85 */
86 TraceInterval interval = allocator.createUnhandledListBySpillPos(spilledIntervals);
87 if (DetailedAsserts.getValue(allocator.getOptions())) {
88 checkIntervals(debug, interval);
89 }
90 if (debug.isLogEnabled()) {
91 try (Indent indent2 = debug.logAndIndent("Sorted intervals")) {
92 for (TraceInterval i = interval; i != null; i = i.next) {
93 debug.log("%5d: %s", i.spillDefinitionPos(), i);
94 }
95 }
96 }
97
98 LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
99 for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
100 try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
101 ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
102 int numInst = instructions.size();
103
104 int lastOpId = -1;
105 // iterate all instructions of the block.
106 for (int j = 0; j < numInst; j++) {
107 LIRInstruction op = instructions.get(j);
|
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 package org.graalvm.compiler.lir.alloc.trace.lsra;
24
25 import static jdk.vm.ci.code.ValueUtil.isRegister;
26 import static org.graalvm.compiler.lir.LIRValueUtil.asVariable;
27 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
28 import static org.graalvm.compiler.lir.LIRValueUtil.isVariable;
29
30 import java.util.ArrayList;
31
32 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
33 import org.graalvm.compiler.core.common.alloc.Trace;
34 import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
35 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
36 import org.graalvm.compiler.debug.Assertions;
37 import org.graalvm.compiler.debug.DebugContext;
38 import org.graalvm.compiler.debug.Indent;
39 import org.graalvm.compiler.lir.LIRInsertionBuffer;
40 import org.graalvm.compiler.lir.LIRInstruction;
41 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
42 import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
43 import org.graalvm.compiler.lir.StandardOp.MoveOp;
44 import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
45 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceInterval.SpillState;
46 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.IntervalPredicate;
47 import org.graalvm.compiler.lir.alloc.trace.lsra.TraceLinearScanPhase.TraceLinearScan;
48 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
49 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory;
50
51 import jdk.vm.ci.code.TargetDescription;
52 import jdk.vm.ci.meta.AllocatableValue;
53
54 final class TraceLinearScanEliminateSpillMovePhase extends TraceLinearScanAllocationPhase {
55
56 private static final IntervalPredicate spilledIntervals = new TraceLinearScanPhase.IntervalPredicate() {
67 boolean shouldEliminateSpillMoves = shouldEliminateSpillMoves(traceBuilderResult, allocator);
68 eliminateSpillMoves(allocator, shouldEliminateSpillMoves, traceBuilderResult, lirGenRes);
69 }
70
71 private static boolean shouldEliminateSpillMoves(TraceBuilderResult traceBuilderResult, TraceLinearScan allocator) {
72 return !traceBuilderResult.incomingSideEdges(traceBuilderResult.getTraceForBlock(allocator.blockAt(0)));
73 }
74
75 // called once before assignment of register numbers
76 @SuppressWarnings("try")
77 private static void eliminateSpillMoves(TraceLinearScan allocator, boolean shouldEliminateSpillMoves, TraceBuilderResult traceBuilderResult, LIRGenerationResult res) {
78 DebugContext debug = allocator.getDebug();
79 try (Indent indent = debug.logAndIndent("Eliminating unnecessary spill moves: Trace%d", traceBuilderResult.getTraceForBlock(allocator.blockAt(0)).getId())) {
80 allocator.sortIntervalsBySpillPos();
81
82 /*
83 * collect all intervals that must be stored after their definition. The list is sorted
84 * by Interval.spillDefinitionPos.
85 */
86 TraceInterval interval = allocator.createUnhandledListBySpillPos(spilledIntervals);
87 if (Assertions.detailedAssertionsEnabled(allocator.getOptions())) {
88 checkIntervals(debug, interval);
89 }
90 if (debug.isLogEnabled()) {
91 try (Indent indent2 = debug.logAndIndent("Sorted intervals")) {
92 for (TraceInterval i = interval; i != null; i = i.next) {
93 debug.log("%5d: %s", i.spillDefinitionPos(), i);
94 }
95 }
96 }
97
98 LIRInsertionBuffer insertionBuffer = new LIRInsertionBuffer();
99 for (AbstractBlockBase<?> block : allocator.sortedBlocks()) {
100 try (Indent indent1 = debug.logAndIndent("Handle %s", block)) {
101 ArrayList<LIRInstruction> instructions = allocator.getLIR().getLIRforBlock(block);
102 int numInst = instructions.size();
103
104 int lastOpId = -1;
105 // iterate all instructions of the block.
106 for (int j = 0; j < numInst; j++) {
107 LIRInstruction op = instructions.get(j);
|