Code Optimization

(最適化)

14th lecture, July 21, 2023

Language Theory and Compilers

https://www.sw.it.aoyama.ac.jp/2023/Compiler/lecture14.html

Martin J. Dürst

AGU

© 2005-23 Martin J. Dürst 青山学院大学

 

Today's Schedule

 

Remaining Schedule

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.

補講について: 補講の内容は期末試験の対象。補講が別の補講と重なる場合は事前に申し出ること。

  

Leftovers from Previous Lecture

  

Summary of Previous Lecture

 

Homework: Code Generation for for Statement

Problem 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 Solution

Restricted 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:

 

Homework: Code Generation for Logical And

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;

 

Homework 2: Example Solution

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:

 

Goals of Optimization

Compilers provide options to choose different levels of optimization

 

Code Analysis

 

Control Flow Analysis: Basic Blocks

 

Control Flow Analysis: Control Graph

 

Control Flow Analysis Example

Example: for loop: for (i=5; i<20; i++) t += 7;

(see homework solution)

Poll: How many basic blocks?

 

Optimization Techniques

 

Optimization Techniques: Faster and Smaller

 

Move Common Code out of Loops: Simple Example

while (a<500) {
a += b*c*d; }

Multiplication moved outside of loop:

e = b*c*d;
while (a<500) {
    a += e;
}

 

Move Common Code out of Loops: Advanced Example

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:

 

Example: Common Instructions

       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:

 

Example: After Extraction

       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:

 

Optimization Techniques: Faster but Larger

Make execution faster even if the amout of code may increase

Examples:

 

Function Inlining

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)

 

Loop Unrolling

 

Optimization Techniques: Machine Dependent

Highly dependent on machine type

 

Example of Instruction Reordering

(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

 

Dynamic Compilation

(also: just-in-time compilation)

 

Examples of Actual Optimization

source

Command: gcc -OX -S -masm=intel -fverbose-asm code.c

Results (assembly language for Intel PCs (CISC)):

Optimization options for gcc: English, Japanese

 

Homework

(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.

 

Glossary

peephole optimization
ピープホール最適化
control flow analysis
制御フロー解析
basic block
基本ブロック
data flow analysis
データフロー解析
constant folding
静式評価、定数たたみこみ
constant propagation
定数伝播
dead code elimination
無用命令の削除
function inlining
関数呼び出しの展開
loop unrolling
繰り返しの展開
dynamic compilation
動的コンパイル