Consider the following class hierarchy:
struct StorageBase {
int i;
};
struct NonPod1 : StorageBase {
int get() { return i; }
NonPod1() {}
};
struct NonPod {
NonPod() { x[0].i = 0; x[1].i = 0; }
NonPod1& operator[](int i) { return *static_cast<NonPod1*>(&x[i]); }
StorageBase x[2];
};
where StorageBase exists to allow the casting in NonPod::operator[] and
at the same time avoid default initialization of NonPod::x (if we were
using NonPod1 x[2]). The following two functions should be optimized
to return 0, the first requires a tree-combiner to do so, the second
requires fold to deal with ((NonPod1 *)&a.x[0])->D.1745.i to see that
x and D.1745 have the same type so it can be folded to a.x[0].i.
int foo(void)
{
NonPod a;
return a[0].get();
}
int bar(void)
{
NonPod a;
return static_cast<NonPod1*>(&a.x[0])->i;
}
--
Summary: Does not fold access to base with cast to different
derived type
Product: gcc
Version: 4.2.0
Status: UNCONFIRMED
Keywords: missed-optimization, TREE
Severity: normal
Priority: P3
Component: middle-end
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: rguenth at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27181