Tutorial :Reduce assembly number of instructions



Question:

I want to reduce (manually) the number of instructions from a Linux assembly file. This will be basically done by searching predefined reductions in an abstract syntax tree.

For example:

pushl <reg1>  popl  <reg1>  

Will be deleted because it makes no sense.

Or:

pushl <something1>  popl  <something2>  

Will become:

movl <something1>, <something2>  

I'm looking for other optimizations that involve a fixed number of instructions. I don't want to search dynamic ranges of instructions.

Could you suggest other similar patterns that can be replaced with less instructions?

Later Edit: Found out, thanks to Richard Pennington, that what I want is peephole optimization.

So I rephrase the question as: suggestions for peephole optimization on Linux assembly code.


Solution:1

Compilers already do such optimizations. Besides, it's not that straightforward decision to make such optimizations, because:

push reg1  pop reg1  

Still leaves value of reg1 at memory location [sp-nn] (where nn = size of reg1 in bytes). So although sp is past it, the code after can assume [sp-nn] contains the value of reg1.

The same applies to other optimization as well:

push some1  pop some2  

And that is usually emitted only when there is no equivalent movl some1, some2 instruction.

If you're trying to optimize a high-level compiler generated code, compilers usually take most of those cases into account. If you're trying to optimize natively written assembly code, then an assembly programmer should write even better code.

I would suggest you to optimize the compiler, rather than optimizing the assembly code, it would provide you a better framework for dealing with intent of the code and register usage etc.


Solution:2

To get more information about what you are trying to do, you might want to look for "peephole optimization".


Solution:3

pushl <something1>  popl  <something2>  

replaced with

mov <something1>, <something2>  

actually increased the size of my program. Weird!

Could you provide some other possible peephole optimizations?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »