public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong
@ 2023-03-02 22:51 pinskia at gcc dot gnu.org
  2023-03-02 23:26 ` [Bug tree-optimization/108997] " nok.raven at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-02 22:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

            Bug ID: 108997
           Summary: GCC prediction on bool comparisons seems wrong
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

I noticed this while looking into 108992.
I suspect we look into this but I am not 100% sure this is a bug.
Testcase:
void use(int *);
void use2(int *);

void foo(bool cond, int * p)
{
    if (cond) {
        use(p);
    }
    use2(p);
}

GCC predicts the branch to be taken 66% of the time, but I would have assume it
should be 50% as it is a bool and there is no other information. I suspect this
is due to prediction when comparing against 0.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
@ 2023-03-02 23:26 ` nok.raven at gmail dot com
  2023-03-02 23:35 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: nok.raven at gmail dot com @ 2023-03-02 23:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

Nikita Kniazev <nok.raven at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nok.raven at gmail dot com

--- Comment #1 from Nikita Kniazev <nok.raven at gmail dot com> ---
Is it about bool?

void use(int *);
void use2(int *);

void foo(int * p, int cond)
{
    if (cond == 789) {
        use(p);
    }
    use2(p);
}

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
  2023-03-02 23:26 ` [Bug tree-optimization/108997] " nok.raven at gmail dot com
@ 2023-03-02 23:35 ` pinskia at gcc dot gnu.org
  2023-03-03  0:05 ` nok.raven at gmail dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-02 23:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Nikita Kniazev from comment #1)
> Is it about bool?
YES.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
  2023-03-02 23:26 ` [Bug tree-optimization/108997] " nok.raven at gmail dot com
  2023-03-02 23:35 ` pinskia at gcc dot gnu.org
@ 2023-03-03  0:05 ` nok.raven at gmail dot com
  2023-03-03  0:10 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: nok.raven at gmail dot com @ 2023-03-03  0:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

--- Comment #3 from Nikita Kniazev <nok.raven at gmail dot com> ---
For cond == 789
  if (cond_2(D) == 789)
    goto <bb 3>; [20.24%]
  else
    goto <bb 4>; [79.76%]

For cond != 789
  if (cond_2(D) != 789)
    goto <bb 3>; [48.88%]
  else
    goto <bb 4>; [51.12%]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-03-03  0:05 ` nok.raven at gmail dot com
@ 2023-03-03  0:10 ` pinskia at gcc dot gnu.org
  2023-03-03  8:43 ` rguenth at gcc dot gnu.org
  2023-03-07 14:49 ` marxin at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-03  0:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Nikita Kniazev from comment #3)
> For cond == 789
>   if (cond_2(D) == 789)
>     goto <bb 3>; [20.24%]
>   else
>     goto <bb 4>; [79.76%]
> 
> For cond != 789
>   if (cond_2(D) != 789)
>     goto <bb 3>; [48.88%]
>   else
>     goto <bb 4>; [51.12%]

an intergal type equaling a specific # is handled this way on purpose. Bool
should be handled differently because it only has two values, 0/1.
Anyways != is not handled specially except for the zero value case ...

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-03-03  0:10 ` pinskia at gcc dot gnu.org
@ 2023-03-03  8:43 ` rguenth at gcc dot gnu.org
  2023-03-07 14:49 ` marxin at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-03-03  8:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Predictions for bb 2
  DS theory heuristics: 33.00%
  combined heuristics: 33.00%
  call heuristics of edge 2->3: 33.00%

so it seems it's about "conditional call" which is reasonable I think.

Doing

void use2(int *);
int x = 1;
void foo(bool cond, int * p)
{
    if (cond) {
        x = 1;
    }
    use2(p);
}

shows even probabilities, so does

  if (cond) use1 (p); else use2 (p);

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug tree-optimization/108997] GCC prediction on bool comparisons seems wrong
  2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-03-03  8:43 ` rguenth at gcc dot gnu.org
@ 2023-03-07 14:49 ` marxin at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: marxin at gcc dot gnu.org @ 2023-03-07 14:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108997

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |marxin at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #6 from Martin Liška <marxin at gcc dot gnu.org> ---
Yes, one can see the heuristics with -fdump-tree-profile_estimate as Richi
showed.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-03-07 14:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-02 22:51 [Bug tree-optimization/108997] New: GCC prediction on bool comparisons seems wrong pinskia at gcc dot gnu.org
2023-03-02 23:26 ` [Bug tree-optimization/108997] " nok.raven at gmail dot com
2023-03-02 23:35 ` pinskia at gcc dot gnu.org
2023-03-03  0:05 ` nok.raven at gmail dot com
2023-03-03  0:10 ` pinskia at gcc dot gnu.org
2023-03-03  8:43 ` rguenth at gcc dot gnu.org
2023-03-07 14:49 ` marxin at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).