Docsity

Instruction Selection - Compilers - Code Generation - Lecture Slides, Slides of Compilers

Partial preview of the text

Download Instruction Selection - Compilers - Code Generation - Lecture Slides and more Slides Compilers in PDF only on Docsity!

COMPILERS

Instruction Selection

Introduction

 IR expresses only one operation in each

node.

 MC performs several IR instructions in a

single MC instruction.

 e.g., fetch and add

BINOP

PLUS e CONST

i

MEM

Jouette Architecture 1/

Name Effect Trees

TEMP

ADD^ ri rj + rk r * MUL i rj * rk

SUB ri rj - rk r / DIV i rj /^ rk

ADDI

CONST

CONST

r CONST i rj + c

SUBI ri^ rj^ - c^ - CONST LOAD ri M[rj + c]

CONST

CONST

CONST

MEM MEM MEM MEM

Note: All tiles on this page have an upward link like ADD

Jouette Architecture 2/

CONST

CONST

CONST

MEM MEM MEM MEM

MOVE MOVE MOVE MOVE

Name Effect Trees

STORE M[rj + c] ri

MOVEM M[rj] M[ri] MEM

MOVE MEM

Tiled Tree 1

MOVE
MEM MEM
MEM

FP CONST a

TEMP i CONST 4

FP CONST x

LOAD r 1 M[fp + a] ADDI MUL ADD LOAD STORE

r 2 r 0 + 4 r 2 r 2 * r 3 r 1 r 1 + r 2 r 4 M[fp + x] M[r 1 + 0] r 4

Operation: a[i] = x

Tiled Tree 2

MOVE
MEM MEM
MEM

FP CONST a

TEMP i CONST 4

FP CONST x

LOAD r 1 M[fp + a] ADDI MUL ADD ADDI MOVEM

r 2 r 0 + 4 r 2 r 2 * r 3 r 1 r 1 + r 2 r 4 fp + x Operation: a[i] = x M[r 1 ] M[r 4 ]

Maximal Munch Algorithm

 Start at the root.

 Find the largest tile that fits.

 Cover the root and possibly several other

nodes with this tile.

 Repeat for each subtree.

 Generates instructions in reverse order.

 If two tiles of equal size match the current

node, choose either.

Maximal Munch Example

CONST 1

MEM

CONST 2

MEM is matched by LOAD (^) +

CONST

MEM

CONST (2) is matched by ADDI

Instructions emitted (in reverse order) are: ADDI r 1  r 0 + 2

LOAD r 2  M[r 1 + 1]

Note: In Jouette, r 0 is always zero!

Dynamic Programming Example 1/

CONST 1

MEM

CONST 2

CONST is only matched by an ADDI instruction with cost 1 The + node can be matched by

CONST

CONST

ADD cost 1 leaves 2 Total 3

ADDI cost 1 leaves 1 Total 2

ADDI cost 1 leaves 1 Total 2

Dynamic Programming Example 2/

The MEM node can be matched by MEM

MEM

CONST

CONST

MEM

LOAD cost 1 leaves 2 Total 3

LOAD cost 1 leaves 1 Total 2

LOAD cost 1 leaves 1 Total 2

Instructions emitted (in reverse order, in second pass) are:

ADDI r 1  r 0 + 1

LOAD r 2  M[r 1 + 2]

Handling CISC Machine Code

 Fewer registers:

 E.g., Pentium has only 6 general registers

 Allocate TEMPs and solve problem later!

 Register use is restricted:

 E.g., MUL on Pentium requires use of eax

 Introduce additional LOAD/MOVE instructions

to copy values.

 Complex addressing modes:

 E.g., Pentium allows ADD [ebp-8],ecx

 Simple code generation still works, but is not

as size-efficient, and can trash registers.

Implementation Issues

 If registers are allocated after instruction

selection, generated code must have

“holes”.

 Assembly code template: LOAD d0,s

 List of source registers: s

 List of destination registers: d

 (^) Including registers trashed by instruction (e.g., return address and return value registers for CALLs)

 Register allocation will then fill in the

holes, by (simplistically) matching source

and destination registers and eliminating

redundancy.