Technique:Recognizing compiler-generated code/Inlined library idioms
Appearance
Topic page of Technique:Recognizing compiler-generated code.
Compiler-inlined memcpy, memset and strlen (rep movs/stos, SIMD loops, repne scasb) and C++ new/delete pairs with their cleanup funclets: how to read them as the library call they replace.
At -O1 and above, compilers replace small or constant-size library calls with instructions and sometimes add C++ unwinding code. These are unverified recognition notes from common compiler output; they were not checked against compiler sources for this page.
Inlined memory operations
- memcpy / memset with a small constant size: a few
movpairs (load, store) of 8, 16, 32 bytes; sizes that are not a multiple get an overlapping final store. Fourmovups/movdquloads and stores = a 64-byte copy; zeroing isxor eax, eaxthen stores orpxor xmm0, xmm0; movups [..], xmm0. rep movsb/rep movsd/rep movsqwith ECX/RCX as count, RSI source, RDI destination: memcpy (MSVC /O1 or /Os, Clang-Os, and any size on CPUs with fast-string support).rep stosb/d/qwith AL/EAX/RAX as the value: memset. The count register holds bytes divided by the element size.- strlen:
repne scasbwithxor eax, eax(AL = 0), thennot ecx/dec ecx(the classic old MSVC form), or a loop comparing 16 bytes at a time withpcmpeqb/pmovmskb/bsf. - Large constant memcpy/memset usually remain calls to the CRT or libc (
memcpy,__memcpy_chkin fortified builds,_memset).__memcpy_chk/__strcpy_chkcome from_FORTIFY_SOURCE.
C++ new and delete
operator new(size)is??2@YAPAXI@Z(MSVC x86; x64??2@YAPEAX_K@Z) or_Znwm/_Znwj(Itanium) followed by a constructor call; a null check is absent for throwing new.operator deleteis??3@YAXPAX@Z/_ZdlPv; sized delete adds the size argument (_ZdlPvm).- A
newwhose constructor can throw gets a cleanup funclet that frees the memory if the constructor throws; MSVC x64 places such funclets in.text$x(dtor$N,catch$N) reached through the UNWIND_INFO tables, so they look like unreachable functions with no callers. See Platform:Microsoft Visual C++ ABI/C++ exception handling; Itanium landing pads are reached from the LSDA call-site table (Platform:Itanium C++ ABI). - The scalar deleting destructor (
??_G, vftable slot 0) calls the destructor and thenoperator deletewhen bit 0 of its argument is set.
See the overview: Technique:Recognizing compiler-generated code.