[llvm] [InstCombine] Added optimisation for trunc (Pow2 >> x) to i1 (PR #157030)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 10 08:19:24 PDT 2025
================
@@ -969,6 +971,25 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
Changed = true;
}
+ const APInt *C1;
+ Value *V1;
+ // OP = { lshr, ashr }
+ // trunc ( OP i8 C1, V1) to i1 -> icmp eq V1, log_2(C1) iff C1 is power of 2
+ if (DestWidth == 1 && match(Src, m_Shr(m_Power2(C1), m_Value(V1)))) {
+ Value *Right = ConstantInt::get(V1->getType(), C1->countr_zero());
+ Value *Icmp = Builder.CreateICmpEQ(V1, Right);
+ return replaceInstUsesWith(Trunc, Icmp);
+ }
+
+ // OP = { lshr, ashr }
+ // trunc ( OP i8 C1, V1) to i1 -> icmp ult V1, log_2(C1 + 1) iff (C1 + 1) is
+ // power of 2
+ if (DestWidth == 1 && match(Src, m_Shr(m_LowBitMask(C1), m_Value(V1)))) {
+ Value *Right = ConstantInt::get(V1->getType(), (*C1 + 1).countr_zero());
----------------
dtcxzyw wrote:
```suggestion
Value *Right = ConstantInt::get(V1->getType(), C1->countr_one());
```
https://github.com/llvm/llvm-project/pull/157030
More information about the llvm-commits
mailing list