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]

Re: Default constructors & arrays


Joern Rennecke writes:
> This is basically a loop unroll problem.  If you completely unroll an
> empty loop, it goes away.

It seems not to be that simple. It is true, it works in case of 
for ( int i = 0; i < 10; ++i );

I have narrowed the problem a bit more. In cases when no default ctor
(and/or dtor) is defined no loops are generated!

struct Complex { 
  float re, im; 
};


int main() {
  Complex c[10]; 

  c[1].re = 1;

  return 0;
}

-->

main:
.LFB1:
        pushl %ebp
.LCFI0:
        movl %esp,%ebp
.LCFI1:
        subl $80,%esp
.LCFI2:
        movl $1065353216,-72(%ebp)
        movl %ebp,%esp
        xorl %eax,%eax
        popl %ebp
        ret


But If I define default (de)constructor then it is recognized that it is
empty, but loop remains (regardless of the size of the array):

struct Complex { 
  float re, im; 

   Complex() {}
  ~Complex() {}
};

-->

main:
.LFB1:
        pushl %ebp
.LCFI0:
        movl %esp,%ebp
.LCFI1:
        subl $80,%esp
.LCFI2:
        movl $9,%eax
        .align 4
.L14:
        subl $1,%eax
        jnc .L14
        movl $1065353216,-72(%ebp)
        leal -80(%ebp),%edx
        movl %ebp,%eax
        cmpl %ebp,%edx
        je .L43
        .align 4
.L37:
        addl $-8,%eax
        cmpl %eax,%edx
        jne .L37
.L43:
        xorl %eax,%eax
        movl %ebp,%esp
        popl %ebp
        ret


> The tricky part is to determine when a loop does not terminate, or might
> not terminate.

I am not sure loop optimizations are working either, simple cases like

int main() {

  int j = 10;
  int k;
  for ( int i = 0; i < 10; i++ ) k = j;
 
  return k;
}

generate:


main:
.LFB1:
        pushl %ebp
.LCFI0:
        movl %esp,%ebp
.LCFI1:
        movl $10,%eax
        movl $9,%edx
        .align 4
.L5:
        decl %edx
        jns .L5
        movl %ebp,%esp
        popl %ebp
        ret

k = j is invariant to loop variable and can be pulled out of the loop!

True, this happens with unroll-loops option:

main:
.LFB1:
        pushl %ebp
.LCFI0:
        movl %esp,%ebp
.LCFI1:
        movl $10,%eax
        movl %ebp,%esp
        popl %ebp
        ret
.LFE1:
.Lfe1:

but ONLY if loop is a short one .. (and probably only if range is
known in advance).

In reality, loop optimizations like pulling invariants out of the loop 
and removing empty loops should happen independently of unrolling
loops.

Igor


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