]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Verify that last argument of __builtin_expect_with_probability is a real cst (PR...
authormarxin <marxin@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 5 Nov 2018 07:33:30 +0000 (07:33 +0000)
committermarxin <marxin@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 5 Nov 2018 07:33:30 +0000 (07:33 +0000)
2018-11-05  Martin Liska  <mliska@suse.cz>

PR c/87811
* predict.c (expr_expected_value_1): Verify
that last argument is a real constants and emit
error.
2018-11-05  Martin Liska  <mliska@suse.cz>

PR c/87811
* gcc.dg/pr87811.c: New test.
* gcc.dg/pr87811-2.c: Likewise.
* gcc.dg/pr87811-3.c: Likewise.
2018-11-05  Martin Liska  <mliska@suse.cz>

PR c/87811
* doc/extend.texi: Update constrain about the last argument
of __builtin_expect_with_probability.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@265785 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/doc/extend.texi
gcc/predict.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr87811-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr87811-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr87811.c [new file with mode: 0644]

index 3ef29c58cbbf7c8e0dd7d526e111eb73a8d0d7ba..b163e69da891583d5ea1af3c1f412739286fcd49 100644 (file)
@@ -1,3 +1,16 @@
+2018-11-05  Martin Liska  <mliska@suse.cz>
+
+       PR c/87811
+       * doc/extend.texi: Update constrain about the last argument
+       of __builtin_expect_with_probability.
+
+2018-11-05  Martin Liska  <mliska@suse.cz>
+
+       PR c/87811
+       * predict.c (expr_expected_value_1): Verify
+       that last argument is a real constants and emit
+       error.
+
 2018-11-05  Martin Liska  <mliska@suse.cz>
 
        PR gcov-profile/77698
index 924037ff586b4e2fd3e0bd55e567386d104d4d4c..7d1444461297f83936cbb98c3a461686767e126b 100644 (file)
@@ -12046,7 +12046,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)
index ab2dc8ed0316df9e126fdcac13913d21913f43bf..80a8d6846f78f945707fe1fb3b2d8ba6ae13b1c7 100644 (file)
@@ -2467,6 +2467,13 @@ 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)
+                   {
+                     error_at (gimple_location (def),
+                               "probability argument %qE must be a compile "
+                               "time constant", prob);
+                     return NULL;
+                   }
                  HOST_WIDE_INT probi
                    = real_to_integer (TREE_REAL_CST_PTR (r));
                  if (probi >= 0 && probi <= REG_BR_PROB_BASE)
@@ -2474,6 +2481,11 @@ expr_expected_value_1 (tree type, tree op0, enum tree_code code,
                      *predictor = PRED_BUILTIN_EXPECT_WITH_PROBABILITY;
                      *probability = probi;
                    }
+                 else
+                   error_at (gimple_location (def),
+                             "probability argument %qE must be a in the "
+                             "range 0.0 to 1.0", prob);
+
                  return gimple_call_arg (def, 1);
                }
 
index 8f5bb2abb1ccd122f21835f0a80d6ecae07eab43..a02831c11309e6187562ad3afd6828e79f144a59 100644 (file)
@@ -1,3 +1,10 @@
+2018-11-05  Martin Liska  <mliska@suse.cz>
+
+       PR c/87811
+       * gcc.dg/pr87811.c: New test.
+       * gcc.dg/pr87811-2.c: Likewise.
+       * gcc.dg/pr87811-3.c: Likewise.
+
 2018-11-05  Martin Liska  <mliska@suse.cz>
 
        PR gcov-profile/77698
diff --git a/gcc/testsuite/gcc.dg/pr87811-2.c b/gcc/testsuite/gcc.dg/pr87811-2.c
new file mode 100644 (file)
index 0000000..aa30ddf
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
+
+void bar (void);
+
+void
+foo (int i)
+{
+  if (__builtin_expect_with_probability (i, 0, 2.0f)) /* { dg-error "probability argument .* must be a in the range 0\\\.0 to 1\\\.0" } */
+    bar ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_expect_with_probability heuristics of edge" "profile_estimate"} } */
diff --git a/gcc/testsuite/gcc.dg/pr87811-3.c b/gcc/testsuite/gcc.dg/pr87811-3.c
new file mode 100644 (file)
index 0000000..720d154
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-profile_estimate" } */
+
+void bar (void);
+
+void
+foo (int i)
+{
+  if (__builtin_expect_with_probability (i, 0, 2.0f))
+    bar ();
+}
diff --git a/gcc/testsuite/gcc.dg/pr87811.c b/gcc/testsuite/gcc.dg/pr87811.c
new file mode 100644 (file)
index 0000000..9045c8e
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
+
+void bar (void);
+
+void
+foo (int i, double d)
+{
+  if (__builtin_expect_with_probability (i, 0, d)) /* { dg-error "probability argument .d. must be a compile time constant" } */
+    bar ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_expect_with_probability heuristics of edge" "profile_estimate"} } */