]> git.ipfire.org Git - thirdparty/gcc.git/commit
optimize std::max early
authorJan Hubicka <jh@suse.cz>
Mon, 19 Jun 2023 16:28:17 +0000 (18:28 +0200)
committerJan Hubicka <jh@suse.cz>
Mon, 19 Jun 2023 16:28:17 +0000 (18:28 +0200)
commit7b34cacc5735385e7e2855d7c0a6fad60ef4a99b
tree54e852243c0f626bc179ddf7ab9de4d8a0a69570
parentc72a7b849853716d94e8d313be5dce3c22850113
optimize std::max early

we currently produce very bad code on loops using std::vector as a stack, since
we fail to inline push_back which in turn prevents SRA and we fail to optimize
out some store-to-load pairs.

I looked into why this function is not inlined and it is inlined by clang.  We
currently estimate it to 66 instructions and inline limits are 15 at -O2 and 30
at -O3.  Clang has similar estimate, but still decides to inline at -O2.

I looked into reason why the body is so large and one problem I spotted is the
way std::max is implemented by taking and returning reference to the values.

  const T& max( const T& a, const T& b );

This makes it necessary to store the values to memory and load them later
and max is used by code computing new size of vector on resize.

We optimize this to MAX_EXPR, but only during late optimizations.  I think this
is a common enough coding pattern and we ought to make this transparent to
early opts and IPA.  The following is easist fix that simply adds phiprop pass
that turns the PHI of address values into PHI of values so later FRE can
propagate values across memory, phiopt discover the MAX_EXPR pattern and DSE
remove the memory stores.

gcc/ChangeLog:

PR tree-optimization/109811
PR tree-optimization/109849
* passes.def: Add phiprop to early optimization passes.
* tree-ssa-phiprop.cc: Allow clonning.

gcc/testsuite/ChangeLog:

PR tree-optimization/109811
PR tree-optimization/109849
* gcc.dg/tree-ssa/phiprop-1.c: New test.
* gcc.dg/tree-ssa/pr21463.c: Adjust template.
gcc/passes.def
gcc/testsuite/gcc.dg/tree-ssa/phiprop-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr21463.c
gcc/tree-ssa-phiprop.cc