From: Sam James <sam@gentoo.org>
To: Jan Hubicka <hubicka@ucw.cz>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: Propagate value ranges of return values
Date: Sun, 19 Nov 2023 11:30:34 +0000 [thread overview]
Message-ID: <871qcmkmgd.fsf@gentoo.org> (raw)
In-Reply-To: <ZVljEevbGcssSuh8@kam.mff.cuni.cz>
Jan Hubicka <hubicka@ucw.cz> writes:
> Hi,
> this patch implements very basic propaation of return value ranges from VRP
> pass. This helps std::vector's push_back since we work out value range of
> allocated block. This propagates only within single translation unit. I hoped
> we will also do the propagation at WPA stage, but that needs more work on
> ipa-cp side.
>
> I also added code auto-detecting return_nonnull and corresponding -Wsuggest-attribute
>
> Variant of this patch bootstrapped/regtested x86_64-linux, testing with
> this version is running. I plan to commit the patch at Monday provided
> there are no issues.
>
> gcc/ChangeLog:
>
> * cgraph.cc (add_detected_attribute_1): New function.
> (cgraph_node::add_detected_attribute): New member function.
> * cgraph.h (struct cgraph_node): Declare it.
> * common.opt: Add Wsuggest-attribute=returns_nonnull.
> * doc/invoke.texi: Document +Wsuggest-attribute=returns_nonnull.
> * gimple-range-fold.cc: Include ipa-prop and dependencies.
> (fold_using_range::range_of_call): Look for return value range.
> * ipa-prop.cc (struct ipa_return_value_summary): New structure.
> (class ipa_return_value_sum_t): New summary.
> (ipa_record_return_value_range): New function.
> (ipa_return_value_range): New function.
> * ipa-prop.h (ipa_return_value_range): Declare.
> (ipa_record_return_value_range): Declare.
> * ipa-pure-const.cc (warn_function_returns_nonnull): New function.
> * ipa-utils.h (warn_function_returns_nonnull): Declare.
> * symbol-summary.h: Fix comment typo.
> * tree-vrp.cc (execute_ranger_vrp): Record return values.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/return-value-range-1.c: New test.
>
> diff --git a/gcc/cgraph.cc b/gcc/cgraph.cc
> index e41e5ad3ae7..71dacf23ce1 100644
> --- a/gcc/cgraph.cc
> +++ b/gcc/cgraph.cc
> @@ -2629,6 +2629,54 @@ cgraph_node::set_malloc_flag (bool malloc_p)
> return changed;
> }
>
> +/* Worker to set malloc flag. */
> +static void
> +add_detected_attribute_1 (cgraph_node *node, const char *attr, bool *changed)
> +{
> + if (!lookup_attribute (attr, DECL_ATTRIBUTES (node->decl)))
> + {
> + DECL_ATTRIBUTES (node->decl) = tree_cons (get_identifier (attr),
> + NULL_TREE, DECL_ATTRIBUTES (node->decl));
> + *changed = true;
> + }
> +
> + ipa_ref *ref;
> + FOR_EACH_ALIAS (node, ref)
> + {
> + cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
> + if (alias->get_availability () > AVAIL_INTERPOSABLE)
> + add_detected_attribute_1 (alias, attr, changed);
> + }
> +
> + for (cgraph_edge *e = node->callers; e; e = e->next_caller)
> + if (e->caller->thunk
> + && (e->caller->get_availability () > AVAIL_INTERPOSABLE))
> + add_detected_attribute_1 (e->caller, attr, changed);
> +}
> +
> +/* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
> +
> +bool
> +cgraph_node::add_detected_attribute (const char *attr)
> +{
> + bool changed = false;
> +
> + if (get_availability () > AVAIL_INTERPOSABLE)
> + add_detected_attribute_1 (this, attr, &changed);
> + else
> + {
> + ipa_ref *ref;
> +
> + FOR_EACH_ALIAS (this, ref)
> + {
> + cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
> + if (alias->get_availability () > AVAIL_INTERPOSABLE)
> + add_detected_attribute_1 (alias, attr, &changed);
> + }
> + }
> + return changed;
> +}
> +
> /* Worker to set noreturng flag. */
> static void
> set_noreturn_flag_1 (cgraph_node *node, bool noreturn_p, bool *changed)
> diff --git a/gcc/cgraph.h b/gcc/cgraph.h
> index cedaaac3a45..cfdd9f693a8 100644
> --- a/gcc/cgraph.h
> +++ b/gcc/cgraph.h
> @@ -1190,6 +1190,10 @@ struct GTY((tag ("SYMTAB_FUNCTION"))) cgraph_node : public symtab_node
>
> bool set_pure_flag (bool pure, bool looping);
>
> + /* Add attribute ATTR to cgraph_node's decl and on aliases of the node
> + if any. */
> + bool add_detected_attribute (const char *attr);
> +
> /* Call callback on function and aliases associated to the function.
> When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
> skipped. */
> diff --git a/gcc/common.opt b/gcc/common.opt
> index d21db5d4a20..0be4f02677c 100644
> --- a/gcc/common.opt
> +++ b/gcc/common.opt
> @@ -781,6 +781,10 @@ Wsuggest-attribute=malloc
> Common Var(warn_suggest_attribute_malloc) Warning
> Warn about functions which might be candidates for __attribute__((malloc)).
>
> +Wsuggest-attribute=returns_nonnull
- or _?
(If changing it, needs adjustment in rest of patch too.)
> +Common Var(warn_suggest_attribute_malloc) Warning
> +Warn about functions which might be candidates for __attribute__((malloc)).
> +
Typo: s/malloc/nonnull/?
next prev parent reply other threads:[~2023-11-19 11:32 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-19 1:21 Jan Hubicka
2023-11-19 11:30 ` Sam James [this message]
2023-11-19 14:31 ` Jan Hubicka
2023-11-19 15:05 ` Jan Hubicka
2023-11-20 16:28 ` Martin Jambor
2023-11-21 11:09 ` Fix 'gcc.dg/tree-ssa/return-value-range-1.c' (was: Propagate value ranges of return values) Thomas Schwinge
2023-11-21 12:18 ` Jan Hubicka
2023-11-21 13:58 ` Propagate value ranges of return values Christophe Lyon
2023-11-21 14:06 ` Jan Hubicka
2023-11-21 21:35 ` Thomas Schwinge
2023-11-23 7:24 ` Andrew Pinski
2023-11-21 21:24 ` Fix 'gcc.dg/tree-ssa/return-value-range-1.c' for 'char' defaulting to 'unsigned' (was: Propagate value ranges of return values) Thomas Schwinge
2023-11-22 10:51 ` Christophe Lyon
2023-11-22 11:05 ` Thomas Schwinge
2023-11-22 17:16 ` Adjust 'libgomp.c/declare-variant-{3,4}-[...]' for inter-procedural value range propagation " Thomas Schwinge
2023-11-20 14:50 ` Propagate value ranges of return values Andrew MacLeod
2023-11-20 15:34 ` Jan Hubicka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871qcmkmgd.fsf@gentoo.org \
--to=sam@gentoo.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=hubicka@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).