From 806562fd6269a3d7e0651d5bbf9085c7708fb5cd Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 7 Aug 2018 13:59:13 +0200 Subject: [PATCH] Add malloc predictor (PR middle-end/83023). 2018-08-07 Martin Liska PR middle-end/83023 * predict.c (expr_expected_value_1): Handle DECL_IS_MALLOC, BUILT_IN_REALLOC and DECL_IS_OPERATOR_NEW. * predict.def (PRED_MALLOC_NONNULL): New predictor. * doc/extend.texi: Document that malloc attribute adds hit to compiler. 2018-08-07 Martin Liska PR middle-end/83023 * gcc.dg/predict-16.c: New test. * g++.dg/predict-1.C: New test. From-SVN: r263355 --- gcc/ChangeLog | 9 ++++++++ gcc/doc/extend.texi | 4 +++- gcc/predict.c | 12 +++++++++++ gcc/predict.def | 5 ++++- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/predict-1.C | 15 +++++++++++++ gcc/testsuite/gcc.dg/predict-16.c | 36 +++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/predict-1.C create mode 100644 gcc/testsuite/gcc.dg/predict-16.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5121968cda2..8bbcceacdd9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2018-08-07 Martin Liska + + PR middle-end/83023 + * predict.c (expr_expected_value_1): Handle DECL_IS_MALLOC, + BUILT_IN_REALLOC and DECL_IS_OPERATOR_NEW. + * predict.def (PRED_MALLOC_NONNULL): New predictor. + * doc/extend.texi: Document that malloc attribute adds + hit to compiler. + 2018-08-06 John David Anglin PR target/86807 diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index bf465d7b93c..183ff1c6348 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -2941,7 +2941,9 @@ that the pointer @var{P} returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by @var{P}. -Using this attribute can improve optimization. Functions like +Using this attribute can improve optimization. Compiler predicts +that a function with the attribute returns non-null in most cases. +Functions like @code{malloc} and @code{calloc} have this property because they return a pointer to uninitialized or zeroed-out storage. However, functions like @code{realloc} do not have this property, as they can return a diff --git a/gcc/predict.c b/gcc/predict.c index a6769eda1c7..96ae10f1319 100644 --- a/gcc/predict.c +++ b/gcc/predict.c @@ -2380,6 +2380,14 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code, } return NULL; } + + if (DECL_IS_MALLOC (decl) || DECL_IS_OPERATOR_NEW (decl)) + { + if (predictor) + *predictor = PRED_MALLOC_NONNULL; + return boolean_true_node; + } + if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) switch (DECL_FUNCTION_CODE (decl)) { @@ -2414,6 +2422,10 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code, if (predictor) *predictor = PRED_COMPARE_AND_SWAP; return boolean_true_node; + case BUILT_IN_REALLOC: + if (predictor) + *predictor = PRED_MALLOC_NONNULL; + return boolean_true_node; default: break; } diff --git a/gcc/predict.def b/gcc/predict.def index 4ed97ed165c..76e6590cc96 100644 --- a/gcc/predict.def +++ b/gcc/predict.def @@ -51,6 +51,10 @@ DEF_PREDICTOR (PRED_NO_PREDICTION, "no prediction", PROB_ALWAYS, 0) DEF_PREDICTOR (PRED_UNCONDITIONAL, "unconditional jump", PROB_ALWAYS, PRED_FLAG_FIRST_MATCH) +/* Return value of malloc function is almost always non-null. */ +DEF_PREDICTOR (PRED_MALLOC_NONNULL, "malloc returned non-NULL", \ + PROB_VERY_LIKELY, PRED_FLAG_FIRST_MATCH) + /* Use number of loop iterations determined by # of iterations analysis to set probability. We don't want to use Dempster-Shaffer theory here, as the predictions is exact. */ @@ -169,7 +173,6 @@ DEF_PREDICTOR (PRED_HOT_LABEL, "hot label", HITRATE (85), 0) DEF_PREDICTOR (PRED_COLD_LABEL, "cold label", PROB_VERY_LIKELY, PRED_FLAG_FIRST_MATCH) - /* The following predictors are used in Fortran. */ /* Branch leading to an integer overflow are extremely unlikely. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c556cb1b5c9..868f4215ef0 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-08-07 Martin Liska + + PR middle-end/83023 + * gcc.dg/predict-16.c: New test. + * g++.dg/predict-1.C: New test. + 2018-08-07 Steve Ellcey Rainer Orth diff --git a/gcc/testsuite/g++.dg/predict-1.C b/gcc/testsuite/g++.dg/predict-1.C new file mode 100644 index 00000000000..8e2032f33a4 --- /dev/null +++ b/gcc/testsuite/g++.dg/predict-1.C @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-profile_estimate" } */ + +#include + +int *r; + +void test() +{ + r = new(std::nothrow) int; + if (r) + __builtin_memset (r, 0, sizeof(int)); +} + +/* { dg-final { scan-tree-dump "malloc returned non-NULL heuristics of edge\[^:\]*: 99.96%" "profile_estimate"} } */ diff --git a/gcc/testsuite/gcc.dg/predict-16.c b/gcc/testsuite/gcc.dg/predict-16.c new file mode 100644 index 00000000000..e1f331b909a --- /dev/null +++ b/gcc/testsuite/gcc.dg/predict-16.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-profile_estimate" } */ + +#include +#include + +void *r; +void *r2; +void *r3; +void *r4; +void *r5; + +void *m (size_t s, int c) +{ + r = malloc (s); + if (r) + memset (r, 0, s); + + r2 = calloc (s, 0); + if (r2) + memset (r2, 0, s); + + r3 = __builtin_malloc (s); + if (r3) + memset (r3, 0, s); + + r4 = __builtin_calloc (s, 0); + if (r4) + memset (r4, 0, s); + + r5 = __builtin_realloc (r4, s); + if (r5) + memset (r4, 0, s); +} + +/* { dg-final { scan-tree-dump-times "malloc returned non-NULL heuristics of edge\[^:\]*: 99.96%" 5 "profile_estimate"} } */ -- 2.30.2