Skip to content

Commit 27b1cd0

Browse files
committed
[AArch64] Utilize XAR for certain vector rotates
Resolves #137162 For cases when there isn't any `XOR` in the transformation, replace with a zero register.
1 parent ba3fa39 commit 27b1cd0

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,7 +4532,9 @@ bool AArch64DAGToDAGISel::trySelectXAR(SDNode *N) {
45324532

45334533
SDValue N0 = N->getOperand(0);
45344534
SDValue N1 = N->getOperand(1);
4535+
45354536
EVT VT = N->getValueType(0);
4537+
SDLoc DL(N);
45364538

45374539
// Essentially: rotr (xor(x, y), imm) -> xar (x, y, imm)
45384540
// Rotate by a constant is a funnel shift in IR which is exanded to
@@ -4558,10 +4560,18 @@ bool AArch64DAGToDAGISel::trySelectXAR(SDNode *N) {
45584560
!TLI->isAllActivePredicate(*CurDAG, N1.getOperand(0)))
45594561
return false;
45604562

4561-
SDValue XOR = N0.getOperand(1);
4562-
if (XOR.getOpcode() != ISD::XOR || XOR != N1.getOperand(1))
4563+
if (N0.getOperand(1) != N1.getOperand(1))
45634564
return false;
45644565

4566+
SDValue R1, R2;
4567+
bool IsXOROperand = true;
4568+
if (N0.getOperand(1).getOpcode() != ISD::XOR) {
4569+
IsXOROperand = false;
4570+
} else {
4571+
R1 = N0.getOperand(1).getOperand(0);
4572+
R2 = N1.getOperand(1).getOperand(1);
4573+
}
4574+
45654575
APInt ShlAmt, ShrAmt;
45664576
if (!ISD::isConstantSplatVector(N0.getOperand(2).getNode(), ShlAmt) ||
45674577
!ISD::isConstantSplatVector(N1.getOperand(2).getNode(), ShrAmt))
@@ -4570,11 +4580,23 @@ bool AArch64DAGToDAGISel::trySelectXAR(SDNode *N) {
45704580
if (ShlAmt + ShrAmt != VT.getScalarSizeInBits())
45714581
return false;
45724582

4573-
SDLoc DL(N);
4583+
if (!IsXOROperand) {
4584+
SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i64);
4585+
SDNode *MOV = CurDAG->getMachineNode(AArch64::MOVIv2d_ns, DL, VT, Zero);
4586+
SDValue MOVIV = SDValue(MOV, 0);
4587+
4588+
SDValue ZSub = CurDAG->getTargetConstant(AArch64::zsub, DL, MVT::i32);
4589+
SDNode *SubRegToReg = CurDAG->getMachineNode(AArch64::SUBREG_TO_REG, DL,
4590+
VT, Zero, MOVIV, ZSub);
4591+
4592+
R1 = N1->getOperand(1);
4593+
R2 = SDValue(SubRegToReg, 0);
4594+
}
4595+
45744596
SDValue Imm =
45754597
CurDAG->getTargetConstant(ShrAmt.getZExtValue(), DL, MVT::i32);
45764598

4577-
SDValue Ops[] = {XOR.getOperand(0), XOR.getOperand(1), Imm};
4599+
SDValue Ops[] = {R1, R2, Imm};
45784600
if (auto Opc = SelectOpcodeFromVT<SelectTypeKind::Int>(
45794601
VT, {AArch64::XAR_ZZZI_B, AArch64::XAR_ZZZI_H, AArch64::XAR_ZZZI_S,
45804602
AArch64::XAR_ZZZI_D})) {
@@ -4591,24 +4613,36 @@ bool AArch64DAGToDAGISel::trySelectXAR(SDNode *N) {
45914613
N1->getOpcode() != AArch64ISD::VLSHR)
45924614
return false;
45934615

4594-
if (N0->getOperand(0) != N1->getOperand(0) ||
4595-
N1->getOperand(0)->getOpcode() != ISD::XOR)
4616+
if (N0->getOperand(0) != N1->getOperand(0))
45964617
return false;
45974618

4598-
SDValue XOR = N0.getOperand(0);
4599-
SDValue R1 = XOR.getOperand(0);
4600-
SDValue R2 = XOR.getOperand(1);
4619+
SDValue R1, R2;
4620+
bool IsXOROperand = true;
4621+
if (N1->getOperand(0)->getOpcode() != ISD::XOR) {
4622+
IsXOROperand = false;
4623+
} else {
4624+
SDValue XOR = N0.getOperand(0);
4625+
R1 = XOR.getOperand(0);
4626+
R2 = XOR.getOperand(1);
4627+
}
46014628

46024629
unsigned HsAmt = N0.getConstantOperandVal(1);
46034630
unsigned ShAmt = N1.getConstantOperandVal(1);
46044631

4605-
SDLoc DL = SDLoc(N0.getOperand(1));
46064632
SDValue Imm = CurDAG->getTargetConstant(
46074633
ShAmt, DL, N0.getOperand(1).getValueType(), false);
46084634

46094635
if (ShAmt + HsAmt != 64)
46104636
return false;
46114637

4638+
if (!IsXOROperand) {
4639+
SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i64);
4640+
SDNode *MOV = CurDAG->getMachineNode(AArch64::MOVIv2d_ns, DL, VT, Zero);
4641+
SDValue MOVIV = SDValue(MOV, 0);
4642+
R1 = N1->getOperand(0);
4643+
R2 = MOVIV;
4644+
}
4645+
46124646
SDValue Ops[] = {R1, R2, Imm};
46134647
CurDAG->SelectNodeTo(N, AArch64::XAR, N0.getValueType(), Ops);
46144648

llvm/test/CodeGen/AArch64/sve2-xar.ll

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,12 @@ define <vscale x 2 x i64> @xar_nxv2i64_l_neg1(<vscale x 2 x i64> %x, <vscale x 2
170170
; OR instead of an XOR.
171171
; TODO: We could use usra instruction here for SVE2.
172172
define <vscale x 2 x i64> @xar_nxv2i64_l_neg2(<vscale x 2 x i64> %x, <vscale x 2 x i64> %y) {
173-
; CHECK-LABEL: xar_nxv2i64_l_neg2:
174-
; CHECK: // %bb.0:
175-
; CHECK-NEXT: orr z0.d, z0.d, z1.d
176-
; CHECK-NEXT: lsr z1.d, z0.d, #4
177-
; CHECK-NEXT: lsl z0.d, z0.d, #60
178-
; CHECK-NEXT: orr z0.d, z0.d, z1.d
179-
; CHECK-NEXT: ret
173+
; SVE2-LABEL: xar_nxv2i64_l_neg2:
174+
; SVE2: // %bb.0:
175+
; SVE2-NEXT: movi v2.2d, #0000000000000000
176+
; SVE2-NEXT: orr z0.d, z0.d, z1.d
177+
; SVE2-NEXT: xar z0.d, z0.d, z2.d, #4
178+
; SVE2-NEXT: ret
180179
%a = or <vscale x 2 x i64> %x, %y
181180
%b = call <vscale x 2 x i64> @llvm.fshl.nxv2i64(<vscale x 2 x i64> %a, <vscale x 2 x i64> %a, <vscale x 2 x i64> splat (i64 60))
182181
ret <vscale x 2 x i64> %b

llvm/test/CodeGen/AArch64/xar.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,22 @@ define <2 x i64> @xar(<2 x i64> %x, <2 x i64> %y) {
1919
ret <2 x i64> %b
2020
}
2121

22+
define <2 x i64> @xar_instead_of_or(<2 x i64> %r) {
23+
; SHA3-LABEL: xar_instead_of_or:
24+
; SHA3: // %bb.0: // %entry
25+
; SHA3-NEXT: movi v1.2d, #0000000000000000
26+
; SHA3-NEXT: xar v0.2d, v0.2d, v1.2d, #39
27+
; SHA3-NEXT: ret
28+
;
29+
; NOSHA3-LABEL: xar_instead_of_or:
30+
; NOSHA3: // %bb.0: // %entry
31+
; NOSHA3-NEXT: shl v1.2d, v0.2d, #25
32+
; NOSHA3-NEXT: usra v1.2d, v0.2d, #39
33+
; NOSHA3-NEXT: mov v0.16b, v1.16b
34+
; NOSHA3-NEXT: ret
35+
entry:
36+
%or = tail call <2 x i64> @llvm.fshl.v2i64(<2 x i64> %r, <2 x i64> %r, <2 x i64> splat (i64 25))
37+
ret <2 x i64> %or
38+
}
39+
2240
declare <2 x i64> @llvm.fshl.v2i64(<2 x i64>, <2 x i64>, <2 x i64>)

0 commit comments

Comments
 (0)