Index: llvm/trunk/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp @@ -4085,8 +4085,9 @@ static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, const Query &Q, unsigned MaxRecurse) { + Type *InVecTy = Op0->getType(); unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); - unsigned InVecNumElts = Op0->getType()->getVectorNumElements(); + unsigned InVecNumElts = InVecTy->getVectorNumElements(); auto *Op0Const = dyn_cast(Op0); auto *Op1Const = dyn_cast(Op1); @@ -4108,11 +4109,22 @@ MaskSelects1 = true; } if (!MaskSelects0 && Op1Const) - return ConstantFoldShuffleVectorInstruction(UndefValue::get(Op0->getType()), + return ConstantFoldShuffleVectorInstruction(UndefValue::get(InVecTy), Op1Const, Mask); if (!MaskSelects1 && Op0Const) - return ConstantFoldShuffleVectorInstruction( - Op0Const, UndefValue::get(Op0->getType()), Mask); + return ConstantFoldShuffleVectorInstruction(Op0Const, + UndefValue::get(InVecTy), Mask); + + // A shuffle of a splat is always the splat itself. Legal if the shuffle's + // value type is same as the input vectors' type. + if (auto *OpShuf = dyn_cast(Op0)) + if (!MaskSelects1 && RetTy == InVecTy && + OpShuf->getMask()->getSplatValue()) + return Op0; + if (auto *OpShuf = dyn_cast(Op1)) + if (!MaskSelects0 && RetTy == InVecTy && + OpShuf->getMask()->getSplatValue()) + return Op1; return nullptr; } Index: llvm/trunk/test/Transforms/InstSimplify/shufflevector.ll =================================================================== --- llvm/trunk/test/Transforms/InstSimplify/shufflevector.ll +++ llvm/trunk/test/Transforms/InstSimplify/shufflevector.ll @@ -29,14 +29,87 @@ define <4 x i32> @splat_operand(<4 x i32> %x) { ; CHECK-LABEL: @splat_operand( ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer -; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <4 x i32> -; CHECK-NEXT: ret <4 x i32> [[SHUF]] +; CHECK-NEXT: ret <4 x i32> [[SPLAT]] ; %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> ret <4 x i32> %shuf } +define <4 x i32> @splat_operand1(<4 x i32> %x, <4 x i32> %y) { +; CHECK-LABEL: @splat_operand1( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <4 x i32> zeroinitializer +; CHECK-NEXT: ret <4 x i32> [[SPLAT]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> %y, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> + ret <4 x i32> %shuf +} + +define <4 x i32> @splat_operand2(<4 x i32> %x, <4 x i32> %y) { +; CHECK-LABEL: @splat_operand2( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: ret <4 x i32> [[SPLAT]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> + ret <4 x i32> %shuf +} + +define <4 x i32> @splat_operand3(<4 x i32> %x) { +; CHECK-LABEL: @splat_operand3( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: ret <4 x i32> [[SPLAT]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> zeroinitializer, <4 x i32> %splat, <4 x i32> + ret <4 x i32> %shuf +} + +define <8 x i32> @splat_operand_negative(<4 x i32> %x) { +; CHECK-LABEL: @splat_operand_negative( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <8 x i32> +; CHECK-NEXT: ret <8 x i32> [[SHUF]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <8 x i32> + ret <8 x i32> %shuf +} + +define <4 x i32> @splat_operand_negative2(<4 x i32> %x, <4 x i32> %y) { +; CHECK-LABEL: @splat_operand_negative2( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> [[Y:%.*]], <4 x i32> +; CHECK-NEXT: ret <4 x i32> [[SHUF]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> + ret <4 x i32> %shuf +} + +define <4 x i32> @splat_operand_negative3(<4 x i32> %x, <4 x i32> %y) { +; CHECK-LABEL: @splat_operand_negative3( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer +; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[Y:%.*]], <4 x i32> [[SPLAT]], <4 x i32> +; CHECK-NEXT: ret <4 x i32> [[SHUF]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer + %shuf = shufflevector <4 x i32> %y, <4 x i32> %splat, <4 x i32> + ret <4 x i32> %shuf +} + +define <4 x i32> @splat_operand_negative4(<4 x i32> %x) { +; CHECK-LABEL: @splat_operand_negative4( +; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> +; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <4 x i32> +; CHECK-NEXT: ret <4 x i32> [[SHUF]] +; + %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> + %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> + ret <4 x i32> %shuf +} + define <4 x i32> @undef_mask(<4 x i32> %x) { ; CHECK-LABEL: @undef_mask( ; CHECK-NEXT: ret <4 x i32> undef