views:

129

answers:

4

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.

+4  A: 

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.

ssg
Good point, you have to be really careful about that.
Krystian
Don't you think that these optimizations might work for simple inputs? For the "movl" optimization I assume that some2 is a register or a memory reference
Victor Hurdugaci
What I mean is the range of optimizations you can come up with are most likely implemented by mature compilers already. Yes, that includes peephole optimizations too. And even if a compiler omitted to optimize something, you should target the compiler to add optimizations not the assembly code in order to avoid potential unwanted side effects.
ssg
+1  A: 

Hi

I don't know how much this would be useful to you. Here are some Assembly Instruction Optimization tips by Mark Larson.

cheers

Andriyev
Quite useful. Thanks!
Victor Hurdugaci
+1  A: 

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

Richard Pennington
That's a good starting point!
Victor Hurdugaci
A: 
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?

Victor Hurdugaci