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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 07:18:29 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,
----------------
dtcxzyw wrote:

```
if (I0->hasOneUse()) {
  if (auto *II = dyn_cast<IntrinsicInst>(I0); II && II->getIntrinsicID() == IntrID) {
     X = II->getArgOperand(0);
     Z = II->getArgOperand(1);
  }
}
```


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


More information about the llvm-commits mailing list