On Sun, 19 Feb 2023 at 01:01, Maciej W. Rozycki <ma...@orcam.me.uk> wrote: > > On Sat, 18 Feb 2023, Andrew Pinski via Gcc-patches wrote: > > > > > If we have division and remainder calculations with the same operands: > > > > > > > > a = b / c; > > > > d = b % c; > > > > > > > > We can replace the calculation of remainder with multiplication + > > > > subtraction, using the result from the previous division: > > > > > > > > a = b / c; > > > > d = a * c; > > > > d = b - d; > > > > > > > > Which will be faster. > > > > > > Do you have any benchmarks that show that performance increase? The ISA > > > manual specifically says the suggested sequence is div+mod, and while > > > those suggestions don't always pan out for real hardware it's likely > > > that at least some implementations will end up with the ISA-suggested > > > fusions. > > > > I suspect I will be needing this kind of patch for the core that I am > > going to be using. > > If anything this should be under a tuning option. > > Barring the fusion case, which indeed asks for a dedicated `divmod' > pattern (and then I suppose a post-reload splitter or a peephole so that > where one of the two results produced has eventually turned out unused, we > have means to discard the unneeded machine instruction), isn't the generic > transformation something for the middle end to do based on RTX costs? Hi, I am not sure if this answers your question, but we have target independent div+mod -> divmod transform on GIMPLE in widening_mul pass if target supports divmod insn. It is implemented in tree-ssa-math-opts.cc:convert_to_divmod.
Thanks, Prathamesh > > Maciej