This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[g++] inheritance break basic optimisation



Hi,

I don't know if this is actually a bug, but it seems like to me.
Under some case, g++ don't remove multilication by 1 applied on integer
(with optimization turned on).

I noticed strange code generation with the following source.

BTW, I'm running gcc-2.95.1 (with -O3) on a Linux-i586.
======= test.cc =======

  class A{
  };

  class Int : public A {
  public:
    Int(int i) : _val(i) {}

    Int& operator*=(const Int& rvalue)  
      {
        _val *= rvalue._val;
        return *this;
      }

    const Int operator*(const Int& rvalue) const 
      {
        return Int(*this) *= rvalue;
      }

  private:  
    int _val;
  };


  Int disassemble_this(Int t)
  {
    return t *= 1;           // '*= 1' is not done, OK
  }                          //  t is just copied

  Int disassemble_this2(const Int t)
  {
    return t * 1;	     // '* 1' _is_ done, 
  }                          // there _is_ a 'imul' by a 1 loaded register

========================

The curious part is that when I remove the inheretance from A
both operations get optimized.  IMHO, there is something about the
copy constructor.  Besides if I add a copy constructor to A, say 

====
class Int : public A {
  // ...
  Int(const Int& i) : _val(i._val) {}   
  // ...
};
====

then, verythings get optimized.

So is this normal, does the default copy constructor forbid optimization
when there is inheritance ?

-- 
Alexandre 'Pollux' Duret-Lutz


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]