Hi.

As Jakub pointed out we should not ICE when last argument
of __builtin_expect_with_probability is not a real cst.
Plus I documented the behavior.

Patch survives regression tests on x86_64-linux-gnu and
bootstraps works fine.

Ready for trunk?
Thanks,
Martin

gcc/ChangeLog:

2018-10-30  Martin Liska  <mli...@suse.cz>

        PR c/87811
        * predict.c (expr_expected_value_1): Verify
        that last argument is a real constants.

gcc/testsuite/ChangeLog:

2018-10-30  Martin Liska  <mli...@suse.cz>

        PR c/87811
        * gcc.dg/pr87811.c: New test.

gcc/ChangeLog:

2018-10-30  Martin Liska  <mli...@suse.cz>

        * doc/extend.texi: Update constrain about the last arguemnt
        of __builtin_expect_with_probability.
---
 gcc/doc/extend.texi            |  3 ++-
 gcc/predict.c                  |  2 ++
 gcc/testsuite/gcc.dg/pr87811.c | 13 +++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr87811.c


diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 4dbb2da39e4..0033bf12e62 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -12030,7 +12030,8 @@ when testing pointer or floating-point values.
 This function has the same semantics as @code{__builtin_expect},
 but the caller provides the expected probability that @var{exp} == @var{c}.
 The last argument, @var{probability}, is a floating-point value in the
-range 0.0 to 1.0, inclusive.
+range 0.0 to 1.0, inclusive.  The @var{probability} argument must be
+a compiler time constant.
 @end deftypefn
 
 @deftypefn {Built-in Function} void __builtin_trap (void)
diff --git a/gcc/predict.c b/gcc/predict.c
index ab2dc8ed031..bb116073e43 100644
--- a/gcc/predict.c
+++ b/gcc/predict.c
@@ -2467,6 +2467,8 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code,
 		  base = build_real_from_int_cst (t, base);
 		  tree r = fold_build2_initializer_loc (UNKNOWN_LOCATION,
 							MULT_EXPR, t, prob, base);
+		  if (TREE_CODE (r) != REAL_CST)
+		    return NULL;
 		  HOST_WIDE_INT probi
 		    = real_to_integer (TREE_REAL_CST_PTR (r));
 		  if (probi >= 0 && probi <= REG_BR_PROB_BASE)
diff --git a/gcc/testsuite/gcc.dg/pr87811.c b/gcc/testsuite/gcc.dg/pr87811.c
new file mode 100644
index 00000000000..f4ee21c4847
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr87811.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-profile_estimate -frounding-math" } */
+
+void bar (void);
+
+void
+foo (int i, double d)
+{
+  if (__builtin_expect_with_probability (i, 0, d))
+    bar ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_expect_with_probability heuristics of edge" "profile_estimate"} } */

Reply via email to