public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101653] New: Bad code generated when optimizing nested for loops
@ 2021-07-27 22:48 phd at phd dot re
  2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: phd at phd dot re @ 2021-07-27 22:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101653
           Summary: Bad code generated when optimizing nested for loops
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: phd at phd dot re
  Target Milestone: ---

Not sure if this was already reported.
Bad code is generated when optimizing nested for loops:

int main(void)
{
    unsigned a, b, c = 0; // issue only occurs for unsigned
    for (a = 0; a < 10; a++) // upper limit for variable a doesn't really
matter
    {
        for (b = 0; b < 2; b++) // bug only for b < 2, no issue for other
limits
        {
            c++; // first iteration: a == 0, b == 0, c == 1
            if (c < a) // c will *never* be smaller than a from now on
            {
                return 123; // and yet this is somehow reachable with -O1/2/3
            }
        }
    }
    return 0; // with -O0 this returns 0 properly
}

Affected gcc versions: 9.1+ (including 11.1 and trunk)
Not affected versions: 8.5-

Bug occurs for -O1, -O2, -O3 (return 123)
No issue for -O0 (properly returns 0)

Both C and C++ are affected.

Live demo:
https://godbolt.org/z/nanc9osq5

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

* [Bug tree-optimization/101653] [9/10/11/12 Regression] Bad code generated when optimizing nested for loops
  2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
@ 2021-07-27 22:55 ` pinskia at gcc dot gnu.org
  2021-07-27 23:01 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-27 22:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
            Summary|Bad code generated when     |[9/10/11/12 Regression] Bad
                   |optimizing nested for loops |code generated when
                   |                            |optimizing nested for loops
          Component|c                           |tree-optimization
   Target Milestone|---                         |9.5

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

* [Bug tree-optimization/101653] [9/10/11/12 Regression] Bad code generated when optimizing nested for loops
  2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
  2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
@ 2021-07-27 23:01 ` jakub at gcc dot gnu.org
  2021-07-27 23:09 ` pinskia at gcc dot gnu.org
  2021-07-27 23:13 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-07-27 23:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org
   Last reconfirmed|                            |2021-07-27
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Started with r9-4145-ga81e2c6240655f60a49c16e0d8bbfd2ba40bba51
So possibly related or the same as PR101508 or PR100740

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

* [Bug tree-optimization/101653] [9/10/11/12 Regression] Bad code generated when optimizing nested for loops
  2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
  2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
  2021-07-27 23:01 ` jakub at gcc dot gnu.org
@ 2021-07-27 23:09 ` pinskia at gcc dot gnu.org
  2021-07-27 23:13 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-27 23:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed,
ivcannon is turning the loops into:
  <bb 2> [local count: 79093198]:
  goto <bb 6>; [100.00%]

  <bb 4> [local count: 357878150]:
  a_8 = a_11 + 1;
  c_14 = c_3 + 2;
  goto <bb 6>; [100.00%]

  <bb 5> [local count: 79093198]:
  # _5 = PHI <123(6)>
  return _5;

  <bb 6> [local count: 375401868]:
  # c_3 = PHI <1(2), c_14(4)>
  # a_11 = PHI <0(2), a_8(4)>
  # ivtmp_20 = PHI <2(2), ivtmp_1(4)>
  c_6 = c_3 + 1;
  ivtmp_1 = ivtmp_20 - 1;
  if (ivtmp_1 == 0)
    goto <bb 5>; [5.50%]
  else
    goto <bb 4>; [94.50%]

Removing all of the if conditions on c < a and turning it into true ...

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

* [Bug tree-optimization/101653] [9/10/11/12 Regression] Bad code generated when optimizing nested for loops
  2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
                   ` (2 preceding siblings ...)
  2021-07-27 23:09 ` pinskia at gcc dot gnu.org
@ 2021-07-27 23:13 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-27 23:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So the loop of PR 100740 is same as this one so marking as a dup.

That is both have a double loop and the inner one has loop exit which is bound
by comparing of the induction variable that is counting how many times inside
the loop it has happened and comparing it against in the inner loop induction
variable.

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

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

end of thread, other threads:[~2021-07-27 23:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2021-07-27 23:01 ` jakub at gcc dot gnu.org
2021-07-27 23:09 ` pinskia at gcc dot gnu.org
2021-07-27 23:13 ` 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).