[llvm] [InstCombine] Fold Minimum over Trailing/Leading Bits Counts (PR #90402)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 21 07:13:23 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:
To make it work with `m_Intrinsic` and its template parameter, we would have to resort to constructions like
```cpp
if (match(I0, m_OneUse(IntrID == Intrinsic::cttz
? m_Intrinsic<Intrinsic::cttz>(m_Value(X), m_Value(Z))
: m_Intrinsic<Intrinsic::ctlz>(m_Value(X), m_Value(Z))))) {
```
Or am I missing some easier solution?
https://github.com/llvm/llvm-project/pull/90402
More information about the llvm-commits
mailing list