public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/112662] New: missed-optimization: loop increment until wrap
@ 2023-11-21 23:10 goon.pri.low at gmail dot com
  2023-11-21 23:23 ` [Bug middle-end/112662] " pinskia at gcc dot gnu.org
  2023-12-30 16:21 ` goon.pri.low at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: goon.pri.low at gmail dot com @ 2023-11-21 23:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112662
           Summary: missed-optimization: loop increment until wrap
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

This function:

unsigned unopt(unsigned a) {
    while (++a > 999);
    return a;
}

unopt:
        lea     eax, [rdi+1]
        xor     edx, edx
        not     edi
        cmp     eax, 999
        cmovbe  edi, edx
        add     eax, edi
        ret

Can be optimized to:

unsigned opt(unsigned a) {
    if (++a > 999) return a;
    return 0;
}

opt:
        lea     eax, [rdi+1]
        xor     edx, edx
        cmp     eax, 999
        cmovbe  eax, edx
        ret

(saving two valuable instructions)

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

* [Bug middle-end/112662] missed-optimization: loop increment until wrap
  2023-11-21 23:10 [Bug middle-end/112662] New: missed-optimization: loop increment until wrap goon.pri.low at gmail dot com
@ 2023-11-21 23:23 ` pinskia at gcc dot gnu.org
  2023-12-30 16:21 ` goon.pri.low at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-21 23:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
   Last reconfirmed|                            |2023-11-21

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
```
unsigned f(unsigned a)
{
  unsigned t = a+1;
  bool tt = t > 999;
  unsigned t3 = ~a;
  unsigned t4;
  if (tt) t4 = t3; else t4 = 0;
  return t+t4;
}
```
Is only optimized by PRE.
Which does not optimize GIMPLE_COND plus it is too late.

Anyways we have:

  _6 = a_2(D) + 1;
  _7 = _6 > 999;
  _8 = ~a_2(D);
  _9 = _7 ? _8 : 0;
  a_4 = _6 + _9;

Which could be optimized to:
  _6 = a_2(D) + 1;
  _7 = _6 > 999;
  a_4 = _7 ? 0 : _6;

Maybe something like:
```
(simplify
 (plus:c (plus@0 @1 integer_onep)
         (cond @2 (bit_not @0) integer_zerop@3))
 (cond @2 @3 @0))
```

I will look into this further, maybe that is it.

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

* [Bug middle-end/112662] missed-optimization: loop increment until wrap
  2023-11-21 23:10 [Bug middle-end/112662] New: missed-optimization: loop increment until wrap goon.pri.low at gmail dot com
  2023-11-21 23:23 ` [Bug middle-end/112662] " pinskia at gcc dot gnu.org
@ 2023-12-30 16:21 ` goon.pri.low at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: goon.pri.low at gmail dot com @ 2023-12-30 16:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from gooncreeper <goon.pri.low at gmail dot com> ---
I believe I got my initial optimized function wrong, it should actually be this


unsigned opt(unsigned a) {
    if (++a > 999) a = 0;
    return a;
}

opt:
        lea     eax, [rdi+1]
        xor     edx, edx
        cmp     eax, 1000
        cmovnb  eax, edx
        ret

Which is a bit more clear also.

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

end of thread, other threads:[~2023-12-30 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-21 23:10 [Bug middle-end/112662] New: missed-optimization: loop increment until wrap goon.pri.low at gmail dot com
2023-11-21 23:23 ` [Bug middle-end/112662] " pinskia at gcc dot gnu.org
2023-12-30 16:21 ` goon.pri.low at gmail dot com

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).