lecture 16 17 code-generation

99
CODE GENERATION LECTURE 16

Upload: iffat-anjum

Post on 08-Feb-2017

496 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Lecture 16 17 code-generation

CODE GENERATIONLECTURE 16

Page 2: Lecture 16 17 code-generation

CODE GENERATIONThe code generation problem is the task of

mapping intermediate code to machine code. Requirements: Correctness

Must preserve semantic meaning of source program

Efficiency Make effective use of available resources Code Generator itself must run efficiently

2

Page 3: Lecture 16 17 code-generation

COMPILER ARCHITECTURE

Scanner(lexical

analysis)

Parser(syntax

analysis)

CodeOptimizer

SemanticAnalysis

(IC generator)

CodeGenerator

SymbolTable

Sourcelanguage

tokensSyntacticstructure

IntermediateLanguage

Targetlanguage

IntermediateLanguage

3

Page 4: Lecture 16 17 code-generation

INPUT TO THE CODE GENERATOR We assume, front end has

Scanned, parsed and translate the source program into a reasonably detailed intermediate representations

Type checking, type conversion and obvious semantic errors have already been detected

Symbol table is able to provide run-time address of the data objects

Intermediate representations may be Postfix notations Three address representations Syntax tree DAG 4

Page 5: Lecture 16 17 code-generation

TARGET PROGRAMS The output of the code generator is the target program. Target architecture: must be well understood

Significantly influences the difficulty of code generation

RISC, CISC Target program may be

Absolute machine language It can be placed in a fixed location of memory and immediately

executedRe-locatable machine language

Subprograms to be compiled separately A set of re-locatable object modules can be linked together

and loaded for execution by a linker5

Page 6: Lecture 16 17 code-generation

ISSUES IN THE DESIGN OF A CODE GENERATOR Instruction Selection Register Allocation Evaluation Order

6

Page 7: Lecture 16 17 code-generation

INSTRUCTION SELECTION There may be a large number of ‘candidate’

machine instructions for a given IR instructionLevel of IR

High: Each IR translates into many machine instructions

Low: Reflects many low-level details of machineNature of the instruction set

Uniformity and completenessEach has own cost and constraints

Accurate cost information is difficult to obtain Cost may be influenced by surrounding context 7

Page 8: Lecture 16 17 code-generation

INSTRUCTION SELECTION For each type of three-address statement, a code

skeleton can be designed that outlines the target code to be generated for that construct. Say, x := y + z

Mov y, R0Add z, R0Mov R0, x

Statement by statement code generation often produces poor code

8

Page 9: Lecture 16 17 code-generation

INSTRUCTION SELECTIONa := b + c

d := a + e

MOV b, R0

ADD c, R0

MOV R0, a

MOV a, R0

ADD e, R0

MOV R0, d

MOV a, R0

MOV R0, a If a is subsequently used

9

Page 10: Lecture 16 17 code-generation

INSTRUCTION SELECTION: MACHINE IDIOMS

10

Page 11: Lecture 16 17 code-generation

REGISTER ALLOCATION How to best use the bounded number of registers. Use or registers

Register allocation We select a set of variables that will reside in registers at each

point in the program Register assignment

We pick the specific register that a variable will reside in. Complications:

special purpose registers operators requiring multiple registers.

Optimal assignment is NP-complete

11

Page 12: Lecture 16 17 code-generation

REGISTER ALLOCATION

r4

12

Page 13: Lecture 16 17 code-generation

REGISTER ALLOCATION

13

Page 14: Lecture 16 17 code-generation

EVALUATION ORDER Choosing the order of instructions to best utilize

resources Picking the optimal order is NP-complete problem

Simplest Approach Don’t mess with re-ordering. Target code will perform all operations in the same order

as the IR code Trickier Approach

Consider re-ordering operations May produce better code

... Get operands into registers just before they are needed... May use registers more efficiently

14

Page 15: Lecture 16 17 code-generation

MOVING RESULTS BACK TO MEMORY When to move results from registers back into

memory? After an operation, the result will be in a register.

Immediately Move data back to memory just after it is

computed. May make more registers available for use

elsewhere. Wait as long as possible before moving it

back Only move data back to memory “at the end”

or “when absolutely necessary” May be able to avoid re-loading it later!

15

Page 16: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #1Simple code generation algorithm:

Define a target code sequence to each intermediate code statement type.

16

Page 17: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #1

17

Page 18: Lecture 16 17 code-generation

EXAMPLE TARGET MACHINE

18

Page 19: Lecture 16 17 code-generation

EVALUATING A POTENTIAL CODE SEQUENCE Each instruction has a “cost”

Cost = Execution Time Execution Time is difficult to predict.

Pipelining, Branches, Delay Slots, etc. Goal: Approximate the real cost

A “Cost Model”

Page 20: Lecture 16 17 code-generation

A BETTER COST MODEL

Page 21: Lecture 16 17 code-generation

COST GENERATION EXAMPLE

21

Page 22: Lecture 16 17 code-generation

BASIC BLOCKS

22

Page 23: Lecture 16 17 code-generation

BASIC BLOCKS

23

Page 24: Lecture 16 17 code-generation

BASIC BLOCKS

24

Page 25: Lecture 16 17 code-generation

CONTROL FLOW GRAPH

25

Page 26: Lecture 16 17 code-generation

ALGORITHM TO PARTITION INSTRUCTIONS INTO BASIC BLOCKS

26

Page 27: Lecture 16 17 code-generation

ALGORITHM TO PARTITION INSTRUCTIONS INTO BASIC BLOCKS INPUT: A sequence of three-address instructions. OUTPUT: A list of the basic blocks for that sequence in which

each instruction is assigned to exactly one basic block. METHOD: First, we determine those instructions in the

intermediate code that are leaders, that is, the first instructions in some basic block. The instruction just past the end of the intermediate program is not included as a leader. The rules for finding leaders are: The first three-address instruction in the intermediate code is a

leader. Any instruction that is the target of a conditional or

unconditional jump is a leader. Any instruction that immediately follows a conditional or

unconditional jump is a leader27

Page 28: Lecture 16 17 code-generation

IDENTIFY LEADERS – EXAMPLE 1:

28

Page 29: Lecture 16 17 code-generation

IDENTIFY LEADERS – EXAMPLE 1:

29

Page 30: Lecture 16 17 code-generation

IDENTIFY LEADERS – EXAMPLE 1:

30

Page 31: Lecture 16 17 code-generation

IDENTIFY LEADERS

31

Page 32: Lecture 16 17 code-generation

IDENTIFY LEADERS – EXAMPLE 2:

32

Page 33: Lecture 16 17 code-generation

IDENTIFY LEADERS – EXAMPLE 2:

According to rule 1 : 1 According to rule 2 : 3, 2,

13 According to rule 3 : 10, 12

33

Page 34: Lecture 16 17 code-generation

LOOK AT EACH BASIC BLOCK IN ISOLATION

34

Page 35: Lecture 16 17 code-generation

COMMON SUB-EXPRESSION ELIMINATION

35

Page 36: Lecture 16 17 code-generation

COMMON SUB-EXPRESSION ELIMINATION

36

Page 37: Lecture 16 17 code-generation

REORDERING INSTRUCTIONS IN A BASIC BLOCK

37

Page 38: Lecture 16 17 code-generation

LIVE VARIABLES

38

Page 39: Lecture 16 17 code-generation

DEAD VARIABLES

39

Page 40: Lecture 16 17 code-generation

LIVENESS EXAMPLE

40

Page 41: Lecture 16 17 code-generation

LIVENESS EXAMPLE

41

Page 42: Lecture 16 17 code-generation

LIVE VARIABLE ANALYSIS

42

Page 43: Lecture 16 17 code-generation

TEMPORARIES

43

Page 44: Lecture 16 17 code-generation

DEAD CODE

44

Page 45: Lecture 16 17 code-generation

TEMPORARIES

45

Page 46: Lecture 16 17 code-generation

ALGEBRAIC TRANSFORMATION

46

Page 47: Lecture 16 17 code-generation

CONTROL FLOW GRAPHS

47

Page 48: Lecture 16 17 code-generation

REPRESENTING FLOW GRAPHS: IDEA 1

48

Page 49: Lecture 16 17 code-generation

REPRESENTING FLOW GRAPHS: IDEA 2

49

Page 50: Lecture 16 17 code-generation

WHAT IS LOOP?

50

Page 51: Lecture 16 17 code-generation

NATURAL LOOP

51

Page 52: Lecture 16 17 code-generation

LOOPS WITH MULTIPLE ENTRIES

52

Page 53: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2 AND #3

53

Page 54: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2

54

Page 55: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2

55

Page 56: Lecture 16 17 code-generation

DEFINITION AND USE OF VARIABLES

56

Page 57: Lecture 16 17 code-generation

THE “NEXT-USE” INFORMATION

57

Page 58: Lecture 16 17 code-generation

THE “NEXT-USE” ALGORITHM

58

Page 59: Lecture 16 17 code-generation

“NEXT-USE” EXAMPLE

59

Page 60: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM

60

Page 61: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM

61

Page 62: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

62

Page 63: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

63

Page 64: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

64

Page 65: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

65

Page 66: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

66

Page 67: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

67

Page 68: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

68

Page 69: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

69

Page 70: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

70

Page 71: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

71

Page 72: Lecture 16 17 code-generation

“NEXT-USE” ALGORITHM - EXAMPLE

D

L ( 1 )72

Page 73: Lecture 16 17 code-generation

WHY LIVE VARIABLE ANALYSIS?

73

Page 74: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2

74

Page 75: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2

75

Page 76: Lecture 16 17 code-generation

DATA NEEDED DURING CODE GENERATION

76

Page 77: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3 (OVERVIEW)

77

Page 78: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3

78

Page 79: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3

79

Page 80: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3

80

Page 81: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3

81

Page 82: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #3

82

Page 83: Lecture 16 17 code-generation

SPECIAL CASE

83

Page 84: Lecture 16 17 code-generation

CODE GENERATION ALGORITHM #2

84

Page 85: Lecture 16 17 code-generation

EXAMPLE

85

Page 86: Lecture 16 17 code-generation

EXAMPLE

86

Page 87: Lecture 16 17 code-generation

EXAMPLE

87

Page 88: Lecture 16 17 code-generation

EXAMPLE

88

Page 89: Lecture 16 17 code-generation

EXAMPLE

89

Page 90: Lecture 16 17 code-generation

EXAMPLE

90

Page 91: Lecture 16 17 code-generation

EXAMPLE

91

Page 92: Lecture 16 17 code-generation

EXAMPLE

92

Page 93: Lecture 16 17 code-generation

EXAMPLE

93

Page 94: Lecture 16 17 code-generation

EXAMPLE

94

Page 95: Lecture 16 17 code-generation

EXAMPLE

95

Page 96: Lecture 16 17 code-generation

EXAMPLE

96

Page 97: Lecture 16 17 code-generation

EXAMPLE

97

Page 98: Lecture 16 17 code-generation

EXAMPLE

98

Page 99: Lecture 16 17 code-generation

ANY QUESTION ?

99