public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts
@ 2022-04-02 13:55 andre.schackier at gmail dot com
  2022-04-04  7:19 ` [Bug target/105136] [11/12 regression] " rguenth at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: andre.schackier at gmail dot com @ 2022-04-02 13:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105136
           Summary: [11/12] Missed optimization regression with 32-bit
                    adds and shifts
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andre.schackier at gmail dot com
  Target Milestone: ---

Given the following source code [godbolt](https://godbolt.org/z/daTxMYWKo)

#include <stdint.h>
int32_t foo(int64_t a, int32_t b, int cond) {
    if (cond) {
        a += ((int64_t)b) << 32;
    }
    return a >> 32;
}

int32_t bar(int64_t a, int32_t b, int cond) {
    int32_t r = a >> 32;
    if (cond) {
        r += b;
    }
    return r;
}

and compiling with "-O3" we get the following assembly:

foo:
        sal     rsi, 32
        mov     rax, rdi
        add     rax, rsi
        test    edx, edx
        cmove   rax, rdi
        shr     rax, 32
        ret
bar:
        sar     rdi, 32
        add     esi, edi
        test    edx, edx
        mov     eax, esi
        cmove   eax, edi
        ret

With gcc-10.3 we get for bar:
bar:
        sar     rdi, 32
        test    edx, edx
        lea     eax, [rsi+rdi]
        cmove   eax, edi
        ret

Also note that neither versions recognize that foo does the same as bar.

Credits: This was entirely found by Trevor Spiteri reported at the llvm-project
here: https://github.com/llvm/llvm-project/issues/54718

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

* [Bug target/105136] [11/12 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
@ 2022-04-04  7:19 ` rguenth at gcc dot gnu.org
  2022-04-04  8:53 ` ubizjak at gmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-04  7:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.3
           Priority|P3                          |P2
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-04-04
          Component|middle-end                  |target
             Target|                            |x86_64-*-*
             Status|UNCONFIRMED                 |NEW
            Summary|[11/12] Missed optimization |[11/12 regression] Missed
                   |regression with 32-bit adds |optimization regression
                   |and shifts                  |with 32-bit adds and shifts

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug target/105136] [11/12 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
  2022-04-04  7:19 ` [Bug target/105136] [11/12 regression] " rguenth at gcc dot gnu.org
@ 2022-04-04  8:53 ` ubizjak at gmail dot com
  2022-04-19 18:08 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ubizjak at gmail dot com @ 2022-04-04  8:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Uroš Bizjak <ubizjak at gmail dot com> ---
The regression in bar: is due to RA regression for:

(insn 28 27 29 2 (parallel [
            (set (reg:SI 89)
                (plus:SI (reg:SI 92)
                    (subreg:SI (reg:DI 87) 0)))
            (clobber (reg:CC 17 flags))
        ]) "pr105136.c":4:11 229 {*addsi_1}
     (expr_list:REG_DEAD (reg:SI 92)
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

gcc-10.3 allocates:

(insn 28 27 29 2 (parallel [
            (set (reg:SI 0 ax [89])
                (plus:SI (reg:SI 4 si [92])
                    (reg:SI 5 di [87])))
            (clobber (reg:CC 17 flags))
        ]) "pr105136.c":4:11 185 {*addsi_1}
     (nil))

while gcc-12 allocates:

(insn 28 27 29 2 (parallel [
            (set (reg:SI 4 si [89])
                (plus:SI (reg:SI 4 si [92])
                    (reg:SI 5 di [87])))
            (clobber (reg:CC 17 flags))
        ]) "pr105136.c":4:11 229 {*addsi_1}
     (nil))

(...)

and reloads the output to ax:

(insn 35 29 30 2 (set (reg:SI 0 ax [89])
        (reg:SI 4 si [89])) "pr105136.c":4:11 81 {*movsi_internal}
     (nil))
(insn 30 35 20 2 (set (reg:SI 0 ax [89])
        (if_then_else:SI (eq (reg:CCZ 17 flags)
                (const_int 0 [0]))
            (reg:SI 5 di [87])
            (reg:SI 0 ax [89]))) "pr105136.c":4:11 1201 {*movsicc_noc}
     (nil))

The pattern allows r/r/r in alternative 3, so the question is why RA doesn't
consider it:

(define_insn "*add<mode>_1"
  [(set (match_operand:SWI48 0 "nonimmediate_operand" "=rm,r,r,r")
        (plus:SWI48
          (match_operand:SWI48 1 "nonimmediate_operand" "%0,0,r,r")
          (match_operand:SWI48 2 "x86_64_general_operand" "re,BM,0,le")))
   (clobber (reg:CC FLAGS_REG))]

The "l" constraint is user defined register constraint:

(define_register_constraint "l" "INDEX_REGS"
 "@internal Any register that can be used as the index in a base+index
  memory access: that is, any general register except the stack pointer.")

so perhaps user defined constraint deters RA from using this alternative.

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

* [Bug target/105136] [11/12 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
  2022-04-04  7:19 ` [Bug target/105136] [11/12 regression] " rguenth at gcc dot gnu.org
  2022-04-04  8:53 ` ubizjak at gmail dot com
@ 2022-04-19 18:08 ` jakub at gcc dot gnu.org
  2022-04-20 15:48 ` vmakarov at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-19 18:08 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org,
                   |                            |vmakarov at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
This started with r11-209-g74dc179a6da33cd00f6d4a93fbb97dc84f610126

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

* [Bug target/105136] [11/12 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
                   ` (2 preceding siblings ...)
  2022-04-19 18:08 ` jakub at gcc dot gnu.org
@ 2022-04-20 15:48 ` vmakarov at gcc dot gnu.org
  2022-04-21  7:51 ` rguenth at gcc dot gnu.org
  2023-05-29 10:06 ` [Bug target/105136] [11/12/13/14 " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: vmakarov at gcc dot gnu.org @ 2022-04-20 15:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Vladimir Makarov <vmakarov at gcc dot gnu.org> ---
I am just saying trivial things here that RA is a NP-complete task and there is
no optimal solution for all tests.  For GCC it is even more complicated as RA
solves code selection tasks too.  Basically we have for this test

p91=di
p92=si
...
p89=p92+p87 (dead p92)
p97=p91>>const (dead p91)
p83=flags?p87:p89 (dead p87, p89)
ax=p83

RA creates the following relations (to propagate assignment costs) for pseudos

p83(ax preferred)---p87---p91(di preferred)
\
 \------------------p89---p92(si preferred)

Only assignment ax for p89 can create the desired code.  Relation costs of
p87--p91 and p89--p92 or p83--p87 and p83--p89 are the same even if we use
--param ira-consider-dup-in-all-alts=1.

To get the right guaranteed solution we need some greedy algorithm which will
take a lot of time to work and check results not only at the end of IRA but at
the end LRA.

I can revert meaningful changes of the patch which resulted in this
degradation.  But as I can see this creates 3 new test failures for tests
avx512fp16-conjugation-1.c and avx512fp16vl-conjugation-1.c.  Also I can not
guarantee that such change will not result in more serious benchmark (e.g.
SPEC) degradation.

But in any I can try to do this.  Although I am not sure taht it is worth to do
this at this stage of gcc-12 release work.

Richard and Jakub, what your thoughts about reverting my patch in question?

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

* [Bug target/105136] [11/12 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
                   ` (3 preceding siblings ...)
  2022-04-20 15:48 ` vmakarov at gcc dot gnu.org
@ 2022-04-21  7:51 ` rguenth at gcc dot gnu.org
  2023-05-29 10:06 ` [Bug target/105136] [11/12/13/14 " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug target/105136] [11/12/13/14 regression] Missed optimization regression with 32-bit adds and shifts
  2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
                   ` (4 preceding siblings ...)
  2022-04-21  7:51 ` rguenth at gcc dot gnu.org
@ 2023-05-29 10:06 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-29 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.5

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 11.4 is being released, retargeting bugs to GCC 11.5.

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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-02 13:55 [Bug middle-end/105136] New: [11/12] Missed optimization regression with 32-bit adds and shifts andre.schackier at gmail dot com
2022-04-04  7:19 ` [Bug target/105136] [11/12 regression] " rguenth at gcc dot gnu.org
2022-04-04  8:53 ` ubizjak at gmail dot com
2022-04-19 18:08 ` jakub at gcc dot gnu.org
2022-04-20 15:48 ` vmakarov at gcc dot gnu.org
2022-04-21  7:51 ` rguenth at gcc dot gnu.org
2023-05-29 10:06 ` [Bug target/105136] [11/12/13/14 " jakub 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).