Curious about loop optimization C++ - assembly



Hi,

Out of curiousity, I sometimes look at the produced assembly after compilation in release mode.

What you often see, is that CPP, always fully addresses registers to copy values from a to b...

While stosb,stosw, stosd etc and the same for movs[x] are one statement, and internally use registers ESI and EDI (source, destination) to copy data.

This seems (imho) more efficient, however, CPP never uses this construct...
it always uses a lot more instructions.

imagine this loop (I simplified the idea, of course, memcpy would be normally used)


DWORD anArray [10000];

// copy array while skipping uneven element positions

for (int mycounter=5000; mycounter != 0; mycounter--, element+=2)
anArray[element] = somesource[element];


could be optimized to

setup source and destination

MOV EDI, [anArray]
MOV ESI, [somesource]
MOV ECX, myCounter
DEC ECX
CLD // forward copy

mylabel:
MOVSD <--- actual loop and copy instruction
LOOP mylabel <-- decrement ECX until ECX == 0


Q: is the mentioned construct, simply not so efficient or is there a reason the C++ compiler team decided not to try to optimize to this level?



.



Relevant Pages

  • Re: CIL question
    ... > compilation as execution methods. ... They need different representations for 32 ... intermediate representation with a infinite number of registers (e.g. ...
    (comp.compilers)
  • Re: question about printf
    ... The above program, upon compilation, ... passing parameters from right to left seems to be common. ... again depends on the underlying CPU architecture. ... registers for parameter passing; the compiler doesn't even use stack! ...
    (comp.lang.c)
  • Re: x86-64 and calling conventions
    ... registers rather than real ones. ... function it will call at the time of compilation perhaps the caller ... footer glue code - possibly the more complex of the two glue code ... Duplicate functions - especially if the function ...
    (comp.compilers)
  • Re: Trying to convert old modules to newer kernels
    ... compilation. ... parameter passing in REGISTERS! ... they've done it to the entire kernel! ... reconfigure to get parameter passing put back like it was? ...
    (Linux-Kernel)
  • Re: template export in dll
    ... separate compilation model (when template is declared with `export' keyword in .H file and implemented in separate .CPP file). ... there is the only one compiler in the world that supports separate compilation model - Comeau C/C++ Compiler front-end. ...
    (microsoft.public.vc.stl)