[PATCH] D122013: [InstCombine] Fold abs of known negative operand when source is sub

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 04:04:47 PDT 2022


bcl5980 updated this revision to Diff 416886.
bcl5980 added a comment.

use update_test_checks.py to autogen test check golden


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122013/new/

https://reviews.llvm.org/D122013

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/abs-intrinsic.ll


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,30 @@
   %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(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_abs(
+; 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
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -791,6 +791,13 @@
   if (Known.isNegative())
     return true;
 
+  if (const OverflowingBinaryOperator *I =
+          dyn_cast<OverflowingBinaryOperator>(Op)) {
+    if (I->getOpcode() == Instruction::Sub && I->hasNoSignedWrap())
+      return isImpliedByDomCondition(ICmpInst::ICMP_SLT, I->getOperand(0),
+                                     I->getOperand(1), CxtI, DL);
+  }
+
   return isImpliedByDomCondition(
       ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122013.416886.patch
Type: text/x-patch
Size: 1892 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220321/68e8e808/attachment.bin>


More information about the llvm-commits mailing list