public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110203] New: Sum should optimize to closed form
@ 2023-06-10 12:37 llvm at rifkin dot dev
  2023-06-10 12:45 ` [Bug tree-optimization/110203] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: llvm at rifkin dot dev @ 2023-06-10 12:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110203
           Summary: Sum should optimize to closed form
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: llvm at rifkin dot dev
  Target Milestone: ---

For both of

int foo(int num) {
    if(num == 1) return 1;
    return num + foo(num - 1);
}

int bar(int num) {
    int sum = 0;
    for(int i = 1; i <= num; i++) {
        sum += i;
    }
    return sum;
}


GCC emits loops:

foo(int):
        xor     eax, eax
        cmp     edi, 1
        je      .L8
.L2:
        mov     edx, edi
        sub     edi, 1
        add     eax, edx
        cmp     edi, 1
        jne     .L2
        add     eax, 1
        ret
.L8:
        mov     eax, 1
        ret
bar(int):
        test    edi, edi
        jle     .L12
        add     edi, 1
        mov     eax, 1
        xor     edx, edx
.L11:
        add     edx, eax
        add     eax, 1
        cmp     eax, edi
        jne     .L11
        mov     eax, edx
        ret
.L12:
        xor     edx, edx
        mov     eax, edx
        ret


GCC should optimize this to the closed form.

There is precedent for this transformation, llvm does it
https://godbolt.org/z/Kfffe5aPz.

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

* [Bug tree-optimization/110203] Sum should optimize to closed form
  2023-06-10 12:37 [Bug tree-optimization/110203] New: Sum should optimize to closed form llvm at rifkin dot dev
@ 2023-06-10 12:45 ` pinskia at gcc dot gnu.org
  2023-06-10 13:04 ` llvm at rifkin dot dev
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-10 12:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is most likely just not taking into account that signed integer overflow
is undefined.

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

* [Bug tree-optimization/110203] Sum should optimize to closed form
  2023-06-10 12:37 [Bug tree-optimization/110203] New: Sum should optimize to closed form llvm at rifkin dot dev
  2023-06-10 12:45 ` [Bug tree-optimization/110203] " pinskia at gcc dot gnu.org
@ 2023-06-10 13:04 ` llvm at rifkin dot dev
  2023-06-10 15:57 ` pinskia at gcc dot gnu.org
  2023-06-10 16:31 ` llvm at rifkin dot dev
  3 siblings, 0 replies; 5+ messages in thread
From: llvm at rifkin dot dev @ 2023-06-10 13:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jeremy R. <llvm at rifkin dot dev> ---
I wasn't able to get this to work with unsigned either
https://godbolt.org/z/bGcW7ebjd but maybe there's some other way to trigger
this optimization

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

* [Bug tree-optimization/110203] Sum should optimize to closed form
  2023-06-10 12:37 [Bug tree-optimization/110203] New: Sum should optimize to closed form llvm at rifkin dot dev
  2023-06-10 12:45 ` [Bug tree-optimization/110203] " pinskia at gcc dot gnu.org
  2023-06-10 13:04 ` llvm at rifkin dot dev
@ 2023-06-10 15:57 ` pinskia at gcc dot gnu.org
  2023-06-10 16:31 ` llvm at rifkin dot dev
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-10 15:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 98960.

*** This bug has been marked as a duplicate of bug 98960 ***

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

* [Bug tree-optimization/110203] Sum should optimize to closed form
  2023-06-10 12:37 [Bug tree-optimization/110203] New: Sum should optimize to closed form llvm at rifkin dot dev
                   ` (2 preceding siblings ...)
  2023-06-10 15:57 ` pinskia at gcc dot gnu.org
@ 2023-06-10 16:31 ` llvm at rifkin dot dev
  3 siblings, 0 replies; 5+ messages in thread
From: llvm at rifkin dot dev @ 2023-06-10 16:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jeremy R. <llvm at rifkin dot dev> ---
Thanks for tracking down the duplicates

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

end of thread, other threads:[~2023-06-10 16:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-10 12:37 [Bug tree-optimization/110203] New: Sum should optimize to closed form llvm at rifkin dot dev
2023-06-10 12:45 ` [Bug tree-optimization/110203] " pinskia at gcc dot gnu.org
2023-06-10 13:04 ` llvm at rifkin dot dev
2023-06-10 15:57 ` pinskia at gcc dot gnu.org
2023-06-10 16:31 ` llvm at rifkin dot dev

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