On Thu, Jul 03, 2025 at 08:12:22PM +0200, Richard Biener wrote: > > So, instead at least for now, the following patch keeps doing the > > optimization, just doesn't perform it in pointer arithmetics. > > pointer_int_sum itself actually adds the multiplication by size_exp, > > so ptr + expr is turned into ptr p+ expr * size_exp, > > so this patch will try to optimize > > ptr + (expr +- cst) > > into > > ptr p+ ((sizetype)expr * size_exp +- (sizetype)cst * size_exp) > > and > > ptr - (expr +- cst) > > into > > ptr p+ -((sizetype)expr * size_exp +- (sizetype)cst * size_exp) > > So the important part is the distribution of the multiplication, not the > (invalid) association?
For the regressions yes. Consider even trivial cases like int a[1024]; int * foo (int x) { return a + (x - 1); } Vanilla trunk as well as trunk with this patch compile this on x86_64 -O2 to movslq %edi, %rdi leaq a-4(,%rdi,4), %rax ret With just the c-common.cc opt removed, I get subl $1, %edi movslq %edi, %rdi leaq a(,%rdi,4), %rax ret instead. I admit I haven't checked other targets, just ia32 and there is no change whatsoever (expectedly, sizetype is 32-bit, so RTL opts can fold it into leal just fine. But my hope is that this patch keeps similar code like before more often, even when clearly it will not be always the case. E.g. for the (x ? a : b)[y - 1] case when a and b are arrays, we used to do (x ? &a - 4 : &b - 4) p+ y * 4 so the subtraction done on each of the branches, now it will be just added later on. But at least on sizetype and not on the narrower type. Jakub