Unoptimized Code Generation • Last time we left off on the procedure p abstraction … Saman Amarasinghe 2 6.035 ©MIT Fall 1998 8*n+16(%rbp) 16(%rbp) – %rdi, %rsi, %rdx, % %rcx, %r8 % 8 andd %r9 %9 8(%rbp) 0(%rbp) -8(%rbp) %b %rbp – marks the beginning of the current frame -8*m-8(%rbp) 0(%rsp) argument n … argument 7 Return address Previous %rbp local 0 … local m Cu urrent • Arguments 0 to 6 are in: Previous The Stack Variable size %rsp – marks the end Saman Amarasinghe 3 6.035 ©MIT Fall 2001 33 Question: • Why y use a stack? Wh y not use the heapp or pre allocated in the data segment? Saman Amarasinghe 4 6.035 ©MIT Fall 2001 Procedure Linkages g Standard p procedure linkage g procedure q prolog Prolog: •Push P h old ld fframe pointer i t •Save calle-saved registers •Make room for temporaries pre-call ll post-return Save caller caller-saved saved registers •Save •Push arguments procedure p prolog Pre-call: Epilog: •Restore callee-saved epilog Pop old frame pointer •Pop •Store return value epilog Post-return: •Restore R t caller-saved ll d •Pop arguments Saman Amarasinghe 5 6.035 ©MIT Fall 2001 Stack • Calling: Caller rbp local variables – Assume %rcx is live and is caller save – Call foo(A, B, C, D, E, F, G, H, I) • A to I are at -8(%rbp) to -72(%rbp) push %rcx push push push mov mov mov mov mov mov call -72(%rbp) -64(%rbp) 64(%rbp) -56(%rbp) -48(%rbp), %r9 -40(%rbp), %r8 -32(%rbp), %rcx -24(%rbp), %rdx -16(%rbp), %rsi -8(%rbp), 8(% b ) % %rdi di foo Saman Amarasinghe return address previous frame pointer calliee saved registers 6 stack temporaries dynamic area caller ll savedd registers it argument 9 argument 8 argument 7 return address 6.035 ©MIT Fall 2001 rsp Stack • Calling: Calliee – Assume %rbx is used in the function and d iis calliee lli save – Assume 40 bytes are required for locals foo: push %rbp mov %rsp %rbp %rsp, enter sub $48, $0 $48, %rsp mov %rbx, -8(%rbp) return address previous frame pointer calliee saved registers local variables stack temporaries dynamic area caller ll savedd registers it argument 9 argument 8 argument 7 return address previous frame pointer calliee saved registers local variables stack temporaries dynamic area Saman Amarasinghe 7 rbp 6.035 ©MIT Fall 2001 rsp Stack • Arguments • Call foo(A, B, C, D, E, F, G, H, I) – – -72(%rbp) push push mov mov mov mov mov mov call -64(%rbp) -56(%rbp) -48(%rbp), %r9 -40(%rbp), %r8 -32(%rbp), %rcx -24(%rbp), %rdx -16(%rbp), %rsi -8(%rbp), %rdi foo stack temporaries dynamic area Access A to F via registers • – local variables Passed in by pushing before the call push or put them in local memory Access rest using 16+xx(%rbp) mov 16(%rbp), %rax mov 24(%rbp), ( p), %r10 CODE DATA Control Flow Global Static Variables Global Dynamic Data Local Variables Temporaries Parameter Passing Read-only Data Procedures Statements Data Access Saman Amarasinghe return address previous frame pointer calliee saved registers caller ll savedd registers it argument 9 argument 8 argument 7 return address previous frame pointer calliee saved registers rbp local variables stack temporaries dynamic area 8 6.035 ©MIT Fall 2001 rsp Stack • Locals and Temporaries – Calculate the size and allocate space on the stack or sub $48, %rsp enter $48, 0 $48 – Access using -8-xx(%rbp) 8 xx(%rbp) mov -28(%rbp), %r10 mov %r11, -20(%rbp) CODE DATA Control Flow Global Static Variables Global Dynamic Data Local Variables Temporaries Parameter Passing Read-only Data Procedures Statements Data Access Saman Amarasinghe return address previous frame pointer calliee saved registers local variables stack temporaries dynamic area caller ll savedd registers it argument 9 argument 8 argument 7 return address previous frame pointer calliee saved registers rbp local variables stack temporaries dynamic area 9 6.035 ©MIT Fall 2001 rsp Stack • Returning Calliee – Assume the return value is the first t temporary return address previous frame pointer calliee saved registers local variables stack temporaries dynamic area – Restore the caller saved register – Put the return value in %rax – Tear-down the call stack mov -8(%rbp), %rbx mov -16(%rbp), %rax mov %rbp, %rsp pop %rbp leave caller ll savedd registers it argument 9 argument 8 argument 7 return address previous frame pointer calliee saved registers local variables stack temporaries ret dynamic area Saman Amarasinghe 10 rbp 6.035 ©MIT Fall 2001 rsp Stack • Returning Caller • (Assume the return value goes to the first ttemporary)) – Restore the stack to reclaim the argument space – Restore the caller save registers – Save the return value call foo add $24, %rsp pop %rcx mov %rax, 8(%rbp) p stack temporaries dynamic area caller ll savedd registers it argument 9 argument 8 argument 7 CODE DATA Procedures Global Static Variables Global Dynamic Data Local Variables Temporaries Parameter Passing g Read-only Data Statements Data Access 11 rbp local variables Control Flow … Saman Amarasinghe return address previous frame pointer calliee saved registers 6.035 ©MIT Fall 2001 rsp 47 Question: • Do you need the $rbp p? • What are the advantages and disadvantages of having $rbp? Saman Amarasinghe 12 6.035 ©MIT Fall 2001 So far we covered.. CODE DATA Procedures Global Static Variables Global Dynamic Data Control Flow Local Variables Temporaries Statements Parameter Passing Data Access Saman Amarasinghe Read-only Data 13 6.035 ©MIT Fall 1998 8 Outline • • • • Generation of expressions and statements Generation of control flow x86-64 Processor Guidelines in writing a code generator Saman Amarasinghe 14 6.035 ©MIT Fall 1998 Expressions p • Expressions p are represented p as trees – Expression may produce a value – Or,, it mayy set the condition codes ((boolean exprs) p ) • How do you map expression trees to the machines? – How to arrange the evaluation order? – Where Wh tto kkeep th the intermediate i t di t values? l ? • Two approaches – Stack Model – Flat List Model Saman Amarasinghe 15 6.035 ©MIT Fall 1998 Evaluating g expression p trees • Stack model – Eval left-sub-tree Put the results on the stack – Eval right-sub-tree Put the results on the stack – Get top two values from the stack perform the operation OP h results l on the h stackk put the OP • Very inefficient! Saman Amarasinghe 16 6.035 ©MIT Fall 1998 Evaluating g expression p trees • Flat List Model – The idea is to linearize the expression tree – Left to Right Depth-First Traversal of the expression tree • Allocate All temporaries i ffor iintermediates di ((all ll the h nodes d off the h tree)) – New temporary for each intermediate – All the temporaries on the stack (for now) – Each expression is a single 3-addr op • x = y op z • Code generation for the 33-addr addr expression – Load y into register %r10 – Load z into register %r11 – Perform op %r10 %r10, %r11 – Store %r11 to x Saman Amarasinghe 17 6.035 ©MIT Fall 1998 Issues in Lowering g Expressions p • Map intermediates to registers? – registers are limited • when the tree is large, registers may be insufficient ⇒ allocate space in the stack • No machine instruction is available – May need to expand the intermediate operation into multiple machine ops ops. • Very inefficient – too manyy copies p – don’t worry, we’ll take care of them in the optimization passes – keep k the th code d generator t very simple i l Saman Amarasinghe 18 6.035 ©MIT Fall 1998 What about statements? • Assignment g statements are simple p – Generate code for RHS expression – Store the resultingg value to the LHS address • But what about conditionals and loops? Saman Amarasinghe 19 6.035 ©MIT Fall 1998 28 Outline • Generation of statements • Generation of control flow • Guidelines in writing g a code generator Saman Amarasinghe 20 6.035 ©MIT Fall 1998 Two Approaches pp • Template p Matching g Approach pp – Peephole Optimization • Algorithmic Approach • Both are based on structural induction – Generate a representation for the sub-parts – Combine them into a representation for the whole Saman Amarasinghe 21 6.035 ©MIT Fall 1998 Generation of control flow: T Template l t Matching M t hi Approach A h • Flatten the control structure – use a template • Put unique labels for control join points • Now generate the appropriate code Saman Amarasinghe 22 6.035 ©MIT Fall 1998 29 Temp plate for conditionals if (test) t true_body b d else false_body false body <do the test> joper lab_true l b t <false_body> jmp lab_end lab_true: <true_body> lab ab_end: e d: Saman Amarasinghe 23 6.035 ©MIT Fall 1998 Example Program if(ax > bx) dx = ax - bx; else dx = bx - ax; <do test> joper .L0 Return address previous frame pointer Local variable px (10) Local variable py (20) Local variable pz (30) Argument 9: cx (30) Argument 8: bx (20) Argument 7: ax (10) Return address previous frame pointer Local variable dx (??) L l variable Local i bl dy d (??) Local variable dz (??) <FALSE BODY> jmp .L1 .L0: <TRUE BODY> .L1: Saman Amarasinghe 24 6.035 rbp rsp ©MIT Fall 1998 Example Program if(ax > bx) dx = ax - bx; else dx = bx - ax; movq movq cmpq jg 16(%rbp), %r10 24(%rbp), %r11 %r10, %r11 .L0 Return address previous frame pointer Local variable px (10) Local variable py (20) Local variable pz (30) Argument 9: cx (30) Argument 8: bx (20) Argument 7: ax (10) Return address previous frame pointer Local variable dx (??) L l variable Local i bl dy d (??) Local variable dz (??) <FALSE BODY> jmp .L1 .L0: <TRUE BODY> .L1: Saman Amarasinghe 25 6.035 rbp rsp ©MIT Fall 1998 Example Program if(ax > bx) dx = ax - bx; else dx = bx - ax; movq movq cmpq jg movq movq b subq movq jmp 16(%rbp), %r10 24(%rbp), %r11 %r10, %r11 .L0 24(%rbp), %r10 16(%rbp), %r11 % 10 % 11 %r10, %r11 %r11, -8(%rbp) .L1 Return address previous frame pointer Local variable px (10) Local variable py (20) Local variable pz (30) Argument 9: cx (30) Argument 8: bx (20) Argument 7: ax (10) Return address previous frame pointer Local variable dx (??) L l variable Local i bl dy d (??) Local variable dz (??) .L0: <TRUE BODY> .L1: Saman Amarasinghe 26 6.035 rbp rsp ©MIT Fall 1998 Example Program if(ax > bx) dx = ax - bx; else dx = bx - ax; movq movq cmpq jg movq movq b subq movq jmp 16(%rbp), %r10 24(%rbp), %r11 %r10, %r11 .L0 24(%rbp), %r10 16(%rbp), %r11 % 10 % 11 %r10, %r11 %r11, -8(%rbp) .L1 movq movq subq movq 16(%rbp), %r10 24(%rbp), %r11 %r10, %r11 %r11, -8(%rbp) 8(%rbp) Return address previous frame pointer Local variable px (10) Local variable py (20) Local variable pz (30) Argument 9: cx (30) Argument 8: bx (20) Argument 7: ax (10) Return address previous frame pointer Local variable dx (??) L l variable Local i bl dy d (??) Local variable dz (??) .L0: .L1: Saman Amarasinghe 27 6.035 rbp rsp ©MIT Fall 1998 Template p for while loops p while (test) b d body Saman Amarasinghe 28 6.035 ©MIT Fall 1998 Template p for while loops p while (test) b d body Saman Amarasinghe lab_cont: <do the test> joper lab lab_body body jmp lab_end lab_body: <b d > <body> jmp lab_cont lab_end: 29 6.035 ©MIT Fall 1998 31 Template p for while loops p while (test) b d body lab_cont: <do the test> joper lab_body lab body jmp lab_end lab_body: <body> <b d > jmp lab_cont lab_end: • An optimized template CODE DATA Control Flow Global Static Variables Global Dynamic Data Local Variables Temporaries Parameter Passing Read-only Data Procedures Statements Data Access Saman Amarasinghe lab_cont: _ <do the test> joper lab_end y <body> jmp lab_cont lab_end: 30 6.035 ©MIT Fall 1998 33 Q Question: • What is the template for? do body while (test) Saman Amarasinghe 31 6.035 ©MIT Fall 1998 33 Question: Q • What is the template for? do body while (test) lab_begin: _ <body> <do test> joper lab_begin Saman Amarasinghe 32 6.035 ©MIT Fall 1998 33 Q Question: • What is a drawback of the template based approach? Saman Amarasinghe 33 6.035 ©MIT Fall 1998 Control Flow Graph p (CFG) ( ) • Starting g point: p high g level intermediate format, symbol tables • Target: CFG – – – – CFG Nodes are Instruction Nodes CFG Edges Represent Flow of Control Forks At Conditional Jump Instructions Merges When Flow of Control Can Reach A Point Multiple Ways – Entry and Exit Nodes Saman Amarasinghe 34 6.035 ©MIT Fall 1998 entry if (x < y) { a = 0; } else { a = 1; } jl xxx < cmpp %r10,, %r11 mov $0, a mov $1, a mov x, %r10 Mov y, %r11 exit Pattern for if then else Saman Amarasinghe 35 6.035 ©MIT Fall 1998 Short-Circuit Conditionals • In program, conditionals have a condition written as a boolean expression ((i < n) && (v[i] != 0)) || i > k) • Semantics say should execute only as much as required to determine condition – Evaluate (v[i] != 0) only if (i < n) is true – Evaluate i > k only if ((i < n) && (v[i] != 0)) is false • Use control-flow graph to represent this shortcircuit evaluation Saman Amarasinghe 36 6.035 ©MIT Fall 1998 Short-Circuit Conditionals entry while (i < n && v[i] != 0) { i = i+1; } jl xxx < cmp %r10, %r11 jl yyy < cmp %r10, %r11 mov %r11 %r11, i exit add $1, %r11 mov i, %r11 Saman Amarasinghe 37 6.035 ©MIT Fall 1998 More Short-Circuit Conditionals if (a < b || c != 0) { i = i+1; } entry jl xxx < cmp %r10, %r11 jne yyy < cmp %r10, %r11 mov %r11, i add dd $1, $1 %r11 % 11 mov i, %r11 Saman Amarasinghe 38 exit 6.035 ©MIT Fall 1998 Routines for Destructuring Program R Representation t ti destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form shortcircuit(c, t, f) generates short short-circuit -circuit form of conditional represented by c if c is true, control flows to t node if c is false,, control flows to f node returns b - b is begin node for condition evaluation new kind of node - nop node Saman Amarasinghe 39 6.035 ©MIT Fall 1998 Destructuring g Seqq Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f seq x y seq x Saman Amarasinghe y 40 6.035 ©MIT Fall 1998 Destructuring g Seqq Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f seq x y 1: (bx,ex) = destruct(x); bx seq x Saman Amarasinghe ex y 41 6.035 ©MIT Fall 1998 Destructuring g Seqq Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f seq x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); bx seq x ex y by ey Saman Amarasinghe 42 6.035 ©MIT Fall 1998 Destructuring g Seqq Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f seq x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: next(ex) = by; bx seq ex by x y ey Saman Amarasinghe 43 6.035 ©MIT Fall 1998 Destructuring g Seqq Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f seq x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: next(ex) = by; 4: return (bx, ey); bx seq ex by x y ey Saman Amarasinghe 44 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y if c x Saman Amarasinghe y 45 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); bx if c x Saman Amarasinghe ex y 46 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); if c x Saman Amarasinghe y 47 bx ex by ey 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: e = new nop; bx if c x Saman Amarasinghe y ex e by 48 ey 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: e = new nop; 4: next(ex) = e; 5: next(ey) = e; bx if c x Saman Amarasinghe y ex e by 49 ey 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: e = new nop; 4: next(ex) = e; 5: next(ey) = e; 6 bc = shortcircuit(c, 6: h i i ( bx, by); ) bx if c x Saman Amarasinghe ex bc y e by 50 ey 6.035 ©MIT Fall 1998 Destructuring g If Nodes destruct(n) ggenerates lowered form of structured code represented p by yn returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y 1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: e = new nop; 4: next(ex) = e; 5: next(ey) = e; 6 bc = shortcircuit(c, 6: h i i ( bx, by); ) 7: 7 return (bc, e); ) bx if c x Saman Amarasinghe ex bc y e by 51 ey 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x while c Saman Amarasinghe x 52 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x 1: e = new nop; while c x e Saman Amarasinghe 53 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x 1: e = new nop; 2: (bx,ex) = destruct(x); while c x bx e ex Saman Amarasinghe 54 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x 1: e = new nop; 2: (bx,ex) = destruct(x); 3: bc = shortcircuit(c, shortcircuit(c bx, e); bc while c x bx e ex Saman Amarasinghe 55 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x 1: e = new nop; 2: (bx,ex) = destruct(x); 3: bc = shortcircuit(c, shortcircuit(c bx, e); 4: next(ex) = bc; bc while c x bx e ex Saman Amarasinghe 56 6.035 ©MIT Fall 1998 Destructuring g While Nodes destruct(n) generates lowered form of structured code represented by n returns (b,e) - b is begin node, e is end node in destructed form if n is i off the th form f while hil c x 1: e = new nop; 2: (bx,ex) = destruct(x); 3: bc = shortcircuit(c, shortcircuit(c bx, e); 4: next(ex) = bc; 5: return (bc, e); bc while c x bx e ex Saman Amarasinghe 57 6.035 ©MIT Fall 1998 Shortcircuiting g And Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 && c2 c1 && c2 Saman Amarasinghe 58 6.035 ©MIT Fall 1998 Shortcircuiting g And Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 && c2 1: b2 = shortcircuit(c2, t, f); c1 && c2 b2 f t Saman Amarasinghe 59 6.035 ©MIT Fall 1998 Shortcircuiting g And Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 && c2 1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, b2, f); b1 c1 && c2 b2 f t Saman Amarasinghe 60 6.035 ©MIT Fall 1998 Shortcircuiting g And Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 && c2 1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, b2, f); 3: return (b1); b 1 c1 && c2 b2 f t Saman Amarasinghe 61 6.035 ©MIT Fall 1998 Shortcircuiting g Or Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 || c2 c1 || c2 Saman Amarasinghe 62 6.035 ©MIT Fall 1998 Shortcircuiting g Or Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 || c2 1: b2 = shortcircuit(c2, t, f); c1 || c2 t b2 f Saman Amarasinghe 63 6.035 ©MIT Fall 1998 Shortcircuiting g Or Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 || c2 1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, t, b2); b1 c1 || c2 t b2 f Saman Amarasinghe 64 6.035 ©MIT Fall 1998 Shortcircuiting g Or Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f c1 || c2 1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, t, b2); 3: return (b1); b1 c1 || c2 t b2 f Saman Amarasinghe 65 6.035 ©MIT Fall 1998 Shortcircuiting g Not Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f ! c1 1: b = shortcircuit(c1, f, t); return(b); b ! c1 f Saman Amarasinghe t 66 6.035 ©MIT Fall 1998 Computed p Conditions shortcircuit(c, t, f) generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if c is i off the th form f e1 < e2 1: b = new cbr(e1 < e2, t, f); 2: return (b); jl e1 < e2 cmp t e1 Saman Amarasinghe 67 f e2 6.035 ©MIT Fall 1998 Nops p In Destructured Representation p entry while (i < n && v[i] != 0) { i = i+1; } jl xxx < cmp %r10, %r11 jl yyy nop < cmp %r10, %r11 mov %r11 %r11, i exit add $1, %r11 mov i, %r11 Saman Amarasinghe 68 6.035 ©MIT Fall 1998 Eliminating Nops Via Peephole O ti i ti Optimization ... ... nop Saman Amarasinghe 69 6.035 ©MIT Fall 1998 46 Question: • What are the pros and cons of temp plate matching vs. algorithmic approach? Saman Amarasinghe 70 6.035 ©MIT Fall 1998 49 Outline • • • • Generation of statements Generation of control flow x86-64 Processor Guidelines in writing a code generator Saman Amarasinghe 71 6.035 ©MIT Fall 1998 Guidelines for the code generator g • Lower the abstraction level slowly y – Do many passes, that do few things (or one thing) • Easier to break the project down, generate and debug • Keep the abstraction level consistent – IR should have ‘correct’ correct semantics at all time • At least you should know the semantics – You may want to run some of the optimizations between the passes. • Use assertions liberally – Use an assertion to check your assumption Saman Amarasinghe 72 6.035 ©MIT Fall 1998 Guidelines for the code generator g • Do the simplest but dumb thing – it i is i okk to generate 0 + 1*x 1* + 0*y 0* – Code is painful to look at; let optimizations improve it • Make sure y you know want can be done at… – Compile time in the compiler – Runtime usingg ggenerated code Saman Amarasinghe 73 6.035 ©MIT Fall 1998 Guidelines for the code generator g • Remember that optimizations will come later – Let the optimizer do the optimizations – Think about what optimizer will need and structure your code accordingly – Example: Register allocation, algebraic simplification, constant propagation • Setup a good testing infrastructure – regression tests • If a input program creates a bug, use it as a regression test – Learn good bug hunting procedures • Example: binary search , delta debugging Saman Amarasinghe 74 6.035 ©MIT Fall 1998 MIT OpenCourseWare http://ocw.mit.edu 6.035 Computer Language Engineering Spring 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.