public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
@ 2023-11-27  9:42 652023330028 at smail dot nju.edu.cn
  2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2023-11-27  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112723
           Summary: Missed optimization for invariants 'c+c' when c +=
                    -2147483648 and c is a global variable
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we noticed that maybe there is a missed optimization for invariants
'c+c' when c+= -2147483648 and c is a global variable.
Such missed optimization affects many related optimizations, such as LICM
(where we found this issue), CSE, and others.
To describe this issue more clearly, we show an example of CSE. And we attach a
link to an example of LICM at the end.

In the following, the value of c+c is invariant between the two expressions.

In this example, c is the global variable.
https://godbolt.org/z/Y3djqKWjh

int a, b, c;
void test() {
    a = c + c;
    c += -2147483648;
    b = c + c;
}

But GCC -O3 -fwrapv or GCC -O3:
test():
        movl    c(%rip), %eax
        leal    (%rax,%rax), %edx
        addl    $-2147483648, %eax
        movl    %eax, c(%rip)
        addl    %eax, %eax      #Redundant computation
        movl    %edx, a(%rip)
        movl    %eax, b(%rip)
        ret

Expected code (Clang):
test():                               # @test()
        mov     eax, dword ptr [rip + c]
        lea     ecx, [rax + rax]
        mov     dword ptr [rip + a], ecx
        add     eax, -2147483648
        mov     dword ptr [rip + c], eax
        mov     dword ptr [rip + b], ecx
        ret

When c appears as a function argument, it works as expected:

https://godbolt.org/z/Tf6hP6M9W
int a, b;
void test(int c) {
    a = c + c;
    c += -2147483648;
    b = c + c;
}
GCC -O3:
test(int):
        leal    (%rdi,%rdi), %eax
        movl    %eax, a(%rip)
        movl    %eax, b(%rip)
        ret

An example of LICM affected by this: https://godbolt.org/z/5T89TWaG9

Thank you very much for your time and effort! We look forward to hearing from
you.

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

* [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
@ 2023-11-27  9:51 ` pinskia at gcc dot gnu.org
  2023-11-27  9:55 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-27  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The argument case is only optimized at the RTL level:
Trying 8 -> 9:
    8: {r87:SI=r85:SI-0x80000000;clobber flags:CC;}
      REG_DEAD r85:SI
      REG_UNUSED flags:CC
    9: {r88:SI=r87:SI<<0x1;clobber flags:CC;}
      REG_DEAD r87:SI
      REG_UNUSED flags:CC
Successfully matched this instruction:
(parallel [
        (set (reg:SI 88)
            (ashift:SI (reg/v:SI 85 [ cD.2766 ])
                (const_int 1 [0x1])))
        (clobber (reg:CC 17 flags))
    ])

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

* [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
  2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
@ 2023-11-27  9:55 ` rguenth at gcc dot gnu.org
  2023-11-27  9:56 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-27  9:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's combine on RTL that is able to eliminate the addition/subtraction and
postreload the redundant shift.  Nothing on GIMPLE optimizes

  c.0_2 = (unsigned int) c_5(D);
  _3 = c.0_2 + 2147483648;
  c_8 = (int) _3;
  _4 = c_8 * 2;

to

  _4 = c_5(D) * 2;

note you need to write

  c += -2147483647 - 1;

to avoid using 'long' (but I guess it's necessary to avoid undefined behavior).

So what's missing is a simplification pattern that ignores ops that only alter
bits that are dont-care in a later operation.  That fits the backprop pass
best, not special simplification patterns.

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

* [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
  2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
  2023-11-27  9:55 ` rguenth at gcc dot gnu.org
@ 2023-11-27  9:56 ` rguenth at gcc dot gnu.org
  2024-03-28 11:13 ` [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483647-1 " 652023330028 at smail dot nju.edu.cn
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-27  9:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-11-27
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

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

* [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (2 preceding siblings ...)
  2023-11-27  9:56 ` rguenth at gcc dot gnu.org
@ 2024-03-28 11:13 ` 652023330028 at smail dot nju.edu.cn
  2024-03-28 17:06 ` [Bug tree-optimization/112723] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-03-28 11:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Yi <652023330028 at smail dot nju.edu.cn> ---
https://godbolt.org/z/zKq3dsqW3
This is a regression since gcc-7.3. If the bisection right:
https://github.com/gcc-mirror/gcc/commit/540b5cb6c70.

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (3 preceding siblings ...)
  2024-03-28 11:13 ` [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483647-1 " 652023330028 at smail dot nju.edu.cn
@ 2024-03-28 17:06 ` pinskia at gcc dot gnu.org
  2024-03-28 17:18 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-28 17:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |normal
   Target Milestone|---                         |11.5
                 CC|                            |pinskia at gcc dot gnu.org

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (4 preceding siblings ...)
  2024-03-28 17:06 ` [Bug tree-optimization/112723] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
@ 2024-03-28 17:18 ` pinskia at gcc dot gnu.org
  2024-03-28 17:47 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-28 17:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
gimple-ssa-strength-reduction definitely should be improved to handle this case
where it should understand that (a+CSTWITHLASTBITSET)*2 can be done as
a*2+NEWCST if the type can wrap. Only can do that if wrapping rather than if
overflow is undefined.

For a non-signed case which should be handled:
```
unsigned a, b, c;
void test() {
    a = c + c;
    c += -2147483648u;
    b = c + c;
}
```

Maybe I will look at this for GCC 15. It might improve other code ...

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (5 preceding siblings ...)
  2024-03-28 17:18 ` pinskia at gcc dot gnu.org
@ 2024-03-28 17:47 ` pinskia at gcc dot gnu.org
  2024-04-05 15:07 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-28 17:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #2)
> 
> So what's missing is a simplification pattern that ignores ops that only
> alter
> bits that are dont-care in a later operation.  That fits the backprop pass
> best, not special simplification patterns.

I think this belongs in gimple-ssa-strength-reduction like it was done for GCC
7.1.0/7.2.0 until that fix went in which was done for all types rather than
just overflow types.

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (6 preceding siblings ...)
  2024-03-28 17:47 ` pinskia at gcc dot gnu.org
@ 2024-04-05 15:07 ` jakub at gcc dot gnu.org
  2024-04-05 15:13 ` jakub at gcc dot gnu.org
  2024-04-13 14:10 ` law at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-05 15:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Why is this marked as a regression?  I don't see 7.1 nor other versions behave
any differently than trunk.

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (7 preceding siblings ...)
  2024-04-05 15:07 ` jakub at gcc dot gnu.org
@ 2024-04-05 15:13 ` jakub at gcc dot gnu.org
  2024-04-13 14:10 ` law at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-05 15:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Ah, with -fwrapv.  Without -fwrapv the testcase invokes UB always, either the
first c + c overflows or the second one, or the c += -2147483647 - 1;
overflows.

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

* [Bug tree-optimization/112723] [11/12/13/14 Regression] Missed optimization for invariants 'c+c' when c += -2147483647-1 and c is a global variable
  2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
                   ` (8 preceding siblings ...)
  2024-04-05 15:13 ` jakub at gcc dot gnu.org
@ 2024-04-13 14:10 ` law at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: law at gcc dot gnu.org @ 2024-04-13 14:10 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org
           Priority|P3                          |P2

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

end of thread, other threads:[~2024-04-13 14:10 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
2023-11-27  9:55 ` rguenth at gcc dot gnu.org
2023-11-27  9:56 ` rguenth at gcc dot gnu.org
2024-03-28 11:13 ` [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483647-1 " 652023330028 at smail dot nju.edu.cn
2024-03-28 17:06 ` [Bug tree-optimization/112723] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2024-03-28 17:18 ` pinskia at gcc dot gnu.org
2024-03-28 17:47 ` pinskia at gcc dot gnu.org
2024-04-05 15:07 ` jakub at gcc dot gnu.org
2024-04-05 15:13 ` jakub at gcc dot gnu.org
2024-04-13 14:10 ` law 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).