Optimisation tricks

Pascal A. Dupuis (dupuis@lei.ucl.ac.be)
Fri, 24 Sep 1999 09:43:20 +0200 (CEST)


Hello,

while looking at some compiled code, I noticed the following :
suppose we have a code snippet like :

void foo(int bar)
{
int i;
for (i = 0; i < bar; i++)
{
do_something();
}
}

Translation :
movl $0,-4(%ebp) ; initialise i
.L2:
movl -4(%ebp),%eax ; move i into %eax
cmpl %eax,-8(%ebp) ; compare %eax to bar
jg .L5 ; go to the do_something part
jmp .L3 ; exit the loop

if the do_something() doesn't use 'i' at all, it may be rewritten as :

register int i;
for (i = bar; i > 0; i--)
{
do_something();
}
translation :
movl -4(%ebp),%ebx ; initialise i (use %ebx)
.L2:
testl %ebx,%ebx ; "compare" i to 0
jg .L5 ; go to the do_something part
jmp .L3 ; exit the loop

this code was produced without optimisation.
First version : one loop contains
-two memory accesses
-one register used
-the jumps

Second version : one loop contains
-no superflous memory access
-one register used
-the jumps

With optimisation, the first version put i and bar in registers, but this
is not always possible.

I know this won't save a lot of cycles, except maybe in very tight loops.
In the first place this is the compiler work, but a small help is welcome.
Similar trick also apply to while loops.

Pascal Dupuis

-- 
«J'ai installe le package contenant m4 afin de configurer mon
 sendmail.cf.. et depuis, lorsque je demarre, je me retrouve avec un:
 Kernel Panic: VFS: unable to mount root fs on 08:01 !!! kesaco?!»
-+- LA in Guide du linuxien pervers : "Alors ça c'est pas de chance !" -+-

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/