[llvm] 4892d3a - [InstCombine] Fold abs with dominating condition

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 5 07:19:32 PDT 2020


Author: Nikita Popov
Date: 2020-09-05T16:18:35+02:00
New Revision: 4892d3a1983b0fae83e9476d8cec1d139da7eae0

URL: https://github.com/llvm/llvm-project/commit/4892d3a1983b0fae83e9476d8cec1d139da7eae0
DIFF: https://github.com/llvm/llvm-project/commit/4892d3a1983b0fae83e9476d8cec1d139da7eae0.diff

LOG: [InstCombine] Fold abs with dominating condition

Similar to D87168, but for abs. If we have a dominating x >= 0
condition, then we know that abs(x) is x. This fold is in
InstCombine, because we need to create a sub instruction for
the x < 0 case.

Differential Revision: https://reviews.llvm.org/D87184

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 311a18c7f584..40f6e9e147d7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -779,6 +779,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     return nullptr;
   case Intrinsic::abs: {
     Value *IIOperand = II->getArgOperand(0);
+    bool IntMinIsPoison = cast<Constant>(II->getArgOperand(1))->isOneValue();
+
     // abs(-x) -> abs(x)
     // TODO: Copy nsw if it was present on the neg?
     Value *X;
@@ -789,6 +791,19 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
       return replaceOperand(*II, 0, X);
 
+    if (Optional<bool> Imp = isImpliedByDomCondition(
+            ICmpInst::ICMP_SGE, IIOperand,
+            Constant::getNullValue(IIOperand->getType()), II, DL)) {
+      // abs(x) -> x if x >= 0
+      if (*Imp)
+        return replaceInstUsesWith(*II, IIOperand);
+
+      // abs(x) -> -x if x < 0
+      if (IntMinIsPoison)
+        return BinaryOperator::CreateNSWNeg(IIOperand);
+      return BinaryOperator::CreateNeg(IIOperand);
+    }
+
     break;
   }
   case Intrinsic::bswap: {

diff  --git a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
index 7442ed7be113..c39424aa75ba 100644
--- a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -132,10 +132,9 @@ define i32 @abs_dom_cond_nopoison(i32 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1
 ; CHECK-NEXT:    br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
 ; CHECK:       true:
-; CHECK-NEXT:    [[A1:%.*]] = call i32 @llvm.abs.i32(i32 [[X]], i1 false)
-; CHECK-NEXT:    ret i32 [[A1]]
+; CHECK-NEXT:    ret i32 [[X]]
 ; CHECK:       false:
-; CHECK-NEXT:    [[A2:%.*]] = call i32 @llvm.abs.i32(i32 [[X]], i1 false)
+; CHECK-NEXT:    [[A2:%.*]] = sub i32 0, [[X]]
 ; CHECK-NEXT:    ret i32 [[A2]]
 ;
   %cmp = icmp sge i32 %x, 0
@@ -155,10 +154,9 @@ define i32 @abs_dom_cond_poison(i32 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1
 ; CHECK-NEXT:    br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
 ; CHECK:       true:
-; CHECK-NEXT:    [[A1:%.*]] = call i32 @llvm.abs.i32(i32 [[X]], i1 true)
-; CHECK-NEXT:    ret i32 [[A1]]
+; CHECK-NEXT:    ret i32 [[X]]
 ; CHECK:       false:
-; CHECK-NEXT:    [[A2:%.*]] = call i32 @llvm.abs.i32(i32 [[X]], i1 true)
+; CHECK-NEXT:    [[A2:%.*]] = sub nsw i32 0, [[X]]
 ; CHECK-NEXT:    ret i32 [[A2]]
 ;
   %cmp = icmp sge i32 %x, 0


        


More information about the llvm-commits mailing list