]> git.ipfire.org Git - thirdparty/gcc.git/commit
Optimize '(X - N * M) / N' to 'X / N - M' if valid
authorJiufu Guo <guojiufu@linux.ibm.com>
Mon, 4 Sep 2023 02:31:04 +0000 (10:31 +0800)
committerguojiufu <guojiufu@linux.ibm.com>
Mon, 4 Sep 2023 02:36:08 +0000 (10:36 +0800)
commit1aceceb1e2d6e86ce183c8cc448750fa03b6f79e
treed4b631ada86aafbb229a6f55ddc8429f560723f5
parent828130772c67c94dce0757d70b4e96fb7c14ea6e
Optimize '(X - N * M) / N' to 'X / N - M' if valid

Integer expression "(X - N * M) / N" can be optimized to "X / N - M" with
the below conditions:
1. There is no wrap/overflow/underflow.
   wrap/overflow/underflow breaks the arithmetic operation.
2. "X - N * M" and "X" are not of opposite sign.
   Here, the operation "/" would be "trunc_div", the fractional part is
   discarded. If "X - N * M" and "X" are in different signs, then trunc_div
   discards the fractional parts (of /N) in different directions.

PR tree-optimization/108757

gcc/ChangeLog:

* match.pd ((X - N * M) / N): New pattern.
((X + N * M) / N): New pattern.
((X + C) div_rshift N): New pattern.

gcc/testsuite/ChangeLog:

* gcc.dg/pr108757-1.c: New test.
* gcc.dg/pr108757-2.c: New test.
* gcc.dg/pr108757.h: New test.
gcc/match.pd
gcc/testsuite/gcc.dg/pr108757-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr108757-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr108757.h [new file with mode: 0644]