1 /* |
|
2 * Copyright (c) 2018, 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.lir.alloc.trace; |
|
26 |
|
27 import static org.graalvm.compiler.lir.LIRValueUtil.asVariable; |
|
28 import static org.graalvm.compiler.lir.LIRValueUtil.isVariable; |
|
29 import static org.graalvm.compiler.lir.alloc.trace.TraceUtil.isTrivialTrace; |
|
30 |
|
31 import java.util.Arrays; |
|
32 import java.util.EnumSet; |
|
33 |
|
34 import org.graalvm.compiler.core.common.alloc.Trace; |
|
35 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; |
|
36 import org.graalvm.compiler.lir.LIR; |
|
37 import org.graalvm.compiler.lir.LIRInstruction; |
|
38 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag; |
|
39 import org.graalvm.compiler.lir.LIRInstruction.OperandMode; |
|
40 import org.graalvm.compiler.lir.StandardOp.JumpOp; |
|
41 import org.graalvm.compiler.lir.StandardOp.LabelOp; |
|
42 import org.graalvm.compiler.lir.ValueProcedure; |
|
43 import org.graalvm.compiler.lir.gen.LIRGenerationResult; |
|
44 import org.graalvm.compiler.lir.ssa.SSAUtil; |
|
45 |
|
46 import jdk.vm.ci.code.TargetDescription; |
|
47 import jdk.vm.ci.meta.Value; |
|
48 |
|
49 /** |
|
50 * Allocates a trivial trace i.e. a trace consisting of a single block with no instructions other |
|
51 * than the {@link LabelOp} and the {@link JumpOp}. |
|
52 */ |
|
53 public final class TrivialTraceAllocator extends TraceAllocationPhase<TraceAllocationPhase.TraceAllocationContext> { |
|
54 |
|
55 @Override |
|
56 protected void run(TargetDescription target, LIRGenerationResult lirGenRes, Trace trace, TraceAllocationContext context) { |
|
57 LIR lir = lirGenRes.getLIR(); |
|
58 assert isTrivialTrace(lir, trace) : "Not a trivial trace! " + trace; |
|
59 AbstractBlockBase<?> block = trace.getBlocks()[0]; |
|
60 assert TraceAssertions.singleHeadPredecessor(trace) : "Trace head with more than one predecessor?!" + trace; |
|
61 AbstractBlockBase<?> pred = block.getPredecessors()[0]; |
|
62 |
|
63 GlobalLivenessInfo livenessInfo = context.livenessInfo; |
|
64 allocate(block, pred, livenessInfo, SSAUtil.phiOutOrNull(lir, block)); |
|
65 } |
|
66 |
|
67 public static void allocate(AbstractBlockBase<?> block, AbstractBlockBase<?> pred, GlobalLivenessInfo livenessInfo, LIRInstruction jump) { |
|
68 // exploit that the live sets are sorted |
|
69 assert TraceAssertions.liveSetsAreSorted(livenessInfo, block); |
|
70 assert TraceAssertions.liveSetsAreSorted(livenessInfo, pred); |
|
71 |
|
72 // setup incoming variables/locations |
|
73 final int[] blockIn = livenessInfo.getBlockIn(block); |
|
74 final Value[] predLocOut = livenessInfo.getOutLocation(pred); |
|
75 int inLenght = blockIn.length; |
|
76 |
|
77 // setup outgoing variables/locations |
|
78 final int[] blockOut = livenessInfo.getBlockOut(block); |
|
79 int outLength = blockOut.length; |
|
80 final Value[] locationOut = new Value[outLength]; |
|
81 |
|
82 assert outLength <= inLenght : "Trivial Trace! There cannot be more outgoing values than incoming."; |
|
83 for (int outIdx = 0, inIdx = 0; outIdx < outLength; inIdx++) { |
|
84 if (blockOut[outIdx] == blockIn[inIdx]) { |
|
85 // set the outgoing location to the incoming value |
|
86 locationOut[outIdx++] = predLocOut[inIdx]; |
|
87 } |
|
88 } |
|
89 |
|
90 /* |
|
91 * Since we do not change any of the location we can just use the outgoing of the |
|
92 * predecessor. |
|
93 */ |
|
94 livenessInfo.setInLocations(block, predLocOut); |
|
95 livenessInfo.setOutLocations(block, locationOut); |
|
96 if (jump != null) { |
|
97 handlePhiOut(jump, blockIn, predLocOut); |
|
98 } |
|
99 } |
|
100 |
|
101 private static void handlePhiOut(LIRInstruction jump, int[] varIn, Value[] locIn) { |
|
102 // handle outgoing phi values |
|
103 ValueProcedure outputConsumer = new ValueProcedure() { |
|
104 @Override |
|
105 public Value doValue(Value value, OperandMode mode, EnumSet<OperandFlag> flags) { |
|
106 if (isVariable(value)) { |
|
107 // since incoming variables are sorted, we can do a binary search |
|
108 return locIn[Arrays.binarySearch(varIn, asVariable(value).index)]; |
|
109 } |
|
110 return value; |
|
111 } |
|
112 }; |
|
113 |
|
114 // Jumps have only alive values (outgoing phi values) |
|
115 jump.forEachAlive(outputConsumer); |
|
116 } |
|
117 |
|
118 } |
|