ipa-sra: Treat REFERENCE_TYPES as always dereferencable
authorMartin Jambor <mjambor@suse.cz>
Tue, 13 Dec 2022 23:33:06 +0000 (00:33 +0100)
committerMartin Jambor <mjambor@suse.cz>
Tue, 13 Dec 2022 23:58:10 +0000 (00:58 +0100)
commit10478270fe0c39c59eb0f35d19356a63bdf3a2ad
tree10fd7181654aecbb9ea1a3b00ee566f3986ec364
parent095a13eda2caf6842096a3ab78b2081c50fe8799
ipa-sra: Treat REFERENCE_TYPES as always dereferencable

C++ and especially Fortran pass data by references which are not
pointers potentially pointing anywhere and so can be assumed to be
safely dereferencable.  This patch teaches IPA-SRA to treat them as
such and avoid the dance we do to prove that we can move loads from
them to the caller.

When we do not know that a dereference will happen all the time, we
need a heuristics so that we do not force memory accesses that normally
happen only rarely.  The patch simply uses the (possibly guessed)
profile and checks whether the (expected) number of loads is at least
half of function invocations invocations - the half is now
configurable with a param as requested by Honza.

gcc/ChangeLog:

2022-12-13  Martin Jambor  <mjambor@suse.cz>

PR ipa/103585
* params.opt (ipa-sra-deref-prob-threshold): New parameter.
* doc/invoke.texi (ipa-sra-deref-prob-threshold): Document it.
* ipa-sra.cc (struct gensum_param_access): New field load_count.
(struct gensum_param_desc): New field safe_ref, adjusted comments.
(by_ref_count): Renamed to unsafe_by_ref_count, adjusted all uses.
(dump_gensum_access): Dump the new field.
(dump_gensum_param_descriptor): Likewise.
(create_parameter_descriptors): Set safe_ref field, move setting
by_ref forward.  Only increment unsafe_by_ref_count for unsafe
by_ref parameters.
(allocate_access): Initialize new field.
(mark_param_dereference): Adjust indentation.  Only add data to
bb_dereferences for unsafe by_ref parameters.
(scan_expr_access): For loads, accumulate BB counts.
(dereference_probable_p): New function.
(check_gensum_access): Fix leading comment, add parameter FUN.
Check cumulative counts of loads for safe by_ref accesses instead
of dereferences.
(process_scan_results): Do not propagate dereference distances for
safe by_ref parameters.  Pass fun to check_gensum_access.  Safe
by_ref params do not need the postdominance check.

gcc/testsuite/ChangeLog:

2022-11-11  Martin Jambor  <mjambor@suse.cz>

* g++.dg/ipa/ipa-sra-5.C: New test
gcc/doc/invoke.texi
gcc/ipa-sra.cc
gcc/params.opt
gcc/testsuite/g++.dg/ipa/ipa-sra-5.C [new file with mode: 0644]