]> git.ipfire.org Git - thirdparty/gcc.git/commit
match.pd: Extend pointer alignment folds
authorRichard Sandiford <richard.sandiford@arm.com>
Thu, 13 Mar 2025 12:03:05 +0000 (12:03 +0000)
committerRichard Sandiford <richard.sandiford@arm.com>
Thu, 13 Mar 2025 12:03:05 +0000 (12:03 +0000)
commitfeb75e4643dca350b946b85ee25f4818ce6ce308
tree66a33cca4faf2969772c61979631fa7d99d518ba
parent7dae3f64c069b942b9abd768fd94af4b2cd0b781
match.pd: Extend pointer alignment folds

We have long had the fold:

/* Pattern match
     tem = (sizetype) ptr;
     tem = tem & algn;
     tem = -tem;
     ... = ptr p+ tem;
   and produce the simpler and easier to analyze with respect to alignment
     ... = ptr & ~algn;  */

But the gimple in gcc.target/aarch64/sve/pr98119.c has a variant in
which a constant is added before the conversion, giving:

     tem = (sizetype) (ptr p+ CST);
     tem = tem & algn;
     tem = -tem;
     ... = ptr p+ tem;

This case is also valid if algn fits within the trailing zero bits
of CST.  Adding CST then has no effect.

Similarly the testcase has:

     tem = (sizetype) (ptr p+ CST1);
     tem = tem & algn;
     tem = CST2 - tem;
     ... = ptr p+ tem;

This folds to:

     ... = (ptr & ~algn) p+ CST2;

if algn fits within the trailing zero bits of both CST1 and CST2.

An alternative would be:

     ... = (ptr p+ CST2) & ~algn;

but I would expect the alignment to be more easily shareable than
the CST2 addition, given that the CST2 addition wasn't being applied
by a POINTER_PLUS_EXPR.

gcc/
* match.pd: Extend pointer alignment folds so that they handle
the case where a constant is added before or after the alignment.

gcc/testsuite/
* gcc.dg/pointer-arith-11.c: New test.
* gcc.dg/pointer-arith-12.c: Likewise.
gcc/match.pd
gcc/testsuite/gcc.dg/pointer-arith-11.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pointer-arith-12.c [new file with mode: 0644]