This is the mail archive of the gcc@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]

EGCS bad on loop invariant optimization ?



Hi all!

Consider 

#include <cstddef>

class array {
public:
    array (size_t i) : sz(i), data (new double[i]) {}

    double operator[] (size_t i) const { return data[i]; }
    double& operator[] (size_t i) { return data[i]; }

private:
    size_t sz;
    double* data;
};

int main ()
{
    const size_t n = 1000;

    array a(n), b(n), c(n);

    for (size_t i=0; i<n; ++i) a[i] = b[i] + c[i];

    return 0;
}

EGCS-980315 with -O6 option generates, on a SPARCstation, this (poor) code:

main:
.LLFB1:
	!#PROLOGUE# 0
	save %sp,-136,%sp
.LLCFI0:
	!#PROLOGUE# 1
	mov 1000,%l2
	mov 0,%l0
	sethi %hi(8000),%l1
	or %l1,%lo(8000),%l1
	st %l2,[%fp-24]
	call __builtin_vec_new,0
	mov %l1,%o0
	st %o0,[%fp-20]
	st %l2,[%fp-32]
	call __builtin_vec_new,0
	mov %l1,%o0
	st %o0,[%fp-28]
	st %l2,[%fp-40]
	call __builtin_vec_new,0
	mov %l1,%o0
	st %o0,[%fp-36]
	mov 0,%o3
.LL15:
	ld [%fp-28],%o0			! b.data
	sll %o3,3,%o1
	ld [%fp-36],%o2			! c.data
	add %o3,1,%o3
	ldd [%o0+%o1],%f4
	cmp %o3,999
	ldd [%o2+%o1],%f2
	faddd %f4,%f2,%f4
	ld [%fp-20],%o0			! a.data
	bleu .LL15
	std %f4,[%o0+%o1]
	ret
	restore %g0,0,%o0



Can't EGCS see that a.data, b.data, c.data are loop invariants and
then move the loads out of the loop ? At least, one can expect
b.data and c.data to be subject of this kind of optimization since the
only way to access them is through the (leaf) *const* class member
fonction `array::operator[](size_t) const'. What am I missing ? 


-- Gaby
"One reason that life is complex is that it has a 
real part and imaginary part." -- Andrew Koenig


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