43972
|
1 |
/*
|
54601
|
2 |
* Copyright (c) 2015, 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 |
*/
|
|
23 |
|
50858
|
24 |
|
|
25 |
|
43972
|
26 |
package org.graalvm.compiler.core.aarch64;
|
|
27 |
|
54601
|
28 |
import jdk.vm.ci.aarch64.AArch64Kind;
|
55509
|
29 |
import jdk.vm.ci.code.CodeUtil;
|
52910
|
30 |
import jdk.vm.ci.meta.AllocatableValue;
|
|
31 |
import jdk.vm.ci.meta.JavaKind;
|
|
32 |
import jdk.vm.ci.meta.Value;
|
|
33 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
|
34 |
import jdk.internal.vm.compiler.collections.Equivalence;
|
52578
|
35 |
import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
|
52910
|
36 |
import org.graalvm.compiler.core.common.LIRKind;
|
54601
|
37 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
43972
|
38 |
import org.graalvm.compiler.core.gen.NodeMatchRules;
|
52578
|
39 |
import org.graalvm.compiler.core.match.ComplexMatchResult;
|
|
40 |
import org.graalvm.compiler.core.match.MatchRule;
|
52910
|
41 |
import org.graalvm.compiler.graph.Node;
|
43972
|
42 |
import org.graalvm.compiler.lir.LIRFrameState;
|
54084
|
43 |
import org.graalvm.compiler.lir.LabelRef;
|
52910
|
44 |
import org.graalvm.compiler.lir.Variable;
|
52578
|
45 |
import org.graalvm.compiler.lir.aarch64.AArch64ArithmeticOp;
|
55509
|
46 |
import org.graalvm.compiler.lir.aarch64.AArch64BitFieldOp;
|
54084
|
47 |
import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow;
|
43972
|
48 |
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
|
52578
|
49 |
import org.graalvm.compiler.nodes.ConstantNode;
|
43972
|
50 |
import org.graalvm.compiler.nodes.DeoptimizingNode;
|
54601
|
51 |
import org.graalvm.compiler.nodes.FixedNode;
|
54084
|
52 |
import org.graalvm.compiler.nodes.IfNode;
|
48190
|
53 |
import org.graalvm.compiler.nodes.NodeView;
|
52578
|
54 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
55 |
import org.graalvm.compiler.nodes.calc.AddNode;
|
52910
|
56 |
import org.graalvm.compiler.nodes.calc.AndNode;
|
52578
|
57 |
import org.graalvm.compiler.nodes.calc.BinaryNode;
|
54601
|
58 |
import org.graalvm.compiler.nodes.calc.IntegerLessThanNode;
|
52578
|
59 |
import org.graalvm.compiler.nodes.calc.LeftShiftNode;
|
52910
|
60 |
import org.graalvm.compiler.nodes.calc.NotNode;
|
|
61 |
import org.graalvm.compiler.nodes.calc.OrNode;
|
52578
|
62 |
import org.graalvm.compiler.nodes.calc.RightShiftNode;
|
52910
|
63 |
import org.graalvm.compiler.nodes.calc.SubNode;
|
52578
|
64 |
import org.graalvm.compiler.nodes.calc.UnsignedRightShiftNode;
|
52910
|
65 |
import org.graalvm.compiler.nodes.calc.XorNode;
|
43972
|
66 |
import org.graalvm.compiler.nodes.memory.Access;
|
|
67 |
|
|
68 |
public class AArch64NodeMatchRules extends NodeMatchRules {
|
52910
|
69 |
private static final EconomicMap<Class<? extends Node>, AArch64ArithmeticOp> nodeOpMap;
|
55509
|
70 |
private static final EconomicMap<Class<? extends BinaryNode>, AArch64BitFieldOp.BitFieldOpCode> bitFieldOpMap;
|
52910
|
71 |
private static final EconomicMap<Class<? extends BinaryNode>, AArch64MacroAssembler.ShiftType> shiftTypeMap;
|
|
72 |
|
|
73 |
static {
|
|
74 |
nodeOpMap = EconomicMap.create(Equivalence.IDENTITY, 5);
|
|
75 |
nodeOpMap.put(AddNode.class, AArch64ArithmeticOp.ADD);
|
|
76 |
nodeOpMap.put(SubNode.class, AArch64ArithmeticOp.SUB);
|
|
77 |
nodeOpMap.put(AndNode.class, AArch64ArithmeticOp.AND);
|
|
78 |
nodeOpMap.put(OrNode.class, AArch64ArithmeticOp.OR);
|
|
79 |
nodeOpMap.put(XorNode.class, AArch64ArithmeticOp.XOR);
|
|
80 |
|
55509
|
81 |
bitFieldOpMap = EconomicMap.create(Equivalence.IDENTITY, 2);
|
|
82 |
bitFieldOpMap.put(UnsignedRightShiftNode.class, AArch64BitFieldOp.BitFieldOpCode.UBFX);
|
|
83 |
bitFieldOpMap.put(LeftShiftNode.class, AArch64BitFieldOp.BitFieldOpCode.UBFIZ);
|
|
84 |
|
52910
|
85 |
shiftTypeMap = EconomicMap.create(Equivalence.IDENTITY, 3);
|
|
86 |
shiftTypeMap.put(LeftShiftNode.class, AArch64MacroAssembler.ShiftType.LSL);
|
|
87 |
shiftTypeMap.put(RightShiftNode.class, AArch64MacroAssembler.ShiftType.ASR);
|
|
88 |
shiftTypeMap.put(UnsignedRightShiftNode.class, AArch64MacroAssembler.ShiftType.LSR);
|
|
89 |
}
|
43972
|
90 |
|
|
91 |
public AArch64NodeMatchRules(LIRGeneratorTool gen) {
|
|
92 |
super(gen);
|
|
93 |
}
|
|
94 |
|
|
95 |
protected LIRFrameState getState(Access access) {
|
|
96 |
if (access instanceof DeoptimizingNode) {
|
|
97 |
return state((DeoptimizingNode) access);
|
|
98 |
}
|
|
99 |
return null;
|
|
100 |
}
|
|
101 |
|
|
102 |
protected AArch64Kind getMemoryKind(Access access) {
|
48190
|
103 |
return (AArch64Kind) gen.getLIRKind(access.asNode().stamp(NodeView.DEFAULT)).getPlatformKind();
|
43972
|
104 |
}
|
|
105 |
|
52910
|
106 |
private AllocatableValue moveSp(AllocatableValue value) {
|
|
107 |
return getLIRGeneratorTool().moveSp(value);
|
|
108 |
}
|
|
109 |
|
55509
|
110 |
private ComplexMatchResult emitBitField(AArch64BitFieldOp.BitFieldOpCode op, ValueNode value, int lsb, int width) {
|
|
111 |
assert op != null;
|
|
112 |
assert value.getStackKind().isNumericInteger();
|
|
113 |
|
|
114 |
return builder -> {
|
|
115 |
Value a = operand(value);
|
|
116 |
Variable result = gen.newVariable(LIRKind.combine(a));
|
|
117 |
AllocatableValue src = moveSp(gen.asAllocatable(a));
|
|
118 |
gen.append(new AArch64BitFieldOp(op, result, src, lsb, width));
|
|
119 |
return result;
|
|
120 |
};
|
|
121 |
}
|
|
122 |
|
54601
|
123 |
private ComplexMatchResult emitBinaryShift(AArch64ArithmeticOp op, ValueNode value, BinaryNode shift,
|
|
124 |
boolean isShiftNot) {
|
52910
|
125 |
AArch64MacroAssembler.ShiftType shiftType = shiftTypeMap.get(shift.getClass());
|
|
126 |
assert shiftType != null;
|
|
127 |
assert value.getStackKind().isNumericInteger();
|
|
128 |
assert shift.getX().getStackKind().isNumericInteger();
|
52578
|
129 |
assert shift.getY() instanceof ConstantNode;
|
|
130 |
|
52910
|
131 |
return builder -> {
|
|
132 |
Value a = operand(value);
|
|
133 |
Value b = operand(shift.getX());
|
|
134 |
Variable result = gen.newVariable(LIRKind.combine(a, b));
|
|
135 |
AllocatableValue x = moveSp(gen.asAllocatable(a));
|
|
136 |
AllocatableValue y = moveSp(gen.asAllocatable(b));
|
|
137 |
int shiftAmount = shift.getY().asJavaConstant().asInt();
|
|
138 |
gen.append(new AArch64ArithmeticOp.BinaryShiftOp(op, result, x, y, shiftType, shiftAmount, isShiftNot));
|
|
139 |
return result;
|
|
140 |
};
|
52578
|
141 |
}
|
|
142 |
|
54601
|
143 |
private ComplexMatchResult emitBitTestAndBranch(FixedNode trueSuccessor, FixedNode falseSuccessor,
|
|
144 |
ValueNode value, double trueProbability, int nbits) {
|
|
145 |
return builder -> {
|
|
146 |
LabelRef trueDestination = getLIRBlock(trueSuccessor);
|
|
147 |
LabelRef falseDestination = getLIRBlock(falseSuccessor);
|
|
148 |
AllocatableValue src = moveSp(gen.asAllocatable(operand(value)));
|
|
149 |
gen.append(new AArch64ControlFlow.BitTestAndBranchOp(trueDestination, falseDestination, src,
|
|
150 |
trueProbability, nbits));
|
|
151 |
return null;
|
|
152 |
};
|
|
153 |
}
|
|
154 |
|
55509
|
155 |
@MatchRule("(And (UnsignedRightShift=shift a Constant=b) Constant=c)")
|
|
156 |
@MatchRule("(LeftShift=shift (And a Constant=c) Constant=b)")
|
|
157 |
public ComplexMatchResult unsignedBitField(BinaryNode shift, ValueNode a, ConstantNode b, ConstantNode c) {
|
|
158 |
JavaKind srcKind = a.getStackKind();
|
|
159 |
assert srcKind.isNumericInteger();
|
|
160 |
AArch64BitFieldOp.BitFieldOpCode op = bitFieldOpMap.get(shift.getClass());
|
|
161 |
assert op != null;
|
|
162 |
int distance = b.asJavaConstant().asInt();
|
|
163 |
long mask = c.asJavaConstant().asLong();
|
|
164 |
|
|
165 |
// The Java(R) Language Specification CHAPTER 15.19 Shift Operators says:
|
|
166 |
// "If the promoted type of the left-hand operand is int(long), then only the five(six)
|
|
167 |
// lowest-order bits of the right-hand operand are used as the shift distance."
|
|
168 |
distance = distance & (srcKind == JavaKind.Int ? 0x1f : 0x3f);
|
|
169 |
|
|
170 |
// Constraint 1: Mask plus one should be a power-of-2 integer.
|
|
171 |
if (!CodeUtil.isPowerOf2(mask + 1)) {
|
|
172 |
return null;
|
|
173 |
}
|
|
174 |
int width = CodeUtil.log2(mask + 1);
|
|
175 |
int srcBits = srcKind.getBitCount();
|
|
176 |
// Constraint 2: Bit field width is less than 31(63) for int(long) as any bit field move
|
|
177 |
// operations can be done by a single shift instruction if the width is 31(63).
|
|
178 |
if (width >= srcBits - 1) {
|
|
179 |
return null;
|
|
180 |
}
|
|
181 |
// Constraint 3: Sum of bit field width and the shift distance is less or equal to 32(64)
|
|
182 |
// for int(long) as the specification of AArch64 bit field instructions.
|
|
183 |
if (width + distance > srcBits) {
|
|
184 |
return null;
|
|
185 |
}
|
|
186 |
return emitBitField(op, a, distance, width);
|
|
187 |
}
|
|
188 |
|
52578
|
189 |
@MatchRule("(Add=binary a (LeftShift=shift b Constant))")
|
|
190 |
@MatchRule("(Add=binary a (RightShift=shift b Constant))")
|
|
191 |
@MatchRule("(Add=binary a (UnsignedRightShift=shift b Constant))")
|
|
192 |
@MatchRule("(Sub=binary a (LeftShift=shift b Constant))")
|
|
193 |
@MatchRule("(Sub=binary a (RightShift=shift b Constant))")
|
|
194 |
@MatchRule("(Sub=binary a (UnsignedRightShift=shift b Constant))")
|
|
195 |
public ComplexMatchResult addSubShift(BinaryNode binary, ValueNode a, BinaryNode shift) {
|
52910
|
196 |
AArch64ArithmeticOp op = nodeOpMap.get(binary.getClass());
|
|
197 |
assert op != null;
|
|
198 |
return emitBinaryShift(op, a, shift, false);
|
|
199 |
}
|
|
200 |
|
|
201 |
@MatchRule("(And=binary a (LeftShift=shift b Constant))")
|
|
202 |
@MatchRule("(And=binary a (RightShift=shift b Constant))")
|
|
203 |
@MatchRule("(And=binary a (UnsignedRightShift=shift b Constant))")
|
|
204 |
@MatchRule("(Or=binary a (LeftShift=shift b Constant))")
|
|
205 |
@MatchRule("(Or=binary a (RightShift=shift b Constant))")
|
|
206 |
@MatchRule("(Or=binary a (UnsignedRightShift=shift b Constant))")
|
|
207 |
@MatchRule("(Xor=binary a (LeftShift=shift b Constant))")
|
|
208 |
@MatchRule("(Xor=binary a (RightShift=shift b Constant))")
|
|
209 |
@MatchRule("(Xor=binary a (UnsignedRightShift=shift b Constant))")
|
|
210 |
@MatchRule("(And=binary a (Not (LeftShift=shift b Constant)))")
|
|
211 |
@MatchRule("(And=binary a (Not (RightShift=shift b Constant)))")
|
|
212 |
@MatchRule("(And=binary a (Not (UnsignedRightShift=shift b Constant)))")
|
|
213 |
@MatchRule("(Or=binary a (Not (LeftShift=shift b Constant)))")
|
|
214 |
@MatchRule("(Or=binary a (Not (RightShift=shift b Constant)))")
|
|
215 |
@MatchRule("(Or=binary a (Not (UnsignedRightShift=shift b Constant)))")
|
|
216 |
@MatchRule("(Xor=binary a (Not (LeftShift=shift b Constant)))")
|
|
217 |
@MatchRule("(Xor=binary a (Not (RightShift=shift b Constant)))")
|
|
218 |
@MatchRule("(Xor=binary a (Not (UnsignedRightShift=shift b Constant)))")
|
|
219 |
public ComplexMatchResult logicShift(BinaryNode binary, ValueNode a, BinaryNode shift) {
|
|
220 |
AArch64ArithmeticOp op = nodeOpMap.get(binary.getClass());
|
|
221 |
assert op != null;
|
|
222 |
ValueNode operand = binary.getX() == a ? binary.getY() : binary.getX();
|
|
223 |
boolean isShiftNot = operand instanceof NotNode;
|
|
224 |
return emitBinaryShift(op, a, shift, isShiftNot);
|
|
225 |
}
|
|
226 |
|
|
227 |
@MatchRule("(Mul (Negate a) b)")
|
|
228 |
@MatchRule("(Negate (Mul a b))")
|
|
229 |
public ComplexMatchResult multiplyNegate(ValueNode a, ValueNode b) {
|
|
230 |
if (a.getStackKind().isNumericInteger() && b.getStackKind().isNumericInteger()) {
|
|
231 |
return builder -> getArithmeticLIRGenerator().emitMNeg(operand(a), operand(b));
|
|
232 |
}
|
|
233 |
return null;
|
|
234 |
}
|
|
235 |
|
|
236 |
@MatchRule("(Add=binary (Mul a b) c)")
|
|
237 |
@MatchRule("(Sub=binary c (Mul a b))")
|
|
238 |
public ComplexMatchResult multiplyAddSub(BinaryNode binary, ValueNode a, ValueNode b, ValueNode c) {
|
|
239 |
JavaKind kindA = a.getStackKind();
|
|
240 |
JavaKind kindB = b.getStackKind();
|
|
241 |
JavaKind kindC = c.getStackKind();
|
|
242 |
if (!kindA.isNumericInteger() || !kindB.isNumericInteger() || !kindC.isNumericInteger()) {
|
|
243 |
return null;
|
|
244 |
}
|
|
245 |
|
52578
|
246 |
if (binary instanceof AddNode) {
|
52910
|
247 |
return builder -> getArithmeticLIRGenerator().emitMAdd(operand(a), operand(b), operand(c));
|
52578
|
248 |
}
|
52910
|
249 |
return builder -> getArithmeticLIRGenerator().emitMSub(operand(a), operand(b), operand(c));
|
52578
|
250 |
}
|
|
251 |
|
54084
|
252 |
/**
|
|
253 |
* ((x & (1 << n)) == 0) -> tbz/tbnz n label.
|
|
254 |
*/
|
|
255 |
@MatchRule("(If (IntegerTest value Constant=a))")
|
|
256 |
public ComplexMatchResult testBitAndBranch(IfNode root, ValueNode value, ConstantNode a) {
|
|
257 |
if (value.getStackKind().isNumericInteger()) {
|
|
258 |
long constant = a.asJavaConstant().asLong();
|
|
259 |
if (Long.bitCount(constant) == 1) {
|
54601
|
260 |
return emitBitTestAndBranch(root.trueSuccessor(), root.falseSuccessor(), value,
|
|
261 |
root.getTrueSuccessorProbability(), Long.numberOfTrailingZeros(constant));
|
54084
|
262 |
}
|
|
263 |
}
|
|
264 |
return null;
|
|
265 |
}
|
|
266 |
|
54601
|
267 |
/**
|
|
268 |
* if x < 0 <=> tbz x, sizeOfBits(x) - 1, label.
|
|
269 |
*/
|
|
270 |
@MatchRule("(If (IntegerLessThan=lessNode x Constant=y))")
|
|
271 |
public ComplexMatchResult checkNegativeAndBranch(IfNode root, IntegerLessThanNode lessNode, ValueNode x, ConstantNode y) {
|
|
272 |
JavaKind xKind = x.getStackKind();
|
|
273 |
assert xKind.isNumericInteger();
|
|
274 |
if (y.isJavaConstant() && (0 == y.asJavaConstant().asLong()) && lessNode.condition().equals(CanonicalCondition.LT)) {
|
|
275 |
return emitBitTestAndBranch(root.falseSuccessor(), root.trueSuccessor(), x,
|
|
276 |
1.0 - root.getTrueSuccessorProbability(), xKind.getBitCount() - 1);
|
|
277 |
}
|
|
278 |
return null;
|
|
279 |
}
|
|
280 |
|
43972
|
281 |
@Override
|
|
282 |
public AArch64LIRGenerator getLIRGeneratorTool() {
|
|
283 |
return (AArch64LIRGenerator) gen;
|
|
284 |
}
|
|
285 |
|
|
286 |
protected AArch64ArithmeticLIRGenerator getArithmeticLIRGenerator() {
|
|
287 |
return (AArch64ArithmeticLIRGenerator) getLIRGeneratorTool().getArithmetic();
|
|
288 |
}
|
|
289 |
}
|