42
42
#include " llvm/TargetParser/Triple.h"
43
43
#include < algorithm>
44
44
#include < cassert>
45
+ #include < cstddef>
45
46
#include < cstdint>
46
47
#include < iterator>
47
48
#include < utility>
@@ -2968,8 +2969,14 @@ static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
2968
2969
// if the type is v8i16 and all the indices are less than 8 then the second
2969
2970
// operand is unused and can be replaced with anything. We choose to replace it
2970
2971
// with the used operand since this reduces the number of instructions overall.
2972
+ //
2973
+ // NOTE: SPLATI shuffle masks may contain UNDEFs, since isSPLATI() treats
2974
+ // UNDEFs as same as SPLATI index.
2975
+ // For other instances we use the last valid index if UNDEF is
2976
+ // encountered.
2971
2977
static SDValue lowerVECTOR_SHUFFLE_VSHF (SDValue Op, EVT ResTy,
2972
2978
const SmallVector<int , 16 > &Indices,
2979
+ const bool isSPLATI,
2973
2980
SelectionDAG &DAG) {
2974
2981
SmallVector<SDValue, 16 > Ops;
2975
2982
SDValue Op0;
@@ -2981,6 +2988,9 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2981
2988
SDLoc DL (Op);
2982
2989
int ResTyNumElts = ResTy.getVectorNumElements ();
2983
2990
2991
+ assert (Indices[0 ] >= 0 &&
2992
+ " shuffle mask starts with an UNDEF, which is not expected" );
2993
+
2984
2994
for (int i = 0 ; i < ResTyNumElts; ++i) {
2985
2995
// Idx == -1 means UNDEF
2986
2996
int Idx = Indices[i];
@@ -2990,9 +3000,17 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
2990
3000
if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2 )
2991
3001
Using2ndVec = true ;
2992
3002
}
2993
-
2994
- for (int Idx : Indices)
3003
+ int LastValidIndex = 0 ;
3004
+ for (size_t i = 0 ; i < Indices.size (); i++) {
3005
+ int Idx = Indices[i];
3006
+ if (Idx < 0 ) {
3007
+ // Continue using splati index or use the last valid index.
3008
+ Idx = isSPLATI ? Indices[0 ] : LastValidIndex;
3009
+ } else {
3010
+ LastValidIndex = Idx;
3011
+ }
2995
3012
Ops.push_back (DAG.getTargetConstant (Idx, DL, MaskEltTy));
3013
+ }
2996
3014
2997
3015
SDValue MaskVec = DAG.getBuildVector (MaskVecTy, DL, Ops);
2998
3016
@@ -3035,7 +3053,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
3035
3053
// splati.[bhwd] is preferable to the others but is matched from
3036
3054
// MipsISD::VSHF.
3037
3055
if (isVECTOR_SHUFFLE_SPLATI (Op, ResTy, Indices, DAG))
3038
- return lowerVECTOR_SHUFFLE_VSHF (Op, ResTy, Indices, DAG);
3056
+ return lowerVECTOR_SHUFFLE_VSHF (Op, ResTy, Indices, true , DAG);
3039
3057
SDValue Result;
3040
3058
if ((Result = lowerVECTOR_SHUFFLE_ILVEV (Op, ResTy, Indices, DAG)))
3041
3059
return Result;
@@ -3051,7 +3069,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
3051
3069
return Result;
3052
3070
if ((Result = lowerVECTOR_SHUFFLE_SHF (Op, ResTy, Indices, DAG)))
3053
3071
return Result;
3054
- return lowerVECTOR_SHUFFLE_VSHF (Op, ResTy, Indices, DAG);
3072
+ return lowerVECTOR_SHUFFLE_VSHF (Op, ResTy, Indices, false , DAG);
3055
3073
}
3056
3074
3057
3075
MachineBasicBlock *
0 commit comments