(最適化)
https://www.sw.it.aoyama.ac.jp/2023/Compiler/lecture14.html
© 2005-23 Martin J. Dürst 青山学院大学
About makeup classes: The material in the makeup class is part of the final exam. If you have another makeup class at the same time, please inform the teacher as soon as possible.
補講について: 補講の内容は期末試験の対象。補講が別の補講と重なる場合は事前に申し出ること。
if
/while
/...)jump
instructionsjump
often uses comparison with 0 as a conditionjump
can be expressed with goto
for
StatementProblem 1: Code generation for for
statement: Convert the C
fragment
for (i=5; i<20; i++) t += 7;
to "Restricted C Language" and "Very Simple Assembly Language".
Hint (for both problems): Try to remove the construct in question
(&&
or for
) by rewriting the C program
(Example: convert for
to while
)
Preparation (use hint): Conversion to while
statement:
i = 5; while (i<20) { t += 7; i++; }
for
Statement, Example SolutionRestricted C:
i = 5; next1: if (i-20 >= 0) goto break1; t += 7; cont1: i = i + 1; // cont1: is the target for continue; goto next1; break1:
Very simple assembly language:
CONST R1, 5 STORE i, R1 next1: LOAD R1, i CONST R2, 20 SUB R1, R1, R2 JUMP>= R1, break1 LOAD R1, t CONST R2, 7 ADD R1, R1, R2 STORE t, R1 cont1: LOAD R1, i CONST R2, 1 ADD R1, R1, R2 STORE i, R1 JUMP next1 break1:
Problem 2: Code generation for logical AND: Convert the C fragment
if (x<7 && y>=14) z = 22;
to "Restricted C Language" and "Very Simple Assembly Language".
Use hint: Conversion to nested if
statement:
if (x<7) if (y>=14) z = 22;
Restricted C:
if (x-7 >= 0) goto endif1; if (y-14 < 0) goto endif1; z = 22; endif1:
Very simple assembly language:
LOAD R1, x CONST R2, 7 SUB R1, R1, R2 JUMP>= R1, endif1 LOAD R1, y CONST R2, 14 SUB R1, R1, R2 JUMP< R1, endif1 CONST R1, 22 STORE z, R1 endif1:
Compilers provide options to choose different levels of optimization
Example: for
loop: for (i=5; i<20; i++) t +=
7;
(see homework solution)
Poll: How many basic blocks?
24 * 60 * 60
⇒ 86400
a = 3; b = a * 4;
a = 3; b = 3 * 4;
STORE a, R1
LOAD R1, a
STORE a, R1
while (a<500) {
a += b*c*d; }
Multiplication moved outside of loop:
e = b*c*d; while (a<500) { a += e; }
a = 0; while (a<100) { a += b; }
At C level, no common code. What about assembly level?
CONST R1, 0 STORE a, R1 next: LOAD R1, a CONST R2, 100 SUB R3, R1, R2 JUMP>= break, R3 LOAD R1, a LOAD R4, b ADD R1, R1, R4 STORE a, R1 JUMP next break:
CONST R1, 0 STORE a, R1 ; superfluous next: LOAD R1, a ; common/superfluous CONST R2, 100 ; common SUB R3, R1, R2 JUMP>= break, R3 LOAD R1, a ; common/superfluous LOAD R4, b ; common ADD R1, R1, R4 STORE a, R1 ; common JUMP next break:
CONST R1, 0 CONST R2, 100 ; extracted LOAD R4, b ; extracted next: SUB R3, R1, R2 JUMP>= break, R3 ADD R1, R1, R4 JUMP next break: STORE a, R1 ; extracted
Total instruction count: 11 → 8
Instructions in loop: 9 → 4
Caution:
Make execution faster even if the amout of code may increase
Examples:
Instead of calling a function, use the code of the function inline
Example:
double square(double x) { return x*x; } square(a);
⇒a*a
(actually in this example, code gets shorter, because function call code is
eliminated)
for (i=0; i<5; i++) a+=b;
⇒ a += b+b+b+b+b;
for (i=0; i<20; i++) a+=b;
for (i=0; i<20; i+=5)
a += b+b+b+b+b;
Highly dependent on machine type
CONST
instructions)x*2
⇒ x+x
or
x<<1
x*5
⇒ x<<2 + x
Example: x/256
⇒ x>>8
(on most
machines, division is slower than shift)
(example: instructions such as LOAD
may take longer, but run in
parallel)
Expression: 743 * a
Before optimization:
CONST R1, 743
LOAD R2, a ; invisible waiting time
MUL R3, R1, R2
After optimization:
LOAD R2, a
CONST R1, 743 ; executed while LOAD still in progress
MUL R3, R1, R2
(also: just-in-time compilation)
Command: gcc -OX -S -masm=intel -fverbose-asm
code.c
-OX
: Level of optimization-S
: Output assembly (.s
)-masm=intel
: right-to-left (⇐, "Intel syntax");-fverbose-asm
: more detailled outputResults (assembly language for Intel PCs (CISC)):
-O0
)-O1
)-O2
)-O3
,
with parallel processing (SIMD, single instruction multiple data)
instructions)Optimization options for gcc
: English,
Japanese
(no need to submit)
Check out optimization options for gcc.
Check out past exam code generation/optimization problems.
Look at past exams and find some questions you can ask tomorrow or on Moodle.