compiler construction dr. naveed ejaz lecture 4. 2 the back end register allocation: have each...
TRANSCRIPT
![Page 1: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/1.jpg)
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Dr. Naveed Ejaz
Lecture 4
![Page 2: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/2.jpg)
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
![Page 3: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/3.jpg)
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
![Page 4: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/4.jpg)
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
![Page 5: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/5.jpg)
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
![Page 6: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/6.jpg)
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
![Page 7: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/7.jpg)
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
![Page 8: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/8.jpg)
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
![Page 9: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/9.jpg)
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
![Page 10: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/10.jpg)
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
![Page 11: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/11.jpg)
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
![Page 12: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/12.jpg)
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
![Page 13: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/13.jpg)
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
![Page 14: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/14.jpg)
14
OptimizerOptimizerOptimizerOptimizer
Modern optimizers are structured as a series of passes
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 15: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/15.jpg)
15
OptimizerOptimizerOptimizerOptimizer
Typical transformations Discover & propagate some
constant value
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 16: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/16.jpg)
16
OptimizerOptimizerOptimizerOptimizer
Typical transformations Move a computation to a less
frequently executed place
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 17: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/17.jpg)
17
OptimizerOptimizerOptimizerOptimizer
Typical transformations Specialize some computation
based on context
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 18: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/18.jpg)
18
OptimizerOptimizerOptimizerOptimizer
Typical transformations Discover a redundant
computation & remove it
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 19: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/19.jpg)
19
OptimizerOptimizerOptimizerOptimizer
Typical transformations Remove useless or
unreachable code
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 20: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/20.jpg)
20
OptimizerOptimizerOptimizerOptimizer
Typical transformations Encode an idiom in some
particularly efficient form
Opt1
IR IR
errors
Opt2
Optn
IR IR Opt3
IR
![Page 21: Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation: Have each value in a register when it is used. Instruction selection](https://reader034.vdocuments.net/reader034/viewer/2022051821/5697bfaa1a28abf838c9a631/html5/thumbnails/21.jpg)
21
Related to CompilersRelated to CompilersRelated to CompilersRelated to Compilers Interpreters (direct execution) Assemblers Preprocessors Text formatters (non-
WYSIWYG) Analysis tools