Skip to content

Commit f759464

Browse files
committed
[LoongArch] Use xvperm.w for cross-lane access within a single vector
1 parent 7f06b5c commit f759464

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,48 @@ static SDValue lowerVECTOR_SHUFFLE_XVSHUF4I(const SDLoc &DL, ArrayRef<int> Mask,
18321832
return lowerVECTOR_SHUFFLE_VSHUF4I(DL, Mask, VT, V1, V2, DAG);
18331833
}
18341834

1835+
/// Lower VECTOR_SHUFFLE into XVPERM (if possible).
1836+
static SDValue lowerVECTOR_SHUFFLE_XVPERM(const SDLoc &DL, ArrayRef<int> Mask,
1837+
MVT VT, SDValue V1, SDValue V2,
1838+
SelectionDAG &DAG) {
1839+
// LoongArch LASX only have XVPERM_W.
1840+
if (Mask.size() != 8 || (VT != MVT::v8i32 && VT != MVT::v8f32))
1841+
return SDValue();
1842+
1843+
unsigned NumElts = VT.getVectorNumElements();
1844+
unsigned HalfSize = NumElts / 2;
1845+
bool FrontLo = true, FrontHi = true;
1846+
bool BackLo = true, BackHi = true;
1847+
1848+
auto inRange = [](int val, int low, int high) {
1849+
return (val == -1) || (val >= low && val < high);
1850+
};
1851+
1852+
for (unsigned i = 0; i < HalfSize; ++i) {
1853+
int Fronti = Mask[i];
1854+
int Backi = Mask[i + HalfSize];
1855+
1856+
FrontLo &= inRange(Fronti, 0, HalfSize);
1857+
FrontHi &= inRange(Fronti, HalfSize, NumElts);
1858+
BackLo &= inRange(Backi, 0, HalfSize);
1859+
BackHi &= inRange(Backi, HalfSize, NumElts);
1860+
}
1861+
1862+
// If both the lower and upper 128-bit parts access only one half of the
1863+
// vector (either lower or upper), avoid using xvperm.w. The latency of
1864+
// xvperm.w(3) is higher than using xvshuf(1) and xvori(1).
1865+
if ((FrontLo && (BackLo || BackHi)) || (FrontHi && (BackLo || BackHi)))
1866+
return SDValue();
1867+
1868+
SmallVector<SDValue, 8> Masks;
1869+
for (unsigned i = 0; i < NumElts; ++i)
1870+
Masks.push_back(Mask[i] == -1 ? DAG.getUNDEF(MVT::i64)
1871+
: DAG.getConstant(Mask[i], DL, MVT::i64));
1872+
SDValue MaskVec = DAG.getBuildVector(MVT::v8i32, DL, Masks);
1873+
1874+
return DAG.getNode(LoongArchISD::XVPERM, DL, VT, V1, MaskVec);
1875+
}
1876+
18351877
/// Lower VECTOR_SHUFFLE into XVPACKEV (if possible).
18361878
static SDValue lowerVECTOR_SHUFFLE_XVPACKEV(const SDLoc &DL, ArrayRef<int> Mask,
18371879
MVT VT, SDValue V1, SDValue V2,
@@ -2235,6 +2277,8 @@ static SDValue lower256BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, MVT VT,
22352277
return Result;
22362278
if ((Result = lowerVECTOR_SHUFFLE_XVSHUF4I(DL, NewMask, VT, V1, V2, DAG)))
22372279
return Result;
2280+
if ((Result = lowerVECTOR_SHUFFLE_XVPERM(DL, NewMask, VT, V1, V2, DAG)))
2281+
return Result;
22382282
if ((Result = lowerVECTOR_SHUFFLEAsLanePermuteAndShuffle(DL, NewMask, VT,
22392283
V1, V2, DAG)))
22402284
return Result;

llvm/test/CodeGen/LoongArch/lasx/shuffle-as-permute-and-shuffle.ll

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,8 @@ define <8 x i32> @shuffle_v8i32(<8 x i32> %a) {
6161
; CHECK-LABEL: shuffle_v8i32:
6262
; CHECK: # %bb.0:
6363
; CHECK-NEXT: pcalau12i $a0, %pc_hi20(.LCPI4_0)
64-
; CHECK-NEXT: xvld $xr2, $a0, %pc_lo12(.LCPI4_0)
65-
; CHECK-NEXT: pcalau12i $a0, %pc_hi20(.LCPI4_1)
66-
; CHECK-NEXT: xvld $xr1, $a0, %pc_lo12(.LCPI4_1)
67-
; CHECK-NEXT: xvpermi.d $xr3, $xr0, 78
68-
; CHECK-NEXT: xvshuf.d $xr2, $xr0, $xr3
69-
; CHECK-NEXT: xvshuf.d $xr1, $xr2, $xr0
70-
; CHECK-NEXT: xvori.b $xr0, $xr1, 0
64+
; CHECK-NEXT: xvld $xr1, $a0, %pc_lo12(.LCPI4_0)
65+
; CHECK-NEXT: xvperm.w $xr0, $xr0, $xr1
7166
; CHECK-NEXT: ret
7267
%shuffle = shufflevector <8 x i32> %a, <8 x i32> poison, <8 x i32> <i32 4, i32 5, i32 0, i32 1, i32 4, i32 5, i32 6, i32 7>
7368
ret <8 x i32> %shuffle
@@ -117,13 +112,8 @@ define <8 x float> @shuffle_v8f32(<8 x float> %a) {
117112
; CHECK-LABEL: shuffle_v8f32:
118113
; CHECK: # %bb.0:
119114
; CHECK-NEXT: pcalau12i $a0, %pc_hi20(.LCPI8_0)
120-
; CHECK-NEXT: xvld $xr2, $a0, %pc_lo12(.LCPI8_0)
121-
; CHECK-NEXT: pcalau12i $a0, %pc_hi20(.LCPI8_1)
122-
; CHECK-NEXT: xvld $xr1, $a0, %pc_lo12(.LCPI8_1)
123-
; CHECK-NEXT: xvpermi.d $xr3, $xr0, 78
124-
; CHECK-NEXT: xvshuf.d $xr2, $xr0, $xr3
125-
; CHECK-NEXT: xvshuf.d $xr1, $xr2, $xr0
126-
; CHECK-NEXT: xvori.b $xr0, $xr1, 0
115+
; CHECK-NEXT: xvld $xr1, $a0, %pc_lo12(.LCPI8_0)
116+
; CHECK-NEXT: xvperm.w $xr0, $xr0, $xr1
127117
; CHECK-NEXT: ret
128118
%shuffle = shufflevector <8 x float> %a, <8 x float> poison, <8 x i32> <i32 4, i32 5, i32 0, i32 1, i32 4, i32 5, i32 6, i32 7>
129119
ret <8 x float> %shuffle

0 commit comments

Comments
 (0)