Peephole Optimization Templates

I read about local optimization methods, but I don’t understand how they are implemented. The idea is that the optimizer each time looks at the "window" of the code and somehow finds the templates and replaces them with more optimized versions.

My question is: how to detect these patterns? (let's say your platform is a virtual machine that outputs assembly code for a created computer, such as Schocken Hack).

Do people really check the code manually (using control flow diagrams or DAGs or something else) and then collect all the identified patterns and encode them into the optimizer? Or there is an automatic way.

For example, you feed optimized code in the analyzer, and it spews the specified patterns. If so, how do you start writing?

+5
source share
2 answers

Classic peephole optimization is not related to power reduction and other things that you call. These are 2-3 sequences of commands, for example,

BRANCH FALSE $1
BRANCH $2
$1:

which can be reduced to

BRANCH TRUE $2

Such sequences can occur in naive code generators, such as single-pass compilers that do not generate ASTs, for example, some of the COBOL compilers I worked on.

+3
source

, . , . GCC, . , . .
, , pass.h GCC.

+1

All Articles