[PATCH] D130862: [LegalizeTypes] Improve splitting for urem by constant for some constants.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 1 10:48:35 PDT 2022


craig.topper added a comment.

In D130862#3691418 <https://reviews.llvm.org/D130862#3691418>, @efriedma wrote:

> Do you have an overall plan written up somewhere?
>
> There are a few different ways you could extend this:
>
> - This could be extended to handle factors of two: for example, `a % 10` -> `((a / 2) % 5) * 2 + (a % 2)`.

Yeah. I was also going to look at that. gcc does something, but what they do can be improved.

> - This could be extended to handle more factors by slicing up numbers differently.  e.g. for `a % 7`, slice the number into three 30-bit pieces, since 2^30 mod 7 = 1.

That is what gcc does and what I was going to look at doing soon.

> - This could possibly be used to implement division: e.g. `a / 5` -> `(a - (a % 5)) * inverse(5)`.  Not sure if this is more efficient than other approaches; might depend on the target.

That's also what gcc does. This interacts poorly with DivRemPairs. If we have a both a division and remainder DivRemPairs rewrites the remainder in terms of division. If we're going to use remainder to do the division, then what DivRemPairs is doing is the wrong direction.

My immediate next step was looking at extending this patch to UDIV using this method for the same constant divisors.

> On the general topic of division by constants, see also discussion on https://github.com/llvm/llvm-project/issues/56153

I hadn't seen that bug, but I had talked to @ndesaulniers which is what motivated this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130862/new/

https://reviews.llvm.org/D130862



More information about the llvm-commits mailing list