[llvm] [InstCombine] Fold Minimum over Trailing/Leading Bits Counts (PR #90402)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 2 11:28:00 PDT 2024


================
@@ -1428,6 +1428,44 @@ static Instruction *foldBitOrderCrossLogicOp(Value *V,
   return nullptr;
 }
 
+/// Fold an unsigned minimum of trailing or leading zero bits counts:
+///   umin(cttz(CtOp, ZeroUndef), ConstOp) --> cttz(CtOp | (1 << ConstOp))
+///   umin(ctlz(CtOp, ZeroUndef), ConstOp) --> ctlz(CtOp | ((1 << (bitwidth-1))
+///                                              >> ConstOp))
+template <Intrinsic::ID IntrID>
+static Value *
+foldMinimumOverTrailingOrLeadingZeroCount(Value *I0, Value *I1,
+                                          const DataLayout &DL,
+                                          InstCombiner::BuilderTy &Builder) {
+  static_assert(IntrID == Intrinsic::cttz || IntrID == Intrinsic::ctlz,
+                "This helper only supports cttz and ctlz intrinsics");
+
+  Value *CtOp;
+  Value *ZeroUndef;
+  if (!match(I0, m_OneUse(
+                     m_Intrinsic<IntrID>(m_Value(CtOp), m_Value(ZeroUndef))))) {
+    return nullptr;
+  }
+
+  auto BitWidth = I1->getType()->getScalarSizeInBits();
----------------
nikic wrote:

```suggestion
  unsigned BitWidth = I1->getType()->getScalarSizeInBits();
```

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


More information about the llvm-commits mailing list