[InstCombine] Fix rare condition violation in canonicalizeClampLike

With a "ult x, 0", the fold in canonicalizeClampLike does not validate
with undef inputs. This condition will usually have been simplified
away, but we should ensure the code is correct in case.
https://alive2.llvm.org/ce/z/S8HQ6H vs https://alive2.llvm.org/ce/z/h2XBJ_

See: https://reviews.llvm.org/D108049
This commit is contained in:
David Green 2021-10-28 15:03:07 +01:00
parent c788cad83b
commit 79011c705b
2 changed files with 12 additions and 4 deletions

View File

@ -1307,6 +1307,13 @@ static Instruction *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
// FIXME: we shouldn't care about lanes that are 'undef' in the end?
switch (Cmp0.getPredicate()) {
case ICmpInst::Predicate::ICMP_ULT:
// Although icmp ult %x, 0 is an unusual thing to try and should generally
// have been simplified, it does not verify with undef inputs so ensure we
// are not in a strange state.
if (!match(C0, m_SpecificInt_ICMP(
ICmpInst::Predicate::ICMP_NE,
APInt::getZero(C0->getType()->getScalarSizeInBits()))))
return nullptr;
break; // Great!
case ICmpInst::Predicate::ICMP_ULE:
// We'd have to increment C0 by one, and for that it must not have all-ones

View File

@ -600,10 +600,11 @@ define <2 x i8> @C0zeroV(<2 x i8> %X, <2 x i8> %y, <2 x i8> %z) {
define <2 x i8> @C0zeroVu(<2 x i8> %X, <2 x i8> %y, <2 x i8> %z) {
; CHECK-LABEL: @C0zeroVu(
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i8> [[X:%.*]], <i8 -10, i8 -10>
; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt <2 x i8> [[X]], <i8 -11, i8 -1>
; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP1]], <2 x i8> [[Y:%.*]], <2 x i8> [[X]]
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[TMP2]], <2 x i8> [[Z:%.*]], <2 x i8> [[TMP3]]
; CHECK-NEXT: [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 10, i8 10>
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> [[A]], <i8 0, i8 10>
; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i8> [[X]], <i8 -10, i8 -10>
; CHECK-NEXT: [[F:%.*]] = select <2 x i1> [[C]], <2 x i8> [[Y:%.*]], <2 x i8> [[Z:%.*]]
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[X]], <2 x i8> [[F]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%a = add <2 x i8> %X, <i8 10, i8 10>