https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113787
--- Comment #14 from rguenther at suse dot de <rguenther at suse dot de> --- On Tue, 13 Feb 2024, hubicka at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113787 > > --- Comment #13 from Jan Hubicka <hubicka at gcc dot gnu.org> --- > So my understanding is that ivopts does something like > > offset = &base2 - &base1 > > and then translate > val = base2[i] > to > val = *((base1+i)+offset) > > Where (base1+i) is then an iv variable. > > I wonder if we consider doing memory reference with base changed via offset a > valid transformation. Is there way to tell when this happens? IVOPTs does the above but it does it (or should) as offset = (uintptr)&base2 - (uintptr)&base1; val = *((T *)((uintptr)base1 + i + offset)) which is OK for points-to as no POINTER_PLUS_EXPR is involved so the resulting pointer points to both base1 and base2 (which isn't optimal but correct). If we somehow get back a POINTER_PLUS that's where things go wrong. Doing the above in C code would be valid input so we have to treat it correctly (OK, the standard only allows back-and-forth pointer-to-integer casts w/o any adjustment, but of course we relax this). IVOPTs then in putting all of the stuff into 'offset' gets at trying a TARGET_MEM_REF based on a NULL base but that's invalid. We then resort to a LEA (ADDR_EXPR of TARGET_MEM_REF) to compute the address which gets us into some phishy argument that it's not valid to decompose ADDR_EXPR of TARGET_MEM_REF to POINTER_PLUS of the TARGET_MEM_REF base and the offset. But that's how it is (points-to treats (address of) TARGET_MEM_REF as pointing to anything ...). > A quick fix would be to run IPA modref before ivopts, but I do not see how > such > transformation can work with rest of alias analysis (PTA etc) It does. Somewhere IPA modref interprets things wrongly, I didn't figure out here though.