Interesting idea. A peephole optimizer that self generates with maybe a recursive backtracking algorithm.
I think it would be a little tricky? First you have to define "equivalent code." Executing code changes the state of the cpu registers including flags, the RAM, and has side effects on I/o space. Different code has different timing. Obviously the goal here is to reduce CPU cycles so changing the timing is a feature not a bug. So I guess your recursive search function would need an emulator that can be instantiated on the (or a) stack and a comparison function that determines "equal" given compared emulator states. If you categorize patterns of instructions that are known to be equivalent you could do some significant optimizations. -- John.
