Compiler construction in 4020 lecture 9 Koen Langendoen

  • Slides: 36
Download presentation
Compiler construction in 4020 – lecture 9 Koen Langendoen Delft University of Technology The

Compiler construction in 4020 – lecture 9 Koen Langendoen Delft University of Technology The Netherlands

Summary of lecture 8 • interpretation • recursive • iterative program text front-end annotated

Summary of lecture 8 • interpretation • recursive • iterative program text front-end annotated AST • simple code generation • • code per AST node stack and register machines weighted register allocation register spilling intermediate code generation interpreter back-end assembly

Quiz 4. 7 Can the weight of a tree can also be used to

Quiz 4. 7 Can the weight of a tree can also be used to reduce the maximum stack height when generating code for a stack machine? If not, why? If yes, how?

Overview annotated AST code generator • code generation for basic blocks • [instruction selection:

Overview annotated AST code generator • code generation for basic blocks • [instruction selection: BURS] • register allocation: graph coloring • instruction ordering: ladder sequences library assembler object file linker executable

Code generation for basic blocks • improve quality of code emitted by simple code

Code generation for basic blocks • improve quality of code emitted by simple code generation • consider multiple AST nodes at a time basic block: a part of the control graph that contains no splits (jumps) or combines (labels) • generate code for maximal basic blocks that cannot be extended by including adjacent AST nodes

Code generation for basic blocks • a basic block consists of expressions and assignments

Code generation for basic blocks • a basic block consists of expressions and assignments { int n; n x n y = = a+1; (b+c) * n; n+1; (b+c) * n; } • fixed sequence (; ) limits code generation • an AST is too restrictive

{ int n; Example AST n x n y = = a+1; (b+c) *

{ int n; Example AST n x n y = = a+1; (b+c) * n; n+1; (b+c) * n; } =: + a n ; ; ; =: =: * 1 + b x n c + n n * 1 + b y n c

Dependency graph • convert AST to a directed acyclic graph (dag) capturing essential data

Dependency graph • convert AST to a directed acyclic graph (dag) capturing essential data dependencies • data flow inside expressions: operands must be evaluated before operator is applied • data flow from a value assigned to variable V to the use of V: the usage of V is not affected by other assignments

AST to dependency graph AST • replace arcs by downwards arrows • • (upwards

AST to dependency graph AST • replace arcs by downwards arrows • • (upwards for destination under assignment) insert data dependencies from use of V to preceding assignment to V insert data dependencies between consecutive assignments to V add roots to the graph (output variables) remove ; -nodes and connecting arrows dependency graph

Example dependency graph { int n; n x n y } = = a+1;

Example dependency graph { int n; n x n y } = = a+1; (b+c) * n; n+1; (b+c) * n; x y * * + b + c a + 1

Common subexpression elimination • common subexpressions occur multiple times and evaluate to the same

Common subexpression elimination • common subexpressions occur multiple times and evaluate to the same value { int n; n x n y } = = a+1; (b+c) * n; n+1; (b+c) * n; x y * * + b + c a + 1

Exercise (7 min. ) • given the code fragment x : = a*a +

Exercise (7 min. ) • given the code fragment x : = a*a + 2*a*b + b*b; y : = a*a – 2*a*b + b*b; draw the dependency graph before and after common subexpression elimination.

Answers

Answers

Answers dependency graph before CSE x y + + + * a 2 -

Answers dependency graph before CSE x y + + + * a 2 - * b b * a b * * a 2 b b * a b

Answers dependency graph after CSE + * * 2 y + + - *

Answers dependency graph after CSE + * * 2 y + + - * * a x b * * a 2 b b * a b

Answers dependency graph after CSE + * * 2 y + + * *

Answers dependency graph after CSE + * * 2 y + + * * a x b -

Break

Break

From dependency graph to code • target: register machine (lecture 8) with additional operations

From dependency graph to code • target: register machine (lecture 8) with additional operations on memory • reg op: = reg Add_Reg R 2, R 1 • reg op: = mem Add_Mem x, R 1 • rewrite nodes with machine instruction templates, and linearize the result • instruction ordering: ladder sequences • register allocation: graph coloring

Linearization of the data dependency graph • example: (a+b)*c – d Load_Mem a, R

Linearization of the data dependency graph • example: (a+b)*c – d Load_Mem a, R 1 Add_Mem b, R 1 Mul_Mem, c, R 1 Sub_Mem d, R 1 • definition of a ladder sequence • each root node is a ladder sequence • a ladder sequence S ending in operator node N can be extended with the left operand of N • if operator N is communitative then S may also extended with the right operand of N

Linearization of the data dependency graph • code generation for a ladder sequence x

Linearization of the data dependency graph • code generation for a ladder sequence x y * * + b + c a + 1 1

Linearization of the data dependency graph • code generation for a ladder sequence +

Linearization of the data dependency graph • code generation for a ladder sequence + b x x R 1, x Store_Mem * Mul_Reg * R T, R 1 RT c Add_Mem + c, R 1 RT Load_Mem b, b R 1 c • instructions from bottom to top, one register

Linearization of the data dependency graph • late evaluation – don’t occupy registers •

Linearization of the data dependency graph • late evaluation – don’t occupy registers • select ladder sequence S without additional incoming dependencies • introduce temporary registers for non-leaf operands, which become additional roots • generate code for S, using R 1 as the ladder register • remove S from the graph • note: code blocks produced in reverse order

Example code generation x 1) ladder: y, *, + Load_Const 1, R 1 Add_Reg

Example code generation x 1) ladder: y, *, + Load_Const 1, R 1 Add_Reg R 3, R 1 Mul_Reg, R 2, R 1 Store_Mem R 1, y 2) ladder: x, * Load_Reg R 2, R 1 Mul_Reg R 3, R 1 Store_Mem R 1, x R 2 y R 3 * + b * + + c a 1 3) ladder: R 2, + Load_Mem b, R 1 Add_Mem c, R 1 Load_Reg R 1, R 2 1 4) ladder: R 3, + Load_Const 1, R 1 Add_Mem c, R 1 Load_Reg R 1, R 3

Exercise (7 min. ) • generate code for the following dependency graph + *

Exercise (7 min. ) • generate code for the following dependency graph + * * 2 y + + * * a x b -

Answers

Answers

x Answers 1) ladder: x, +, + Load_Reg R 2, R 1 Add_Reg R

x Answers 1) ladder: x, +, + Load_Reg R 2, R 1 Add_Reg R 3, R 1 Add_Reg, R 4, R 1 Store_Mem R 1, x 2) ladder: y, +, Load_Reg R 2, R 1 Sub_Reg R 3, R 1 Add_Reg, R 4, R 1 Store_Mem R 1, y + R 2 R 3 + * * a y R 4 + * - b * 2 3) ladder: R 3, *, * Load_Const 2, R 1 Mul_Reg Ra, R 1 Mul_Reg, Rb, R 1 Load_Reg R 1, R 3 4) ladder: R 2, * Load_Reg Ra, R 1 Mul_Reg Ra, R 1 Load_Reg R 1, R 2 5) ladder: R 4, * Load_Reg Rb, R 1 Mul_Reg Rb, R 1 Load_Reg R 1, R 4

Register allocation by graph coloring • procedure-wide register allocation • only live variables require

Register allocation by graph coloring • procedure-wide register allocation • only live variables require register storage dataflow analysis: a variable is live at node N if the value it holds is used on some path further down the control-flow graph; otherwise it is dead • two variables(values) interfere when their live ranges overlap

Live analysis a b c d : = : = read(); a read(); b

Live analysis a b c d : = : = read(); a read(); b read(); c d a + b*c; d < 10 e e : = c+8; print(c); f : = 10; e e : = f + d; print(f); print(e); f a : = read(); b : = read(); c : = read(); d : = a + b*c; if (d < 10 ) then e : = c+8; print(c); else f : = 10; e : = f + d; print(f); fi print(e);

Register interference graph a b c d : = : = read(); a read();

Register interference graph a b c d : = : = read(); a read(); b read(); c d a + b*c; d < 10 e e : = c+8; print(c); f : = 10; e e : = f + d; print(f); print(e); f a b c d e f

Graph coloring • NP complete problem a b • heuristic: color easy nodes last

Graph coloring • NP complete problem a b • heuristic: color easy nodes last c d • • find node N with lowest degree e remove N from the graph color the simplified graph set color of N to the first color that is not used by any of N’s neighbors f

Exercise (7 min. ) { int tmp_2 ab = 2*a*b; int tmp_aa = a*a;

Exercise (7 min. ) { int tmp_2 ab = 2*a*b; int tmp_aa = a*a; int tmp_bb = b*b; x : = tmp_aa + tmp_2 ab + tmp_bb; y : = tmp_aa - tmp_2 ab + tmp_bb; } given that a and b are live on entry and dead on exit, and that x and y are live on exit: (a) construct the register interference graph (b) color the graph; how many register are needed?

Answers

Answers

Answers a tmp_2 ab x b tmp_aa tmp_bb 4 registers are needed y

Answers a tmp_2 ab x b tmp_aa tmp_bb 4 registers are needed y

Code optimization • preprocessing • • constant folding strength reduction a[1] *(a+4*1) *(a+4) 4*i

Code optimization • preprocessing • • constant folding strength reduction a[1] *(a+4*1) *(a+4) 4*i i<<2 in-lining. . . • postprocessing • peephole optimization: replace inefficient patterns Load_Reg R 2, R 1 Load_Reg R 1, R 2 Load_Reg R 2, R 1

Summary annotated AST code generator • dependency graphs assembly • code generation for basic

Summary annotated AST code generator • dependency graphs assembly • code generation for basic blocks • instruction selection: BURS • register allocation: graph coloring • instruction ordering: ladder sequences library assembler object file linker executable

Homework • study sections: • 4. 2. 6 BURS code generation • assignment 2

Homework • study sections: • 4. 2. 6 BURS code generation • assignment 2 (next week, chap 6): • make Asterix OO • deadline June 4 08: 59 • print handout for next week [blackboard]