Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1337,6 +1337,12 @@ if (!Sel1->hasOneUse()) return nullptr; + // If the types do not match, look through any truncs to the underlying + // instruction. + if (Cmp00->getType() != X->getType()) + if (auto *T = dyn_cast(X)) + X = T->getOperand(0); + // We now can finish matching the condition of the outermost select: // it should either be the X itself, or an addition of some constant to X. Constant *C1; @@ -1407,15 +1413,29 @@ if (!match(Precond2, m_One())) return nullptr; + // If we are matching from a truncated input, we need to sext the + // ReplacementLow and ReplacementHigh values. Only do the transform if they + // are free to extend due to being constants. + if (X->getType() != Sel0.getType() && + (!isa(ReplacementLow) || !isa(ReplacementHigh))) + return nullptr; + // All good, finally emit the new pattern. Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl); Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl); - Value *MaybeReplacedLow = - Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X); - Instruction *MaybeReplacedHigh = - SelectInst::Create(ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow); - - return MaybeReplacedHigh; + Value *MaybeReplacedLow = Builder.CreateSelect( + ShouldReplaceLow, Builder.CreateSExt(ReplacementLow, X->getType()), X); + + // Create the final select. If we looked through a truncate above, we will + // need to retruncate the result. + if (X->getType() == Sel0.getType()) + return SelectInst::Create(ShouldReplaceHigh, ReplacementHigh, + MaybeReplacedLow); + + Value *MaybeReplacedHigh = Builder.CreateSelect( + ShouldReplaceHigh, Builder.CreateSExt(ReplacementHigh, X->getType()), + MaybeReplacedLow); + return new TruncInst(MaybeReplacedHigh, Sel0.getType()); } // If we have Index: llvm/test/Transforms/InstCombine/truncating-saturate.ll =================================================================== --- llvm/test/Transforms/InstCombine/truncating-saturate.ll +++ llvm/test/Transforms/InstCombine/truncating-saturate.ll @@ -57,12 +57,11 @@ define i16 @testi32i16i8(i32 %add) { ; CHECK-LABEL: @testi32i16i8( -; CHECK-NEXT: [[A:%.*]] = add i32 [[ADD:%.*]], 128 -; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A]], 256 -; CHECK-NEXT: [[T:%.*]] = trunc i32 [[ADD]] to i16 -; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[ADD]], -1 -; CHECK-NEXT: [[F:%.*]] = select i1 [[C]], i16 127, i16 -128 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP]], i16 [[T]], i16 [[F]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[ADD:%.*]], -128 +; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[ADD]], i32 -128 +; CHECK-NEXT: [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 127 +; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 127 +; CHECK-NEXT: [[R:%.*]] = trunc i32 [[TMP4]] to i16 ; CHECK-NEXT: ret i16 [[R]] ; %a = add i32 %add, 128 @@ -76,12 +75,11 @@ define <4 x i16> @testv4i32i16i8(<4 x i32> %add) { ; CHECK-LABEL: @testv4i32i16i8( -; CHECK-NEXT: [[A:%.*]] = add <4 x i32> [[ADD:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ult <4 x i32> [[A]], -; CHECK-NEXT: [[T:%.*]] = trunc <4 x i32> [[ADD]] to <4 x i16> -; CHECK-NEXT: [[C:%.*]] = icmp sgt <4 x i32> [[ADD]], -; CHECK-NEXT: [[F:%.*]] = select <4 x i1> [[C]], <4 x i16> , <4 x i16> -; CHECK-NEXT: [[R:%.*]] = select <4 x i1> [[CMP]], <4 x i16> [[T]], <4 x i16> [[F]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[ADD:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[ADD]], <4 x i32> +; CHECK-NEXT: [[TMP3:%.*]] = icmp slt <4 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP4:%.*]] = select <4 x i1> [[TMP3]], <4 x i32> [[TMP2]], <4 x i32> +; CHECK-NEXT: [[R:%.*]] = trunc <4 x i32> [[TMP4]] to <4 x i16> ; CHECK-NEXT: ret <4 x i16> [[R]] ; %a = add <4 x i32> %add, @@ -310,12 +308,11 @@ define i16 @differentconsts(i32 %x, i16 %replacement_low, i16 %replacement_high) { ; CHECK-LABEL: @differentconsts( -; CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[X:%.*]], 128 -; CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i16 256, i16 -1 -; CHECK-NEXT: [[T2:%.*]] = add i32 [[X]], 16 -; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 144 -; CHECK-NEXT: [[T4:%.*]] = trunc i32 [[X]] to i16 -; CHECK-NEXT: [[R:%.*]] = select i1 [[T3]], i16 [[T4]], i16 [[T1]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], -16 +; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt i32 [[X]], 127 +; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[X]] to i16 +; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP1]], i16 256, i16 [[TMP3]] +; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i16 -1, i16 [[TMP4]] ; CHECK-NEXT: ret i16 [[R]] ; %t0 = icmp slt i32 %x, 128