public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
@ 2023-06-20 17:11 theodort at inf dot ethz.ch
  2023-06-21  8:33 ` [Bug tree-optimization/110327] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-06-20 17:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110327
           Summary: [12/13/14 Regression] Missed Dead Code Elimination
                    when using __builtin_unreachable since
                    r12-4790-g4b3a325f07a
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: theodort at inf dot ethz.ch
  Target Milestone: ---

https://godbolt.org/z/eT1jqzrT5

Given the following code:

void foo(void);
static int a, c;
static int *d, *e = &a;
static int **f = &d, **g = &e;
static int ***h = &g;
static int ****i = &h;
static short j;
static char(k)(char b) {
    if (!(((b) >= -28) && ((b) <= -28))) {
        __builtin_unreachable();
    }
    return 0;
}
static int l() {
    a = -28;
    for (; a != 2; a++) {
        j = c;
        c = 0;
        for (; c != 2; c = j)
            if (a) break;
        k(****i);
        if (****i >= 5 ^ 1) {
            ***i = *f;
            if (c < 1) return 1;
            if (a) foo();
            ;
        }
    }
    return a;
}
int main() { l(); }

gcc-trunk -O2 does not eliminate the call to foo:

main:
        subq    $8, %rsp
        movl    $-28, %eax
        movl    $-28, a(%rip)
        .p2align 4,,10
        .p2align 3
.L10:
        movl    c(%rip), %edx
        testl   %eax, %eax
        jne     .L19
        cmpw    $2, %dx
        je      .L4
.L5:
        jmp     .L5
        .p2align 4,,10
        .p2align 3
.L4:
        movl    $2, c(%rip)
        movl    $2, %esi
.L3:
        movq    h(%rip), %rdx
        movq    (%rdx), %rdx
        movq    (%rdx), %rcx
        cmpl    $4, (%rcx)
        jg      .L7
        movq    d(%rip), %rcx
        movq    %rcx, (%rdx)
        testl   %esi, %esi
        je      .L16
        testl   %eax, %eax
        jne     .L20
.L7:
        movl    a(%rip), %eax
        addl    $1, %eax
        movl    %eax, a(%rip)
        cmpl    $2, %eax
        jne     .L10
.L16:
        xorl    %eax, %eax
        addq    $8, %rsp
        ret
        .p2align 4,,10
        .p2align 3
.L20:
        call    foo
        jmp     .L7
.L19:
        xorl    %edx, %edx
        xorl    %esi, %esi
        movl    %edx, c(%rip)
        jmp     .L3

gcc-11.4.0 -O2 eliminates the call to foo:

main:
        movl    $-28, a(%rip)
        movq    h(%rip), %rcx
        movl    $-28, %edx
        .p2align 4,,10
        .p2align 3
.L8:
        movzwl  c(%rip), %eax
        movl    $0, c(%rip)
        testl   %edx, %edx
        jne     .L2
        .p2align 4,,10
        .p2align 3
.L3:
        cmpw    $2, %ax
        jne     .L3
        movl    $2, c(%rip)
        movq    (%rcx), %rax
        movq    (%rax), %rdx
        cmpl    $4, (%rdx)
        jg      .L5
        movq    d(%rip), %rdx
        movq    %rdx, (%rax)
.L5:
        movl    a(%rip), %eax
        leal    1(%rax), %edx
        movl    %edx, a(%rip)
        cmpl    $2, %edx
        jne     .L8
.L13:
        xorl    %eax, %eax
        ret
.L2:
        movq    (%rcx), %rax
        movq    (%rax), %rdx
        cmpl    $4, (%rdx)
        jg      .L5
        movq    d(%rip), %rdx
        movq    %rdx, (%rax)
        jmp     .L13

Bisects to r12-4790-g4b3a325f07a

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

* [Bug tree-optimization/110327] [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
  2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
@ 2023-06-21  8:33 ` rguenth at gcc dot gnu.org
  2023-06-24  8:09 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-21  8:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.4
           Keywords|                            |missed-optimization

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

* [Bug tree-optimization/110327] [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
  2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
  2023-06-21  8:33 ` [Bug tree-optimization/110327] " rguenth at gcc dot gnu.org
@ 2023-06-24  8:09 ` pinskia at gcc dot gnu.org
  2023-11-24  3:55 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-24  8:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
What is interesting is that the call to foo is still there on the gimple level
in GCC 11, it is only on the RTL level it is able to be removed ....

What I see missing on the gimple level on the trunk is a jump threading
opportunity (but might be too complex to do):
  <bb 8> [local count: 12528922893]:
  # c.3_16 = PHI <0(4), 2(7)>
  _9 = h;
  _10 = *_9;
  _11 = *_10;
  _12 = *_11;
  if (_12 <= 4)
    goto <bb 10>; [50.00%]
  else
    goto <bb 9>; [50.00%]

...

  <bb 10> [local count: 6264461501]:
  _15 = d;
  *_10 = _15;
  if (c.3_16 == 0)
    goto <bb 11>; [5.50%]
  else
    goto <bb 12>; [94.50%]

  <bb 12> [local count: 5919916023]:
  if (a.11_30 != 0)
    goto <bb 13>; [33.00%]
  else
    goto <bb 9>; [67.00%]

The threading is:
4->8->10->11
7->8->10->12

This requires copying bb 8 and 10. I am not saying it is the right thing to do
but rather that is how to optimize this out I think.


There is also this one too:
  <bb 3> [local count: 12528922784]:
  # a.11_30 = PHI <_18(14), -28(2)>
  c.0_3 = c;
  if (a.11_30 != 0)
    goto <bb 4>; [5.50%]
  else
    goto <bb 5>; [94.50%]


2->3->4
14->3

But as I said I have not looked into more than that.

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

* [Bug tree-optimization/110327] [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
  2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
  2023-06-21  8:33 ` [Bug tree-optimization/110327] " rguenth at gcc dot gnu.org
  2023-06-24  8:09 ` pinskia at gcc dot gnu.org
@ 2023-11-24  3:55 ` pinskia at gcc dot gnu.org
  2024-03-08 15:31 ` law at gcc dot gnu.org
  2024-03-09  7:32 ` law at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-24  3:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
                 CC|                            |pinskia at gcc dot gnu.org
   Last reconfirmed|                            |2023-11-24

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug tree-optimization/110327] [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
  2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-11-24  3:55 ` pinskia at gcc dot gnu.org
@ 2024-03-08 15:31 ` law at gcc dot gnu.org
  2024-03-09  7:32 ` law at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-08 15:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

* [Bug tree-optimization/110327] [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a
  2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2024-03-08 15:31 ` law at gcc dot gnu.org
@ 2024-03-09  7:32 ` law at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-09  7:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jeffrey A. Law <law at gcc dot gnu.org> ---
Two block copies isn't fatal when the second one is the one with the actual
jump thread.  But costing does get more complex.  

Basically we copy 8 so that we can isolate its two incoming paths which thread
differently in bb10.  That's pretty standard stuff.

It looks like that particular threading possibility is hidden until after DOM3
is complete.  Prior to and during DOM3, there's another block in the way.


  # c.3_16 = PHI <0(3), 2(5)>
  _9 = h;
  _10 = *_9;
  _11 = *_10;     
  _12 = *_11; 
  _13 = (char) _12;
  if (_13 != -28)
    goto <bb 8>; [0.00%]
  else
    goto <bb 9>; [100.00%]
;;    succ:       8
;;                9

;;   basic block 8, loop depth 0
;;    pred:       7
  __builtin_unreachable ();
;;    succ:

;;   basic block 9, loop depth 1
;;    pred:       7
  if (_12 <= 4)
    goto <bb 10>; [50.00%]
  else
    goto <bb 15>; [50.00%]
;;    succ:       10
;;                15

;;   basic block 10, loop depth 1
;;    pred:       9
  _15 = d;
  *_10 = _15;
  if (c.3_16 <= 0)
    goto <bb 11>; [5.50%]
  else
    goto <bb 12>; [94.50%]

Note bb9.

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

end of thread, other threads:[~2024-03-09  7:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-20 17:11 [Bug tree-optimization/110327] New: [12/13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r12-4790-g4b3a325f07a theodort at inf dot ethz.ch
2023-06-21  8:33 ` [Bug tree-optimization/110327] " rguenth at gcc dot gnu.org
2023-06-24  8:09 ` pinskia at gcc dot gnu.org
2023-11-24  3:55 ` pinskia at gcc dot gnu.org
2024-03-08 15:31 ` law at gcc dot gnu.org
2024-03-09  7:32 ` 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).