public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2)
@ 2021-04-19  9:24 zhendong.su at inf dot ethz.ch
  2021-04-20  8:26 ` [Bug ipa/100145] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: zhendong.su at inf dot ethz.ch @ 2021-04-19  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100145
           Summary: missed optimization for dead code elimination at -O3
                    (vs. -O2)
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhendong.su at inf dot ethz.ch
  Target Milestone: ---

[639] % gcctk -v
Using built-in specs.
COLLECT_GCC=gcctk
COLLECT_LTO_WRAPPER=/local/suz-local/software/local/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-trunk/configure --disable-bootstrap
--prefix=/local/suz-local/software/local/gcc-trunk --enable-languages=c,c++
--disable-werror --enable-multilib --with-system-zlib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.1 20210419 (experimental) [master revision
b412ce8e961:756887f4cee:d64720a07f611c55e8c815c775a852d650a2e738] (GCC)
[640] %
[640] % gcctk -O2 -S -o O2.s small.c
[641] % gcctk -O3 -S -o O3.s small.c
[642] %
[642] % wc O2.s O3.s
  77  164 1080 O2.s
  88  187 1234 O3.s
 165  351 2314 total
[643] %
[643] % grep foo O2.s
[644] % grep foo O3.s
        call    foo
[645] %
[645] % cat small.c
int b, c, d, e;
void foo();
int a(int f, int p) { return (p && f) || (f && p < 0 && p + 1) ? 0 : f + p; }
static int g() {
  int *h = &c;
  *h = 0;
  if (b) {
    int *i = &e;
    while (1) {
      *h = a(0, i != 0);
      if (*h)
        return 0;
    }
  }
  return 0;
}
int main() {
  int *k = &c;
  g();
  d = 1;
  if (*k > 100)
    foo();
  return 0;
}

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

* [Bug ipa/100145] missed optimization for dead code elimination at -O3 (vs. -O2)
  2021-04-19  9:24 [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2) zhendong.su at inf dot ethz.ch
@ 2021-04-20  8:26 ` rguenth at gcc dot gnu.org
  2021-04-20  8:50 ` [Bug gcov-profile/100145] " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-20  8:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|tree-optimization           |ipa
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-04-20
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org,
                   |                            |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
            Version|unknown                     |11.0

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
At -O2 we optimize things in thread3, at -O3 we have a PHI less there and thus
do no backwards threading which is because 'c' wasn't PREd for some reason
(-fno-tree-vectorize or -fno-tree-partial-pre do not help)

 int main ()
 {
-  int D.2001;
-  int b.1_7;
-  int prephitmp_16;
+  int _1;
+  int b.1_6;

   <bb 2> [local count: 1073741824]:
   c = 0;
-  b.1_7 = b;
-  if (b.1_7 != 0)
+  b.1_6 = b;
+  if (b.1_6 != 0)
     goto <bb 3>; [34.00%]
   else
     goto <bb 4>; [66.00%]

-  <bb 3> [local count: 3318838410]:
+  <bb 3> [local count: 365072224]:
   c = 1;

   <bb 4> [local count: 1073741824]:
-  # prephitmp_16 = PHI <0(2), 1(3)>
   d = 1;
-  if (prephitmp_16 > 100)
+  _1 = c;
+  if (_1 > 100)
     goto <bb 5>; [33.00%]
   else
     goto <bb 6>; [67.00%]


the issue seems to be the guessed profile (but BB counts are the same!):

+Skipping partial redundancy for expression {mem_ref<0B>,addr_expr<&c>}@.MEM_7
(
0001), no redundancy on to be optimized for speed edge

so that leaves the "global" hot count we IPA compute somehow?  Honza?

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

* [Bug gcov-profile/100145] missed optimization for dead code elimination at -O3 (vs. -O2)
  2021-04-19  9:24 [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2) zhendong.su at inf dot ethz.ch
  2021-04-20  8:26 ` [Bug ipa/100145] " rguenth at gcc dot gnu.org
@ 2021-04-20  8:50 ` rguenth at gcc dot gnu.org
  2021-05-20 13:57 ` rguenth at gcc dot gnu.org
  2023-06-09 17:20 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-20  8:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|ipa                         |gcov-profile

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
So we get optimize_edge_for_size_p () returning OPTIMIZE_SIZE_BALANCED and
thus optimize_edge_for_speed_p which is

340     bool
341     optimize_edge_for_speed_p (edge e)
342     {
343       return !optimize_edge_for_size_p (e);
344     }

return false.  And that's likely because this is 'main'.  At -O2 BB counts
are broken:

  <bb 2> [local count: 1073741824]:
  c = 0;
  b.1_7 = b;
  if (b.1_7 != 0)
    goto <bb 3>; [34.00%]
  else
    goto <bb 7>; [66.00%]

  <bb 7> [local count: 708669600]:
  goto <bb 4>; [100.00%]

  <bb 3> [local count: 3318838410]:
  c = 1;

  <bb 4> [local count: 1073741824]:
  d = 1;

here 3->4 artificially appears 'hot'.  The bogus counts appear during IPA
inlining of g() into main.

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

* [Bug gcov-profile/100145] missed optimization for dead code elimination at -O3 (vs. -O2)
  2021-04-19  9:24 [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2) zhendong.su at inf dot ethz.ch
  2021-04-20  8:26 ` [Bug ipa/100145] " rguenth at gcc dot gnu.org
  2021-04-20  8:50 ` [Bug gcov-profile/100145] " rguenth at gcc dot gnu.org
@ 2021-05-20 13:57 ` rguenth at gcc dot gnu.org
  2023-06-09 17:20 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-05-20 13:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note after inlining we still have a backedge into BB 3:

  <bb 2> [local count: 1073741824]:
  c = 0;
  b.1_7 = b;
  if (b.1_7 != 0)
    goto <bb 3>; [34.00%]
  else
    goto <bb 7>; [66.00%]

  <bb 3> [local count: 3318838410]:
  _9 = 1;
  _10 = 0;
  _11 = _9 & _10;
  if (_11 != 0)
    goto <bb 6>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 4> [local count: 1659419208]:
  _12 = 0;
  _13 = _10 & _12;
  if (_13 != 0)
    goto <bb 6>; [50.00%]
  else
    goto <bb 5>; [50.00%]

  <bb 5> [local count: 829709603]:
  iftmp.0_14 = 1;

  <bb 6> [local count: 3318838410]:
  # iftmp.0_15 = PHI <0(3), 0(4), iftmp.0_14(5)>
  _19 = iftmp.0_15;
  _8 = _19;
  c = _8;
  if (_8 != 0)
    goto <bb 7>; [11.00%]
  else
    goto <bb 3>; [89.00%]

  <bb 7> [local count: 1073741824]:

but that's quickly elided by CCP after inlining producing

  <bb 2> [local count: 1073741824]:
  c = 0;
  b.1_7 = b;
  if (b.1_7 != 0)
    goto <bb 3>; [34.00%]
  else
    goto <bb 7>; [66.00%]

  <bb 3> [local count: 3318838410]:
  if (0 != 0)
    goto <bb 6>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 4> [local count: 1659419208]:
  if (0 != 0)
    goto <bb 6>; [50.00%]
  else
    goto <bb 5>; [50.00%]

  <bb 5> [local count: 829709603]:

  <bb 6> [local count: 3318838410]:
  c = 1;
  if (1 != 0)
    goto <bb 7>; [11.00%]
  else
    goto <bb 3>; [89.00%]

  <bb 7> [local count: 1073741824]:

and the CFG cleanup ends up with the following:

  <bb 2> [local count: 1073741824]:
  c = 0;
  b.1_7 = b;
  if (b.1_7 != 0)
    goto <bb 3>; [34.00%]
  else
    goto <bb 4>; [66.00%]

  <bb 3> [local count: 3318838410]:
  c = 1;

  <bb 4> [local count: 1073741824]:
  d = 1;
  _1 = c;
  if (_1 > 100)
    goto <bb 5>; [33.00%]
  else
    goto <bb 6>; [67.00%]

  <bb 5> [local count: 354334800]:
  foo ();

  <bb 6> [local count: 1073741824]:
  return 0;

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

* [Bug gcov-profile/100145] missed optimization for dead code elimination at -O3 (vs. -O2)
  2021-04-19  9:24 [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2) zhendong.su at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2021-05-20 13:57 ` rguenth at gcc dot gnu.org
@ 2023-06-09 17:20 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-09 17:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #2)
> So we get optimize_edge_for_size_p () returning OPTIMIZE_SIZE_BALANCED and
> thus optimize_edge_for_speed_p which is
> 
> 340     bool
> 341     optimize_edge_for_speed_p (edge e)
> 342     {
> 343       return !optimize_edge_for_size_p (e);
> 344     }
> 
> return false.  And that's likely because this is 'main'. 

Yes it is.
If we do s/main/c123/
And then add:
```
int main()
{
        return c123();
}
```
The -O3 issue goes away.

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

end of thread, other threads:[~2023-06-09 17:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-19  9:24 [Bug tree-optimization/100145] New: missed optimization for dead code elimination at -O3 (vs. -O2) zhendong.su at inf dot ethz.ch
2021-04-20  8:26 ` [Bug ipa/100145] " rguenth at gcc dot gnu.org
2021-04-20  8:50 ` [Bug gcov-profile/100145] " rguenth at gcc dot gnu.org
2021-05-20 13:57 ` rguenth at gcc dot gnu.org
2023-06-09 17:20 ` pinskia 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).