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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 1 02:36:02 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))
+static Value *
+foldMinimumOverTrailingOrLeadingZeroCount(Intrinsic::ID IntrID, Value *I0,
+                                          Value *I1, const DataLayout &DL,
+                                          InstCombiner::BuilderTy &Builder) {
+  assert((IntrID == Intrinsic::cttz || IntrID == Intrinsic::ctlz) &&
+         "This helper only supports cttz and ctlz intrinsics");
+
+  if (I0->hasOneUse()) {
+    if (auto *II0 = dyn_cast<IntrinsicInst>(I0);
+        II0 && II0->getIntrinsicID() == IntrID) {
----------------
nikic wrote:

Make this `match(I0, m_OneUse(m_Intrinsic<IntrID>(m_Value(X), m_Value(Z)))`? Need to change the IntrID to a template param for that.

Also early return please.

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


More information about the llvm-commits mailing list