1

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.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103

3 Answers3

3

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.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • 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 Nov 28 '09 at 16:24
  • 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. – Sedat Kapanoglu Nov 28 '09 at 17:26
  • "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" is incorrect because the next interrupt in the system will overwrite it. The only thing that can be assumed is that it can contain any value. – Olof Forshell Feb 08 '11 at 22:14
1

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

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
0
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
  • 28,177
  • 5
  • 87
  • 103