public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/44186]  New: Wrong code generated with -O2 and above
@ 2010-05-18  3:37 eyakubovich at gmail dot com
  2010-05-18  3:50 ` [Bug c++/44186] " pinskia at gmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: eyakubovich at gmail dot com @ 2010-05-18  3:37 UTC (permalink / raw)
  To: gcc-bugs

This is a stripped down code from proposed Boost.Move library. Asserts don't
fire with -O0 and -O1 but do with -O2 and -O3

#include <assert.h>

template <class T>
class rv : public T
{
   rv();
   ~rv();
   rv(rv const&);
   void operator=(rv const&);
};

template <class T>
rv<T>& move(T& x)
{
   return *static_cast<rv<T>* >(&x);
}

//A movable class
class movable
{
   movable(movable &);
   movable& operator=(movable&);
public:
   operator rv<movable>&()
   {  return *reinterpret_cast< rv<movable>* >(this);  }
   operator const rv<movable>&() const
   {  return *reinterpret_cast<const rv<movable>* >(this);  }

private:
   int value_;

public:
   movable() : value_(1){}

   //Move constructor and assignment
   movable(rv<movable>& m)
   {  value_ = m.value_;   m.value_ = 0;  }

   movable & operator=(rv<movable>& m)
   {  value_ = m.value_;   m.value_ = 0;  return *this;  }

   bool moved() const //Observer
   {  return value_ == 0; }
};

movable function(movable m)
{
   return movable(move(m));
}

int main()
{
   {
      movable m;
      movable m2(move(m));
      assert(m.moved());
      assert(!m2.moved());
   }

   {
      movable m;
      movable m3(function(movable(move(m))));
      assert(m.moved());
      assert(!m3.moved());
   }

   {
      movable m;
      movable m4(function(move(m)));
      assert(m.moved());
      assert(!m4.moved());
   }

   return 0;
}


-- 
           Summary: Wrong code generated with -O2 and above
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: eyakubovich at gmail dot com


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


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

* Re: [Bug c++/44186]  New: Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
  2010-05-18  3:50 ` [Bug c++/44186] " pinskia at gmail dot com
@ 2010-05-18  3:50 ` Andrew Pinski
  2010-05-18  6:31 ` [Bug c++/44186] " pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Andrew Pinski @ 2010-05-18  3:50 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs



Sent from my iPhone

On May 17, 2010, at 8:37 PM, "eyakubovich at gmail dot com" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

> This is a stripped down code from proposed Boost.Move library.  
> Asserts don't
> fire with -O0 and -O1 but do with -O2 and -O3
>
> #include <assert.h>
>
> template <class T>
> class rv : public T
> {
>   rv();
>   ~rv();
>   rv(rv const&);
>   void operator=(rv const&);
> };
>
> template <class T>
> rv<T>& move(T& x)
> {
>   return *static_cast<rv<T>* >(&x);
> }
>
> //A movable class
> class movable
> {
>   movable(movable &);
>   movable& operator=(movable&);
> public:
>   operator rv<movable>&()
>   {  return *reinterpret_cast< rv<movable>* >(this);  }
>   operator const rv<movable>&() const
>   {  return *reinterpret_cast<const rv<movable>* >(this);  }
>
> private:
>   int value_;
>
> public:
>   movable() : value_(1){}
>
>   //Move constructor and assignment
>   movable(rv<movable>& m)
>   {  value_ = m.value_;   m.value_ = 0;  }
>
>   movable & operator=(rv<movable>& m)
>   {  value_ = m.value_;   m.value_ = 0;  return *this;  }
>
>   bool moved() const //Observer
>   {  return value_ == 0; }
> };
>
> movable function(movable m)
> {
>   return movable(move(m));
> }
>
> int main()
> {
>   {
>      movable m;
>      movable m2(move(m));

The above code cause aliasing violations because you acess a movable  
object via a rv<movable> (from now on just refered to as rv for short)  
one. So you have a type rv who's base type is movable. note both rv  
and movable are non pods which I think removes the case for upcasting  
and then accessing the first member.


>      assert(m.moved());
>      assert(!m2.moved());
>   }
>
>   {
>      movable m;
>      movable m3(function(movable(move(m))));
>      assert(m.moved());
>      assert(!m3.moved());
>   }
>
>   {
>      movable m;
>      movable m4(function(move(m)));
>      assert(m.moved());
>      assert(!m4.moved());
>   }
>
>   return 0;
> }
>
>
> -- 
>           Summary: Wrong code generated with -O2 and above
>           Product: gcc
>           Version: 4.4.4
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c++
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: eyakubovich at gmail dot com
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44186
>


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

* [Bug c++/44186] Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
@ 2010-05-18  3:50 ` pinskia at gmail dot com
  2010-05-18  3:50 ` [Bug c++/44186] New: " Andrew Pinski
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gmail dot com @ 2010-05-18  3:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gmail dot com  2010-05-18 03:50 -------
Subject: Re:   New: Wrong code generated with -O2 and above



Sent from my iPhone

On May 17, 2010, at 8:37 PM, "eyakubovich at gmail dot com"
<gcc-bugzilla@gcc.gnu.org 
 > wrote:

> This is a stripped down code from proposed Boost.Move library.  
> Asserts don't
> fire with -O0 and -O1 but do with -O2 and -O3
>
> #include <assert.h>
>
> template <class T>
> class rv : public T
> {
>   rv();
>   ~rv();
>   rv(rv const&);
>   void operator=(rv const&);
> };
>
> template <class T>
> rv<T>& move(T& x)
> {
>   return *static_cast<rv<T>* >(&x);
> }
>
> //A movable class
> class movable
> {
>   movable(movable &);
>   movable& operator=(movable&);
> public:
>   operator rv<movable>&()
>   {  return *reinterpret_cast< rv<movable>* >(this);  }
>   operator const rv<movable>&() const
>   {  return *reinterpret_cast<const rv<movable>* >(this);  }
>
> private:
>   int value_;
>
> public:
>   movable() : value_(1){}
>
>   //Move constructor and assignment
>   movable(rv<movable>& m)
>   {  value_ = m.value_;   m.value_ = 0;  }
>
>   movable & operator=(rv<movable>& m)
>   {  value_ = m.value_;   m.value_ = 0;  return *this;  }
>
>   bool moved() const //Observer
>   {  return value_ == 0; }
> };
>
> movable function(movable m)
> {
>   return movable(move(m));
> }
>
> int main()
> {
>   {
>      movable m;
>      movable m2(move(m));

The above code cause aliasing violations because you acess a movable  
object via a rv<movable> (from now on just refered to as rv for short)  
one. So you have a type rv who's base type is movable. note both rv  
and movable are non pods which I think removes the case for upcasting  
and then accessing the first member.


>      assert(m.moved());
>      assert(!m2.moved());
>   }
>
>   {
>      movable m;
>      movable m3(function(movable(move(m))));
>      assert(m.moved());
>      assert(!m3.moved());
>   }
>
>   {
>      movable m;
>      movable m4(function(move(m)));
>      assert(m.moved());
>      assert(!m4.moved());
>   }
>
>   return 0;
> }
>
>
> -- 
>           Summary: Wrong code generated with -O2 and above
>           Product: gcc
>           Version: 4.4.4
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c++
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: eyakubovich at gmail dot com
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44186
>


-- 


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


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

* [Bug c++/44186] Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
  2010-05-18  3:50 ` [Bug c++/44186] " pinskia at gmail dot com
  2010-05-18  3:50 ` [Bug c++/44186] New: " Andrew Pinski
@ 2010-05-18  6:31 ` pinskia at gcc dot gnu dot org
  2010-05-18  9:26 ` paolo dot carlini at oracle dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-05-18  6:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2010-05-18 06:31 -------
*** Bug 44187 has been marked as a duplicate of this bug. ***


-- 


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


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

* [Bug c++/44186] Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
                   ` (2 preceding siblings ...)
  2010-05-18  6:31 ` [Bug c++/44186] " pinskia at gcc dot gnu dot org
@ 2010-05-18  9:26 ` paolo dot carlini at oracle dot com
  2010-05-18  9:53 ` rguenth at gcc dot gnu dot org
  2010-05-18 10:08 ` paolo dot carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-05-18  9:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from paolo dot carlini at oracle dot com  2010-05-18 09:25 -------
Note however, that both the warning and the miscompilation do not happen with
current 4_5-branch and mainline...


-- 


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


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

* [Bug c++/44186] Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
                   ` (3 preceding siblings ...)
  2010-05-18  9:26 ` paolo dot carlini at oracle dot com
@ 2010-05-18  9:53 ` rguenth at gcc dot gnu dot org
  2010-05-18 10:08 ` paolo dot carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-05-18  9:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2010-05-18 09:53 -------
(In reply to comment #3)
> Note however, that both the warning and the miscompilation do not happen with
> current 4_5-branch and mainline...

Which is because we see the must-alias and punt.  After all this just
invokes undefined behavior which includes "works" and "does not work".


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c++/44186] Wrong code generated with -O2 and above
  2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
                   ` (4 preceding siblings ...)
  2010-05-18  9:53 ` rguenth at gcc dot gnu dot org
@ 2010-05-18 10:08 ` paolo dot carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-05-18 10:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from paolo dot carlini at oracle dot com  2010-05-18 10:08 -------
Ok ;)


-- 


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


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

end of thread, other threads:[~2010-05-18 10:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-18  3:37 [Bug c++/44186] New: Wrong code generated with -O2 and above eyakubovich at gmail dot com
2010-05-18  3:50 ` [Bug c++/44186] " pinskia at gmail dot com
2010-05-18  3:50 ` [Bug c++/44186] New: " Andrew Pinski
2010-05-18  6:31 ` [Bug c++/44186] " pinskia at gcc dot gnu dot org
2010-05-18  9:26 ` paolo dot carlini at oracle dot com
2010-05-18  9:53 ` rguenth at gcc dot gnu dot org
2010-05-18 10:08 ` paolo dot carlini at oracle dot com

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