43972
|
1 |
/*
|
46344
|
2 |
* Copyright (c) 2013, 2017, 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 |
*/
|
|
23 |
package org.graalvm.compiler.lir.sparc;
|
|
24 |
|
46371
|
25 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
|
26 |
import static jdk.vm.ci.sparc.SPARC.CPU;
|
|
27 |
import static jdk.vm.ci.sparc.SPARC.g0;
|
|
28 |
import static jdk.vm.ci.sparc.SPARCKind.WORD;
|
|
29 |
import static jdk.vm.ci.sparc.SPARCKind.XWORD;
|
43972
|
30 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BPCC;
|
|
31 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CBCOND;
|
|
32 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.FBPCC;
|
|
33 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.INSTRUCTION_SIZE;
|
|
34 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm10;
|
|
35 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm11;
|
|
36 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm13;
|
|
37 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm5;
|
|
38 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.ANNUL;
|
|
39 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.NOT_ANNUL;
|
|
40 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_NOT_TAKEN;
|
|
41 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_TAKEN;
|
|
42 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Fcc0;
|
|
43 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Icc;
|
|
44 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Xcc;
|
|
45 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Always;
|
|
46 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Equal;
|
|
47 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Equal;
|
|
48 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Greater;
|
|
49 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_GreaterOrEqual;
|
|
50 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Less;
|
|
51 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_LessOrEqual;
|
|
52 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedGreaterOrEqual;
|
|
53 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrEqual;
|
|
54 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrGreater;
|
|
55 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrLess;
|
|
56 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrLessOrEqual;
|
|
57 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Greater;
|
|
58 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterEqual;
|
|
59 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterEqualUnsigned;
|
|
60 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterUnsigned;
|
|
61 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Less;
|
|
62 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessEqual;
|
|
63 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessEqualUnsigned;
|
|
64 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessUnsigned;
|
|
65 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.NotEqual;
|
|
66 |
import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Op3s.Subcc;
|
|
67 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
|
|
68 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
|
|
69 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
|
|
70 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
71 |
import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
|
|
72 |
import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
|
|
73 |
import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
|
|
74 |
import static org.graalvm.compiler.lir.sparc.SPARCMove.const2reg;
|
|
75 |
import static org.graalvm.compiler.lir.sparc.SPARCOP3Op.emitOp3;
|
|
76 |
|
|
77 |
import java.util.ArrayList;
|
|
78 |
import java.util.EnumSet;
|
|
79 |
import java.util.List;
|
46371
|
80 |
|
48861
|
81 |
import org.graalvm.collections.EconomicMap;
|
|
82 |
import org.graalvm.collections.Equivalence;
|
43972
|
83 |
import org.graalvm.compiler.asm.Assembler;
|
|
84 |
import org.graalvm.compiler.asm.Assembler.LabelHint;
|
|
85 |
import org.graalvm.compiler.asm.Label;
|
|
86 |
import org.graalvm.compiler.asm.sparc.SPARCAssembler;
|
|
87 |
import org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict;
|
|
88 |
import org.graalvm.compiler.asm.sparc.SPARCAssembler.CC;
|
|
89 |
import org.graalvm.compiler.asm.sparc.SPARCAssembler.CMOV;
|
|
90 |
import org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag;
|
|
91 |
import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
|
|
92 |
import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
|
|
93 |
import org.graalvm.compiler.core.common.calc.Condition;
|
|
94 |
import org.graalvm.compiler.debug.GraalError;
|
|
95 |
import org.graalvm.compiler.lir.LIRInstructionClass;
|
|
96 |
import org.graalvm.compiler.lir.LabelRef;
|
|
97 |
import org.graalvm.compiler.lir.Opcode;
|
|
98 |
import org.graalvm.compiler.lir.StandardOp;
|
|
99 |
import org.graalvm.compiler.lir.SwitchStrategy;
|
|
100 |
import org.graalvm.compiler.lir.SwitchStrategy.BaseSwitchClosure;
|
|
101 |
import org.graalvm.compiler.lir.Variable;
|
|
102 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
|
103 |
|
|
104 |
import jdk.vm.ci.code.Register;
|
|
105 |
import jdk.vm.ci.meta.AllocatableValue;
|
|
106 |
import jdk.vm.ci.meta.Constant;
|
|
107 |
import jdk.vm.ci.meta.JavaConstant;
|
|
108 |
import jdk.vm.ci.meta.PlatformKind;
|
|
109 |
import jdk.vm.ci.meta.Value;
|
|
110 |
import jdk.vm.ci.sparc.SPARC.CPUFeature;
|
|
111 |
import jdk.vm.ci.sparc.SPARCKind;
|
|
112 |
|
|
113 |
public class SPARCControlFlow {
|
|
114 |
// This describes the maximum offset between the first emitted (load constant in to scratch,
|
|
115 |
// if does not fit into simm5 of cbcond) instruction and the final branch instruction
|
|
116 |
private static final int maximumSelfOffsetInstructions = 2;
|
|
117 |
|
|
118 |
public static final class ReturnOp extends SPARCBlockEndOp {
|
|
119 |
public static final LIRInstructionClass<ReturnOp> TYPE = LIRInstructionClass.create(ReturnOp.class);
|
|
120 |
public static final SizeEstimate SIZE = SizeEstimate.create(2);
|
|
121 |
|
|
122 |
@Use({REG, ILLEGAL}) protected Value x;
|
|
123 |
|
|
124 |
public ReturnOp(Value x) {
|
|
125 |
super(TYPE, SIZE);
|
|
126 |
this.x = x;
|
|
127 |
}
|
|
128 |
|
|
129 |
@Override
|
|
130 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
131 |
emitCodeHelper(crb, masm);
|
|
132 |
}
|
|
133 |
|
|
134 |
public static void emitCodeHelper(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
135 |
masm.ret();
|
|
136 |
// On SPARC we always leave the frame (in the delay slot).
|
|
137 |
crb.frameContext.leave(crb);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
public static final class CompareBranchOp extends SPARCBlockEndOp implements SPARCDelayedControlTransfer {
|
|
142 |
public static final LIRInstructionClass<CompareBranchOp> TYPE = LIRInstructionClass.create(CompareBranchOp.class);
|
|
143 |
public static final SizeEstimate SIZE = SizeEstimate.create(3);
|
|
144 |
static final EnumSet<SPARCKind> SUPPORTED_KINDS = EnumSet.of(XWORD, WORD);
|
|
145 |
|
46344
|
146 |
@Use({REG}) protected AllocatableValue x;
|
43972
|
147 |
@Use({REG, CONST}) protected Value y;
|
|
148 |
private ConditionFlag conditionFlag;
|
|
149 |
protected final LabelRef trueDestination;
|
|
150 |
protected LabelHint trueDestinationHint;
|
|
151 |
protected final LabelRef falseDestination;
|
|
152 |
protected LabelHint falseDestinationHint;
|
|
153 |
protected final SPARCKind kind;
|
|
154 |
protected final boolean unorderedIsTrue;
|
|
155 |
private boolean emitted = false;
|
|
156 |
private int delaySlotPosition = -1;
|
|
157 |
private double trueDestinationProbability;
|
|
158 |
|
46344
|
159 |
public CompareBranchOp(AllocatableValue x, Value y, Condition condition, LabelRef trueDestination, LabelRef falseDestination, SPARCKind kind, boolean unorderedIsTrue,
|
|
160 |
double trueDestinationProbability) {
|
43972
|
161 |
super(TYPE, SIZE);
|
46371
|
162 |
assert x.getPlatformKind() == y.getPlatformKind() : String.format("PlatformKind of x must match PlatformKind of y. %s!=%s", x.getPlatformKind(), y.getPlatformKind());
|
43972
|
163 |
this.x = x;
|
|
164 |
this.y = y;
|
|
165 |
this.trueDestination = trueDestination;
|
|
166 |
this.falseDestination = falseDestination;
|
|
167 |
this.kind = kind;
|
|
168 |
this.unorderedIsTrue = unorderedIsTrue;
|
|
169 |
this.trueDestinationProbability = trueDestinationProbability;
|
|
170 |
conditionFlag = fromCondition(kind.isInteger(), condition, unorderedIsTrue);
|
|
171 |
}
|
|
172 |
|
|
173 |
@Override
|
|
174 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
175 |
if (emitted) { // Only if delayed control transfer is used we must check this
|
|
176 |
assert masm.position() - delaySlotPosition == 4 : "Only one instruction can be stuffed into the delay slot";
|
|
177 |
}
|
|
178 |
if (!emitted) {
|
|
179 |
requestHints(masm);
|
|
180 |
int targetPosition = getTargetPosition(masm);
|
|
181 |
if (canUseShortBranch(crb, masm, targetPosition)) {
|
|
182 |
emitted = emitShortCompareBranch(crb, masm);
|
|
183 |
}
|
|
184 |
if (!emitted) { // No short compare/branch was used, so we go into fallback
|
|
185 |
emitted = emitLongCompareBranch(crb, masm, true);
|
|
186 |
emitted = true;
|
|
187 |
}
|
|
188 |
}
|
|
189 |
assert emitted;
|
|
190 |
}
|
|
191 |
|
|
192 |
private boolean emitLongCompareBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm, boolean withDelayedNop) {
|
|
193 |
emitOp3(masm, Subcc, x, y);
|
|
194 |
return emitBranch(crb, masm, kind, conditionFlag, trueDestination, falseDestination, withDelayedNop, trueDestinationProbability);
|
|
195 |
}
|
|
196 |
|
|
197 |
private static int getTargetPosition(Assembler asm) {
|
|
198 |
return asm.position() + maximumSelfOffsetInstructions * asm.target.wordSize;
|
|
199 |
}
|
|
200 |
|
|
201 |
@Override
|
|
202 |
public void emitControlTransfer(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
203 |
requestHints(masm);
|
|
204 |
// When we use short branches, no delay slot is available
|
|
205 |
int targetPosition = getTargetPosition(masm);
|
|
206 |
if (!canUseShortBranch(crb, masm, targetPosition)) {
|
|
207 |
emitted = emitLongCompareBranch(crb, masm, false);
|
|
208 |
if (emitted) {
|
|
209 |
delaySlotPosition = masm.position();
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
private void requestHints(SPARCMacroAssembler masm) {
|
|
215 |
if (trueDestinationHint == null) {
|
|
216 |
this.trueDestinationHint = masm.requestLabelHint(trueDestination.label());
|
|
217 |
}
|
|
218 |
if (falseDestinationHint == null) {
|
|
219 |
this.falseDestinationHint = masm.requestLabelHint(falseDestination.label());
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Tries to use the emit the compare/branch instruction.
|
|
225 |
* <p>
|
|
226 |
* CBcond has follwing limitations
|
|
227 |
* <ul>
|
|
228 |
* <li>Immediate field is only 5 bit and is on the right
|
|
229 |
* <li>Jump offset is maximum of -+512 instruction
|
|
230 |
*
|
|
231 |
* <p>
|
|
232 |
* We get from outside
|
|
233 |
* <ul>
|
|
234 |
* <li>at least one of trueDestination falseDestination is within reach of +-512
|
|
235 |
* instructions
|
|
236 |
* <li>two registers OR one register and a constant which fits simm13
|
|
237 |
*
|
|
238 |
* <p>
|
|
239 |
* We do:
|
|
240 |
* <ul>
|
|
241 |
* <li>find out which target needs to be branched conditionally
|
|
242 |
* <li>find out if fall-through is possible, if not, a unconditional branch is needed after
|
|
243 |
* cbcond (needJump=true)
|
|
244 |
* <li>if no fall through: we need to put the closer jump into the cbcond branch and the
|
|
245 |
* farther into the jmp (unconditional branch)
|
|
246 |
* <li>if constant on the left side, mirror to be on the right
|
|
247 |
* <li>if constant on right does not fit into simm5, put it into a scratch register
|
|
248 |
*
|
|
249 |
* @param crb
|
|
250 |
* @param masm
|
|
251 |
* @return true if the branch could be emitted
|
|
252 |
*/
|
|
253 |
private boolean emitShortCompareBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
46371
|
254 |
boolean isLong = kind == SPARCKind.XWORD;
|
43972
|
255 |
ConditionFlag actualConditionFlag = conditionFlag;
|
|
256 |
Label actualTrueTarget = trueDestination.label();
|
|
257 |
Label actualFalseTarget = falseDestination.label();
|
|
258 |
Label tmpTarget;
|
|
259 |
boolean needJump;
|
|
260 |
if (crb.isSuccessorEdge(trueDestination)) {
|
|
261 |
actualConditionFlag = conditionFlag.negate();
|
|
262 |
tmpTarget = actualTrueTarget;
|
|
263 |
actualTrueTarget = actualFalseTarget;
|
|
264 |
actualFalseTarget = tmpTarget;
|
|
265 |
needJump = false;
|
|
266 |
} else {
|
|
267 |
needJump = !crb.isSuccessorEdge(falseDestination);
|
|
268 |
int targetPosition = getTargetPosition(masm);
|
|
269 |
if (needJump && !isShortBranch(masm, targetPosition, trueDestinationHint, actualTrueTarget)) {
|
|
270 |
// we have to jump in either way, so we must put the shorter
|
|
271 |
// branch into the actualTarget as only one of the two jump targets
|
|
272 |
// is guaranteed to be simm10
|
|
273 |
actualConditionFlag = actualConditionFlag.negate();
|
|
274 |
tmpTarget = actualTrueTarget;
|
|
275 |
actualTrueTarget = actualFalseTarget;
|
|
276 |
actualFalseTarget = tmpTarget;
|
|
277 |
}
|
|
278 |
}
|
46371
|
279 |
emitCBCond(masm, x, y, actualTrueTarget, actualConditionFlag, isLong);
|
43972
|
280 |
if (needJump) {
|
|
281 |
masm.jmp(actualFalseTarget);
|
|
282 |
masm.nop();
|
|
283 |
}
|
|
284 |
return true;
|
|
285 |
}
|
|
286 |
|
46371
|
287 |
private static void emitCBCond(SPARCMacroAssembler masm, Value actualX, Value actualY, Label actualTrueTarget, ConditionFlag cFlag, boolean isLong) {
|
43972
|
288 |
PlatformKind xKind = actualX.getPlatformKind();
|
46371
|
289 |
Register rs1 = asRegister(actualX, xKind);
|
43972
|
290 |
if (isJavaConstant(actualY)) {
|
|
291 |
JavaConstant c = asJavaConstant(actualY);
|
|
292 |
long constantY = c.isNull() ? 0 : c.asLong();
|
46371
|
293 |
try (ScratchRegister scratch = masm.getScratchRegister()) {
|
|
294 |
if (SPARCMacroAssembler.isSimm5(constantY)) {
|
|
295 |
CBCOND.emit(masm, cFlag, isLong, rs1, (int) constantY, actualTrueTarget);
|
|
296 |
} else { // !simm5
|
|
297 |
Register rs2 = scratch.getRegister();
|
|
298 |
masm.setx(constantY, rs2, false);
|
|
299 |
CBCOND.emit(masm, cFlag, isLong, rs1, rs2, actualTrueTarget);
|
|
300 |
}
|
|
301 |
}
|
43972
|
302 |
} else {
|
46371
|
303 |
Register rs2 = asRegister(actualY, xKind);
|
|
304 |
CBCOND.emit(masm, cFlag, isLong, rs1, rs2, actualTrueTarget);
|
43972
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
private boolean canUseShortBranch(CompilationResultBuilder crb, SPARCAssembler asm, int position) {
|
|
309 |
if (!asm.hasFeature(CPUFeature.CBCOND)) {
|
|
310 |
return false;
|
|
311 |
}
|
|
312 |
if (!((SPARCKind) x.getPlatformKind()).isInteger()) {
|
|
313 |
return false;
|
|
314 |
}
|
|
315 |
// Do not use short branch, if the y value is a constant and does not fit into simm5 but
|
|
316 |
// fits into simm13; this means the code with CBcond would be longer as the code without
|
|
317 |
// CBcond.
|
|
318 |
if (isJavaConstant(y) && !isSimm5(asJavaConstant(y)) && isSimm13(asJavaConstant(y))) {
|
|
319 |
return false;
|
|
320 |
}
|
|
321 |
boolean hasShortJumpTarget = false;
|
|
322 |
if (!crb.isSuccessorEdge(trueDestination)) {
|
|
323 |
hasShortJumpTarget |= isShortBranch(asm, position, trueDestinationHint, trueDestination.label());
|
|
324 |
}
|
|
325 |
if (!crb.isSuccessorEdge(falseDestination)) {
|
|
326 |
hasShortJumpTarget |= isShortBranch(asm, position, falseDestinationHint, falseDestination.label());
|
|
327 |
}
|
|
328 |
return hasShortJumpTarget;
|
|
329 |
}
|
|
330 |
|
|
331 |
@Override
|
|
332 |
public void resetState() {
|
|
333 |
emitted = false;
|
|
334 |
delaySlotPosition = -1;
|
|
335 |
}
|
|
336 |
|
|
337 |
@Override
|
|
338 |
public void verify() {
|
|
339 |
super.verify();
|
|
340 |
assert SUPPORTED_KINDS.contains(kind) : kind;
|
|
341 |
assert !isConstantValue(x);
|
|
342 |
assert x.getPlatformKind().equals(kind) && (isConstantValue(y) || y.getPlatformKind().equals(kind)) : x + " " + y;
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
public static boolean isShortBranch(SPARCAssembler asm, int position, LabelHint hint, Label label) {
|
|
347 |
int disp = 0;
|
|
348 |
boolean dispValid = true;
|
|
349 |
if (label.isBound()) {
|
|
350 |
disp = label.position() - position;
|
|
351 |
} else if (hint != null && hint.isValid()) {
|
|
352 |
disp = hint.getTarget() - hint.getPosition();
|
|
353 |
} else {
|
|
354 |
dispValid = false;
|
|
355 |
}
|
|
356 |
if (dispValid) {
|
|
357 |
if (disp < 0) {
|
|
358 |
disp -= maximumSelfOffsetInstructions * asm.target.wordSize;
|
|
359 |
} else {
|
|
360 |
disp += maximumSelfOffsetInstructions * asm.target.wordSize;
|
|
361 |
}
|
|
362 |
return isSimm10(disp >> 2);
|
|
363 |
} else if (hint == null) {
|
|
364 |
asm.requestLabelHint(label);
|
|
365 |
}
|
|
366 |
return false;
|
|
367 |
}
|
|
368 |
|
|
369 |
public static final class BranchOp extends SPARCBlockEndOp implements StandardOp.BranchOp {
|
|
370 |
public static final LIRInstructionClass<BranchOp> TYPE = LIRInstructionClass.create(BranchOp.class);
|
|
371 |
public static final SizeEstimate SIZE = SizeEstimate.create(2);
|
|
372 |
protected final ConditionFlag conditionFlag;
|
|
373 |
protected final LabelRef trueDestination;
|
|
374 |
protected final LabelRef falseDestination;
|
|
375 |
protected final SPARCKind kind;
|
|
376 |
protected final double trueDestinationProbability;
|
|
377 |
|
|
378 |
public BranchOp(ConditionFlag conditionFlag, LabelRef trueDestination, LabelRef falseDestination, SPARCKind kind, double trueDestinationProbability) {
|
|
379 |
super(TYPE, SIZE);
|
|
380 |
this.trueDestination = trueDestination;
|
|
381 |
this.falseDestination = falseDestination;
|
|
382 |
this.kind = kind;
|
|
383 |
this.conditionFlag = conditionFlag;
|
|
384 |
this.trueDestinationProbability = trueDestinationProbability;
|
|
385 |
}
|
|
386 |
|
|
387 |
@Override
|
|
388 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
389 |
emitBranch(crb, masm, kind, conditionFlag, trueDestination, falseDestination, true, trueDestinationProbability);
|
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
private static boolean emitBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm, SPARCKind kind, ConditionFlag conditionFlag, LabelRef trueDestination, LabelRef falseDestination,
|
|
394 |
boolean withDelayedNop, double trueDestinationProbability) {
|
|
395 |
Label actualTarget;
|
|
396 |
ConditionFlag actualConditionFlag;
|
|
397 |
boolean needJump;
|
|
398 |
BranchPredict predictTaken;
|
|
399 |
if (falseDestination != null && crb.isSuccessorEdge(trueDestination)) {
|
|
400 |
actualConditionFlag = conditionFlag != null ? conditionFlag.negate() : null;
|
|
401 |
actualTarget = falseDestination.label();
|
|
402 |
needJump = false;
|
|
403 |
predictTaken = trueDestinationProbability < .5d ? PREDICT_TAKEN : PREDICT_NOT_TAKEN;
|
|
404 |
} else {
|
|
405 |
actualConditionFlag = conditionFlag;
|
|
406 |
actualTarget = trueDestination.label();
|
|
407 |
needJump = falseDestination != null && !crb.isSuccessorEdge(falseDestination);
|
|
408 |
predictTaken = trueDestinationProbability > .5d ? PREDICT_TAKEN : PREDICT_NOT_TAKEN;
|
|
409 |
}
|
|
410 |
if (!withDelayedNop && needJump) {
|
|
411 |
// We cannot make use of the delay slot when we jump in true-case and false-case
|
|
412 |
return false;
|
|
413 |
}
|
|
414 |
if (kind.isFloat()) {
|
|
415 |
FBPCC.emit(masm, Fcc0, actualConditionFlag, NOT_ANNUL, predictTaken, actualTarget);
|
|
416 |
} else {
|
|
417 |
assert kind.isInteger();
|
|
418 |
CC cc = kind.equals(WORD) ? Icc : Xcc;
|
|
419 |
BPCC.emit(masm, cc, actualConditionFlag, NOT_ANNUL, predictTaken, actualTarget);
|
|
420 |
}
|
|
421 |
if (withDelayedNop) {
|
|
422 |
masm.nop(); // delay slot
|
|
423 |
}
|
|
424 |
if (needJump) {
|
|
425 |
masm.jmp(falseDestination.label());
|
|
426 |
}
|
|
427 |
return true;
|
|
428 |
}
|
|
429 |
|
|
430 |
public static class StrategySwitchOp extends SPARCBlockEndOp {
|
|
431 |
public static final LIRInstructionClass<StrategySwitchOp> TYPE = LIRInstructionClass.create(StrategySwitchOp.class);
|
|
432 |
protected Constant[] keyConstants;
|
|
433 |
private final LabelRef[] keyTargets;
|
|
434 |
private LabelRef defaultTarget;
|
|
435 |
@Alive({REG}) protected Value key;
|
|
436 |
@Alive({REG, ILLEGAL}) protected Value constantTableBase;
|
|
437 |
@Temp({REG}) protected Value scratch;
|
|
438 |
protected final SwitchStrategy strategy;
|
46344
|
439 |
private final EconomicMap<Label, LabelHint> labelHints;
|
43972
|
440 |
private final List<Label> conditionalLabels = new ArrayList<>();
|
|
441 |
|
46344
|
442 |
public StrategySwitchOp(Value constantTableBase, SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, AllocatableValue key, Variable scratch) {
|
43972
|
443 |
this(TYPE, constantTableBase, strategy, keyTargets, defaultTarget, key, scratch);
|
|
444 |
}
|
|
445 |
|
46344
|
446 |
protected StrategySwitchOp(LIRInstructionClass<? extends StrategySwitchOp> c, Value constantTableBase, SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget,
|
|
447 |
AllocatableValue key,
|
|
448 |
Variable scratch) {
|
43972
|
449 |
super(c);
|
|
450 |
this.strategy = strategy;
|
|
451 |
this.keyConstants = strategy.getKeyConstants();
|
|
452 |
this.keyTargets = keyTargets;
|
|
453 |
this.defaultTarget = defaultTarget;
|
|
454 |
this.constantTableBase = constantTableBase;
|
|
455 |
this.key = key;
|
|
456 |
this.scratch = scratch;
|
46344
|
457 |
this.labelHints = EconomicMap.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE);
|
43972
|
458 |
assert keyConstants.length == keyTargets.length;
|
|
459 |
assert keyConstants.length == strategy.keyProbabilities.length;
|
|
460 |
}
|
|
461 |
|
|
462 |
@Override
|
|
463 |
public void emitCode(final CompilationResultBuilder crb, final SPARCMacroAssembler masm) {
|
|
464 |
final Register keyRegister = asRegister(key);
|
|
465 |
final Register constantBaseRegister = AllocatableValue.ILLEGAL.equals(constantTableBase) ? g0 : asRegister(constantTableBase);
|
|
466 |
strategy.run(new SwitchClosure(keyRegister, constantBaseRegister, crb, masm));
|
|
467 |
}
|
|
468 |
|
|
469 |
public class SwitchClosure extends BaseSwitchClosure {
|
|
470 |
private int conditionalLabelPointer = 0;
|
|
471 |
|
|
472 |
protected final Register keyRegister;
|
|
473 |
protected final Register constantBaseRegister;
|
|
474 |
protected final CompilationResultBuilder crb;
|
|
475 |
protected final SPARCMacroAssembler masm;
|
|
476 |
|
|
477 |
protected SwitchClosure(Register keyRegister, Register constantBaseRegister, CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
478 |
super(crb, masm, keyTargets, defaultTarget);
|
|
479 |
this.keyRegister = keyRegister;
|
|
480 |
this.constantBaseRegister = constantBaseRegister;
|
|
481 |
this.crb = crb;
|
|
482 |
this.masm = masm;
|
|
483 |
}
|
|
484 |
|
|
485 |
/**
|
|
486 |
* This method caches the generated labels over two assembly passes to get information
|
|
487 |
* about branch lengths.
|
|
488 |
*/
|
|
489 |
@Override
|
|
490 |
public Label conditionalJump(int index, Condition condition) {
|
|
491 |
Label label;
|
|
492 |
if (conditionalLabelPointer <= conditionalLabels.size()) {
|
|
493 |
label = new Label();
|
|
494 |
conditionalLabels.add(label);
|
|
495 |
conditionalLabelPointer = conditionalLabels.size();
|
|
496 |
} else {
|
|
497 |
// TODO: (sa) We rely here on the order how the labels are generated during
|
|
498 |
// code generation; if the order is not stable ower two assembly passes, the
|
|
499 |
// result can be wrong
|
|
500 |
label = conditionalLabels.get(conditionalLabelPointer++);
|
|
501 |
}
|
|
502 |
conditionalJump(index, condition, label);
|
|
503 |
return label;
|
|
504 |
}
|
|
505 |
|
|
506 |
@Override
|
|
507 |
protected void conditionalJump(int index, Condition condition, Label target) {
|
|
508 |
JavaConstant constant = (JavaConstant) keyConstants[index];
|
|
509 |
CC conditionCode;
|
|
510 |
Long bits = constant.asLong();
|
|
511 |
switch (constant.getJavaKind()) {
|
|
512 |
case Char:
|
|
513 |
case Byte:
|
|
514 |
case Short:
|
|
515 |
case Int:
|
|
516 |
conditionCode = CC.Icc;
|
|
517 |
break;
|
|
518 |
case Long:
|
|
519 |
conditionCode = CC.Xcc;
|
|
520 |
break;
|
|
521 |
default:
|
|
522 |
throw new GraalError("switch only supported for int, long and object");
|
|
523 |
}
|
|
524 |
ConditionFlag conditionFlag = fromCondition(keyRegister.getRegisterCategory().equals(CPU), condition, false);
|
|
525 |
LabelHint hint = requestHint(masm, target);
|
|
526 |
boolean isShortConstant = isSimm5(constant);
|
|
527 |
int cbCondPosition = masm.position();
|
|
528 |
if (!isShortConstant) { // Load constant takes one instruction
|
|
529 |
cbCondPosition += INSTRUCTION_SIZE;
|
|
530 |
}
|
|
531 |
boolean canUseShortBranch = masm.hasFeature(CPUFeature.CBCOND) && isShortBranch(masm, cbCondPosition, hint, target);
|
|
532 |
if (bits != null && canUseShortBranch) {
|
|
533 |
if (isShortConstant) {
|
|
534 |
CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, (int) (long) bits, target);
|
|
535 |
} else {
|
|
536 |
Register scratchRegister = asRegister(scratch);
|
|
537 |
const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY);
|
|
538 |
CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, scratchRegister, target);
|
|
539 |
}
|
|
540 |
} else {
|
|
541 |
if (bits != null && isSimm13(constant)) {
|
|
542 |
masm.cmp(keyRegister, (int) (long) bits); // Cast is safe
|
|
543 |
} else {
|
|
544 |
Register scratchRegister = asRegister(scratch);
|
|
545 |
const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY);
|
|
546 |
masm.cmp(keyRegister, scratchRegister);
|
|
547 |
}
|
|
548 |
BPCC.emit(masm, conditionCode, conditionFlag, ANNUL, PREDICT_TAKEN, target);
|
|
549 |
masm.nop(); // delay slot
|
|
550 |
}
|
|
551 |
}
|
|
552 |
}
|
|
553 |
|
|
554 |
protected LabelHint requestHint(SPARCMacroAssembler masm, Label label) {
|
|
555 |
LabelHint hint = labelHints.get(label);
|
|
556 |
if (hint == null) {
|
|
557 |
hint = masm.requestLabelHint(label);
|
|
558 |
labelHints.put(label, hint);
|
|
559 |
}
|
|
560 |
return hint;
|
|
561 |
}
|
|
562 |
|
|
563 |
protected int estimateEmbeddedSize(Constant c) {
|
|
564 |
JavaConstant v = (JavaConstant) c;
|
|
565 |
if (!SPARCAssembler.isSimm13(v)) {
|
|
566 |
return v.getJavaKind().getByteCount();
|
|
567 |
} else {
|
|
568 |
return 0;
|
|
569 |
}
|
|
570 |
}
|
|
571 |
|
|
572 |
@Override
|
|
573 |
public SizeEstimate estimateSize() {
|
|
574 |
int constantBytes = 0;
|
|
575 |
for (Constant c : keyConstants) {
|
|
576 |
constantBytes += estimateEmbeddedSize(c);
|
|
577 |
}
|
|
578 |
return new SizeEstimate(4 * keyTargets.length, constantBytes);
|
|
579 |
}
|
|
580 |
}
|
|
581 |
|
|
582 |
public static final class TableSwitchOp extends SPARCBlockEndOp {
|
|
583 |
public static final LIRInstructionClass<TableSwitchOp> TYPE = LIRInstructionClass.create(TableSwitchOp.class);
|
|
584 |
|
|
585 |
private final int lowKey;
|
|
586 |
private final LabelRef defaultTarget;
|
|
587 |
private final LabelRef[] targets;
|
|
588 |
@Alive protected Value index;
|
|
589 |
@Temp protected Value scratch;
|
|
590 |
|
|
591 |
public TableSwitchOp(final int lowKey, final LabelRef defaultTarget, final LabelRef[] targets, Variable index, Variable scratch) {
|
|
592 |
super(TYPE);
|
|
593 |
this.lowKey = lowKey;
|
|
594 |
this.defaultTarget = defaultTarget;
|
|
595 |
this.targets = targets;
|
|
596 |
this.index = index;
|
|
597 |
this.scratch = scratch;
|
|
598 |
}
|
|
599 |
|
|
600 |
@Override
|
|
601 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
602 |
Register value = asRegister(index, SPARCKind.WORD);
|
|
603 |
Register scratchReg = asRegister(scratch, SPARCKind.XWORD);
|
|
604 |
|
|
605 |
// Compare index against jump table bounds
|
|
606 |
int highKey = lowKey + targets.length - 1;
|
|
607 |
|
|
608 |
// subtract the low value from the switch value
|
|
609 |
if (isSimm13(lowKey)) {
|
|
610 |
masm.sub(value, lowKey, scratchReg);
|
|
611 |
} else {
|
|
612 |
try (ScratchRegister sc = masm.getScratchRegister()) {
|
|
613 |
Register scratch2 = sc.getRegister();
|
|
614 |
masm.setx(lowKey, scratch2, false);
|
|
615 |
masm.sub(value, scratch2, scratchReg);
|
|
616 |
}
|
|
617 |
}
|
|
618 |
int upperLimit = highKey - lowKey;
|
|
619 |
try (ScratchRegister sc = masm.getScratchRegister()) {
|
|
620 |
Register scratch2 = sc.getRegister();
|
|
621 |
if (isSimm13(upperLimit)) {
|
|
622 |
masm.cmp(scratchReg, upperLimit);
|
|
623 |
} else {
|
|
624 |
masm.setx(upperLimit, scratch2, false);
|
|
625 |
masm.cmp(scratchReg, upperLimit);
|
|
626 |
}
|
|
627 |
|
|
628 |
// Jump to default target if index is not within the jump table
|
|
629 |
if (defaultTarget != null) {
|
|
630 |
BPCC.emit(masm, Icc, GreaterUnsigned, NOT_ANNUL, PREDICT_TAKEN, defaultTarget.label());
|
|
631 |
masm.nop(); // delay slot
|
|
632 |
}
|
|
633 |
|
|
634 |
// Load jump table entry into scratch and jump to it
|
|
635 |
masm.sll(scratchReg, 3, scratchReg); // Multiply by 8
|
|
636 |
// Zero the left bits sll with shcnt>0 does not mask upper 32 bits
|
|
637 |
masm.srl(scratchReg, 0, scratchReg);
|
|
638 |
masm.rdpc(scratch2);
|
|
639 |
|
|
640 |
// The jump table follows four instructions after rdpc
|
|
641 |
masm.add(scratchReg, 4 * 4, scratchReg);
|
|
642 |
masm.jmpl(scratch2, scratchReg, g0);
|
|
643 |
}
|
|
644 |
masm.nop();
|
|
645 |
|
|
646 |
// Emit jump table entries
|
|
647 |
for (LabelRef target : targets) {
|
|
648 |
BPCC.emit(masm, Xcc, Always, NOT_ANNUL, PREDICT_TAKEN, target.label());
|
|
649 |
masm.nop(); // delay slot
|
|
650 |
}
|
|
651 |
}
|
|
652 |
|
|
653 |
@Override
|
|
654 |
public SizeEstimate estimateSize() {
|
|
655 |
return SizeEstimate.create(17 + targets.length * 2);
|
|
656 |
}
|
|
657 |
}
|
|
658 |
|
|
659 |
@Opcode("CMOVE")
|
|
660 |
public static final class CondMoveOp extends SPARCLIRInstruction {
|
|
661 |
public static final LIRInstructionClass<CondMoveOp> TYPE = LIRInstructionClass.create(CondMoveOp.class);
|
|
662 |
|
|
663 |
@Def({REG, HINT}) protected Value result;
|
|
664 |
@Use({REG, CONST}) protected Value trueValue;
|
|
665 |
@Use({REG, CONST}) protected Value falseValue;
|
|
666 |
|
|
667 |
private final ConditionFlag condition;
|
|
668 |
private final CC cc;
|
|
669 |
private final CMOV cmove;
|
|
670 |
|
|
671 |
public CondMoveOp(CMOV cmove, CC cc, ConditionFlag condition, Value trueValue, Value falseValue, Value result) {
|
|
672 |
super(TYPE);
|
|
673 |
this.result = result;
|
|
674 |
this.condition = condition;
|
|
675 |
this.trueValue = trueValue;
|
|
676 |
this.falseValue = falseValue;
|
|
677 |
this.cc = cc;
|
|
678 |
this.cmove = cmove;
|
|
679 |
}
|
|
680 |
|
|
681 |
@Override
|
|
682 |
public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
|
|
683 |
if (result.equals(trueValue)) { // We have the true value in place, do he opposite
|
|
684 |
cmove(masm, condition.negate(), falseValue);
|
|
685 |
} else if (result.equals(falseValue)) {
|
|
686 |
cmove(masm, condition, trueValue);
|
|
687 |
} else { // We have to move one of the input values to the result
|
|
688 |
ConditionFlag actualCondition = condition;
|
|
689 |
Value actualTrueValue = trueValue;
|
|
690 |
Value actualFalseValue = falseValue;
|
|
691 |
if (isJavaConstant(falseValue) && isSimm11(asJavaConstant(falseValue))) {
|
|
692 |
actualCondition = condition.negate();
|
|
693 |
actualTrueValue = falseValue;
|
|
694 |
actualFalseValue = trueValue;
|
|
695 |
}
|
|
696 |
SPARCMove.move(crb, masm, result, actualFalseValue, SPARCDelayedControlTransfer.DUMMY);
|
|
697 |
cmove(masm, actualCondition, actualTrueValue);
|
|
698 |
}
|
|
699 |
}
|
|
700 |
|
|
701 |
private void cmove(SPARCMacroAssembler masm, ConditionFlag localCondition, Value value) {
|
|
702 |
if (isConstantValue(value)) {
|
|
703 |
cmove.emit(masm, localCondition, cc, asImmediate(asJavaConstant(value)), asRegister(result));
|
|
704 |
} else {
|
|
705 |
cmove.emit(masm, localCondition, cc, asRegister(value), asRegister(result));
|
|
706 |
}
|
|
707 |
}
|
|
708 |
|
|
709 |
@Override
|
|
710 |
public SizeEstimate estimateSize() {
|
|
711 |
int constantSize = 0;
|
|
712 |
if (isJavaConstant(trueValue) && !SPARCAssembler.isSimm13(asJavaConstant(trueValue))) {
|
|
713 |
constantSize += trueValue.getPlatformKind().getSizeInBytes();
|
|
714 |
}
|
|
715 |
if (isJavaConstant(falseValue) && !SPARCAssembler.isSimm13(asJavaConstant(falseValue))) {
|
|
716 |
constantSize += trueValue.getPlatformKind().getSizeInBytes();
|
|
717 |
}
|
|
718 |
return SizeEstimate.create(3, constantSize);
|
|
719 |
}
|
|
720 |
}
|
|
721 |
|
|
722 |
public static ConditionFlag fromCondition(boolean integer, Condition cond, boolean unorderedIsTrue) {
|
|
723 |
if (integer) {
|
|
724 |
switch (cond) {
|
|
725 |
case EQ:
|
|
726 |
return Equal;
|
|
727 |
case NE:
|
|
728 |
return NotEqual;
|
|
729 |
case BT:
|
|
730 |
return LessUnsigned;
|
|
731 |
case LT:
|
|
732 |
return Less;
|
|
733 |
case BE:
|
|
734 |
return LessEqualUnsigned;
|
|
735 |
case LE:
|
|
736 |
return LessEqual;
|
|
737 |
case AE:
|
|
738 |
return GreaterEqualUnsigned;
|
|
739 |
case GE:
|
|
740 |
return GreaterEqual;
|
|
741 |
case AT:
|
|
742 |
return GreaterUnsigned;
|
|
743 |
case GT:
|
|
744 |
return Greater;
|
|
745 |
}
|
|
746 |
throw GraalError.shouldNotReachHere("Unimplemented for: " + cond);
|
|
747 |
} else {
|
|
748 |
switch (cond) {
|
|
749 |
case EQ:
|
|
750 |
return unorderedIsTrue ? F_UnorderedOrEqual : F_Equal;
|
|
751 |
case NE:
|
|
752 |
return ConditionFlag.F_NotEqual;
|
|
753 |
case LT:
|
|
754 |
return unorderedIsTrue ? F_UnorderedOrLess : F_Less;
|
|
755 |
case LE:
|
|
756 |
return unorderedIsTrue ? F_UnorderedOrLessOrEqual : F_LessOrEqual;
|
|
757 |
case GE:
|
|
758 |
return unorderedIsTrue ? F_UnorderedGreaterOrEqual : F_GreaterOrEqual;
|
|
759 |
case GT:
|
|
760 |
return unorderedIsTrue ? F_UnorderedOrGreater : F_Greater;
|
|
761 |
}
|
|
762 |
throw GraalError.shouldNotReachHere("Unkown condition: " + cond);
|
|
763 |
}
|
|
764 |
}
|
|
765 |
}
|