[llvm] Fold (a % b) lt/ge (b-1) where b is a power of 2 (PR #72504)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 11:19:43 PST 2023


dtcxzyw wrote:

> @dtcxzyw Does llvm.assume require special handling?

You need to check the sign of `C`. `@llvm.assume` is used to add preconditions for alive2.

> 
> ```
> {
>     Value *X;
>     const APInt *C;
>     ICmpInst::Predicate Pred = I.getPredicate();
>     if ((Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_SLT) &&
>         match(Op0, m_OneUse(m_SRem(m_Value(X), m_APInt(C)))) &&
>         match(Op1, m_OneUse(m_Add(m_SpecificInt(*C), m_AllOnes()))) &&
>         !(C->isNegative())) {
>       // icmp slt (X % C), (C - 1) --> icmp ne (X % C), (C - 1)
>       auto *NewCmp = Builder.CreateICmpNE(Op0, Op1);
> ```

nit: Don't create instructions that may be unused.

> ```
>       // icmp sge (X % C), (C - 1) --> icmp eq (X % C), (C - 1)
>       if (Pred == ICmpInst::ICMP_SGE)
>         NewCmp = Builder.CreateICmpEQ(Op0, Op1);
>       return replaceInstUsesWith(I, NewCmp);
>     }
>   }
> ```


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


More information about the llvm-commits mailing list