unoptimized code generation (cont.) - mit opencoursewareprevious frame pointer return address stack...

75
Unoptimized Code Generation

Upload: others

Post on 16-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Unoptimized Code Generation

Page 2: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

• Last time we left off on the procedurepabstraction …

Saman Amarasinghe 2 6.035 ©MIT Fall 1998

Page 3: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

The Stack • Arguments 0 to 6 argument n8*n+16(%rbp)

viou

s

are in: – %rdi, %rsi, %rdx,

% % 8 d % 9 Return address

… argument 7

8(%rbp)

16(%rbp) Pre

%rcx, %r8 and %r9

% b local 0

Previous %rbp0(%rbp)

-8(%rbp)

urre

nt

%rbp – marks the beginning

of the current frame 0(%rsp)

… local m-8*m-8(%rbp) C

u

of the current frame

%rsp – marks the end

Variable size

Saman Amarasinghe 3 6.035 ©MIT Fall 2001

Page 4: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

33

QQuestion:

• Whyy use a stack? Wh yy not use the heapp or ppre-allocated in the data segment?

Saman Amarasinghe 4 6.035 ©MIT Fall 2001

Page 5: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

t

Procedure Linkagesg

Standard procedure linkage Pre-call:

•Save caller-saved registers p g procedure p

prolog procedure q

Save caller saved registers

•Push arguments

Prolog:

P h ld f i

ll

prolog •Push old frame pointer

•Save calle-saved registers

•Make room for temporaries pre-call

post-return epilog

Epilog:

•Restore callee-saved

•Pop old frame pointer

epilog

Pop old frame pointer

•Store return value

Post-return:

R t ll d

Saman Amarasinghe 5 6.035 ©MIT Fall 2001

•Restore caller-saved

•Pop arguments

Page 6: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointer return address

Stack calliee saved rbp

local variables

registers• Calling: Caller – Assume %rcx is live and

is caller save stack temporaries

dynamic area

ll d i t

– Call foo(A, B, C, D, E, F, G, H, I) • A to I are at -8(%rbp) to -72(%rbp) rsp

caller saved registers argument 9 argument 8 argument 7

push %rcx

push -72(%rbp)push -64(%rbp)

return addresspush 64(%rbp) push -56(%rbp) mov -48(%rbp), %r9 mov -40(%rbp), %r8 mov -32(%rbp), %rcx

mov -24(%rbp), %rdx

mov -16(%rbp), %rsi

8(% b ) % di mov -8(%rbp), %rdi

call foo

Saman Amarasinghe 6 6.035 ©MIT Fall 2001

Page 7: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointer return address

Stack calliee saved rbp

local variables

registers• Calling: Calliee – Assume %rbx is used in the function

d i lli stack temporaries

dynamic area

ll d i t

and is calliee save – Assume 40 bytes are required for locals

caller saved registers argument 9 argument 8 argument 7

foo:

push %rbp

mov %rsp %rbp

previous frame pointer return address

mov %rsp, %rbp

sub $48, %rsp

mov %rbx, -8(%rbp)

rsp

calliee saved registers

enter $48, $0

registers

local variables

stack temporaries

Saman Amarasinghe 7 6.035 ©MIT Fall 2001

dynamic area

Page 8: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointerreturn address

Stack calliee saved

local variables

registers• Arguments• Call foo(A, B, C, D, E, F, G, H, I)

– Passed in by pushing before the call

stack temporaries

dynamic area

ll d i t

push -72(%rbp)

push -64(%rbp)push -56(%rbp) mov -48(%rbp), %r9mov -40(%rbp), %r8mov -32(%rbp), %rcx

caller saved registers argument 9argument 8argument 7

mov -24(%rbp), %rdxmov -16(%rbp), %rsimov -8(%rbp), %rdicall foo

– Access A to F via registers

previous frame pointerreturn address• or put them in local memory

– Access rest using 16+xx(%rbp)

mov 16(%rbp), %rax

mov 24(%rbp), %r10calliee saved

registers

rbp( p),

rsp

registers

local variables

stack temporaries

CODE DATA

Procedures

Control Flow Global Static VariablesGlobal Dynamic Data

Local Variables

Saman Amarasinghe 8 6.035 ©MIT Fall 2001

dynamic arearsp

Statements

Data AccessParameter Passing

Read-only Data

Temporaries

Page 9: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointerreturn address

Stack calliee saved

local variables

registers• Locals and Temporaries

– Calculate the size and allocate stack temporaries

dynamic area

ll d i t

space on the stacksub $48, %rsp

$48 0 caller saved registers argument 9argument 8argument 7

or enter $48, 0

Access using 8 xx(%rbp)previous frame pointer

return address– Access using -8-xx(%rbp)mov -28(%rbp), %r10

mov %r11, -20(%rbp) calliee savedregisters

rbp

rsp

registers

local variables

stack temporaries

CODE DATA

Procedures

Control Flow Global Static VariablesGlobal Dynamic Data

Local Variables

Saman Amarasinghe 9 6.035 ©MIT Fall 2001

dynamic arearsp

Statements

Data AccessParameter Passing

Read-only Data

Temporaries

Page 10: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointer return address

Stack calliee saved

local variables

registers• Returning Calliee – Assume the return value is the first

t stack temporaries

dynamic area

ll d i t

temporary

– Restore the caller saved register caller saved registers

argument 9 argument 8 argument 7

– Put the return value in %rax – Tear-down the call stack

previous frame pointer return address

mov -8(%rbp), %rbx

mov -16(%rbp), %rax calliee saved registers

rbp

mov %rbp, %rsp

pop %rbp

ret rsp

registers

local variables

stack temporaries

leave

Saman Amarasinghe 10 6.035 ©MIT Fall 2001

rsp dynamic area

Page 11: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

previous frame pointerreturn address

Stack calliee savedrbp

local variables

registers• Returning Caller• (Assume the return value goes to the first

t ) stack temporaries

dynamic area

ll d i t

temporary)– Restore the stack to reclaim the

argument spacecaller saved registers

argument 9argument 8argument 7

– Restore the caller save registers– Save the return value

rsp

call foo CODE DATA

add $24, %rsp

pop %rcx

mov %rax, 8(%rbp)

Control Flow

Procedures

Statements

Global Static VariablesGlobal Dynamic Data

Local Variables

Parameter PassingTemporaries

Saman Amarasinghe 11 6.035 ©MIT Fall 2001

p

…Data Access

gRead-only Data

Page 12: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

47

QQuestion:

• Do yyou need the $rbpp? • What are the advantages and disadvantages of

having $rbp?having $rbp?

Saman Amarasinghe 12 6.035 ©MIT Fall 2001

Page 13: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

So far we covered..CODE DATA

Procedures Global Static Variables

Global Dynamic DataControl Flow

Global Dynamic Data

Local Variables

Statements Temporaries

Parameter Passing

Data Access

Parameter Passing

Read-only Data

Saman Amarasinghe 13 6.035 ©MIT Fall 1998

Page 14: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Outline 8

• 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

Page 15: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Expressionsp

• Expressions are represented as treesp p – Expression may produce a value – Or, it may set the condition codes (boolean exprs), y ( p )

• How do you map expression trees to the machines? – How to arrange the evaluation order?

Wh t k th i t di t l ?– Where to keep the intermediate values? • Two approaches

– Stack Model – Flat List Model

Saman Amarasinghe 15 6.035 ©MIT Fall 1998

Page 16: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Evaluating expression treesg p

• Stack model– Eval left-sub-tree

Put the results on the stack– Eval right-sub-tree

Put the results on the stackOP

– Get top two values from the stackperform the operation OP

h l h kput the results on the stack• Very inefficient!

Saman Amarasinghe 16 6.035 ©MIT Fall 1998

Page 17: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Evaluating expression treesg p• Flat List Model

– The idea is to linearize the expression tree – Left to Right Depth-First Traversal of the expression tree

All i f i di ( ll h d f h )• Allocate temporaries for intermediates (all the nodes of the 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 3-addr expressionCode generation for the 3 addr expression

– Load y into register %r10 – Load z into register %r11 – Perform op %r10 %r11

Saman Amarasinghe 17 6.035 ©MIT Fall 1998

Perform op %r10, %r11

– Store %r11 to x

Page 18: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Issues in Lowering Expressionsg 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 opsmachine ops.• Very inefficient

– too many copiesy p– don’t worry, we’ll take care of them in the optimization

passesk th d t i l

Saman Amarasinghe 18 6.035 ©MIT Fall 1998

– keep the code generator very simple

Page 19: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

What about statements?

• Assignment statements are simpleg p – Generate code for RHS expression – Store the resulting value to the LHS address g

• But what about conditionals and loops?• But what about conditionals and loops?

Saman Amarasinghe 19 6.035 ©MIT Fall 1998

Page 20: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

28

Outline • Generation of statements • Generation of control flow • Guidelines in writingg a code ggenerator

Saman Amarasinghe 20 6.035 ©MIT Fall 1998

Page 21: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Two Approachespp

• Template Matching Approachp g 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

Page 22: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Generation of control flow:

T l t M t hi A h Template Matching Approach• Flatten the control structure

• Now generate the appropriate code Put unique labels for control join points– use a template

• Put unique labels for control join points

Saman Amarasinghe 22 6.035 ©MIT Fall 1998

Page 23: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

ab e d:

Tempplate for conditionals 29

if (test)t b d

<do the test> jjoper l b t

false_body

true_bodyelse false body

Saman Amarasinghe 23 6.035 ©MIT Fall 1998

lab_true <false_body>

_

jmp lab_end lab_true:

<true_body>lab end:

Page 24: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Example Programif(ax > bx)if(ax > bx)

dx = ax - bx;else

dx = bx - ax;

<do test>

joper .L0

<FALSE BODY>

previous frame pointerReturn address

Local variable px (10) Local variable py (20)<FALSE BODY>

jmp .L1.L0:

Argument 9: cx (30)Argument 8: bx (20)

Local variable py (20) Local variable pz (30)

<TRUE BODY> previous frame pointerReturn address rbp

Argument 7: ax (10)

Local variable dx (??) L l i bl d (??)

Saman Amarasinghe 24 6.035 ©MIT Fall 1998

.L1: rspLocal variable dy (??) Local variable dz (??)

Page 25: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Example Programif(ax > bx)if(ax > bx)

dx = ax - bx;else

dx = bx - ax;

movq 16(%rbp), %r10movq 24(%rbp), %r11cmpq %r10, %r11cmpq %r10, %r11jg .L0

<FALSE BODY>

previous frame pointerReturn address

Local variable px (10) Local variable py (20)<FALSE BODY>

jmp .L1.L0:

Argument 9: cx (30)Argument 8: bx (20)

Local variable py (20) Local variable pz (30)

<TRUE BODY> previous frame pointerReturn address rbp

Argument 7: ax (10)

Local variable dx (??) L l i bl d (??)

Saman Amarasinghe 25 6.035 ©MIT Fall 1998

.L1: rspLocal variable dy (??) Local variable dz (??)

Page 26: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Example Programif(ax > bx)if(ax > bx)

dx = ax - bx;else

dx = bx - ax;

movq 16(%rbp), %r10movq 24(%rbp), %r11cmpq %r10, %r11cmpq %r10, %r11jg .L0movq 24(%rbp), %r10movq 16(%rbp), %r11

b % 10 % 11

previous frame pointerReturn address

Local variable px (10) Local variable py (20)subq %r10, %r11

movq %r11, -8(%rbp)jmp .L1

.L0:

Argument 9: cx (30)Argument 8: bx (20)

Local variable py (20) Local variable pz (30)

<TRUE BODY> previous frame pointerReturn address rbp

Argument 7: ax (10)

Local variable dx (??) L l i bl d (??)

Saman Amarasinghe 26 6.035 ©MIT Fall 1998

.L1: rspLocal variable dy (??) Local variable dz (??)

Page 27: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Example Programif(ax > bx)if(ax > bx)

dx = ax - bx;else

dx = bx - ax;

movq 16(%rbp), %r10movq 24(%rbp), %r11cmpq %r10, %r11cmpq %r10, %r11jg .L0movq 24(%rbp), %r10movq 16(%rbp), %r11

b % 10 % 11

previous frame pointerReturn address

Local variable px (10) Local variable py (20)subq %r10, %r11

movq %r11, -8(%rbp)jmp .L1

.L0:

Argument 9: cx (30)Argument 8: bx (20)

Local variable py (20) Local variable pz (30)

movq 16(%rbp), %r10movq 24(%rbp), %r11subq %r10, %r11movq %r11, -8(%rbp)

previous frame pointerReturn address rbp

Argument 7: ax (10)

Local variable dx (??) L l i bl d (??)

Saman Amarasinghe 27 6.035 ©MIT Fall 1998

movq %r11, 8(%rbp).L1: rsp

Local variable dy (??) Local variable dz (??)

Page 28: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Template for while loopsp p while (test)b dbody

Saman Amarasinghe 28 6.035 ©MIT Fall 1998

Page 29: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Template for while loopsp p lab_cont:

while (test) <do the test>

b d joper lab_bodyjoper lab body body jmp lab_end

lab_body: <b d > <body> jmp lab_cont

lab_end:

Saman Amarasinghe 29 6.035 ©MIT Fall 1998

Page 30: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Template for while loops31

p pwhile (test)b d

lab_cont:<do the test>joper lab bodybody joper lab_bodyjmp lab_end

lab_body:<b d ><body>jmp lab_cont

lab_end:

• An optimized template

lab_cont:_<do the test>joper lab_end<body>

CODE DATA

Procedures

Control Flow Global Static VariablesGlobal Dynamic Data

Local Variables

Saman Amarasinghe 30 6.035 ©MIT Fall 1998

yjmp lab_cont

lab_end:

Statements

Data AccessParameter Passing

Read-only Data

Temporaries

Page 31: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

33

Question:Q • What is the template for?

do bodybody

while (test)

Saman Amarasinghe 31 6.035 ©MIT Fall 1998

Page 32: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Question: 33

Q • What is the template for?

do bodybody

while (test)

lab_begin:_<body><do test>

Saman Amarasinghe 32 6.035 ©MIT Fall 1998

joper lab_begin

Page 33: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

33

Question:Q • What is a drawback of the template based approach?approach?

Saman Amarasinghe 33 6.035 ©MIT Fall 1998

Page 34: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Control Flow Graph (CFG)p ( )

• Starting point: high level intermediate format,g p g symbol tables

• Target: CFGTarget: CFG – CFG Nodes are Instruction Nodes – CFG Edges Represent Flow of ControlCFG Edges Represent Flow of Control – Forks At Conditional Jump Instructions

Merges When Flow of Control Can Reach A PointMerges When Flow of Control Can Reach A Point Multiple Ways

– Entry and Exit Nodes

Saman Amarasinghe 34 6.035 ©MIT Fall 1998

Entry and Exit Nodes

Page 35: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

if (x < y) {

entry

jl xxx if (x < y) { a = 0;

} else { <

jl xxx

cmp %r10, %r11 } { a = 1;

} mov x, %r10 Mov y, %r11 mov $0, a mov $1, a

p ,

exit

Saman Amarasinghe 35 6.035 ©MIT Fall 1998

Pattern for if then else

Page 36: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

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

falsefalse • Use control-flow graph to represent this short-

circuit evaluationcircuit evaluation

Saman Amarasinghe 36 6.035 ©MIT Fall 1998

Page 37: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Short-Circuit Conditionals while (i < n && v[i] != 0) {

i = i+1; entry

i = i+1; }

<

jl xxx

< cmp %r10, %r11

jl yyy

mov %r11 i

< cmp %r10, %r11

exit mov %r11, i

add $1, %r11

Saman Amarasinghe 37 6.035 ©MIT Fall 1998

mov i, %r11

Page 38: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

More Short-Circuit Conditionals if (a < b || c != 0) {

i = i+1; entry

i = i+1; } jl xxx

< cmp %r10, %r11

<

jne yyy

< cmp %r10, %r11

mov %r11, i

dd $1 % 11

exit

add $1, %r11

mov i, %r11

Saman Amarasinghe 38 6.035 ©MIT Fall 1998

Page 39: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

-

Routines for Destructuring Program R t tiRepresentation

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-circuit form of conditional represented by cgenerates short 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

Saman Amarasinghe 39 6.035 ©MIT Fall 1998

new kind of node - nop node

Page 40: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring Seq Nodesg q 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 i f th fif n is of the form seq x y

seq

x y

Saman Amarasinghe 40 6.035 ©MIT Fall 1998

Page 41: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring Seq Nodesg q 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 i f th fif n is of the form seq x y

1: (bx,ex) = destruct(x);

seq bx

e x y ex

Saman Amarasinghe 41 6.035 ©MIT Fall 1998

Page 42: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring Seq Nodesg q 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 i f th fif n is of the form seq x y

1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y);

seq bx

e b x y ex by

ey

Saman Amarasinghe 42 6.035 ©MIT Fall 1998

Page 43: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring Seq Nodesg q 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 i f th fif n is of the form seq x y

1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: next(e ) = b ;3: next(ex) by;

seq bx

e b x y ex by

ey

Saman Amarasinghe 43 6.035 ©MIT Fall 1998

Page 44: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring Seq Nodesg q 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 i f th fif n is of the form seq x y

1: (bx,ex) = destruct(x); 2: (by,ey) = destruct(y); 3: next(e ) = b ; 4: return (b e );3: next(ex) by; 4: return (bx, ey);

seq bx

e b x y ex by

ey

Saman Amarasinghe 44 6.035 ©MIT Fall 1998

Page 45: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y returns (b,e) - b is begin node, e is end node in destructed form if n is of the form if c x y

ifif c yx

Saman Amarasinghe 45 6.035 ©MIT Fall 1998

Page 46: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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);

if bx ex if

c yx

Saman Amarasinghe 46 6.035 ©MIT Fall 1998

Page 47: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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 bx ex if

c yx by ey

Saman Amarasinghe 47 6.035 ©MIT Fall 1998

Page 48: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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;

if bx ex if

c y e

x by ey

Saman Amarasinghe 48 6.035 ©MIT Fall 1998

Page 49: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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;

if bx ex if

c y e

x by ey

Saman Amarasinghe 49 6.035 ©MIT Fall 1998

Page 50: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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 b h i i ( b b )6: bc = shortcircuit(c, bx, by);

if b bx ex if

c y bc e

x by ey

Saman Amarasinghe 50 6.035 ©MIT Fall 1998

Page 51: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring If Nodesgdestruct(n)

generates lowered form of structured code represented by ng p y 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 b h i i ( b b ) 7 (b )6: bc = shortcircuit(c, bx, by); 7: return (bc, e);

if b bx ex if

c y bc e

x by ey

Saman Amarasinghe 51 6.035 ©MIT Fall 1998

Page 52: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

while

c x

Saman Amarasinghe 52 6.035 ©MIT Fall 1998

Page 53: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

1: e = new nop;

while

c x

e

Saman Amarasinghe 53 6.035 ©MIT Fall 1998

Page 54: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

1: e = new nop; 2: (bx,ex) = destruct(x);

while

c x

e bx

Saman Amarasinghe 54 6.035 ©MIT Fall 1998

ex

Page 55: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

1: e = new nop; 2: (bx,ex) = destruct(x); 3: b = shortcircuit(c b e);3: bc shortcircuit(c, bx, e);

while bc

c x

e bx

Saman Amarasinghe 55 6.035 ©MIT Fall 1998

ex

Page 56: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

1: e = new nop; 2: (bx,ex) = destruct(x); 3: b = shortcircuit(c b e); 4: next(e ) = b ;3: bc shortcircuit(c, bx, e); 4: next(ex) bc;

while bc

c x

e bx

Saman Amarasinghe 56 6.035 ©MIT Fall 1998

ex

Page 57: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Destructuring While Nodes gdestruct(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 i f th f hilif n is of the form while c x

1: e = new nop; 2: (bx,ex) = destruct(x); 3: b = shortcircuit(c b e); 4: next(e ) = b ; 5: return (b e);3: bc shortcircuit(c, bx, e); 4: next(ex) bc; 5: return (bc, e);

while bc

c x

e bx

Saman Amarasinghe 57 6.035 ©MIT Fall 1998

ex

Page 58: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting And Conditions gshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f &&if c is of the form c1 && c2

c1 && c21 2

Saman Amarasinghe 58 6.035 ©MIT Fall 1998

Page 59: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting And Conditions gshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f &&if c is of the form c1 && c2

1: b2 = shortcircuit(c2, t, f);

c1 && c2 b 1 2

f b2

Saman Amarasinghe 59 6.035 ©MIT Fall 1998

t

Page 60: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting And Conditions gshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f &&if c is of the form c1 && c2

1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, b2, f);

b

c1 && c2

b1

b 1 2

f b2

Saman Amarasinghe 60 6.035 ©MIT Fall 1998

t

Page 61: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting And Conditions gshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f &&if c is of the form c1 && c2

1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, b2, f); 3: return (b ); b 3: return (b1);

c1 && c2

b1

b 1 2

f b2

Saman Amarasinghe 61 6.035 ©MIT Fall 1998

t

Page 62: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting Or Conditionsgshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f ||if c is of the form c1 || c2

c1 || c21 || 2

Saman Amarasinghe 62 6.035 ©MIT Fall 1998

Page 63: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting Or Conditionsgshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f ||if c is of the form c1 || c2

1: b2 = shortcircuit(c2, t, f);

c1 || c2 b 1 || 2 b2t

Saman Amarasinghe 63 6.035 ©MIT Fall 1998

f

Page 64: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting Or Conditionsgshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f ||if c is of the form c1 || c2

1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, t, b2); b

c1 || c2

b1

b 1 || 2 b2t

Saman Amarasinghe 64 6.035 ©MIT Fall 1998

f

Page 65: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting Or Conditionsgshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f ||if c is of the form c1 || c2

1: b2 = shortcircuit(c2, t, f); 2: b1 = shortcircuit(c1, t, b2); 3: return (b ); b3: return (b1);

c1 || c2

b1

b 1 || 2 b2t

Saman Amarasinghe 65 6.035 ©MIT Fall 1998

f

Page 66: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Shortcircuiting Not Conditionsgshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f !if c is of the form ! c1

1: b = shortcircuit(c1, f, t); return(b);

b ! c1

f t

Saman Amarasinghe 66 6.035 ©MIT Fall 1998

Page 67: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Computed Conditionspshortcircuit(c, t, f)

generates shortcircuit form of conditional represented by c returns b - b is begin node of shortcircuit form if i f th f <if c is of the form e1 < e2

1: b = new cbr(e1 < e2, t, f); 2: return (b);

e1 < e2 cmp

jl

t f e1 e2

cmpt f

Saman Amarasinghe 67 6.035 ©MIT Fall 1998

Page 68: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Nops In Destructured Representationp p while (i < n && v[i] != 0) {

i = i+1; entry

i = i+1; }

<

jl xxx

< cmp %r10, %r11

jl yyy

nop mov %r11 i

< cmp %r10, %r11

exit mov %r11, i

add $1, %r11

Saman Amarasinghe 68 6.035 ©MIT Fall 1998

mov i, %r11

Page 69: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Eliminating Nops Via Peephole O ti i tiOptimization

... ...

nop

Saman Amarasinghe 69 6.035 ©MIT Fall 1998

Page 70: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

46

QQuestion:

• What are the ppros and cons of tempplate matching vs. algorithmic approach?

Saman Amarasinghe 70 6.035 ©MIT Fall 1998

Page 71: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Outline 49

• Generation of statements • Generation of control flow • x86-64 Processor • Guidelines in writing a code generator

Saman Amarasinghe 71 6.035 ©MIT Fall 1998

Page 72: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Guidelines for the code generatorg

• Lower the abstraction level slowlyy – 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’ semantics at all timeIR should have correct semantics at all time

• At least you should know the semantics

– You may want to run some of the optimizationsYou may want to run some of the optimizations between the passes.

• Use assertions liberally

Saman Amarasinghe 72 6.035 ©MIT Fall 1998

Use assertions liberally – Use an assertion to check your assumption

Page 73: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Guidelines for the code generatorg• Do the simplest but dumb thing

i i k 0 1* 0*– it is ok to generate 0 + 1*x + 0*y – Code is painful to look at; let optimizations improve it

• Make sure you know want can be done at…y– Compile time in the compiler – Runtime using generated codeg g

Saman Amarasinghe 73 6.035 ©MIT Fall 1998

Page 74: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

Guidelines for the code generatorg• Remember that optimizations will come later

Let the optimizer do the optimizations– Let the optimizer do the optimizations – Think about what optimizer will need and structure your

code accordinglycode 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

Saman Amarasinghe 74 6.035 ©MIT Fall 1998

• Example: binary search , delta debugging

Page 75: Unoptimized code generation (cont.) - MIT OpenCourseWareprevious frame pointer return address Stack calliee saved local variables registers • Locals and Temporaries – Calculate

MIT OpenCourseWarehttp://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.