lecture 23 basic blocks topics code generation readings: 9 april 17, 2006 csce 531 compiler...
Post on 20-Dec-2015
214 views
TRANSCRIPT
Lecture 23 Basic Blocks
Lecture 23 Basic Blocks
Topics Topics Code Generation
Readings: 9Readings: 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
– 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
– 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
– 5 – CSCE 531 Spring 2006
Target programsTarget programs
Target ArchitecturesTarget Architectures
CISCCISC
RISCRISC
Target ArchitecturesTarget Architectures
Object modulesObject modules
AssemblyAssembly
– 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
– 7 – CSCE 531 Spring 2006
Memory managementMemory management
– 8 – CSCE 531 Spring 2006
Instruction selectionInstruction selection
– 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
– 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
– 11 – CSCE 531 Spring 2006
Other Cost MetricsOther Cost Metrics
– 12 – CSCE 531 Spring 2006
Run-Time Storage ManagmentRun-Time Storage Managment
– 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
– 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
– 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.
– 16 – CSCE 531 Spring 2006
Example 9.3 – Dot ProductExample 9.3 – Dot Product
– 17 – CSCE 531 Spring 2006
Basic Blocks for Dot Product CodeBasic Blocks for Dot Product Code
– 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
– 19 – CSCE 531 Spring 2006
Flow GraphsFlow Graphs
Successor blockSuccessor block
Predecessor blockPredecessor block
LoopsLoops
Strongly connectedStrongly connected
Inner loopInner loop
– 20 – CSCE 531 Spring 2006
ExampleExample
– 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”
– 22 – CSCE 531 Spring 2006
A Simple Code Generation AlgorithmA Simple Code Generation Algorithm
– 23 – CSCE 531 Spring 2006
GetRegGetReg