[InstSimplify] sle on i1 also encodes implication

We already support SGE, so the same logic should hold for SLE with
the LHS and RHS swapped.

I didn't see this in the wild. Just happened to walk past this code
and thought it was odd that it was asymmetric in what condition
codes it handled.

Reviewed By: spatel, reames

Differential Revision: https://reviews.llvm.org/D131805
This commit is contained in:
Craig Topper 2022-08-15 08:17:54 -07:00
parent d230055234
commit ef8c34e954
2 changed files with 17 additions and 0 deletions

View File

@ -2868,6 +2868,11 @@ static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
return getTrue(ITy);
break;
case ICmpInst::ICMP_SLE:
/// SLE follows the same logic as SGE with the LHS and RHS swapped.
if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
return getTrue(ITy);
break;
}
return nullptr;

View File

@ -255,3 +255,15 @@ define i1 @test_sge(i32 %length.i, i32 %i) {
%res = icmp sge i1 %var30, %var29
ret i1 %res
}
; X <=(s) Y == Y ==> X (i1 1 becomes -1 for reasoning)
define i1 @test_sle(i32 %length.i, i32 %i) {
; CHECK-LABEL: @test_sle(
; CHECK-NEXT: ret i1 true
;
%iplus1 = add nsw nuw i32 %i, 1
%var29 = icmp ult i32 %i, %length.i
%var30 = icmp ult i32 %iplus1, %length.i
%res = icmp sle i1 %var29, %var30
ret i1 %res
}