lecture 23 basic blocks topics code generation readings: 9 april 17, 2006 csce 531 compiler...

23
Lecture 23 Basic Blocks Topics Topics Code Generation Readings: 9 Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

Post on 20-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

Lecture 23 Basic Blocks

Lecture 23 Basic Blocks

Topics Topics Code Generation

Readings: 9Readings: 9

April 17, 2006

CSCE 531 Compiler Construction

Page 2: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 2 – CSCE 531 Spring 2006

OverviewOverviewLast Time – Lec22 slides 1-14, 15-16Last Time – Lec22 slides 1-14, 15-16

Finishing touches on Arrays in expressions Project 5

Today’s Lecture Today’s Lecture Questions on Project 5 – Functions Code Generation

References: Chapter 9References: Chapter 9

Page 3: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 3 – CSCE 531 Spring 2006

Code GenerationCode Generation

Chapter 9Chapter 9

Issues in Code GenerationIssues in Code Generation

Input to code generatorInput to code generator

Target programsTarget programs

Memory managementMemory management

Instruction selectionInstruction selection

Register allocationRegister allocation

Page 4: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 4 – CSCE 531 Spring 2006

Input to code generatorInput to code generator

QuadruplesQuadruples

TriplesTriples

Indirect triplesIndirect triples

Parse trees Parse trees

Syntax treesSyntax trees

Mixtures – some trees some quadsMixtures – some trees some quads

Page 5: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 5 – CSCE 531 Spring 2006

Target programsTarget programs

Target ArchitecturesTarget Architectures

CISCCISC

RISCRISC

Target ArchitecturesTarget Architectures

Object modulesObject modules

AssemblyAssembly

Page 6: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 6 – CSCE 531 Spring 2006

Target Machine ArchitectureTarget Machine Architecture

RISC vs CISCRISC vs CISC

Byte addressable, word addressable?Byte addressable, word addressable?

Byte order? Big Endian vs littleByte order? Big Endian vs little

Address Modes supported by architectureAddress Modes supported by architecture Absolute Register Indexed d(R) d + contents(R) Indirect register *d(R) contents(d + contents(R))

Cost of address modes in references to memoryCost of address modes in references to memory

Page 7: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 7 – CSCE 531 Spring 2006

Memory managementMemory management

Page 8: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 8 – CSCE 531 Spring 2006

Instruction selectionInstruction selection

Page 9: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 9 – CSCE 531 Spring 2006

Instruction CostsInstruction Costs

Floating point costsFloating point costs FPadd = 2 time units FPmultiply = 5 time units FPdivide=9 time units

Page 521 – costs measured in instruction length Page 521 – costs measured in instruction length (somewhat dated, but simple to understand)(somewhat dated, but simple to understand)

mov R0, R1 mov R0, R1 - cost = 1- cost = 1

mov R5, memmov R5, mem - cost = 2- cost = 2

add $1, r3add $1, r3 - cost = 2- cost = 2

sub 4(R0), *12(R1) - cost =3sub 4(R0), *12(R1) - cost =3

Page 10: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 10 – CSCE 531 Spring 2006

Instruction Costs of Blocks: a = b + cInstruction Costs of Blocks: a = b + c

mov b, R0mov b, R0

addadd c, R0c, R0

movmov R0, aR0, a

Page 11: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 11 – CSCE 531 Spring 2006

Other Cost MetricsOther Cost Metrics

Page 12: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 12 – CSCE 531 Spring 2006

Run-Time Storage ManagmentRun-Time Storage Managment

Page 13: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 13 – CSCE 531 Spring 2006

HeapHeap

Really Operating System Problem not Compilers jobReally Operating System Problem not Compilers job

System CallsSystem Calls ptr = malloc(size) free(ptr) Free space list

What functionality the compiler needs to supply?What functionality the compiler needs to supply? Just pointers, function calls

Page 14: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 14 – CSCE 531 Spring 2006

Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs

To generate better code, we will need to analyze the To generate better code, we will need to analyze the structure of code written as list of quadruplesstructure of code written as list of quadruples

A Basic Block is a sequence of consecutive A Basic Block is a sequence of consecutive statements in which flow of control enters at the statements in which flow of control enters at the beginning and leaves at the end without possibility beginning and leaves at the end without possibility of branching except at the endof branching except at the end

Define/Use: a:= b + cDefine/Use: a:= b + c This statement defines “a” It uses “b” and “c”

A name (identifier) is said to be live at a given point if A name (identifier) is said to be live at a given point if its value is after that point in the programits value is after that point in the program

Page 15: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 15 – CSCE 531 Spring 2006

Leaders of the BlockLeaders of the Block

A leader is the first statement of a basic block.A leader is the first statement of a basic block.

The algorithm for Basic BlocksThe algorithm for Basic Blocks

1.1. Determine the leaders first.Determine the leaders first.i. The first statement is a leader.

ii. Any statement that is the target of a branch is a leader.

iii. Any statement immediately following a branch is a leader.

2.2. For each leader the block extends from the leader For each leader the block extends from the leader up to the statement just prior to the next leader.up to the statement just prior to the next leader.

Page 16: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 16 – CSCE 531 Spring 2006

Example 9.3 – Dot ProductExample 9.3 – Dot Product

Page 17: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 17 – CSCE 531 Spring 2006

Basic Blocks for Dot Product CodeBasic Blocks for Dot Product Code

Page 18: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 18 – CSCE 531 Spring 2006

Transformations on Basic BlocksTransformations on Basic Blocks

Common subexpression eliminationCommon subexpression elimination

Dead-code eliminationDead-code elimination

Renaming temporary variablesRenaming temporary variables

Reordering independent statementsReordering independent statements

Page 19: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 19 – CSCE 531 Spring 2006

Flow GraphsFlow Graphs

Successor blockSuccessor block

Predecessor blockPredecessor block

LoopsLoops

Strongly connectedStrongly connected

Inner loopInner loop

Page 20: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 20 – CSCE 531 Spring 2006

ExampleExample

Page 21: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 21 – CSCE 531 Spring 2006

Computing Next UsesComputing Next Uses

Scan block backwards from last statement to leaderScan block backwards from last statement to leader

For statement (i) x := y op zFor statement (i) x := y op z Attach to statement I, current info from symbol table on the

liveness of x y and z In the symbol table Mark x “not live” = “no next use” In the symbol table change next uses of y and z to (i)

“statement number i”

Page 22: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 22 – CSCE 531 Spring 2006

A Simple Code Generation AlgorithmA Simple Code Generation Algorithm

Page 23: Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

– 23 – CSCE 531 Spring 2006

GetRegGetReg