[InstCombine] Fold comparison of abs with int min

If the abs is poisoning, this is already folded to true/false.
For non-poisoning abs, we can convert this to a comparison with
the operand.
This commit is contained in:
Nikita Popov 2020-09-08 20:23:03 +02:00
parent d5f2743f9a
commit 8a9bdb31d1
2 changed files with 5 additions and 6 deletions

View File

@ -3090,9 +3090,10 @@ Instruction *InstCombinerImpl::foldICmpEqIntrinsicWithConstant(
switch (II->getIntrinsicID()) {
case Intrinsic::abs:
// abs(A) == 0 -> A == 0
if (C.isNullValue())
// abs(A) == INT_MIN -> A == INT_MIN
if (C.isNullValue() || C.isMinSignedValue())
return new ICmpInst(Cmp.getPredicate(), II->getArgOperand(0),
Constant::getNullValue(Ty));
ConstantInt::get(Ty, C));
break;
case Intrinsic::bswap:

View File

@ -275,8 +275,7 @@ define i1 @abs_ne_int_min_poison(i8 %x) {
define i1 @abs_eq_int_min_nopoison(i8 %x) {
; CHECK-LABEL: @abs_eq_int_min_nopoison(
; CHECK-NEXT: [[ABS:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 false)
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[ABS]], -128
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], -128
; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = call i8 @llvm.abs.i8(i8 %x, i1 false)
@ -286,8 +285,7 @@ define i1 @abs_eq_int_min_nopoison(i8 %x) {
define i1 @abs_ne_int_min_nopoison(i8 %x) {
; CHECK-LABEL: @abs_ne_int_min_nopoison(
; CHECK-NEXT: [[ABS:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 false)
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[ABS]], -128
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], -128
; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = call i8 @llvm.abs.i8(i8 %x, i1 false)