------- Comment #1 from pinskia at gmail dot com  2008-09-30 18:11 -------
Subject: Re:   New: C++ over-eager optimization when working with a pointer



Sent from my iPhone

On Sep 30, 2008, at 10:26 AM, "jrenggli at gmail dot com"
<[EMAIL PROTECTED] 
 > wrote:

> The following code results in an error when optimized using -O2 or - 
> O3 flag,
> but works fine with -O1. I've also been able to "confuse" the  
> optimizer by
> adding some code just after or before the line "iX += pA2->iX;" but  
> I'm not
> sure how safe that method is.
>
> ---------------------- Full code starts here ----------------------
>
> #include <iostream>
>
> class A
> {
>  private:
>    int32_t iX;
>
>  public:
>    explicit A(int x) : iX(x) {}
>
>  public:
>    A& operator*=(const A& a);
>
>  public:
>                int32_t getX() const { return iX; }
> };
>
> ////////////////////////////////////////////////////////////////
>
> A& A::operator*=(const A& B)
> {
>  const A* pA2 = &B;
>
>  int iBothSigns = 0;
>  if (iX >= 0 && pA2->iX >= 0)
>  {
>    iBothSigns = 1;
>  }
>  else if (iX < 0 && pA2->iX < 0)
>  {
>    iBothSigns = -1;
>  }
>
>  iX += pA2->iX;
>
>  if (iX >= 0 && iBothSigns == -1) // Expected


Signed interger overflow is undefined.



>
>  {
>                std::cout << "OK" << std::endl;
>    return *this;
>  }
>  else if (iX < 0 && iBothSigns == 1) // Not expected
>  {
>                std::cout << "Unexpected" << std::endl;
>    return *this;
>  }
>        std::cout << "Error" << std::endl;
>
>  return *this;
> }
>
> ////////////////////////////////////////////////////////////////
>
> int main()
> {
>        A n1(-2147483648);
>        A n2(-2147483648);
>
>        std::cout << "1: expected -2147483648, got " << n1.getX() <<  
> std::endl;
>        std::cout << "2: expected -2147483648, got " << n2.getX() <<  
> std::endl;
>        n1 *= n2;
>        std::cout << "3: expected previous line to be OK" << std::endl;
>        return 0;
> }
>
> ---------------------- end of code ----------------------
>
> ---------------------- compiler output ----------------------
>
> g++ -o bugreport.o -c -v -Wall -O3 -pedantic -ansi -save-temps  
> bugreport.cpp
> Using built-in specs.
> Target: i486-linux-gnu
> Configured with: ../src/configure -v
> --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
> --enable-shared --with-system-zlib --libexecdir=/usr/lib
> --without-included-gettext --enable-threads=posix --enable-nls
> --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
> --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc -- 
> enable-mpfr
> --enable-targets=all --enable-checking=release --build=i486-linux-gnu
> --host=i486-linux-gnu --target=i486-linux-gnu
> Thread model: posix
> gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
> /usr/lib/gcc/i486-linux-gnu/4.2.3/cc1plus -E -quiet -v -D_GNU_SOURCE
> bugreport.cpp -mtune=generic -ansi -Wall -pedantic -O3 -fpch- 
> preprocess -o
> bugreport.ii
> ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
> ignoring nonexistent directory
> "/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../i486-linux-gnu/include"
> ignoring nonexistent directory "/usr/include/i486-linux-gnu"
> #include "..." search starts here:
> #include <...> search starts here:
> /usr/include/c++/4.2
> /usr/include/c++/4.2/i486-linux-gnu
> /usr/include/c++/4.2/backward
> /usr/local/include
> /usr/lib/gcc/i486-linux-gnu/4.2.3/include
> /usr/include
> End of search list.
> /usr/lib/gcc/i486-linux-gnu/4.2.3/cc1plus -fpreprocessed  
> bugreport.ii -quiet
> -dumpbase bugreport.cpp -mtune=generic -ansi -auxbase-strip  
> bugreport.o -O3
> -Wall -pedantic -ansi -version -fstack-protector -fstack-protector -o
> bugreport.s
> GNU C++ version 4.2.3 (Ubuntu 4.2.3-2ubuntu7) (i486-linux-gnu)
>        compiled by GNU C version 4.2.3 (Ubuntu 4.2.3-2ubuntu7).
> GGC heuristics: --param ggc-min-expand=99 --param ggc-min- 
> heapsize=129349
> Compiler executable checksum: 9cf91ba46d80e564052e4fbab0d6561b
> bugreport.cpp:55: warning: this decimal constant is unsigned only in  
> ISO C90
> bugreport.cpp:56: warning: this decimal constant is unsigned only in  
> ISO C90
> as --traditional-format -V -Qy -o bugreport.o bugreport.s
> GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU  
> Binutils
> for Ubuntu) 2.18.0.20080103
> g++ -o bugreport bugreport.o
>
>
> -- 
>           Summary: C++ over-eager optimization when working with a  
> pointer
>           Product: gcc
>           Version: 4.2.3
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c++
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: jrenggli at gmail dot com
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37685
>


-- 


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

Reply via email to