[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 07:43:16 PDT 2025
================
@@ -969,6 +971,26 @@ 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_APInt(C1), m_Value(V1))) &&
+ (*C1 + 1).isPowerOf2()) {
----------------
dtcxzyw wrote:
```suggestion
if (DestWidth == 1 && match(Src, m_Shr(m_LowBitMask(C1), m_Value(V1)))) {
```
https://github.com/llvm/llvm-project/pull/157030
More information about the llvm-commits
mailing list