Brief description of compilation principle process and intermediate code optimization

1. The diagram of the compilation process is as follows:
For more information, please pay attention to the official account " code miscellaneous forum "!
Write picture description here

The role of lexical analysis : to find the word. Such as int a=b+c; the result is: int, a, =, b, +, c and;
syntax analysis function: find expressions, program segments, statements, etc. For example, the syntax analysis result of int a=b=c; is the statement int a=b+c.
Semantic analysis function: check whether the type matches, etc.
To learn more, please pay attention to the public account " code miscellaneous forum "!
2. Intermediate code optimization

The so-called code optimization refers to the equivalent (meaning that the running result of the program does not change) transformation of the program code. Program code can be intermediate code (such as quaternary code) or object code. The meaning of equivalence is to make the running result of the code after transformation the same as that of the code before transformation. The meaning of optimization is that the final generated target code is short (shorter running time and smaller space occupied), and the space-time efficiency is optimized. In principle, optimization can be performed at various stages of compilation, but the most important type is optimization of intermediate code, which does not depend on a specific computer.

Optimization purpose : To perform equivalent transformation on the compiled program without changing the running effect of the program, so that it can generate more efficient object code.

Optimization principles :

Principle of equivalence. After optimization, it should not change the result of program operation.
Principle of equivalence. The running time of the object code generated after optimization is shorter and the storage space occupied is smaller.
cost-effective principle. Better optimization results should be achieved at a lower cost as much as possible.
To learn more, please pay attention to the public account " code miscellaneous forum "!

Attached:

Loop optimization code extraction :
set the basic block at the loop entry, and lift the loop-invariant operations in the loop into the basic block, and the lifted part must be the necessary operation at the loop exit. As shown in the picture:

Write picture description here

PS:
1. Code optimization is to perform equivalent transformation on the program to improve the efficiency of the target program, usually only the intermediate code is optimized. It usually includes three parts: control flow analysis, data flow analysis and transformation.
2. Based on the basic block of the program, the optimization within the basic block is called local optimization, the optimization across basic blocks is global optimization, and loop optimization is optimized for loops, which is a part of global optimization.
3. Common subexpression deletion, copy propagation, useless code deletion, code externalization, strength weakening, and induction variable deletion are some commonly used local or global code optimization methods.
To learn more, please pay attention to the public account " code miscellaneous forum "!

Guess you like

Origin blog.csdn.net/qq_34417408/article/details/78126087