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

Re: Loop oddity


I did a little more tweaking.  Putting something into the loop so it
isn't optimized away...


  int i;
  int j;
  j = 0;
  for (i=1000000;i;i--){j+=i;};


And compiling with -O3 funroll-loops gives this code

        xorl    %eax, %eax
        movl    $1000000, %edx

.L5:
        leal    -28(%eax,%edx,8), %eax
        leal    -92(%eax,%edx,8), %ecx
        leal    -70(%ecx,%edx,4), %eax
        subl    $20, %edx
        jne     .L5


Loop from 15 to 35: 4 real insns.
Continue at insn 26.
Biv 59: insn 29 const (-1)
Biv 59: verified
Biv 59: initialized at insn 14: initial value (1000000)
Cannot eliminate biv 59: biv used in insn 25.
Sorted combine statistics:



Loop from 15 to 35: 4 real insns.
Continue at insn 26.
Biv 59: insn 29 const (-1)
Biv 59: verified
Biv 59: initialized at insn 14: initial value (1000000)
Cannot eliminate biv 59: biv used in insn 25.
Sorted combine statistics:

Loop unrolling: 1000000 iterations.
Unrolling loop 20 times.
Biv 59 safe to split.



But if we change the loop to

 for (i=1000001;i;i--){j+=i;};

we get instead:


        movl    $1000001, %edx
        movl    %edx, %eax
        decl    %edx
        .p2align 4,,15
.L28:
        je      .L9
        leal    -6(%eax,%edx,4), %eax
        subl    $4, %edx
        jmp     .L28


Loop from 15 to 35: 4 real insns.
Continue at insn 26.
Biv 59: insn 29 const (-1)
Biv 59: verified
Biv 59: initialized at insn 14: initial value (1000001)
Cannot eliminate biv 59: biv used in insn 25.
Sorted combine statistics:



Loop from 15 to 35: 4 real insns.
Continue at insn 26.
Biv 59: insn 29 const (-1)
Biv 59: verified
Biv 59: initialized at insn 14: initial value (1000001)
Cannot eliminate biv 59: biv used in insn 25.
Sorted combine statistics:

Loop unrolling: 1000001 iterations.
Loop unrolling: No factors found.
Unrolling loop 8 times.
Preconditioning: Success, number of iterations known, 1000001.
Biv 59 safe to split.




I'm an assembly language programmer, not a compiler guy.  And I haven't
really looked at the unrolling code.

But in the second example, after unrolling the first iteration through
the loop the reamining code is identical to the first example.  Seems to
my naive low-level way of thinking a little strange that the compiler
generates such different code...

I'll add a to-do to see if I can understand why and maybe create a
patch.


-- 
Tom Crispin
Centaur Technology
512-493-8625
crispin@centtech.com


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