public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization
@ 2023-09-21 11:27 aegges at web dot de
  2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: aegges at web dot de @ 2023-09-21 11:27 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111517
           Summary: Optimization -O1 removes necessary loop for
                    initialization
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aegges at web dot de
  Target Milestone: ---

We have found a regression in gcc 12 (and later) in comparison to gcc 11 (and
clang) which occurs if optimization (at least -O1) is enabled.
In the following C++ code the member variables a1 and e1 are initialized in a
loop in a init() method which is called in the constructor.  

#include <iostream>
#include <array>

class TestClass
{
public:
   TestClass()
   { 
       for(int i=0; i<20; i++)
       {
          a1[i] = i;
       }

       init();
    }

    void init();
    unsigned int a1[20];
    unsigned char e1[20][20];
};

void TestClass::init()
{
        for (int b1 = 0; b1 < 20; b1++)
        {
                for (int b2 = 0; b2 < 20; b2++)
                {
                        e1[b1][b2] = a1[b2];
                }
        }
}

TestClass tmp;

int main()
{
    for(int i=0; i < 20 ; i++)
        std::cout << std::to_string(tmp.e1[i][i]) << " ";
}
expected output: (gcc <= 11 or gcc >=12 w/o optimization)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

actual output: 8gcc >=12 with optimization)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 


I have used godbolt to verify the different behavior (see
https://godbolt.org/z/MnoPE8Yfr )

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

end of thread, other threads:[~2023-09-22  8:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
2023-09-21 16:19 ` [Bug tree-optimization/111517] " pinskia at gcc dot gnu.org
2023-09-21 18:34 ` theodort at inf dot ethz.ch
2023-09-21 18:46 ` [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba pinskia at gcc dot gnu.org
2023-09-21 18:50 ` pinskia at gcc dot gnu.org
2023-09-22  5:46 ` rguenth at gcc dot gnu.org
2023-09-22  8:04 ` aegges at web dot de

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).