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
#include
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 )