[Mono-list] Optimization in Mono

Andrew Birkett andy@nobugs.org
01 Oct 2002 09:12:27 +0100

On Tue, 2002-10-01 at 06:15, mani gandan wrote:
> What extent optimization is achieved in MONO?

There's two places where you can perform optimization.  Firstly, you can
make mcs produce more efficient IL by performing constant folding,
common subexpression elimination, loop invariant motion and things like
that.  Currently, mcs currently implements constant folding, some loop
inversion and dead code elimination.  These are the kind of operations
which you'd see being performed on the intermediate representation of a
traditional compiler.  I think we'd get a big improvement from adding a
peephole optimizer, which looks at small sequences of IL (the peephole)
and transforms them into more efficient sequences.  It doesn't require
complex dataflow analysis.  I would be interested in working on this (or
a generic IL optimizer) once  I've finished the RC2 encryption stuff
which I'm now doing.

The second place where you can optimize is in the jit, where you convert
from IL into assembly fragments on-the-fly.  This is similar to the
backend of a traditional compiler.  There's lots of ways to map IL
sequences into assembly sequences.  Mono uses a tree matching scheme
similar to the lcc compiler - it reconstructs the flat stack-based IL
code into a tree and then uses template-matching to convert subtrees
into assembly.  Then you have to allocate registers to the assembly
sequences, which is a tricky in general, and particularly tricky on the
x86 where you have so few general purpose registers.  Unlike a
traditional compiler, a JIT compiler has to do all this very quickly. 
There's no point spending ten seconds optimizing a routine which is only
going to be called once and take a millisecond to run.  There is a huge
amount of research papers which cover efficient code generation for
Java, which will be immediately relevant to mono.