[llvm] [InstCombine] Use m_Power2 to check for powers of 2 (PR #102500)

Rose Silicon via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 09:22:02 PDT 2024


https://github.com/RSilicon created https://github.com/llvm/llvm-project/pull/102500

None

>From d3dff5ade644df662e2eb0e1e1177b0d94e68588 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Thu, 8 Aug 2024 12:16:12 -0400
Subject: [PATCH] [InstCombine] Use m_Power2 to check for powers of 2

---
 llvm/lib/IR/Constants.cpp                                | 4 ++--
 llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp    | 4 ++--
 llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp  | 4 ++--
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index a1c9e925a024fe..aca26d9f53130a 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2636,7 +2636,7 @@ Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
 Constant *ConstantExpr::getExactLogBase2(Constant *C) {
   Type *Ty = C->getType();
   const APInt *IVal;
-  if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
+  if (match(C, m_Power2(IVal)))
     return ConstantInt::get(Ty, IVal->logBase2());
 
   // FIXME: We can extract pow of 2 of splat constant for scalable vectors.
@@ -2654,7 +2654,7 @@ Constant *ConstantExpr::getExactLogBase2(Constant *C) {
       Elts.push_back(Constant::getNullValue(Ty->getScalarType()));
       continue;
     }
-    if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
+    if (!match(Elt, m_Power2(IVal)))
       return nullptr;
     Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2()));
   }
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 3bd086230cbec5..a6a774b5aafcbb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1604,8 +1604,8 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
   }
 
   // (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit
-  if (match(&I, m_c_Add(m_And(m_Value(A), m_APInt(C1)), m_Deferred(A))) &&
-      C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countl_zero())) {
+  if (match(&I, m_c_Add(m_And(m_Value(A), m_Power2(C1)), m_Deferred(A))) &&
+      ComputeNumSignBits(A) > C1->countl_zero()) {
     Constant *NewMask = ConstantInt::get(RHS->getType(), *C1 - 1);
     return BinaryOperator::CreateAnd(A, NewMask);
   }
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 2db05c669145b9..a6ddc75e57481c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2425,12 +2425,12 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
       return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, *ShiftC));
 
     const APInt *AddC;
-    if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
+    if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Power2(AddC))))) {
       // If we are masking the result of the add down to exactly one bit and
       // the constant we are adding has no bits set below that bit, then the
       // add is flipping a single bit. Example:
       // (X + 4) & 4 --> (X & 4) ^ 4
-      if (Op0->hasOneUse() && C->isPowerOf2() && (*AddC & (*C - 1)) == 0) {
+      if ((*AddC & (*C - 1)) == 0) {
         assert((*C & *AddC) != 0 && "Expected common bit");
         Value *NewAnd = Builder.CreateAnd(X, Op1);
         return BinaryOperator::CreateXor(NewAnd, Op1);
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 0fb8b639c97b95..bacdf8a2a2a957 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3152,9 +3152,9 @@ static bool isAllocSiteRemovable(Instruction *AI,
           // alignment.
           const APInt *Alignment;
           const APInt *Size;
-          return match(CB->getArgOperand(0), m_APInt(Alignment)) &&
+          return match(CB->getArgOperand(0), m_Power2(Alignment)) &&
                  match(CB->getArgOperand(1), m_APInt(Size)) &&
-                 Alignment->isPowerOf2() && Size->urem(*Alignment).isZero();
+                 Size->urem(*Alignment).isZero();
         };
         auto *CB = dyn_cast<CallBase>(AI);
         LibFunc TheLibFunc;



More information about the llvm-commits mailing list