Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -791,6 +791,10 @@ if (Known.isNegative()) return true; + Value *X, *Y; + if (match(Op, m_NSWSub(m_Value(X), m_Value(Y)))) + return isImpliedByDomCondition(ICmpInst::ICMP_SLT, X, Y, CxtI, DL); + return isImpliedByDomCondition( ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL); } Index: llvm/test/Transforms/InstCombine/abs-intrinsic.ll =================================================================== --- llvm/test/Transforms/InstCombine/abs-intrinsic.ll +++ llvm/test/Transforms/InstCombine/abs-intrinsic.ll @@ -424,3 +424,115 @@ %r = call i32 @llvm.abs.i32(i32 %s, i1 true) ret i32 %r } + +; Test from https://github.com/llvm/llvm-project/issues/54132. +define i32 @sub_abs_gt(i32 %x, i32 %y) { +; CHECK-LABEL: @sub_abs_gt( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] +; CHECK: cond.true: +; CHECK-NEXT: br label [[COND_END]] +; CHECK: cond.end: +; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[X]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[R]] +; +entry: + %cmp = icmp sgt i32 %x, %y + br i1 %cmp, label %cond.true, label %cond.end + +cond.true: + %sub = sub nsw i32 %x, %y + %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true) + %add = add nsw i32 %0, %y + br label %cond.end + +cond.end: + %r = phi i32 [ %add, %cond.true ], [ 0, %entry ] + ret i32 %r +} + +define i32 @sub_abs_lt(i32 %x, i32 %y) { +; CHECK-LABEL: @sub_abs_lt( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] +; CHECK: cond.true: +; CHECK-NEXT: br label [[COND_END]] +; CHECK: cond.end: +; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[Y]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[R]] +; +entry: + %cmp = icmp slt i32 %x, %y + br i1 %cmp, label %cond.true, label %cond.end + +cond.true: + %sub = sub nsw i32 %x, %y + %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true) + %add = add nsw i32 %0, %x + br label %cond.end + +cond.end: + %r = phi i32 [ %add, %cond.true ], [ 0, %entry ] + ret i32 %r +} + +define i32 @sub_abs_wrong_pred(i32 %x, i32 %y) { +; CHECK-LABEL: @sub_abs_wrong_pred( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] +; CHECK: cond.true: +; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]] +; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.abs.i32(i32 [[SUB]], i1 true) +; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], [[X]] +; CHECK-NEXT: br label [[COND_END]] +; CHECK: cond.end: +; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[ADD]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[R]] +; +entry: + %cmp = icmp ugt i32 %x, %y + br i1 %cmp, label %cond.true, label %cond.end + +cond.true: + %sub = sub nsw i32 %x, %y + %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true) + %add = add nsw i32 %0, %x + br label %cond.end + +cond.end: + %r = phi i32 [ %add, %cond.true ], [ 0, %entry ] + ret i32 %r +} + +define i32 @sub_abs_no_nsw(i32 %x, i32 %y) { +; CHECK-LABEL: @sub_abs_no_nsw( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]] +; CHECK: cond.true: +; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[X]], [[Y]] +; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.abs.i32(i32 [[SUB]], i1 true) +; CHECK-NEXT: [[ADD:%.*]] = add i32 [[TMP0]], [[Y]] +; CHECK-NEXT: br label [[COND_END]] +; CHECK: cond.end: +; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[ADD]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[R]] +; +entry: + %cmp = icmp sgt i32 %x, %y + br i1 %cmp, label %cond.true, label %cond.end + +cond.true: + %sub = sub i32 %x, %y + %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true) + %add = add i32 %0, %y + br label %cond.end + +cond.end: + %r = phi i32 [ %add, %cond.true ], [ 0, %entry ] + ret i32 %r +} +