[llvm] [InstCombine] Simplify compare abs(X) and X. (PR #76385)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 29 04:41:24 PST 2023


================
@@ -7109,6 +7109,47 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
     }
   }
 
+  {
+    Value *X;
+    Constant *C;
+    if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X),
+                                                        m_Constant(C)))) &&
+        match(Op1, m_Specific(X))) {
+      auto *NullValue = Constant::getNullValue(X->getType());
+      auto *MinVal = ConstantInt::get(
+          X->getType(),
+          APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
+      bool IsIntMinPosion = C->isZeroValue();
+      switch (Pred) {
+      case CmpInst::ICMP_ULE:
+      case CmpInst::ICMP_SGE:
+        return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
+      case CmpInst::ICMP_UGT:
+      case CmpInst::ICMP_SLT:
+        return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
+      case CmpInst::ICMP_UGE:
+      case CmpInst::ICMP_SLE:
+      case CmpInst::ICMP_EQ: {
+        auto *SGE = Builder.CreateICmpSGE(X, NullValue);
+        return replaceInstUsesWith(
+            I, IsIntMinPosion
+                   ? Builder.CreateOr(SGE, Builder.CreateICmpEQ(X, MinVal))
+                   : SGE);
+      }
+      case CmpInst::ICMP_ULT:
+      case CmpInst::ICMP_NE:
+      case CmpInst::ICMP_SGT: {
+        auto *SLT = Builder.CreateICmpSLT(X, NullValue);
+        return replaceInstUsesWith(
+            I, IsIntMinPosion
+                   ? Builder.CreateAnd(SLT, Builder.CreateICmpNE(X, MinVal))
+                   : SLT);
+      }
+      default:
+        break;
----------------
dtcxzyw wrote:

```suggestion
        llvm_unreachable("Invalid predicate!");
```

https://github.com/llvm/llvm-project/pull/76385


More information about the llvm-commits mailing list