diff mbox

Print 2 digits after decimal delimiter for BB frequencies

Message ID 0ca80770-780b-cd7b-fa7f-e7f65ec9a7e1@suse.cz
State Accepted
Commit e19ef86d01cee1e094ab518951862ddf77ccb03d
Headers show

Commit Message

Martin Liška Dec. 8, 2016, 5:26 p.m. UTC
On 12/08/2016 05:39 PM, Martin Sebor wrote:
> On 12/08/2016 05:55 AM, Martin Liška wrote:

>> With the patch applied, one can distinguish between PROB_VERY_UNLIKELY and

>> real zero probability:

> 

> I tried to see if formatting the expression

> 

>   e->probability * 100.0 / REG_BR_PROB_BASE

> 

> with "%.2f" is guaranteed to output non-zero when e->probability

> is non-zero.  If I got the values right then the smallest non-zero

> e->probability can be as low as 1, and REG_BR_PROB_BASE is 10000.

> That evaluates to 0.0099999999999999985 which Glibc formats as

> 0.01 because (AFAIK) its printf rounds to nearest.


Thank you for playing with that.

> 

> If this is guaranteed then I think it's fine.  Otherwise, if there

> is a chance that the printed result could be 0.00% for a non-zero

> probability it might be worth to detect it and make sure it's at

> least 0.01% to avoid the same confusion I had with the 0.00%.


Yep, I like that assumption.

> 

> FWIW, if GCC uses integers rather than floats internally to make

> decisions (I don't know) then also printing integers would give

> the most accurate results.  E.g., something like

>   printf ("%2u.%u%%", e->probability / 100, e->probability % 100)


That's bit tricky as you need "%2u.%02u%%", where '02u' depends on result of 
REG_BR_PROB_BASE / 100. We plan with Honza to utilize probably sreal type
instead of the scaled integers. So that I prepared a new patch (not tested yet).

Ideas?
Thanks,
Martin


> 

> Thanks

> Martin

> 

>>

>> f ()

>> {

>>   int _1;

>>

>>   <bb 2> [100.00%]:

>>   _1 = __builtin_sprintf (&d2, "%i", 12);

>>   if (_1 != 2)

>>     goto <bb 3>; [0.04%]

>>   else

>>     goto <bb 4>; [99.96%]

>>

>>   <bb 3> [0.04%]:

>>   __builtin_abort ();

>>

>>   <bb 4> [99.96%]:

>>   return;

>>

>> }

>>

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

>>

>> Ready to be installed?

>> Martin

>>

>
diff mbox

Patch

From afbc46f8ebcdf00d320a066b692537399cd189c0 Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Thu, 1 Dec 2016 10:55:55 +0100
Subject: [PATCH] Print 2 digits after decimal delimiter for BB frequencies

gcc/ChangeLog:

2016-12-01  Martin Liska  <mliska@suse.cz>

	* gimple-pretty-print.c (dump_probability): New function.
	(dump_edge_probability): Use the function.
	(dump_gimple_label): Likewise.
	(dump_gimple_bb_header): Likewise.

gcc/testsuite/ChangeLog:

2016-12-02  Martin Liska  <mliska@suse.cz>

	* gcc.dg/tree-ssa/20040703-1.c: Update scanned pattern.
	* gcc.dg/tree-ssa/dump-2.c: Likewise.
---
 gcc/gimple-pretty-print.c                  | 30 ++++++++++++++++++++++++------
 gcc/testsuite/gcc.dg/tree-ssa/20040703-1.c |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/dump-2.c     |  2 +-
 3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/gcc/gimple-pretty-print.c b/gcc/gimple-pretty-print.c
index b5e866d..27a22c4 100644
--- a/gcc/gimple-pretty-print.c
+++ b/gcc/gimple-pretty-print.c
@@ -72,13 +72,32 @@  debug_gimple_stmt (gimple *gs)
   print_gimple_stmt (stderr, gs, 0, TDF_VOPS|TDF_MEMSYMS);
 }
 
+
+/* Return formatted string of a VALUE probability
+   (biased by REG_BR_PROB_BASE).  Returned string is allocated
+   by xstrdup_for_dump.  */
+
+static const char *
+dump_probability (int value)
+{
+  char buf[16];
+  float minimum = 0.01f;
+
+  gcc_assert (0 <= value && value <= REG_BR_PROB_BASE);
+  float fvalue = value * 100.0f / REG_BR_PROB_BASE;
+  if (fvalue < minimum && value > 0)
+    return "[0.01%]";
+
+  sprintf (buf, "[%.2f%%]", fvalue);
+  return xstrdup_for_dump (buf);
+}
+
 /* Dump E probability to BUFFER.  */
 
 static void
 dump_edge_probability (pretty_printer *buffer, edge e)
 {
-  pp_scalar (buffer, " [%.1f%%]",
-	     e->probability * 100.0 / REG_BR_PROB_BASE);
+  pp_scalar (buffer, " %s", dump_probability (e->probability));
 }
 
 /* Print GIMPLE statement G to FILE using SPC indentation spaces and
@@ -1023,8 +1042,7 @@  dump_gimple_label (pretty_printer *buffer, glabel *gs, int spc, int flags)
       dump_generic_node (buffer, label, spc, flags, false);
       basic_block bb = gimple_bb (gs);
       if (bb && !(flags & TDF_GIMPLE))
-	pp_scalar (buffer, " [%.1f%%]",
-		   bb->frequency * 100.0 / REG_BR_PROB_BASE);
+	pp_scalar (buffer, " %s", dump_probability (bb->frequency));
       pp_colon (buffer);
     }
   if (flags & TDF_GIMPLE)
@@ -2590,8 +2608,8 @@  dump_gimple_bb_header (FILE *outf, basic_block bb, int indent, int flags)
 	  if (flags & TDF_GIMPLE)
 	    fprintf (outf, "%*sbb_%d:\n", indent, "", bb->index);
 	  else
-	    fprintf (outf, "%*s<bb %d> [%.1f%%]:\n", indent, "", bb->index,
-		     bb->frequency * 100.0 / REG_BR_PROB_BASE);
+	    fprintf (outf, "%*s<bb %d> %s:\n",
+		     indent, "", bb->index, dump_probability (bb->frequency));
 	}
     }
 }
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20040703-1.c b/gcc/testsuite/gcc.dg/tree-ssa/20040703-1.c
index 2980047..eb9fb56 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/20040703-1.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/20040703-1.c
@@ -9,4 +9,4 @@  float foo(float x)
 }
 
 /* We should *not* fold the arithmetic.  */
-/* { dg-final { scan-tree-dump-times "0\\.0\[^%\]" 0 "dom2"} } */
+/* { dg-final { scan-tree-dump-times "0\\.0\[^%0\]" 0 "dom2"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dump-2.c b/gcc/testsuite/gcc.dg/tree-ssa/dump-2.c
index 11cde92..8a63af4 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/dump-2.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/dump-2.c
@@ -6,4 +6,4 @@  int f(void)
   return 0;
 }
 
-/* { dg-final { scan-tree-dump "<bb \[0-9\]> \\\[100\\\.0%\\\]:" "optimized" } } */
+/* { dg-final { scan-tree-dump "<bb \[0-9\]> \\\[100\\\.00%\\\]:" "optimized" } } */
-- 
2.10.2