compiler construction dr. naveed ejaz lecture 4. 2 the back end register allocation: have each...
TRANSCRIPT
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Dr. Naveed Ejaz
Lecture 4
2
The Back EndThe Back EndThe Back EndThe Back End
Register Allocation: Have each value in a register
when it is used.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
3
The Back EndThe Back EndThe Back EndThe Back End
Register Allocation: Manage a limited set of
resources – register file.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
4
The Back EndThe Back EndThe Back EndThe Back End
Register Allocation: Can change instruction choices
and insert LOADs and STOREs.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
5
The Back EndThe Back EndThe Back EndThe Back End
Register Allocation: Optimal register allocation is
NP-Complete.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
6
The Back EndThe Back EndThe Back EndThe Back End
Instruction Scheduling: Avoid hardware stalls and
interlocks.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
7
The Back EndThe Back EndThe Back EndThe Back End
Instruction Scheduling: Use all functional units
productively.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
8
The Back EndThe Back EndThe Back EndThe Back End
Instruction Scheduling: Optimal scheduling is
NP-Complete in nearly all cases.
Instructionselection
IR machine code
errors
Registerallocation
Instructionscheduling
IR IR
9
Three-pass CompilerThree-pass CompilerThree-pass CompilerThree-pass Compiler
Intermediate stage for code improvement or optimization
FrontEnd
Sourcecode
machine code
errors
MiddleEnd
BackEnd
IR IR
10
Three-pass CompilerThree-pass CompilerThree-pass CompilerThree-pass Compiler
Analyzes IR and rewrites (or transforms) IR
FrontEnd
Sourcecode
machine code
errors
MiddleEnd
BackEnd
IR IR
11
Three-pass CompilerThree-pass CompilerThree-pass CompilerThree-pass Compiler
Primary goal is to reduce running time of the compiled code
FrontEnd
Sourcecode
machine code
errors
MiddleEnd
BackEnd
IR IR
12
Three-pass CompilerThree-pass CompilerThree-pass CompilerThree-pass Compiler
May also improve space usage, power consumption, ...
FrontEnd
Sourcecode
machine code
errors
MiddleEnd
BackEnd
IR IR
13
Three-pass CompilerThree-pass CompilerThree-pass CompilerThree-pass Compiler
Must preserve “meaning” of the code.
Measured by values of named variables
FrontEnd
Sourcecode
machine code
errors
MiddleEnd
BackEnd
IR IR
14
OptimizerOptimizerOptimizerOptimizer
Modern optimizers are structured as a series of passes
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
15
OptimizerOptimizerOptimizerOptimizer
Typical transformations Discover & propagate some
constant value
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
16
OptimizerOptimizerOptimizerOptimizer
Typical transformations Move a computation to a less
frequently executed place
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
17
OptimizerOptimizerOptimizerOptimizer
Typical transformations Specialize some computation
based on context
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
18
OptimizerOptimizerOptimizerOptimizer
Typical transformations Discover a redundant
computation & remove it
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
19
OptimizerOptimizerOptimizerOptimizer
Typical transformations Remove useless or
unreachable code
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
20
OptimizerOptimizerOptimizerOptimizer
Typical transformations Encode an idiom in some
particularly efficient form
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
21
Related to CompilersRelated to CompilersRelated to CompilersRelated to Compilers Interpreters (direct execution) Assemblers Preprocessors Text formatters (non-
WYSIWYG) Analysis tools