public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
@ 2023-09-21  9:40 theodort at inf dot ethz.ch
  2023-09-21 15:21 ` [Bug tree-optimization/111515] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-09-21  9:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111515
           Summary: [14 Regression] Missed Dead Code Elimination since
                    r14-4089-gd45ddc2c04e
           Product: gcc
           Version: unknown
            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/K9bbM4fc9

Given the following code:

void foo(void);
static struct a {
    short b;
    int c;
    char d;
    int e;
    unsigned f;
} h, j, k = {0, 4274716299}, l, *aa = &j;
static char g;
static short i;
static int m, ah;
static int *ac = &j.e;
static void n(struct a o) {
    int aj = o.c;
    int *ak = &h.e;
    *ak = g && aj;
    if (!(((aj) >= 0) && ((aj) <= 4274716299))) {
        __builtin_unreachable();
    }
    i = ah;
}
int main() {
    n(l);
    *aa = h;
    m = *ac;
    if (m < 10)
        ;
    else
        foo();
    ;
    n(k);
}

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

main:
        subq    $8, %rsp
        xorl    %edi, %edi
        call    n.isra.0
        movdqa  h(%rip), %xmm0
        movl    h+16(%rip), %eax
        movaps  %xmm0, j(%rip)
        cmpl    $9, j+12(%rip)
        movl    %eax, j+16(%rip)
        jg      .L6
.L4:
        movl    $-20250997, %edi
        xorl    %eax, %eax
        call    n.isra.0
        addq    $8, %rsp
        ret
.L6:
        call    foo
        jmp     .L4

gcc-13.2.0 -O2 eliminates the call to foo:

main:
        movl    $0, h+12(%rip)
        movl    h+16(%rip), %eax
        movdqa  h(%rip), %xmm0
        movl    %eax, j+16(%rip)
        movaps  %xmm0, j(%rip)

Bisects to r14-4089-gd45ddc2c04e

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
@ 2023-09-21 15:21 ` rguenth at gcc dot gnu.org
  2023-09-21 15:34 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-09-21 15:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-09-21
   Target Milestone|---                         |14.0
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
            Version|unknown                     |14.0
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I will have a look.

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
  2023-09-21 15:21 ` [Bug tree-optimization/111515] " rguenth at gcc dot gnu.org
@ 2023-09-21 15:34 ` pinskia at gcc dot gnu.org
  2023-09-21 15:55 ` theodort at inf dot ethz.ch
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21 15:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks like an early jump threading happens in n and then the size differences
causes inlining heurstics not to chose the function for inlining because it is
inlining into `main`. If we change main to `f` or `main1`, then n is inlined
and there is no missed optimization ...

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
  2023-09-21 15:21 ` [Bug tree-optimization/111515] " rguenth at gcc dot gnu.org
  2023-09-21 15:34 ` pinskia at gcc dot gnu.org
@ 2023-09-21 15:55 ` theodort at inf dot ethz.ch
  2023-10-16 11:26 ` theodort at inf dot ethz.ch
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-09-21 15:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Theodoros Theodoridis <theodort at inf dot ethz.ch> ---
I'm re-reducing the case so that the missed optimization does not depend on
inlining to main (the bisection will stay the same). I'll post the updated code
soon.

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-09-21 15:55 ` theodort at inf dot ethz.ch
@ 2023-10-16 11:26 ` theodort at inf dot ethz.ch
  2023-12-13 13:46 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-10-16 11:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Theodoros Theodoridis <theodort at inf dot ethz.ch> ---
It turns out that the unreduced test case is also depended on not-inlining to
main. I will be more careful with filtering out such cases in the future.

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2023-10-16 11:26 ` theodort at inf dot ethz.ch
@ 2023-12-13 13:46 ` rguenth at gcc dot gnu.org
  2023-12-13 13:53 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-13 13:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think performing the threading early is sound, it duplicates one stmt
(store to h) for removing one conditional branch at runtime.  And the
estimate is computed correctly and 1 is what we allow.

In _theory_ we could look at early-inline-insns and avoid any growth
that would push us over the edge, but I'm not sure how feasible that would be.

In principle we could also stop local optimizations once we reach a size
that's good enough for early inlining.  Or remember the original size
before early opts and choose the minimum of that and the final IL we're
attempting to inline.

Honza?

As for the bug I think everything works as designed, so not optimizing this
anymore is a trade-off, aka not really a bug.  But interesting nevertheless.

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (4 preceding siblings ...)
  2023-12-13 13:46 ` rguenth at gcc dot gnu.org
@ 2023-12-13 13:53 ` rguenth at gcc dot gnu.org
  2023-12-14 15:54 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-13 13:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
OTOH the eliminated branch is

  if (aj_6 < 0)
    goto <bb 6>; [INV]
  else
    goto <bb 7>; [INV]

  <bb 6> :
  __builtin_unreachable ();

which isn't too interesting to eliminate as we'll remove it from all
paths later anyway.  Possibly worth special-casing and might avoid
some threading around assert() calls implemented this way.  Threads
around abort () are still useful as we keep them.  So not sure if
relevant in practice and thus worth the trouble.

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (5 preceding siblings ...)
  2023-12-13 13:53 ` rguenth at gcc dot gnu.org
@ 2023-12-14 15:54 ` rguenth at gcc dot gnu.org
  2024-03-07 21:53 ` law at gcc dot gnu.org
  2024-05-07  7:42 ` [Bug tree-optimization/111515] [14/15 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-14 15:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
So like the following which disables threading ending in a

 if (..)
   __builtin_unreachable ();

branch when we thread to the _reachable_ side of it.  We keep threading
to the __builtin_unreachable () side as that might result in resolving
earlier branches on the path.  It's going to be interesting to argue
that __builtin_abort () shouldn't be treated this way (but I think
__builtin_unreachable and __builtin_unreachable_trap should not differ
for QOI reasons?)

diff --git a/gcc/tree-ssa-threadbackward.cc b/gcc/tree-ssa-threadbackward.cc
index c45f4b261ad..22f6254cf98 100644
--- a/gcc/tree-ssa-threadbackward.cc
+++ b/gcc/tree-ssa-threadbackward.cc
@@ -878,6 +878,26 @@ back_threader_profitability::profitable_path_p (const
vec<basic_block> &m_path,
                 "non-empty latch\n");
       return false;
     }
+
+  /* We don't want to duplicate a path when the branch we resolve is
+     going to be elided anyway unless we figure the path is unreachable
+     itself.  */
+  edge_iterator ei;
+  edge e;
+  gcall *call;
+  FOR_EACH_EDGE (e, ei, taken_edge->src->succs)
+      if (e != taken_edge
+         && (call = safe_as_a <gcall *> (*gsi_last_bb (e->dest)))
+         && (gimple_call_builtin_p (call, BUILT_IN_UNREACHABLE)
+             || gimple_call_builtin_p (call, BUILT_IN_UNREACHABLE_TRAP)))
+       {
+         if (dump_file && (dump_flags & TDF_DETAILS))
+           fprintf (dump_file,
+                    "  FAIL: Thread ends on the reachable side of a branch "
+                    "to unreachable\n");
+         return false;
+       }
+
   if (dump_file && (dump_flags & TDF_DETAILS))
     fputc ('\n', dump_file);
   return true;

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

* [Bug tree-optimization/111515] [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (6 preceding siblings ...)
  2023-12-14 15:54 ` rguenth at gcc dot gnu.org
@ 2024-03-07 21:53 ` law at gcc dot gnu.org
  2024-05-07  7:42 ` [Bug tree-optimization/111515] [14/15 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-07 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

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] 10+ messages in thread

* [Bug tree-optimization/111515] [14/15 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e
  2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
                   ` (7 preceding siblings ...)
  2024-03-07 21:53 ` law at gcc dot gnu.org
@ 2024-05-07  7:42 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-07  7:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|14.0                        |14.2

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 14.1 is being released, retargeting bugs to GCC 14.2.

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

end of thread, other threads:[~2024-05-07  7:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21  9:40 [Bug tree-optimization/111515] New: [14 Regression] Missed Dead Code Elimination since r14-4089-gd45ddc2c04e theodort at inf dot ethz.ch
2023-09-21 15:21 ` [Bug tree-optimization/111515] " rguenth at gcc dot gnu.org
2023-09-21 15:34 ` pinskia at gcc dot gnu.org
2023-09-21 15:55 ` theodort at inf dot ethz.ch
2023-10-16 11:26 ` theodort at inf dot ethz.ch
2023-12-13 13:46 ` rguenth at gcc dot gnu.org
2023-12-13 13:53 ` rguenth at gcc dot gnu.org
2023-12-14 15:54 ` rguenth at gcc dot gnu.org
2024-03-07 21:53 ` law at gcc dot gnu.org
2024-05-07  7:42 ` [Bug tree-optimization/111515] [14/15 " rguenth 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).