Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,12 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
}

if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
C.isNonNegative() && (C - *C2).isNonNegative() &&
computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
return new ICmpInst(ICmpInst::getSignedPredicate(Pred), X,
ConstantInt::get(Ty, C - *C2));

auto CR = ConstantRange::makeExactICmpRegion(Pred, C).subtract(*C2);
const APInt &Upper = CR.getUpper();
const APInt &Lower = CR.getLower();
Expand Down
69 changes: 69 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3102,3 +3102,72 @@ define i1 @uge_add_C2_pow2_C_neg(i8 %x) {
}

declare void @llvm.assume(i1)

; Change an unsigned predicate to signed in icmp (add x, C1), C2
define i1 @icmp_add_constant_with_constant_ult_to_slt(i32 range(i32 -4, 10) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ult_to_slt(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 8
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add nsw i32 %x, 5
%cmp = icmp ult i32 %add, 13
ret i1 %cmp
}

define i1 @icmp_add_constant_with_constant_ugt_to_sgt(i32 range(i32 -4, 10) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ugt_to_sgt(
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], 2
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add nsw i32 %x, 10
%cmp = icmp ugt i32 %add, 12
ret i1 %cmp
}

; Negative test: x + C1 may be negative
define i1 @icmp_add_constant_with_constant_ult_to_slt_neg1(i32 range(i32 -5, 10) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ult_to_slt_neg1(
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[X:%.*]], 4
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 20
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add nsw i32 %x, 4
%cmp = icmp ult i32 %add, 20
ret i1 %cmp
}

; Negative test: missing nsw flag
define i1 @icmp_add_constant_with_constant_ult_to_slt_neg2(i8 range(i8 -4, 120) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ult_to_slt_neg2(
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X:%.*]], 15
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[ADD]], 20
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add i8 %x, 15
%cmp = icmp ult i8 %add, 20
ret i1 %cmp
}

; Negative test: C2 is negative
define i1 @icmp_add_constant_with_constant_ult_to_slt_neg3(i32 range(i32 -4, 10) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ult_to_slt_neg3(
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[X:%.*]], 4
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], -6
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add nsw i32 %x, 4
%cmp = icmp ult i32 %add, -6
ret i1 %cmp
}

; Negative test: C2 - C1 is negative
define i1 @icmp_add_constant_with_constant_ult_to_slt_neg4(i32 range(i32 -4, 10) %x) {
; CHECK-LABEL: @icmp_add_constant_with_constant_ult_to_slt_neg4(
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[X:%.*]], 5
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 2
; CHECK-NEXT: ret i1 [[CMP]]
;
%add = add nsw i32 %x, 5
%cmp = icmp ult i32 %add, 2
ret i1 %cmp
}
2 changes: 1 addition & 1 deletion llvm/test/Transforms/LoopVectorize/interleaved-accesses.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ define void @PR27626_5(ptr %a, i32 %x, i32 %y, i32 %z, i64 %n) {
; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[SMAX]], -4
; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[TMP0]], 1
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i64 [[TMP1]], 1
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 6
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp slt i64 [[N]], 10
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
; CHECK: vector.ph:
; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[TMP2]], 9223372036854775804
Expand Down