Commit 806562fd by Martin Liska Committed by Martin Liska

Add malloc predictor (PR middle-end/83023).

2018-08-07  Martin Liska  <mliska@suse.cz>

        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  <mliska@suse.cz>

        PR middle-end/83023
	* gcc.dg/predict-16.c: New test.
	* g++.dg/predict-1.C: New test.

From-SVN: r263355
parent ea2329d1
2018-08-07 Martin Liska <mliska@suse.cz>
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 <danglin@gcc.gnu.org> 2018-08-06 John David Anglin <danglin@gcc.gnu.org>
PR target/86807 PR target/86807
......
...@@ -2941,7 +2941,9 @@ that the pointer @var{P} returned by the function cannot alias any ...@@ -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 other pointer valid when the function returns, and moreover no
pointers to valid objects occur in any storage addressed by @var{P}. 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 @code{malloc} and @code{calloc} have this property because they return
a pointer to uninitialized or zeroed-out storage. However, functions a pointer to uninitialized or zeroed-out storage. However, functions
like @code{realloc} do not have this property, as they can return a like @code{realloc} do not have this property, as they can return a
......
...@@ -2380,6 +2380,14 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code, ...@@ -2380,6 +2380,14 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code,
} }
return NULL; 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) if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
switch (DECL_FUNCTION_CODE (decl)) switch (DECL_FUNCTION_CODE (decl))
{ {
...@@ -2414,6 +2422,10 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code, ...@@ -2414,6 +2422,10 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code,
if (predictor) if (predictor)
*predictor = PRED_COMPARE_AND_SWAP; *predictor = PRED_COMPARE_AND_SWAP;
return boolean_true_node; return boolean_true_node;
case BUILT_IN_REALLOC:
if (predictor)
*predictor = PRED_MALLOC_NONNULL;
return boolean_true_node;
default: default:
break; break;
} }
......
...@@ -51,6 +51,10 @@ DEF_PREDICTOR (PRED_NO_PREDICTION, "no prediction", PROB_ALWAYS, 0) ...@@ -51,6 +51,10 @@ DEF_PREDICTOR (PRED_NO_PREDICTION, "no prediction", PROB_ALWAYS, 0)
DEF_PREDICTOR (PRED_UNCONDITIONAL, "unconditional jump", PROB_ALWAYS, DEF_PREDICTOR (PRED_UNCONDITIONAL, "unconditional jump", PROB_ALWAYS,
PRED_FLAG_FIRST_MATCH) 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 /* Use number of loop iterations determined by # of iterations
analysis to set probability. We don't want to use Dempster-Shaffer analysis to set probability. We don't want to use Dempster-Shaffer
theory here, as the predictions is exact. */ theory here, as the predictions is exact. */
...@@ -169,7 +173,6 @@ DEF_PREDICTOR (PRED_HOT_LABEL, "hot label", HITRATE (85), 0) ...@@ -169,7 +173,6 @@ DEF_PREDICTOR (PRED_HOT_LABEL, "hot label", HITRATE (85), 0)
DEF_PREDICTOR (PRED_COLD_LABEL, "cold label", PROB_VERY_LIKELY, DEF_PREDICTOR (PRED_COLD_LABEL, "cold label", PROB_VERY_LIKELY,
PRED_FLAG_FIRST_MATCH) PRED_FLAG_FIRST_MATCH)
/* The following predictors are used in Fortran. */ /* The following predictors are used in Fortran. */
/* Branch leading to an integer overflow are extremely unlikely. */ /* Branch leading to an integer overflow are extremely unlikely. */
......
2018-08-07 Martin Liska <mliska@suse.cz>
PR middle-end/83023
* gcc.dg/predict-16.c: New test.
* g++.dg/predict-1.C: New test.
2018-08-07 Steve Ellcey <sellcey@cavium.com> 2018-08-07 Steve Ellcey <sellcey@cavium.com>
Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE> Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
......
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
#include <new>
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"} } */
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
#include <stdlib.h>
#include <string.h>
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"} } */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment