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

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 08:35:25 PDT 2024


================
@@ -1428,6 +1428,46 @@ 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 Instruction *
+foldMinimumOverTrailingOrLeadingZeroCount(Instruction *OrigInst, Value *I0,
+                                          Value *I1, const DataLayout &DL,
+                                          InstCombiner::BuilderTy &Builder) {
+  static_assert(IntrID == Intrinsic::cttz || IntrID == Intrinsic::ctlz,
----------------
mskamp wrote:

Of course, if it's okay to not use the matchers, let's do it this way. I've adapted the implementation accordingly.

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


More information about the llvm-commits mailing list