[Bug c++/51329] New: O3 optimizes away a loop

andrew.mcpherson at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Nov 28 00:46:00 GMT 2011


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51329

             Bug #: 51329
           Summary: O3 optimizes away a loop
    Classification: Unclassified
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: andrew.mcpherson@gmail.com


Created attachment 25923
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25923
preprocessor output

The following code generates the wrong exit code when compiled as follows:

/usr/bin/g++ -O3 -o bugexample bugexample.cpp

when compiled with O2, it works.  If a printf is placed in the right place it
also works.

Apologies to the lack of a simpler example, the bug disappears with small
changes.

g++ -v
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~123/src/configure --disable-checking
--enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib
--build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10-
--host=x86_64-apple-darwin10 --target=i686-apple-darwin10
--with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)


#include <vector>
int test(std::vector<char>& a, int& b)
{
    std::vector<int> z;
    z.push_back(10);
    z.push_back(10);

    int d = (int)a.size();

    int x = 1;
    for (int j = 0; j < 2; j++)
    {
        int c = j - 1;

        for (int i = 0; i < d; i++) 
        {
            if (j == 0)
            {
            }
            else if (i == 0)
            {
            }
            else
            {
                if (a[j] == a[i - 1])
                {
                    b = c + 1;
                    x = 2;
                }
                z[i] = 1;
            }
        }
    }

    return x;
}


int main(int argc, char* argv[])
{
    std::vector<char> a;
    a.push_back('a');
    a.push_back('a');
    int b = 1;
    return test(a,b);
}



More information about the Gcc-bugs mailing list