Skip to content

Commit d2fae9a

Browse files
committed
[MIPS][MSA] Handle UNDEFs in shuffle indices for VSHF
Currently VSHF does not handle UNDEF indices. However isSPLATI() is able to handle undefs, which may pass indices with undefs to this function. Adding a check to handle undefs in shuffle indices. Also, shuffle mask widened from v2 vector types are guranteed to contain UNDEFs. These shuffle lower logics can handle UNDEFs, so we just leave it as is, except for VSHF, which we must use whatever necessary to fill the UNDEFs.
1 parent 68e7055 commit d2fae9a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

llvm/lib/Target/Mips/MipsSEISelLowering.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "llvm/TargetParser/Triple.h"
4343
#include <algorithm>
4444
#include <cassert>
45+
#include <cstddef>
4546
#include <cstdint>
4647
#include <iterator>
4748
#include <utility>
@@ -2952,8 +2953,14 @@ static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
29522953
// if the type is v8i16 and all the indices are less than 8 then the second
29532954
// operand is unused and can be replaced with anything. We choose to replace it
29542955
// with the used operand since this reduces the number of instructions overall.
2956+
//
2957+
// NOTE: SPLATI shuffle masks may contain UNDEFs, since isSPLATI() treats
2958+
// UNDEFs as same as SPLATI index.
2959+
// For other instances we use the last valid index if UNDEF is
2960+
// encountered.
29552961
static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29562962
const SmallVector<int, 16> &Indices,
2963+
const bool isSPLATI,
29572964
SelectionDAG &DAG) {
29582965
SmallVector<SDValue, 16> Ops;
29592966
SDValue Op0;
@@ -2965,6 +2972,9 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29652972
SDLoc DL(Op);
29662973
int ResTyNumElts = ResTy.getVectorNumElements();
29672974

2975+
assert(Indices[0] >= 0 &&
2976+
"shuffle mask starts with an UNDEF, which is not expected");
2977+
29682978
for (int i = 0; i < ResTyNumElts; ++i) {
29692979
// Idx == -1 means UNDEF
29702980
int Idx = Indices[i];
@@ -2974,9 +2984,14 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29742984
if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
29752985
Using2ndVec = true;
29762986
}
2977-
2978-
for (int Idx : Indices)
2987+
for (size_t i = 0; i < Indices.size(); i++) {
2988+
int Idx = Indices[i];
2989+
if (Indices[i] < 0) {
2990+
// Continue using splati index or use the last valid index.
2991+
Idx = isSPLATI ? Indices[0] : Indices[i - 1];
2992+
}
29792993
Ops.push_back(DAG.getTargetConstant(Idx, DL, MaskEltTy));
2994+
}
29802995

29812996
SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
29822997

@@ -3019,7 +3034,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
30193034
// splati.[bhwd] is preferable to the others but is matched from
30203035
// MipsISD::VSHF.
30213036
if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
3022-
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3037+
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, true, DAG);
30233038
SDValue Result;
30243039
if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
30253040
return Result;
@@ -3035,7 +3050,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
30353050
return Result;
30363051
if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
30373052
return Result;
3038-
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3053+
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, false, DAG);
30393054
}
30403055

30413056
MachineBasicBlock *

0 commit comments

Comments
 (0)