Code Optimization

(最適化)

14th lecture, July 19, 2019

Language Theory and Compilers

http://www.sw.it.aoyama.ac.jp/2019/Compiler/lecture14.html

Martin J. Dürst

AGU

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

Today's Schedule

 

Remaining Schedule

 

Summary of Previous Lecture

 

Homework 1: Code Generation for for Statement

Problem 1: Code generation for for statement: Convert the C fragment
for (i=0; i<20; i++) x*=y;
to "Restricted C Language" and "Very Simple Assembly Language".

Preparation (use hint): Conversion to while statement:

i = 0;
while (i<20) {
    x *= y;
    i++;
}

 

Homework 1: Example Solution

都合により削除

 

Homework 2: Code Generation for Logical And

Problem 2 (bonus problem/発展問題): Code generation for logical AND: Convert the C fragment
if (c<4 && f>=12) a=d;
to "Restricted C Language" and "Very Simple Assembly Language".

Use hint: Conversion to nested if statement:

if (c<4)
    if (f>=12)
        a = d;

 

Homework 2: Example Solution

都合により削除

 

Goals of Optimization

Compilers provide options to choose different levels of optimization

 

Optimization Methods

 

Optimization Techniques: Faster and Smaller

Repeatedly try to use different techniques to continuously optimize the code further
(good example for Ruby implementation (video))

 

Optimization Techniques: Faster but Larger

Make execution faster even if the amout of code may increase

 

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

without optimization (gcc -O0 -S code.c)

with optimization (gcc -O1 -S code.c)

more optimized (gcc -O2 -S code.c)

much more optimized (gcc -O3 -S code.c, with parallel processing instructions)

(assembly language for Intel PCs (CISC))

Optimization options for gcc: English, Japanese

 

授業改善のための学生アンケート

WEB アンケート

お願い: 自由記述に必ず良かった点、問題点を具体的に書きましょう

(悪い例: 発音が分かりにくい; 良い例: さ行が濁っているかどうか分かりにくい)

 

Homework

(no need to submit)

Prepare for term final exam. In particular, have a look at some optimization problems.

 

Glossary

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