match.pd: Use ranges to optimize some x * y / y to x [PR97997]
authorJakub Jelinek <jakub@redhat.com>
Thu, 26 Nov 2020 15:24:07 +0000 (16:24 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 26 Nov 2020 15:24:07 +0000 (16:24 +0100)
commita3ebc13492ff238873f2c6a7a3e51abefec1d052
treea1b090730fe8015726b131268147dafe149d07b2
parentc76b3f9e83353a4cd437ca137c1fb835c9b5c21f
match.pd: Use ranges to optimize some x * y / y to x [PR97997]

For signed integers with undefined overflow we already optimize x * y / y
into x, but for signed integers with -fwrapv or unsigned integers we don't.
The following patch allows optimizing that into just x if value ranges
prove that x * y will never overflow.
It uses the global SSA_NAME_RANGE_INFO only, because like mentioned
in another PR we don't currently have a way to tell the ranger from match.pd
the use stmt (and we'd need in that case to tell ranger to only follow
SSA_NAME_DEF_STMTs + SSA_NAME_RANGE_INFO and never go in the other
direction, as following immediate uses seems forbidden in match.pd).
Another possibility would be to optimize this during vrp, but on the
other side the optimization itself is match.pd-ish.

2020-11-26  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/97997
* match.pd ((t * 2) / 2) -> t): Optimize even for defined
overflow if ranges prove there is no overflow.

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