Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 22 Oct 2014, 23:58
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Reduced compiled size by eliminating redundant assembly
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [3 Posts]  
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4353

PostPosted: Tue 29 Oct 2013, 12:12    Post subject:  Reduced compiled size by eliminating redundant assembly  

The topic is covered with better formatting at:
http://stackoverflow.com/questions/19663795/automatic-code-deduplication-of-assembly-language

but I am interested in puppy-like solutions too, maybe someone can figure out a novel way to do this in 10 lines of awk Smile

I've been going through some Assembly Programming Videos to get a better understanding of how to manually optimize the *.s files left after compiling with gcc/g++ -S ... One of the topics covered was Refactoring Redundant Code that demonstrates how to move redundant code to its own labeled block ending with a ret and replacing it with a call.

Out of curiosity, trying to reduce 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 resulting *.s files for redundancy using a lightly patched (for .s files) version of duplo. Only -fwhole-program (now deprecated IIRC) had a significant effect toward eliminating duplicate code across files (I assume its replacement(s) -flto would behave similarly during link time - roughly equivalent to compiling with -ffunction-sections -fdata-sections and linking with --gc-sections) but still misses significantly large blocks of code.

The example given in the video is 2 blocks containing:
Code:
mov eax,power
mul ebx
mov power,eax
inc count
which it replaces with call CalculateNextPower and CalculateNextPower looks like:
Code:
CalculateNextPower:
mov eax,power
mul ebx
mov power,eax
inc count
ret
Am I missing a compiler option (or even a standalone tool) that does this automatically when compiling for size (including other compilers: clang, icc, etc..) or is this functionality absent (for a reason?).

If it doesn't exist, it could be possible to modify duplo to ignore lines starting with a '.' or ';' (and others?) and replace duplicate code blocks with calls to functions with the duplicate code, but I am open to other suggestions that would work directly with the compiler's internal representations.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4353

PostPosted: Tue 29 Oct 2013, 15:42    Post subject:  

Ok, I patched duplo for assembly files:

basic howto:
Code:
compile your c/cpp files with
gcc -S <options_here> *.c
gcc -S <options_here> *.cpp
for x in *.s; do;
echo $x
done > files.txt
duplo -ml 5 -ip files.txt out.txt


If you downloaded this version, try the one in the next post.

_________________
Web Programming - Pet Packaging 100 & 101

Last edited by technosaurus on Tue 29 Oct 2013, 20:49; edited 1 time in total
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4353

PostPosted: Tue 29 Oct 2013, 20:47    Post subject:  

repatched duplo, need to keep labels in place (I thought it ended the string but it actually just removed the labels)
duplo-asm.patch.gz
Description 
gz

 Download 
Filename  duplo-asm.patch.gz 
Filesize  1.11 KB 
Downloaded  69 Time(s) 
duplo.tar.gz
Description 
gz

 Download 
Filename  duplo.tar.gz 
Filesize  14.44 KB 
Downloaded  70 Time(s) 

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [3 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0530s ][ Queries: 12 (0.0109s) ][ GZIP on ]