Skip to content

Commit 976244b

Browse files
committed
[RISCV] Canonicalize vrot{l,r} to vrev8 when lowering shuffle as rotate
A rotate of 8 bits of an e16 vector in either direction is equivalent to a byteswap, i.e. vrev8. There is a generic combine on ISD::ROT{L,R} to canonicalize these rotations to byteswaps, but on fixed vectors they are legalized before they have the chance to be combined. This patch teaches the rotate vector_shuffle lowering to emit these rotations as byteswaps to match the scalable vector behaviour. Reviewed By: reames Differential Revision: https://reviews.llvm.org/D158195
1 parent a61c4a0 commit 976244b

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,15 +4293,22 @@ static SDValue lowerVECTOR_SHUFFLEAsRotate(ShuffleVectorSDNode *SVN,
42934293
MVT ContainerVT = getContainerForFixedLengthVector(DAG, RotateVT, Subtarget);
42944294
SDValue VL =
42954295
getDefaultVLOps(RotateVT, ContainerVT, DL, DAG, Subtarget).second;
4296-
SDValue RotateAmtSplat = DAG.getNode(
4297-
RISCVISD::VMV_V_X_VL, DL, ContainerVT, DAG.getUNDEF(ContainerVT),
4298-
DAG.getConstant(RotateAmt, DL, Subtarget.getXLenVT()), VL);
4299-
RotateAmtSplat =
4300-
convertFromScalableVector(RotateVT, RotateAmtSplat, DAG, Subtarget);
4296+
SDValue Op = DAG.getBitcast(RotateVT, SVN->getOperand(0));
4297+
4298+
SDValue Rotate;
4299+
// A rotate of an i16 by 8 bits either direction is equivalent to a byteswap,
4300+
// so canonicalize to vrev8.
4301+
if (RotateVT.getScalarType() == MVT::i16 && RotateAmt == 8) {
4302+
Rotate = DAG.getNode(ISD::BSWAP, DL, RotateVT, Op);
4303+
} else {
4304+
SDValue RotateAmtSplat = DAG.getNode(
4305+
RISCVISD::VMV_V_X_VL, DL, ContainerVT, DAG.getUNDEF(ContainerVT),
4306+
DAG.getConstant(RotateAmt, DL, Subtarget.getXLenVT()), VL);
4307+
RotateAmtSplat =
4308+
convertFromScalableVector(RotateVT, RotateAmtSplat, DAG, Subtarget);
4309+
Rotate = DAG.getNode(ISD::ROTL, DL, RotateVT, Op, RotateAmtSplat);
4310+
}
43014311

4302-
SDValue Rotate =
4303-
DAG.getNode(ISD::ROTL, DL, RotateVT,
4304-
DAG.getBitcast(RotateVT, SVN->getOperand(0)), RotateAmtSplat);
43054312
return DAG.getBitcast(VT, Rotate);
43064313
}
43074314

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-reverse.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ define <2 x i8> @reverse_v2i8(<2 x i8> %a) {
180180
; ZVBB-LABEL: reverse_v2i8:
181181
; ZVBB: # %bb.0:
182182
; ZVBB-NEXT: vsetivli zero, 1, e16, mf4, ta, ma
183-
; ZVBB-NEXT: vror.vi v8, v8, 8
183+
; ZVBB-NEXT: vrev8.v v8, v8
184184
; ZVBB-NEXT: ret
185185
%res = call <2 x i8> @llvm.experimental.vector.reverse.v2i8(<2 x i8> %a)
186186
ret <2 x i8> %res

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-rotate.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ define <8 x i8> @shuffle_v8i8_as_i16(<8 x i8> %v) {
202202
; ZVBB-V-LABEL: shuffle_v8i8_as_i16:
203203
; ZVBB-V: # %bb.0:
204204
; ZVBB-V-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
205-
; ZVBB-V-NEXT: vror.vi v8, v8, 8
205+
; ZVBB-V-NEXT: vrev8.v v8, v8
206206
; ZVBB-V-NEXT: ret
207207
;
208208
; ZVBB-ZVE32X-LABEL: shuffle_v8i8_as_i16:
209209
; ZVBB-ZVE32X: # %bb.0:
210210
; ZVBB-ZVE32X-NEXT: vsetivli zero, 4, e16, m2, ta, ma
211-
; ZVBB-ZVE32X-NEXT: vror.vi v8, v8, 8
211+
; ZVBB-ZVE32X-NEXT: vrev8.v v8, v8
212212
; ZVBB-ZVE32X-NEXT: ret
213213
%shuffle = shufflevector <8 x i8> %v, <8 x i8> poison, <8 x i32> <i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6>
214214
ret <8 x i8> %shuffle

0 commit comments

Comments
 (0)