I have a bit of a disagreement with the optimization toward memset()
calls. In one of my libraries, libteklti, I have a function named
ucharempty(), which frees a uchar_t (unique character structure) from
memory. If the user elects to have the memory erased prior to calling
free(), memset() is supposed to reset the memory about to be freed.
Source code for the libteklti project is on SourceForge at:
http://cvs.sourceforge.net/viewcvs.py/libteklti/
Anyway, in gcc 4.0.1, I have noticed that the optimization for memset()
calls has a few extra instructions in the generated assembly that do not
need to be inserted.
If you look closely, you can see that %edi can be automatically loaded
directly without problems, and that (%eax) can be directly loaded into
(%esp). Is this behavior intentional (for bugs I don't know about in
earlier processors) or could this optimization be fixed to use less
instructions?
The following disassembly output is an example for the line:
0x08049172 <ucharempty+53>: mov 0x8(%ebp),%eax
0x08049175 <ucharempty+56>: mov 0x4(%eax),%edx
0x08049178 <ucharempty+59>: mov 0x8(%ebp),%eax
0x0804917b <ucharempty+62>: mov (%eax),%ebx
0x0804917d <ucharempty+64>: mov %ebx,%edi
0x0804917f <ucharempty+66>: cld
0x08049180 <ucharempty+67>: mov %edx,%ecx
0x08049182 <ucharempty+69>: mov $0x0,%al
0x08049184 <ucharempty+71>: repz stos %al,%es:(%edi)
0x08049186 <ucharempty+73>: mov %ebx,%eax
0x08049188 <ucharempty+75>: mov %eax,(%esp)
0x0804918b <ucharempty+78>: call 0x8048c08 <free>
associated C code:
#ifdef TEKLTI_ENFORCE_PRIVACY
free(
memset(
uchrtofree->uchar_t_ascii,
'\0',
sizeof(char) * uchrtofree->uchar_t_asciilen
)
);
#else /* not USE_386_ASM_ENFORCE_PRIVACY */
For reference:
Dump of assembler code for function ucharempty, for comparison:
From uchar.c:
0x0804913d <ucharempty+0>: push %ebp
0x0804913e <ucharempty+1>: mov %esp,%ebp
0x08049140 <ucharempty+3>: push %edi
0x08049141 <ucharempty+4>: push %ebx
0x08049142 <ucharempty+5>: sub $0x10,%esp
0x08049145 <ucharempty+8>: cmpl $0x0,0x8(%ebp)
0x08049149 <ucharempty+12>: jne 0x8049172 <ucharempty+53>
0x0804914b <ucharempty+14>: mov 0x804c1cc,%eax
0x08049150 <ucharempty+19>: mov %eax,0xc(%esp)
0x08049154 <ucharempty+23>: movl $0x35,0x8(%esp)
0x0804915c <ucharempty+31>: movl $0x1,0x4(%esp)
0x08049164 <ucharempty+39>: movl $0x804b120,(%esp)
0x0804916b <ucharempty+46>: call 0x8048c58 <free+80>
0x08049170 <ucharempty+51>: jmp 0x804919b <ucharempty+94>
0x08049172 <ucharempty+53>: mov 0x8(%ebp),%eax
0x08049175 <ucharempty+56>: mov 0x4(%eax),%edx
0x08049178 <ucharempty+59>: mov 0x8(%ebp),%eax
0x0804917b <ucharempty+62>: mov (%eax),%ebx
0x0804917d <ucharempty+64>: mov %ebx,%edi
0x0804917f <ucharempty+66>: cld
0x08049180 <
ucharempty+67>: mov %edx,%ecx
0x08049182 <ucharempty+69>: mov $0x0,%al
0x08049184 <ucharempty+71>: repz stos %al,%es:(%edi)
0x08049186 <ucharempty+73>: mov %ebx,%eax
0x08049188 <ucharempty+75>: mov %eax,(%esp)
0x0804918b <ucharempty+78>: call 0x8048c08 <free>
0x08049190 <ucharempty+83>: mov 0x8(%ebp),%eax
0x08049193 <ucharempty+86>: mov %eax,(%esp)
0x08049196 <ucharempty+89>: call 0x8048c08 <free>
0x0804919b <ucharempty+94>: add $0x10,%esp
0x0804919e <ucharempty+97>: pop %ebx
0x0804919f <ucharempty+98>: pop %edi
0x080491a0 <ucharempty+99>: pop %ebp
0x080491a1 <ucharempty+100>: ret
End of assembler dump.
From uchar.386.c:
Dump of assembler code for function ucharempty:
0x08048fde <ucharempty+0>: push %ebp
0x08048fdf <ucharempty+1>: mov %esp,%ebp
0x08048fe1 <ucharempty_passpushpop+0>: mov 0x8(%ebp),%ecx
0x08048fe4 <ucharempty_passpushpop+3>: push %edi
0x08048fe5 <ucharempty_passpushpop+4>: mov (%ecx),%edi
0x08048fe7 <ucharempty_passpushpop+6>: push %edi
0x08048fe8 <ucharempty_passpushpop+7>: mov 0x4(%ecx),%ecx
0x08048feb <ucharempty_passpushpop+10>: mov $0x0,%al
0x08048fed <ucharempty_passpushpop+12>: repz stos %al,%es:(%edi)
0x08048fef <ucharempty_passpushpop+14>: call 0x8048be0 <free>
0x08048ff4 <ucharempty_passpushpop+19>: pop %edi
0x08048ff5 <ucharempty_passpushpop+20>: pop %edi
0x08048ff6 <ucharempty_passpushpop+21>: mov %ebp,%esp
0x08048ff8 <ucharempty_passpushpop+23>: pop %ebp
0x08048ff9 <ucharempty_passpushpop+24>: jmp 0x8048be0 <free>
End of assembler dump.