Node:Peephole optimization, Next:VM disassembler, Previous:VM code generation, Up:Using the generated code
You need peephole optimization only if you want to use superinstructions. But having the code for it does not hurt much if you do not use superinstructions.
A simple greedy peephole optimization algorithm is used for
superinstruction selection: every time gen_inst
compiles a VM
instruction, it checks if it can combine it with the last VM instruction
(which may also be a superinstruction resulting from a previous peephole
optimization); if so, it changes the last instruction to the combined
instruction instead of laying down i
at the current *ctp
.
The code for peephole optimization is in vmgen-ex/peephole.c
.
You can use this file almost verbatim. Vmgen generates
file-peephole.i
which contains data for the peephoile
optimizer.
You have to call init_peeptable()
after initializing
vm_prim
, and before compiling any VM code to initialize data
structures for peephole optimization. After that, compiling with the VM
code generation functions will automatically combine VM instructions
into superinstructions. Since you do not want to combine instructions
across VM branch targets (otherwise there will not be a proper VM
instruction to branch to), you have to call BB_BOUNDARY
(see VM code generation) at branch targets.