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]
Other format: [Raw text]

[Bug tree-optimization/22486] New: Upcasts are not folded away


In C++, a common idiom is to use upcasting to traverse the inheritance
hierarchy "backwards".  F.i. the following simple code

template <class T>
struct Base {
  const T& unwrap() const { return *(static_cast<const T *>(this)); }
  int data[2];
};

struct Object;
struct Object : Base<Object>
{
  int get(int i) const { return data[i]; }
};

int foo(const Base<Object>& o)
{
  return o.unwrap().get(0) + o.unwrap().get(1);
}


which currently is (not) optimized to

;; Function int foo(const Base<Object>&) (_Z3fooRK4BaseI6ObjectE)

int foo(const Base<Object>&) (o)
{
  struct Object & D.1752;

<bb 0>:
  D.1752 = (struct Object *) o;
  return D.1752->D.1743.data[0] + D.1752->D.1743.data[1];

}

should not have the cast and just access o->data[0] and o->data[1]
directly.

-- 
           Summary: Upcasts are not folded away
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P2
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rguenth at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org


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


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