public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97077] New: Missed loop unrolling with range for over initializer list
@ 2020-09-16 21:01 bmburstein at gmail dot com
  2020-09-17  6:32 ` [Bug c++/97077] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: bmburstein at gmail dot com @ 2020-09-16 21:01 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97077
           Summary: Missed loop unrolling with range for over initializer
                    list
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bmburstein at gmail dot com
  Target Milestone: ---

In the following code:

#include <initializer_list>

int foo(int);

int main() {
    for(int i=0; i<5; ++i) {
        foo(i);
    }
    for(int i : {0,1,2,3,4}) {
        foo(i);
    }
}

The 2 loops should probably produce identical output. However, the output I see
here https://godbolt.org/z/z1We36 (using the trunk from 2020-09-15) shows that
the first loop (the traditional one) is indeed unrolled as expected, but the
second one is not.

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

* [Bug c++/97077] Missed loop unrolling with range for over initializer list
  2020-09-16 21:01 [Bug c++/97077] New: Missed loop unrolling with range for over initializer list bmburstein at gmail dot com
@ 2020-09-17  6:32 ` rguenth at gcc dot gnu.org
  2021-07-04 14:13 ` magiblot at hotmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-09-17  6:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-09-17

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
This is because the second loop has a load from {0,1,2,3,4} in its body and
thus
appears larger to unroll (we don't estimate those loads to go away - a missed
optimization).

  static const int C.0[5] = {0, 1, 2, 3, 4};
...
  <bb 4> [local count: 894749065]:
  # __for_begin_19 = PHI <__for_begin_10(5), &C.0(7)>
  # prephitmp_3 = PHI <pretmp_15(5), 0(7)>
  # ivtmp_14 = PHI <ivtmp_8(5), 5(7)>
  foo (prephitmp_3);
  __for_begin_10 = __for_begin_19 + 4;
  ivtmp_8 = ivtmp_14 - 1;
  if (ivtmp_8 == 0)
    goto <bb 6>; [20.00%]
  else
    goto <bb 5>; [80.00%]

  <bb 5> [local count: 715756304]:
  pretmp_15 = MEM[(const int *)__for_begin_19 + 4B];
  goto <bb 4>; [100.00%]


Estimating sizes for loop 2
 BB: 4, after_exit: 0
  size:   2 foo (prephitmp_3);
  size:   1 __for_begin_10 = __for_begin_19 + 4;
  size:   1 ivtmp_8 = ivtmp_14 - 1;
   Induction variable computation will be folded away.
  size:   2 if (ivtmp_8 == 0)
   Exit condition will be eliminated in peeled copies.
   Exit condition will be eliminated in last copy.
   Constant conditional.
 BB: 5, after_exit: 1
  size:   1 pretmp_15 = MEM[(const int *)__for_begin_19 + 4B];
size: 7-3, last_iteration: 6-3
  Loop size: 7
  Estimated size after unrolling: 12
Not unrolling loop 2: size would grow.
Not unrolling loop 2: contains call and code would grow.

at some point I had patches to improve this but they had negative ripple-down
effects so I reverted them.

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

* [Bug c++/97077] Missed loop unrolling with range for over initializer list
  2020-09-16 21:01 [Bug c++/97077] New: Missed loop unrolling with range for over initializer list bmburstein at gmail dot com
  2020-09-17  6:32 ` [Bug c++/97077] " rguenth at gcc dot gnu.org
@ 2021-07-04 14:13 ` magiblot at hotmail dot com
  2021-12-11  1:31 ` pinskia at gcc dot gnu.org
  2021-12-27  4:11 ` [Bug tree-optimization/97077] " pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: magiblot at hotmail dot com @ 2021-07-04 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

magiblot at hotmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |magiblot at hotmail dot com

--- Comment #2 from magiblot at hotmail dot com ---
According to Godbolt, from GCC 7.1 to GCC 7.5 the loop gets unrolled if the
initializer list has no more than four elements:

https://godbolt.org/z/YhKjn4qj3

So this is not only a missed optimization, but also a regression.

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

* [Bug c++/97077] Missed loop unrolling with range for over initializer list
  2020-09-16 21:01 [Bug c++/97077] New: Missed loop unrolling with range for over initializer list bmburstein at gmail dot com
  2020-09-17  6:32 ` [Bug c++/97077] " rguenth at gcc dot gnu.org
  2021-07-04 14:13 ` magiblot at hotmail dot com
@ 2021-12-11  1:31 ` pinskia at gcc dot gnu.org
  2021-12-27  4:11 ` [Bug tree-optimization/97077] " pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-11  1:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2020-09-17 00:00:00         |2021-12-10

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Maybe we need to realize the induction variable __for_begin is constant memory
so the read from it will be optimized away. That might help.

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

* [Bug tree-optimization/97077] Missed loop unrolling with range for over initializer list
  2020-09-16 21:01 [Bug c++/97077] New: Missed loop unrolling with range for over initializer list bmburstein at gmail dot com
                   ` (2 preceding siblings ...)
  2021-12-11  1:31 ` pinskia at gcc dot gnu.org
@ 2021-12-27  4:11 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-27  4:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 85747.

*** This bug has been marked as a duplicate of bug 85747 ***

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

end of thread, other threads:[~2021-12-27  4:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-16 21:01 [Bug c++/97077] New: Missed loop unrolling with range for over initializer list bmburstein at gmail dot com
2020-09-17  6:32 ` [Bug c++/97077] " rguenth at gcc dot gnu.org
2021-07-04 14:13 ` magiblot at hotmail dot com
2021-12-11  1:31 ` pinskia at gcc dot gnu.org
2021-12-27  4:11 ` [Bug tree-optimization/97077] " 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).