diff mbox series

Fix merging of 2 predictors (PR tree-optimization/86925).

Message ID be73e285-f871-b297-bd55-bc399efc8ed0@suse.cz
State New
Headers show
Series Fix merging of 2 predictors (PR tree-optimization/86925). | expand

Commit Message

Martin Liška Aug. 13, 2018, 12:58 p.m. UTC
Hi.

Following patch handles and issue seen in Linux kernel. It's about
__builtin_expects seen in a PHI node.

Another issue I saw is compilation with -frounding-math. In that case
we should use non-rounding math for folding of probability value in
__builtin_with_probability.

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.

Ready to be installed?
Martin

gcc/ChangeLog:

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

        PR tree-optimization/86925
	* predict.c (expr_expected_value_1): When taking
        later predictor, assign also probability.
        Use fold_build2_initializer_loc in order to fold
        the expression in -frounding-math.

gcc/testsuite/ChangeLog:

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

        PR tree-optimization/86925
	* gcc.dg/predict-20.c: New test.
	* gcc.dg/predict-21.c: New test.
---
 gcc/predict.c                     | 11 ++++++++---
 gcc/testsuite/gcc.dg/predict-20.c | 23 +++++++++++++++++++++++
 gcc/testsuite/gcc.dg/predict-21.c | 13 +++++++++++++
 3 files changed, 44 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/predict-20.c
 create mode 100644 gcc/testsuite/gcc.dg/predict-21.c

Comments

Jeff Law Aug. 15, 2018, 4:20 a.m. UTC | #1
On 08/13/2018 06:58 AM, Martin Liška wrote:
> Hi.
> 
> Following patch handles and issue seen in Linux kernel. It's about
> __builtin_expects seen in a PHI node.
> 
> Another issue I saw is compilation with -frounding-math. In that case
> we should use non-rounding math for folding of probability value in
> __builtin_with_probability.
> 
> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
> 
> Ready to be installed?
> Martin
> 
> gcc/ChangeLog:
> 
> 2018-08-13  Martin Liska  <mliska@suse.cz>
> 
>         PR tree-optimization/86925
> 	* predict.c (expr_expected_value_1): When taking
>         later predictor, assign also probability.
>         Use fold_build2_initializer_loc in order to fold
>         the expression in -frounding-math.
> 
> gcc/testsuite/ChangeLog:
> 
> 2018-08-13  Martin Liska  <mliska@suse.cz>
> 
>         PR tree-optimization/86925
> 	* gcc.dg/predict-20.c: New test.
> 	* gcc.dg/predict-21.c: New test.
OK.
jeff
diff mbox series

Patch

diff --git a/gcc/predict.c b/gcc/predict.c
index 3fbe3b704b3..8c8e79153fc 100644
--- a/gcc/predict.c
+++ b/gcc/predict.c
@@ -2332,13 +2332,17 @@  expr_expected_value_1 (tree type, tree op0, enum tree_code code,
 	      if (arg == PHI_RESULT (def))
 		continue;
 
+	      HOST_WIDE_INT probability2;
 	      new_val = expr_expected_value (arg, visited, &predictor2,
-					     probability);
+					     &probability2);
 
 	      /* It is difficult to combine value predictors.  Simply assume
 		 that later predictor is weaker and take its prediction.  */
 	      if (*predictor < predictor2)
-		*predictor = predictor2;
+		{
+		  *predictor = predictor2;
+		  *probability = probability2;
+		}
 	      if (!new_val)
 		return NULL;
 	      if (!val)
@@ -2423,7 +2427,8 @@  expr_expected_value_1 (tree type, tree op0, enum tree_code code,
 		  tree base = build_int_cst (integer_type_node,
 					     REG_BR_PROB_BASE);
 		  base = build_real_from_int_cst (t, base);
-		  tree r = fold_build2 (MULT_EXPR, t, prob, base);
+		  tree r = fold_build2_initializer_loc (UNKNOWN_LOCATION,
+							MULT_EXPR, t, prob, base);
 		  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/predict-20.c b/gcc/testsuite/gcc.dg/predict-20.c
new file mode 100644
index 00000000000..31d01835b80
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/predict-20.c
@@ -0,0 +1,23 @@ 
+/* PR tree-optimization/86925 */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-profile_estimate" } */
+
+int a, b;
+
+void
+c ()
+{
+  for (;;)
+    {
+      if (__builtin_expect (b < 0, 0))
+	break;
+      if (__builtin_expect (a, 1))
+	break;
+    }
+  int d = b < 0;
+  if (__builtin_expect (d, 0))
+    asm("");
+}
+
+/* { dg-final { scan-tree-dump-times "__builtin_expect heuristics of edge" 3 "profile_estimate"} } */
diff --git a/gcc/testsuite/gcc.dg/predict-21.c b/gcc/testsuite/gcc.dg/predict-21.c
new file mode 100644
index 00000000000..5949e5f4c19
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/predict-21.c
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-profile_estimate -frounding-math" } */
+
+extern int global;
+
+void foo (int base)
+{
+  for (int i = 0; __builtin_expect_with_probability (i < base, 1, 0.05f); i++)
+    global++;
+}
+
+/* { dg-final { scan-tree-dump "first match heuristics: 5.00%" "profile_estimate"} } */
+/* { dg-final { scan-tree-dump "__builtin_expect_with_probability heuristics of edge .*->.*: 5.00%" "profile_estimate"} } */