Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Compiler

Construction
Lecture 4
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors

Register Allocation:
•Have each value in a register
when it is used.
2
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors

Register Allocation:
•Manage a limited set of resources
– register file.
3
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors
Register Allocation:
• Can change instruction choices and insert LOADs and STOREs.

4
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors

Register Allocation:
•Optimal register allocation is NP-
Complete.
5
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors

Instruction Scheduling:
•Avoid hardware stalls and
interlocks.
6
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors

Instruction Scheduling:
•Use all functional units
productively.
7
The Back End
IR Instruction IR Register IR Instruction machine
selection allocation scheduling code

errors
Instruction Scheduling:
• Optimal scheduling is
NP-Complete in nearly all cases.

8
Three-pass Compiler
Source Front IR Middle IR Back machine
code End End End code

errors

•Intermediate stage for code


improvement or optimization

9
Three-pass Compiler
Source Front IR Middle IR Back machine
code End End End code

errors

•Analyzes IR and rewrites (or


transforms) IR

10
Three-pass Compiler
Source Front IR Middle IR Back machine
code End End End code

errors

•Primary goal is to reduce running


time of the compiled code

11
Three-pass Compiler
Source Front IR Middle IR Back machine
code End End End code

errors

•May also improve space usage,


power consumption, ...

12
Three-pass Compiler
Source Front IR Middle IR Back machine
code End End End code

errors
• Must preserve “meaning” of the code.
• Measured by values of named variables

13
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
•Modern optimizers are structured
as a series of passes

14
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Discover & propagate some
constant value
15
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Move a computation to a less
frequently executed place
16
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Specialize some computation
based on context
17
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Discover a redundant
computation & remove it
18
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Remove useless or unreachable
code
19
Optimizer
IR
IR Opt IR Opt IR Opt Opt IR
1 2 3 n

errors
Typical transformations
•Encode an idiom in some
particularly efficient form
20
Role of Run-time System
• Memory management
• Allocate/deallocate
• Garbage collection
• Run-time type checking
• Error/exception processing
• Interface to OS – I/O
• Support for parallelism
• Parallel threads
• Communication and synchronization
21
Related to Compilers
• Interpreters (direct execution)
• Assemblers
• Preprocessors
• Text formatters (non-WYSIWYG)
• Analysis tools

22

You might also like