[llvm] c85df3c - [InstCombine] refactor fold for icmp with trunc op; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 3 09:43:25 PDT 2021


Author: Sanjay Patel
Date: 2021-11-03T12:43:15-04:00
New Revision: c85df3c7d5ee91210d9d433b5a5b94ab1e5fd3be

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

LOG: [InstCombine] refactor fold for icmp with trunc op; NFC

There are at least 3 related folds we can add here - see D112634.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 6f73c4e3f423..c34330521f32 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4614,18 +4614,22 @@ static Instruction *foldICmpWithTrunc(ICmpInst &ICmp,
   // The trunc masks high bits while the compare may effectively mask low bits.
   Value *X;
   const APInt *C;
-  if (match(Op0, m_OneUse(m_Trunc(m_Value(X)))) && match(Op1, m_Power2(C))) {
-    if (Pred == ICmpInst::ICMP_ULT) {
-      // (trunc X) u< Pow2C --> (X & MaskC) == 0
-      unsigned SrcBits = X->getType()->getScalarSizeInBits();
-      unsigned DstBits = Op0->getType()->getScalarSizeInBits();
-      APInt MaskC = APInt::getOneBitSet(SrcBits, DstBits) - C->zext(SrcBits);
+  if (!match(Op0, m_OneUse(m_Trunc(m_Value(X)))) || !match(Op1, m_APInt(C)))
+    return nullptr;
+
+  unsigned SrcBits = X->getType()->getScalarSizeInBits();
+  if (Pred == ICmpInst::ICMP_ULT) {
+    if (C->isPowerOf2()) {
+      // If C is a power-of-2:
+      // (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
+      Constant *MaskC = ConstantInt::get(X->getType(), (-*C).zext(SrcBits));
       Value *And = Builder.CreateAnd(X, MaskC);
       Constant *Zero = ConstantInt::getNullValue(X->getType());
       return new ICmpInst(ICmpInst::ICMP_EQ, And, Zero);
     }
-    // TODO: Handle ugt.
+    // TODO: Handle C is negative-power-of-2.
   }
+  // TODO: Handle ugt.
 
   return nullptr;
 }


        


More information about the llvm-commits mailing list