public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
@ 2022-11-17 20:46 ppalka at gcc dot gnu.org
  2022-11-17 20:48 ` [Bug tree-optimization/107740] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-11-17 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107740
           Summary: if-to-switch conversion happens for simple predicate
                    function when compiled with gcc but not with g++
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppalka at gcc dot gnu.org
  Target Milestone: ---

For the following testcase, the ||'s are converted into a switch statement when
compiling with gcc but not with g++ (despite only minor differences in their
GENERIC trees) and ultimately leads to seemingly worse codegen when compiling
with g++ vs gcc:

$ cat is_whitespace.c
int
is_whitespace (char c)
{
  return (c == ' '
          || c == '\n'
          || c == '\r'
          || c == '\t');
}


$ gcc -fdump-tree-{original,iftoswitch}=/dev/stdout -O2 is_whitespace.C

;; Function is_whitespace (null)
;; enabled by -tree-original


{
  return (c == 32 || c == 10) || (c == 13 || c == 9);
}


;; Function is_whitespace (is_whitespace, funcdef_no=0, decl_uid=2735,
cgraph_uid=1, symbol_order=0)

;; Canonical GIMPLE case clusters: 9-10 13 32 
;; BT can be built: BT:9-32 
Removing basic block 3
Expanded into a new gimple STMT: switch (c_8(D)) <default: <L6> [INV], case 9:
<L5> [INV], case 10: <L5> [INV], case 13: <L5> [INV], case 32: <L5> [INV]>

int is_whitespace (char c)
{
  _Bool _1;
  _Bool _2;
  _Bool _3;
  int iftmp.0_7;

  <bb 2> :
  _1 = c_8(D) == 32;
  _2 = c_8(D) == 10;
  _3 = _1 | _2;
  switch (c_8(D)) <default: <L6> [INV], case 9: <L5> [INV], case 10: <L5>
[INV], case 13: <L5> [INV], case 32: <L5> [INV]>

  <bb 3> :
<L5>:

  <bb 4> :
  # iftmp.0_7 = PHI <1(3), 0(2)>
<L6>:
  return iftmp.0_7;

}


$ g++ -fdump-tree-{original,iftoswitch}=/dev/stdout -O2 is_whitespace.C

;; Function int is_whitespace(char) (null)
;; enabled by -tree-original


return <retval> = (int) ((c == 32 || c == 10) || (c == 13 || c == 9));


;; Function is_whitespace (_Z13is_whitespacec, funcdef_no=0, decl_uid=2757,
cgraph_uid=1, symbol_order=0)

int is_whitespace (char c)
{
  bool _1;
  bool _2;
  bool _3;
  bool _4;
  bool _5;
  bool _6;
  bool iftmp.0_7;
  int _11;

  <bb 2> :
  _1 = c_8(D) == 32;
  _2 = c_8(D) == 10;
  _3 = _1 | _2;
  if (_3 != 0)
    goto <bb 4>; [INV]
  else
    goto <bb 3>; [INV]

  <bb 3> :
  _4 = c_8(D) == 13;
  _5 = c_8(D) == 9;
  _6 = _4 | _5;

  <bb 4> :
  # iftmp.0_7 = PHI <1(2), _6(3)>
  _11 = (int) iftmp.0_7;
  return _11;

}

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

* [Bug tree-optimization/107740] if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
  2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
@ 2022-11-17 20:48 ` pinskia at gcc dot gnu.org
  2022-11-17 20:59 ` [Bug tree-optimization/107740] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-17 20:48 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There might be already another bug about this specific case (code) even.

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

* [Bug tree-optimization/107740] [12/13 Regression] if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
  2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
  2022-11-17 20:48 ` [Bug tree-optimization/107740] " pinskia at gcc dot gnu.org
@ 2022-11-17 20:59 ` pinskia at gcc dot gnu.org
  2022-11-17 21:00 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-17 20:59 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Target Milestone|---                         |12.3
   Last reconfirmed|                            |2022-11-17
             Status|UNCONFIRMED                 |NEW
            Summary|if-to-switch conversion     |[12/13 Regression]
                   |happens for simple          |if-to-switch conversion
                   |predicate function when     |happens for simple
                   |compiled with gcc but not   |predicate function when
                   |with g++                    |compiled with gcc but not
                   |                            |with g++

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The difference is PHI-OPT:
For C++ we have:

  _4 = c_8(D) == 13;
  _5 = c_8(D) == 9;
  _6 = _4 | _5;
  if (_6 != 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :

  <bb 5> :
  # iftmp.0_7 = PHI <1(4), 0(3)>
Which phi-opt1 changes to:

  _4 = c_8(D) == 13;
  _5 = c_8(D) == 9;
  _6 = _4 | _5;

  <bb 4> :
  # iftmp.0_7 = PHI <1(2), _6(3)> (note type is bool)


But for C we have:

  if (_6 != 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :

  <bb 5> :
  # iftmp.0_7 = PHI <1(4), 0(3)> (note the type is int)

phi-opt1 is the "early phi-opt" which tries not to do it here.

Also this is a regression from GCC 11.3.0.

There has been improvements to phi-opt and there was slight IR change due to
cleanup cfg after cddce1 which allowed phi-opt to do the change here. Note
PHI-OPT is not at fault really but if-to-switch not handling the slightly
different IR really.

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

* [Bug tree-optimization/107740] [12/13 Regression] if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
  2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
  2022-11-17 20:48 ` [Bug tree-optimization/107740] " pinskia at gcc dot gnu.org
  2022-11-17 20:59 ` [Bug tree-optimization/107740] [12/13 Regression] " pinskia at gcc dot gnu.org
@ 2022-11-17 21:00 ` pinskia at gcc dot gnu.org
  2023-01-13 12:44 ` rguenth at gcc dot gnu.org
  2023-05-08 12:26 ` [Bug tree-optimization/107740] [12/13/14 " rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-17 21:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>phi-opt1 is the "early phi-opt" which tries not to do it here.

Let me expand on that, it tries not to insert a cast here to do the conversion
from bool to int.

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

* [Bug tree-optimization/107740] [12/13 Regression] if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
  2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-11-17 21:00 ` pinskia at gcc dot gnu.org
@ 2023-01-13 12:44 ` rguenth at gcc dot gnu.org
  2023-05-08 12:26 ` [Bug tree-optimization/107740] [12/13/14 " rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-01-13 12:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug tree-optimization/107740] [12/13/14 Regression] if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++
  2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-01-13 12:44 ` rguenth at gcc dot gnu.org
@ 2023-05-08 12:26 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-08 12:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|12.3                        |12.4

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 12.3 is being released, retargeting bugs to GCC 12.4.

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

end of thread, other threads:[~2023-05-08 12:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17 20:46 [Bug tree-optimization/107740] New: if-to-switch conversion happens for simple predicate function when compiled with gcc but not with g++ ppalka at gcc dot gnu.org
2022-11-17 20:48 ` [Bug tree-optimization/107740] " pinskia at gcc dot gnu.org
2022-11-17 20:59 ` [Bug tree-optimization/107740] [12/13 Regression] " pinskia at gcc dot gnu.org
2022-11-17 21:00 ` pinskia at gcc dot gnu.org
2023-01-13 12:44 ` rguenth at gcc dot gnu.org
2023-05-08 12:26 ` [Bug tree-optimization/107740] [12/13/14 " rguenth 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).