public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional
@ 2013-08-19 19:24 warp at iki dot fi
  2014-03-11 13:28 ` [Bug rtl-optimization/58195] " bisqwit at iki dot fi
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: warp at iki dot fi @ 2013-08-19 19:24 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58195

            Bug ID: 58195
           Summary: Missed optimization opportunity when returning a
                    conditional
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: warp at iki dot fi

When compiling the following function:

int a(int input)
{
    return input == 0 ? 0 : -input;
}

gcc is unable to see that the conditional and returning 0 can be optimized
away, producing:

        movl    %edi, %edx
        xorl    %eax, %eax
        negl    %edx
        testl   %edi, %edi
        cmovne  %edx, %eax
        ret

For the record, I found out the above when I was testing what gcc would do with
this function:

int a(int input)
{
    int value = 0;
    for(int n = input; n != 0; ++n)
        ++value;
    return value;
}

gcc is able to optimize that into the same asm code as above, but no further
(not even if the conditional is written explicitly, as written in the first
function above.)


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

* [Bug rtl-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
@ 2014-03-11 13:28 ` bisqwit at iki dot fi
  2014-03-11 13:32 ` [Bug tree-optimization/58195] " rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: bisqwit at iki dot fi @ 2014-03-11 13:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58195

Joel Yliluoma <bisqwit at iki dot fi> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bisqwit at iki dot fi

--- Comment #1 from Joel Yliluoma <bisqwit at iki dot fi> ---
Problem confirmed on "gcc (GCC) 4.9.0 20140303 (experimental)" (SVN version) in
both 32-bit and 64-bit mode using -Ofast.

For comparison, Clang++ produces this instead (even on -O1): 

        negl    %edi
        movl    %edi, %eax
        ret

GCC misses an optimization opportunity here.


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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
  2014-03-11 13:28 ` [Bug rtl-optimization/58195] " bisqwit at iki dot fi
@ 2014-03-11 13:32 ` rguenth at gcc dot gnu.org
  2014-03-11 13:44 ` glisse at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-03-11 13:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58195

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-03-11
          Component|rtl-optimization            |tree-optimization
     Ever confirmed|0                           |1
      Known to fail|                            |4.9.0

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
phiopt should notice

  <bb 2>:
  if (input_2(D) != 0)
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  iftmp.0_3 = -input_2(D);

  <bb 4>:
  # iftmp.0_1 = PHI <iftmp.0_3(3), 0(2)>

and value-replace iftmp.0_1 by -input_2(D).


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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
  2014-03-11 13:28 ` [Bug rtl-optimization/58195] " bisqwit at iki dot fi
  2014-03-11 13:32 ` [Bug tree-optimization/58195] " rguenth at gcc dot gnu.org
@ 2014-03-11 13:44 ` glisse at gcc dot gnu.org
  2020-04-10  9:24 ` bisqwit at iki dot fi
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: glisse at gcc dot gnu.org @ 2014-03-11 13:44 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58195

--- Comment #3 from Marc Glisse <glisse at gcc dot gnu.org> ---
This looks like the unary version of:
http://gcc.gnu.org/ml/gcc-patches/2014-03/msg00014.html

and something that could be auto-generated from a pattern like:
(cond (ne @0 0) (negate @0) 0) -> (negate @0)
;-)


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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
                   ` (2 preceding siblings ...)
  2014-03-11 13:44 ` glisse at gcc dot gnu.org
@ 2020-04-10  9:24 ` bisqwit at iki dot fi
  2021-05-24  7:14 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: bisqwit at iki dot fi @ 2020-04-10  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Joel Yliluoma <bisqwit at iki dot fi> ---
Still confirmed on GCC 10 (Debian 10-20200324-1) 10.0.1 20200324 (experimental)
[master revision
596c90d3559:023579257f5:906b3eb9df6c577d3f6e9c3ea5c9d7e4d1e90536]


Seems I lack the oomph to update the "confirmed" state of this report.

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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
                   ` (3 preceding siblings ...)
  2020-04-10  9:24 ` bisqwit at iki dot fi
@ 2021-05-24  7:14 ` pinskia at gcc dot gnu.org
  2021-06-05  1:28 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-05-24  7:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |25290
                 CC|                            |pinskia at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine,  After PR 25290, the below match.pd pattern will just work :).

(In reply to Marc Glisse from comment #3) 
> and something that could be auto-generated from a pattern like:
> (cond (ne @0 0) (negate @0) 0) -> (negate @0)


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25290
[Bug 25290] PHI-OPT could be rewritten so that is uses match

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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
                   ` (4 preceding siblings ...)
  2021-05-24  7:14 ` pinskia at gcc dot gnu.org
@ 2021-06-05  1:28 ` pinskia at gcc dot gnu.org
  2021-11-17  9:56 ` pinskia at gcc dot gnu.org
  2022-11-19 23:24 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-06-05  1:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the loop based one is a little more complex due to the way GCC IR handles
overflow being wrapping or undefined; I am just going to fix the case where
overflow is defined as wrapping. The other case needs more work and maybe even
a huge change in the IR handling.

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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
                   ` (5 preceding siblings ...)
  2021-06-05  1:28 ` pinskia at gcc dot gnu.org
@ 2021-11-17  9:56 ` pinskia at gcc dot gnu.org
  2022-11-19 23:24 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-17  9:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #5)
> Mine,  After PR 25290, the below match.pd pattern will just work :).
> 
> (In reply to Marc Glisse from comment #3) 
> > and something that could be auto-generated from a pattern like:
> > (cond (ne @0 0) (negate @0) 0) -> (negate @0)

Actually that was implemented with r12-2041.

It is just the loop that is left.  We do get the optimization if we use -fwrapv
:).

I will handle the loop one for GCC 13.

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

* [Bug tree-optimization/58195] Missed optimization opportunity when returning a conditional
  2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
                   ` (6 preceding siblings ...)
  2021-11-17  9:56 ` pinskia at gcc dot gnu.org
@ 2022-11-19 23:24 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-19 23:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the loop one looks like:
  if (input_4(D) != 0)
    goto <bb 3>; [89.00%]
  else
    goto <bb 4>; [11.00%]

  <bb 3> [local count: 105119324]:
  _1 = (unsigned int) input_4(D);
  _3 = -_1;
  value_2 = (int) _3;

  <bb 4> [local count: 118111600]:
  # value_10 = PHI <value_2(3), 0(2)>

---- CUT ----
Which avoids undefined overflow for INT_MIN.

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

end of thread, other threads:[~2022-11-19 23:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-19 19:24 [Bug rtl-optimization/58195] New: Missed optimization opportunity when returning a conditional warp at iki dot fi
2014-03-11 13:28 ` [Bug rtl-optimization/58195] " bisqwit at iki dot fi
2014-03-11 13:32 ` [Bug tree-optimization/58195] " rguenth at gcc dot gnu.org
2014-03-11 13:44 ` glisse at gcc dot gnu.org
2020-04-10  9:24 ` bisqwit at iki dot fi
2021-05-24  7:14 ` pinskia at gcc dot gnu.org
2021-06-05  1:28 ` pinskia at gcc dot gnu.org
2021-11-17  9:56 ` pinskia at gcc dot gnu.org
2022-11-19 23:24 ` pinskia 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).