+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>
PR target/86807
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
}
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))
{
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;
}
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. */
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. */
+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>
Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
--- /dev/null
+/* { 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"} } */
--- /dev/null
+/* { 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"} } */