I watched several assembly videos to better understand how to manually optimize * .s files left over after compilation with gcc/g++ -S ... One of the topics discussed was Refactoring redundant code , which demonstrates how to move redundant code to your own labeled block ending with ret and replacing it with call .
The example shown in the video is two blocks containing:
mov eax,power mul ebx mov power,eax inc count
which he replaces with call CalculateNextPower , and CalculateNextPower looks like this:
CalculateNextPower: mov eax,power mul ebx mov power,eax inc count ret
Out of curiosity, trying to reduce the compiled size, I compiled some C and C ++ projects with -S and various optimizations, including -Os, -O2, -O3, -pipe, -combine and -fwhole-program, and analyzed the result * .s for redundancy using a slightly corrected (for .s files) version of duplo . Only the -fwhole-program (now obsolete IIRC) had a significant impact on the elimination of duplicate code for files (I assume that its replacement (-s) -flto will behave similarly during the link - roughly equivalent to compiling with -ffunction-sections -fdata -sections and linking with -gc partitions), but still skips significantly large blocks of code.
Manual optimization using duplo output reduced the size by ~ 10% in a random C project and almost 30% in a random C ++ project, when only adjacent assembly blocks were deduplicated with at least 5 adjacent duplicate instructions.
I miss the compiler option (or even a stand-alone tool) that automatically removes redundant assembly when compiling for size (including other compilers: clang, icc, etc.) or is this function missing (for some reason?)
If this does not exist, duplo could be modified to ignore lines beginning with the character '.' or ';' (and others?) and replace duplicate code blocks with function calls with duplicate code, but I am open to other sentences that will work directly with the internal representations of the compiler (preferably clang or gcc).
Edit: I fixed duplication to identify duplicate assembly blocks here , but manual refactoring is still required for this. As long as the same compiler is used to generate the code, it may be possible (but probably slower) to identify the largest blocks of the duplicated code, put them in your own “function” block and replace the code with CALL with this block.