Skip to content

Commit 1fea712

Browse files
authored
[ValueTracking] Infer X u<= X +nuw Y for any Y (#75524)
Alive2: https://alive2.llvm.org/ce/z/kiGxCf Fixes #70374.
1 parent af2d740 commit 1fea712

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8102,10 +8102,9 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
81028102
}
81038103

81048104
case CmpInst::ICMP_ULE: {
8105-
const APInt *C;
8106-
8107-
// LHS u<= LHS +_{nuw} C for any C
8108-
if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
8105+
// LHS u<= LHS +_{nuw} V for any V
8106+
if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
8107+
cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
81098108
return true;
81108109

81118110
// RHS >> V u<= RHS for any V

llvm/test/Transforms/InstSimplify/implies.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,44 @@ define i1 @test_uge_icmp(i32 %length.i, i32 %i) {
316316
ret i1 %res
317317
}
318318

319+
; Test from PR70374
320+
define i1 @pr70374(i32 %x, i32 %y, i32 %z) {
321+
; CHECK-LABEL: @pr70374(
322+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[Y:%.*]], [[Z:%.*]]
323+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ule i32 [[ADD]], [[X:%.*]]
324+
; CHECK-NEXT: ret i1 [[CMP1]]
325+
;
326+
%add = add nuw i32 %y, %z
327+
%cmp1 = icmp ule i32 %add, %x
328+
%cmp2 = icmp uge i32 %x, %y
329+
%res = and i1 %cmp2, %cmp1
330+
ret i1 %res
331+
}
332+
333+
define i1 @pr70374_commuted_add(i32 %x, i32 %y, i32 %z) {
334+
; CHECK-LABEL: @pr70374_commuted_add(
335+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[Z:%.*]], [[Y:%.*]]
336+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ule i32 [[ADD]], [[X:%.*]]
337+
; CHECK-NEXT: ret i1 [[CMP1]]
338+
;
339+
%add = add nuw i32 %z, %y
340+
%cmp1 = icmp ule i32 %add, %x
341+
%cmp2 = icmp uge i32 %x, %y
342+
%res = and i1 %cmp2, %cmp1
343+
ret i1 %res
344+
}
345+
346+
define i1 @test_uge_icmp_value(i32 %length.i, i32 %i, i32 %j) {
347+
; CHECK-LABEL: @test_uge_icmp_value(
348+
; CHECK-NEXT: ret i1 true
349+
;
350+
%iplusj = add nuw i32 %i, %j
351+
%var29 = icmp uge i32 %length.i, %i
352+
%var30 = icmp uge i32 %length.i, %iplusj
353+
%res = icmp ule i1 %var30, %var29
354+
ret i1 %res
355+
}
356+
319357
; negative case, X + 1 <(s) Y !==> X <(s) Y (X = 0x7fffffff, Y = 0x7fbfffff)
320358
define i1 @test_sgt_icmp_no_nsw(i32 %length.i, i32 %i) {
321359
; CHECK-LABEL: @test_sgt_icmp_no_nsw(

0 commit comments

Comments
 (0)