SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Downloaden Sie, um offline zu lesen
Code Generation & Register
Allocation
Object code forms - register allocation and
assignment, DAG for register allocation,
Generic code generation algorithms.
Register Allocation and Assignment
• Instruction involving only register operands are shorter and faster than
those involving memory operands.
• This approach has the advantage that it simplifies the design of a
compiler.
Disadvantage:
• It uses registers inefficiently;
• Certain registers may go unused over substantial portions of code,
while unnecessary loads and stores are generated.
Some Issues in Register Allocation
• Which values in a program reside in registers?
– register allocation
• In which register?
– register assignment
The Problem
• Global Register Allocation assumes that allocation is done beyond basic
blocks and usually at function level
• Decision problem related to register allocation:
– Given an intermediate language program represented as a control flow
graph and a number k, is there an assignment of registers to program
variables such that no conflicting variables are assigned the same register,
no extra loads or stores are introduced, and at most k registers are used?
• This problem has been shown to be NP-hard.
• Graph colouring is the most popular heuristic used.
• However, there are simpler algorithms as well
Various strategies used in register assignment
• Global Register Allocation
• Usage Counts
• Register Assignment for Outer Loops
• Register Allocation by Graph Coloring
Global Register Allocation
• When generating the code, the registers are used to hold the value for
the duration of single block.
• All the live variables are stored at the end of each block.
• For the variables that are used consistently we can allocate specific
set of registers.
• Hence allocation of variables to specific registers that is consistent
across the block boundaries is called global register allocation.
• The global register allocation has a strategy of storing the most
frequently used variables in fixed registers throughout the loop
• Another strategy is to assign some fixed number of global
registers to hold the most active value in each inner loop.
• In C program, we can use register storage class to demand for
register allocation globally.
Global Register Allocation
Usage Counts
• Allocate registers for variables used within loops
• Requires information about liveness of variables at the entry and
exit of each Basic Block (BB) of a loop
• Once a variable is computed into a register, it stays in that register
until the end of the BB (subject to existence of next-uses)
• Load/Store instructions cost 2 units (because they occupy two
words)
• The usage count gives the idea about how many units of cost can be
saved by selecting a specific variable for global register allocation.
• The approximate formula for usage count for the loop L in some basic
block B can be given as, i.e. Total savings per variable x is
෍
𝑩𝒍𝒐𝒄𝒌 𝑩 𝒊𝒏 𝑳
(𝒖𝒔𝒆 𝒙, 𝑩 + 𝟐 ∗ 𝒍𝒊𝒗𝒆(𝒙, 𝑩))
– Where use (x, B) is number of times x used in block B prior to
any definition of x and
– live (x, B) =1 is live on exit from B; otherwise live(x)=0.
Usage Counts
Global Register Allocation via Usage Counts (for Single
Loops)
use(a, B1)=0, since a is defined before any use.
use + 2 * live
• a is live on exit from B1 and is assigned a value there, but is not
live on exit from B2, B3, or B4.
– 2*live(a , B1)=2.
– use(a, B1)=0, since a is defined before any use.
– use(a, B2)=use(a, B3)=1 and use(a, B4)=0.
Global Register Allocation via Usage Counts
(for Single Loops)
Global Register Allocation via Usage Counts (for
Nested Loops)
• We first assign registers for inner loops and then
consider outer loops. Let L1 nest L2
– If an outer loop L1, contains an inner loop L2,
• For variables assigned registers in L2, but not in L1,
– load these variables on entry to L2 and store
them on exit from L2
• For variables assigned registers in L1, but not in L2,
– store these variables on entry to L2 and load
them on exit from L2
{
int H,A,L;
{
float x,y;
:
:
}
}
L1
L2
Register Allocation by Graph Coloring
• A register is needed for a computation but all available registers are in use, the
contents of one of the used registers must be stored (Spilled) into a memory
location in order to free up register.
– Graph coloring is a simple systematic technique for allocating registers and
managing register spills.
• In this method, two passes are used.
– In the first pass, target-machine instruction are selected as though there
were infinite number of symbolic registers; in effect, names used in the
intermediate code become names of registers and the three-address
statements become machine language statements.
– In the second pass, for each procedure a register-interference graph is
constructed in which the nodes are symbolic registers and an edge connects
two nodes if one is live at a point where the other is defined.
• An attempt is made to color the register-interference graph using k colors,
where k is the number of assignable registers.
• The main idea is to replace temporary variables with some fixed set of
registers
• First: Need to know which variables are live after each instruction
– Two live variables cannot be allocated to the same register
simultaneously
Register allocation
• For every node n in Control Flow Graph, we have out[n]
– Set of temporaries live out of n
• Two variables interfere if
– both initially live (i.e. function args), or
– both appear in out[n] for any n, or
– one is defined and the other is in out[n]
• x = b - c where x is dead & b is live interfere
• How to assign registers to variables?
Interference graph
• Nodes of the graph = variables
• Edges connect variables that interfere with one another
• Nodes will be assigned a color corresponding to the register
assigned to the variable
• Two colors cannot be next to one another in the graph
Instructions Live variables
b = a + 2
c = b * b
b = c + 1
return b * a
Interference graph
Instructions Live variables
b = a + 2
c = b * b
b = c + 1
return b * a b,a
Interference graph
Instructions Live variables
b = a + 2
c = b * b
b = c + 1 a,c
return b * a b,a
Interference graph
Instructions Live variables
b = a + 2
c = b * b b,a
b = c + 1 a,c
return b * a b,a
Interference graph
Instructions Live variables
b = a + 2 a
c = b * b b,a
b = c + 1 a,c
return b * a b,a
Interference graph
a
c
b
ax
bx
color register
Instructions Live variables
b = a + 2 a
c = b * b b,a
b = c + 1 a,c
return b * a b,a
Interference graph
a
c
b
ax
bx
color register
Instructions Live variables
b = a + 2 a
c = b * b a, b
b = c + 1 a, c
return b * a a, b
Graph coloring
• Questions:
– Can we efficiently find a coloring of the graph whenever possible?
– Can we efficiently find the optimum coloring of the graph?
– How do we choose registers to avoid move instructions?
– What do we do when there are not enough colors (registers) to color the graph?
Kempe’s algorithm for finding a K-coloring of a graph
• Assume K=2
• Step 1 (simplify): find a node with at most K-1 edges and cut it out of the
graph. (i.e. remember this node on a stack for later stages.)
• Once a coloring is found for the simpler graph, we can always color the node
we saved on the stack
• Step 2 (color): when the simplified sub-graph has been colored, add back the
node on the top of the stack and assign it a color, which is not taken by one of
the adjacent nodes
Coloring
b
e
d
ax
bx
color register
a
c
stack:
Coloring
b
e
d
a
stack:
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
a
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
b
a
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
d
b
a
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
b
a
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
a
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
e
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
c
c
ax
bx
color register
Coloring
b
e
d
a
stack:
c
ax
bx
color register
• Register spilling or reloading:
– If there are not enough registers to hold all the variables, some variables may
be moved to and from RAM. This process is called "spilling" the registers
• Coalescing
– In the context of register allocation, coalescing is the act of merging variable-
to-variable move operations by allocating those two variables to the same
location.
– The coalescing operation takes place after the interference graph is built.
Once two nodes have been coalesced, they must get the same color and be
allocated to the same register.
Coalescing
• Allocate source and destination of copy to same register
• To eliminate register-to-register copies
• Combines live ranges
• Examples
Code Generation – Main Issues
• Transformation
– Type of Intermediate Code Representation  Quadruple
– Intermediate code to Machine code (binary or assembly)
– We assume quadruples and CFG to be available
• Selection of instruction  Which instructions to generate?
– For the quadruple A = A+1, we may generate
inc A
or
Load A, R1
Add #1, R1
Store R1, A
– One sequence is faster than the other (cost implication)
• Ordering of Instruction  In which order?
– Some orders may use fewer registers and/or may be faster
• Register allocation  Which registers to use?
– Optimal assignment of registers to variables is difficult to
achieve
• Optimize for memory, time or power?
• Is the code generator easily retargetable to other machines?
– Can the code generator be produced automatically from
specifications of the machine?
Code Generation – Main Issues
Samples of Generated Code
• B = A[i]
Load i, R1 // R1 = i
Mult R1, 4, R1 // R1 = R1*4
// each element of array
// A is 4 bytes long
Load A(R1), R2 // R2=(A+R1)
Store R2, B // B = R2
• X[j] = Y
Load Y, R1 // R1 = Y
Load j, R2 // R2 = j
Mult R2, 4, R2 // R2=R2*4
Store R1, X(R2)// X(R2)=R1
• if X < Y goto L
Load X, R1
Load Y, R2
Cmp R1, R2 // compare words at
memory addresses pointed by
registers R1 and R2
Blt L //jump to L if (R1) is less
than (R2)
Blt – Branch if Less Than
A Simple Code Generator - I
• Treat each quadruple as a ‘macro’
– Example: The quad A = B + C will result in
Load B, R1
Load C, R2
Add R2, R1
Store R1, A
• Results in inefficient code
– Repeated load/store of registers
• Very simple to implement
Load B, R1
Add C, R1
Store R1, A
(OR)
• Track values in registers and reuse them
– If any operand is already in a register, take advantage of it.
• Register descriptors (RD)
– Tracks <register, variable name> pairs
– A single register can contain values of multiple names, if they are all
copies.
• Address descriptors (AD)
– Tracks <variable name, locations> pairs
– A single name may have its value in multiple locations, such as, memory,
register, and stack
A Simple Code Generator - II
• Register descriptor :
– Register descriptor is used to inform the code generator about the availability of
registers.
– Register descriptor keeps track of values stored in each register.
– Whenever a new register is required during code generation, this descriptor is
consulted for register availability.
• Address descriptor :
– Values of the names (identifiers) used in the program might be stored at
different locations while in execution.
– Address descriptors are used to keep track of memory locations where the
values of identifiers are stored.
– These locations may include CPU registers, heaps, stacks, memory or a
combination of the mentioned locations.
Code generator keeps both the descriptor updated in real-time
A Simple Code Generator - II
Example – use of RD & AD
• For a load statement, LD R1, x  the code generator:
– updates the Register Descriptor R1 that has value of x and
– updates the Address Descriptor (x) to show that one instance of x is in R1.
• Keep the computed result in a register as long as possible
• Store only at the end of a basic block or when that register is needed for
another computation
– On exit from a basic block, store only live variables which are not in their
memory locations already (use address descriptors to determine the later)
– If liveness information is not known, assume that all variables are live at all
times
A Simple Code Generator - II
Example
• A = B+C
1. If B and C are in registers R1 and R2, then generate
• ADD R2, R1 (cost = 1, result in R1)
– legal only if B is not live after the statement. Because B is
destroyed. Earlier R1 contains B.
2. If R1 contains B, but C is in memory
i. ADD C, R1 (cost = 2, result in R1) //we need to fetch from memory
location C
or
ii. LOAD C, R2
ADD R2, R1 (cost = 3, result in R1)
– legal only if B is not live after the statement
– attractive if the value of C is subsequently used (it can be taken
from R2). We need not again load C to register R2.
Next Use Information
• Next use info is used in code generation and register allocation
• Next use of A in quad i is j if
Quad i : A = ... (assignment to variable A)
(control flows from i to j with no assignments to A)
Quad j : = A op B (usage of A) //Next use of A in Quad i is in Quad j.
• In computing next use, we assume that on exit from the basic block
– All temporaries are considered non-live
– All programmer defined variables (and non-temps) are live
• Each procedure/function call is assumed to start a basic block
• Next use is computed on a backward scan on the quadruples in a basic
block, starting from the end
• Next use information is stored in the symbol table
Terminologies used
• Next Use Info  usefully to retain those values which are used later in
registers
• nlv - not live
• lv - live
• lu -last use
• nu - next use
• nnu – not next use
Consider Basic Block B2
Example of computing Next Use
• we can compute next use information (nu) using last use information (lu)
Computing Next Use by Backward Scan
The algorithm
Note on Algorithm
• There is only local register allocation
• Location L is obtained by GETREG(). L could be a memory location. But
normally, it is a register.
• Location of B is B’.
– For each variable, Address descriptor tells us where exactly it is located.
• Update descriptors  L contains the result. L contains a value for A.
• Code generator uses getReg function to determine the status of available
registers and the location of name values.
• getReg() works as follows:
– If variable Y is already in register R, it uses that register.
– else if some register R is available, it uses that register.
– else if both the above options are not possible, it chooses a register that
requires minimal number of load and store instructions.
Function getReg()
Function GETREG()
Example – To understand code generation process
• T,U, and V are temporaries - not live at the end of the block
– If it is not live, we need not store it at the end of the block.
• W is a non-temporary - live at the end of the block
– If it is live, we need to store it at the end of the block
• Assume that there are 2 registers.
• Initially, all the registers are free, when we process 1st instruction in the
basic block.
Example
• Finally, code generation is completed for this basic block.
DAG representation of basic blocks
• DAG is useful data structures for implementing transformations on
basic blocks
• Gives a picture of how value computed by a statement is used in
subsequent statements
• Good way of determining common sub-expressions
• A DAG for a basic block has following labels on the nodes
– leaves are labeled by unique identifiers, either variable names or
constants
– interior nodes are labeled by an operator symbol
– nodes are also optionally given a sequence of identifiers for labels
DAG representation: Example
Code Generation from DAG
Rearranging order of the code
• Consider following basic block and its DAG.
• Three address code for the DAG (assuming only two registers are
available)
• Rearranging the code as shown below,
Rearranging order of the code
Reference
• A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman,
Compilers Principles, Techniques and Tools,
Pearson Edition, 2013.
P. Kuppusamy - Lexical Analyzer

Weitere ähnliche Inhalte

Was ist angesagt?

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Dynamic Programming Code-Optimization Algorithm (Compiler Design)Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Dynamic Programming Code-Optimization Algorithm (Compiler Design)Dhrumil Panchal
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dagsindhu mathi
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksMohammad Vaseem Akaram
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code GeneratorDarshan sai Reddy
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 

Was ist angesagt? (20)

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Parsing
ParsingParsing
Parsing
 
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Dynamic Programming Code-Optimization Algorithm (Compiler Design)Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Code generator
Code generatorCode generator
Code generator
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Instruction codes
Instruction codesInstruction codes
Instruction codes
 

Ähnlich wie Code generation in Compiler Design

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Integrated Register Allocation introduction
Integrated Register Allocation introductionIntegrated Register Allocation introduction
Integrated Register Allocation introductionShiva Chen
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloringsean chen
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEPRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEnikhilcse1
 
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).pptMohibKhan79
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
 
11_ InstructionSetsAddressingModes .pdf
11_ InstructionSetsAddressingModes .pdf11_ InstructionSetsAddressingModes .pdf
11_ InstructionSetsAddressingModes .pdfWilliamTom9
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Iffat Anjum
 
Introduction to 8086 microprocessor
Introduction to 8086 microprocessorIntroduction to 8086 microprocessor
Introduction to 8086 microprocessorShreyans Pathak
 
Reduced instruction set computers
Reduced instruction set computersReduced instruction set computers
Reduced instruction set computersSyed Zaid Irshad
 
Memory allocation
Memory allocationMemory allocation
Memory allocationsanya6900
 
Cerebellar Model Articulation Controller
Cerebellar Model Articulation ControllerCerebellar Model Articulation Controller
Cerebellar Model Articulation ControllerZahra Sadeghi
 

Ähnlich wie Code generation in Compiler Design (20)

Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Integrated Register Allocation introduction
Integrated Register Allocation introductionIntegrated Register Allocation introduction
Integrated Register Allocation introduction
 
0015.register allocation-graph-coloring
0015.register allocation-graph-coloring0015.register allocation-graph-coloring
0015.register allocation-graph-coloring
 
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPEPRESENTATION ON DATA STRUCTURE AND THEIR TYPE
PRESENTATION ON DATA STRUCTURE AND THEIR TYPE
 
456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt456589.-Compiler-Design-Code-Generation (1).ppt
456589.-Compiler-Design-Code-Generation (1).ppt
 
Cryptography
CryptographyCryptography
Cryptography
 
Uc 2(vii)
Uc 2(vii)Uc 2(vii)
Uc 2(vii)
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
11_ InstructionSetsAddressingModes .pdf
11_ InstructionSetsAddressingModes .pdf11_ InstructionSetsAddressingModes .pdf
11_ InstructionSetsAddressingModes .pdf
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Programming in C.pptx
Programming in C.pptxProgramming in C.pptx
Programming in C.pptx
 
CO_Chapter2.ppt
CO_Chapter2.pptCO_Chapter2.ppt
CO_Chapter2.ppt
 
Co ppt
Co pptCo ppt
Co ppt
 
Introduction to 8086 microprocessor
Introduction to 8086 microprocessorIntroduction to 8086 microprocessor
Introduction to 8086 microprocessor
 
Reduced instruction set computers
Reduced instruction set computersReduced instruction set computers
Reduced instruction set computers
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
RISC.ppt
RISC.pptRISC.ppt
RISC.ppt
 
13 risc
13 risc13 risc
13 risc
 
Arithmetic & Logic Unit
Arithmetic & Logic UnitArithmetic & Logic Unit
Arithmetic & Logic Unit
 
Cerebellar Model Articulation Controller
Cerebellar Model Articulation ControllerCerebellar Model Articulation Controller
Cerebellar Model Articulation Controller
 

Mehr von Kuppusamy P

Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnnKuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentationKuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancementKuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matchingKuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersKuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithmsKuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basicsKuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using ProgrammingKuppusamy P
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Kuppusamy P
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or FunctionsKuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statementsKuppusamy P
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Java introduction
Java introductionJava introduction
Java introductionKuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningKuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationKuppusamy P
 

Mehr von Kuppusamy P (20)

Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnn
 
Deep learning
Deep learningDeep learning
Deep learning
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
 
Image enhancement
Image enhancementImage enhancement
Image enhancement
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matching
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithms
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basics
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using Programming
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software
 
Strings in java
Strings in javaStrings in java
Strings in java
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
 
Java arrays
Java arraysJava arrays
Java arrays
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statements
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Java data types
Java data typesJava data types
Java data types
 
Java introduction
Java introductionJava introduction
Java introduction
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classification
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Code generation in Compiler Design

  • 1. Code Generation & Register Allocation Object code forms - register allocation and assignment, DAG for register allocation, Generic code generation algorithms.
  • 2. Register Allocation and Assignment • Instruction involving only register operands are shorter and faster than those involving memory operands. • This approach has the advantage that it simplifies the design of a compiler. Disadvantage: • It uses registers inefficiently; • Certain registers may go unused over substantial portions of code, while unnecessary loads and stores are generated.
  • 3. Some Issues in Register Allocation • Which values in a program reside in registers? – register allocation • In which register? – register assignment
  • 4. The Problem • Global Register Allocation assumes that allocation is done beyond basic blocks and usually at function level • Decision problem related to register allocation: – Given an intermediate language program represented as a control flow graph and a number k, is there an assignment of registers to program variables such that no conflicting variables are assigned the same register, no extra loads or stores are introduced, and at most k registers are used? • This problem has been shown to be NP-hard. • Graph colouring is the most popular heuristic used. • However, there are simpler algorithms as well
  • 5. Various strategies used in register assignment • Global Register Allocation • Usage Counts • Register Assignment for Outer Loops • Register Allocation by Graph Coloring
  • 6. Global Register Allocation • When generating the code, the registers are used to hold the value for the duration of single block. • All the live variables are stored at the end of each block. • For the variables that are used consistently we can allocate specific set of registers. • Hence allocation of variables to specific registers that is consistent across the block boundaries is called global register allocation.
  • 7. • The global register allocation has a strategy of storing the most frequently used variables in fixed registers throughout the loop • Another strategy is to assign some fixed number of global registers to hold the most active value in each inner loop. • In C program, we can use register storage class to demand for register allocation globally. Global Register Allocation
  • 8. Usage Counts • Allocate registers for variables used within loops • Requires information about liveness of variables at the entry and exit of each Basic Block (BB) of a loop • Once a variable is computed into a register, it stays in that register until the end of the BB (subject to existence of next-uses) • Load/Store instructions cost 2 units (because they occupy two words)
  • 9. • The usage count gives the idea about how many units of cost can be saved by selecting a specific variable for global register allocation. • The approximate formula for usage count for the loop L in some basic block B can be given as, i.e. Total savings per variable x is ෍ 𝑩𝒍𝒐𝒄𝒌 𝑩 𝒊𝒏 𝑳 (𝒖𝒔𝒆 𝒙, 𝑩 + 𝟐 ∗ 𝒍𝒊𝒗𝒆(𝒙, 𝑩)) – Where use (x, B) is number of times x used in block B prior to any definition of x and – live (x, B) =1 is live on exit from B; otherwise live(x)=0. Usage Counts
  • 10. Global Register Allocation via Usage Counts (for Single Loops) use(a, B1)=0, since a is defined before any use. use + 2 * live
  • 11. • a is live on exit from B1 and is assigned a value there, but is not live on exit from B2, B3, or B4. – 2*live(a , B1)=2. – use(a, B1)=0, since a is defined before any use. – use(a, B2)=use(a, B3)=1 and use(a, B4)=0. Global Register Allocation via Usage Counts (for Single Loops)
  • 12. Global Register Allocation via Usage Counts (for Nested Loops) • We first assign registers for inner loops and then consider outer loops. Let L1 nest L2 – If an outer loop L1, contains an inner loop L2, • For variables assigned registers in L2, but not in L1, – load these variables on entry to L2 and store them on exit from L2 • For variables assigned registers in L1, but not in L2, – store these variables on entry to L2 and load them on exit from L2 { int H,A,L; { float x,y; : : } } L1 L2
  • 13. Register Allocation by Graph Coloring • A register is needed for a computation but all available registers are in use, the contents of one of the used registers must be stored (Spilled) into a memory location in order to free up register. – Graph coloring is a simple systematic technique for allocating registers and managing register spills. • In this method, two passes are used. – In the first pass, target-machine instruction are selected as though there were infinite number of symbolic registers; in effect, names used in the intermediate code become names of registers and the three-address statements become machine language statements. – In the second pass, for each procedure a register-interference graph is constructed in which the nodes are symbolic registers and an edge connects two nodes if one is live at a point where the other is defined. • An attempt is made to color the register-interference graph using k colors, where k is the number of assignable registers.
  • 14. • The main idea is to replace temporary variables with some fixed set of registers • First: Need to know which variables are live after each instruction – Two live variables cannot be allocated to the same register simultaneously
  • 15. Register allocation • For every node n in Control Flow Graph, we have out[n] – Set of temporaries live out of n • Two variables interfere if – both initially live (i.e. function args), or – both appear in out[n] for any n, or – one is defined and the other is in out[n] • x = b - c where x is dead & b is live interfere • How to assign registers to variables?
  • 16. Interference graph • Nodes of the graph = variables • Edges connect variables that interfere with one another • Nodes will be assigned a color corresponding to the register assigned to the variable • Two colors cannot be next to one another in the graph Instructions Live variables b = a + 2 c = b * b b = c + 1 return b * a
  • 17. Interference graph Instructions Live variables b = a + 2 c = b * b b = c + 1 return b * a b,a
  • 18. Interference graph Instructions Live variables b = a + 2 c = b * b b = c + 1 a,c return b * a b,a
  • 19. Interference graph Instructions Live variables b = a + 2 c = b * b b,a b = c + 1 a,c return b * a b,a
  • 20. Interference graph Instructions Live variables b = a + 2 a c = b * b b,a b = c + 1 a,c return b * a b,a
  • 21. Interference graph a c b ax bx color register Instructions Live variables b = a + 2 a c = b * b b,a b = c + 1 a,c return b * a b,a
  • 22. Interference graph a c b ax bx color register Instructions Live variables b = a + 2 a c = b * b a, b b = c + 1 a, c return b * a a, b
  • 23. Graph coloring • Questions: – Can we efficiently find a coloring of the graph whenever possible? – Can we efficiently find the optimum coloring of the graph? – How do we choose registers to avoid move instructions? – What do we do when there are not enough colors (registers) to color the graph? Kempe’s algorithm for finding a K-coloring of a graph • Assume K=2 • Step 1 (simplify): find a node with at most K-1 edges and cut it out of the graph. (i.e. remember this node on a stack for later stages.) • Once a coloring is found for the simpler graph, we can always color the node we saved on the stack • Step 2 (color): when the simplified sub-graph has been colored, add back the node on the top of the stack and assign it a color, which is not taken by one of the adjacent nodes
  • 35. • Register spilling or reloading: – If there are not enough registers to hold all the variables, some variables may be moved to and from RAM. This process is called "spilling" the registers • Coalescing – In the context of register allocation, coalescing is the act of merging variable- to-variable move operations by allocating those two variables to the same location. – The coalescing operation takes place after the interference graph is built. Once two nodes have been coalesced, they must get the same color and be allocated to the same register.
  • 36. Coalescing • Allocate source and destination of copy to same register • To eliminate register-to-register copies • Combines live ranges • Examples
  • 37. Code Generation – Main Issues • Transformation – Type of Intermediate Code Representation  Quadruple – Intermediate code to Machine code (binary or assembly) – We assume quadruples and CFG to be available • Selection of instruction  Which instructions to generate? – For the quadruple A = A+1, we may generate inc A or Load A, R1 Add #1, R1 Store R1, A – One sequence is faster than the other (cost implication)
  • 38. • Ordering of Instruction  In which order? – Some orders may use fewer registers and/or may be faster • Register allocation  Which registers to use? – Optimal assignment of registers to variables is difficult to achieve • Optimize for memory, time or power? • Is the code generator easily retargetable to other machines? – Can the code generator be produced automatically from specifications of the machine? Code Generation – Main Issues
  • 39. Samples of Generated Code • B = A[i] Load i, R1 // R1 = i Mult R1, 4, R1 // R1 = R1*4 // each element of array // A is 4 bytes long Load A(R1), R2 // R2=(A+R1) Store R2, B // B = R2 • X[j] = Y Load Y, R1 // R1 = Y Load j, R2 // R2 = j Mult R2, 4, R2 // R2=R2*4 Store R1, X(R2)// X(R2)=R1 • if X < Y goto L Load X, R1 Load Y, R2 Cmp R1, R2 // compare words at memory addresses pointed by registers R1 and R2 Blt L //jump to L if (R1) is less than (R2) Blt – Branch if Less Than
  • 40. A Simple Code Generator - I • Treat each quadruple as a ‘macro’ – Example: The quad A = B + C will result in Load B, R1 Load C, R2 Add R2, R1 Store R1, A • Results in inefficient code – Repeated load/store of registers • Very simple to implement Load B, R1 Add C, R1 Store R1, A (OR)
  • 41. • Track values in registers and reuse them – If any operand is already in a register, take advantage of it. • Register descriptors (RD) – Tracks <register, variable name> pairs – A single register can contain values of multiple names, if they are all copies. • Address descriptors (AD) – Tracks <variable name, locations> pairs – A single name may have its value in multiple locations, such as, memory, register, and stack A Simple Code Generator - II
  • 42. • Register descriptor : – Register descriptor is used to inform the code generator about the availability of registers. – Register descriptor keeps track of values stored in each register. – Whenever a new register is required during code generation, this descriptor is consulted for register availability. • Address descriptor : – Values of the names (identifiers) used in the program might be stored at different locations while in execution. – Address descriptors are used to keep track of memory locations where the values of identifiers are stored. – These locations may include CPU registers, heaps, stacks, memory or a combination of the mentioned locations. Code generator keeps both the descriptor updated in real-time A Simple Code Generator - II
  • 43. Example – use of RD & AD • For a load statement, LD R1, x  the code generator: – updates the Register Descriptor R1 that has value of x and – updates the Address Descriptor (x) to show that one instance of x is in R1.
  • 44. • Keep the computed result in a register as long as possible • Store only at the end of a basic block or when that register is needed for another computation – On exit from a basic block, store only live variables which are not in their memory locations already (use address descriptors to determine the later) – If liveness information is not known, assume that all variables are live at all times A Simple Code Generator - II
  • 45. Example • A = B+C 1. If B and C are in registers R1 and R2, then generate • ADD R2, R1 (cost = 1, result in R1) – legal only if B is not live after the statement. Because B is destroyed. Earlier R1 contains B. 2. If R1 contains B, but C is in memory i. ADD C, R1 (cost = 2, result in R1) //we need to fetch from memory location C or ii. LOAD C, R2 ADD R2, R1 (cost = 3, result in R1) – legal only if B is not live after the statement – attractive if the value of C is subsequently used (it can be taken from R2). We need not again load C to register R2.
  • 46. Next Use Information • Next use info is used in code generation and register allocation • Next use of A in quad i is j if Quad i : A = ... (assignment to variable A) (control flows from i to j with no assignments to A) Quad j : = A op B (usage of A) //Next use of A in Quad i is in Quad j. • In computing next use, we assume that on exit from the basic block – All temporaries are considered non-live – All programmer defined variables (and non-temps) are live • Each procedure/function call is assumed to start a basic block • Next use is computed on a backward scan on the quadruples in a basic block, starting from the end • Next use information is stored in the symbol table
  • 47. Terminologies used • Next Use Info  usefully to retain those values which are used later in registers • nlv - not live • lv - live • lu -last use • nu - next use • nnu – not next use
  • 50. • we can compute next use information (nu) using last use information (lu) Computing Next Use by Backward Scan
  • 52. Note on Algorithm • There is only local register allocation • Location L is obtained by GETREG(). L could be a memory location. But normally, it is a register. • Location of B is B’. – For each variable, Address descriptor tells us where exactly it is located. • Update descriptors  L contains the result. L contains a value for A.
  • 53. • Code generator uses getReg function to determine the status of available registers and the location of name values. • getReg() works as follows: – If variable Y is already in register R, it uses that register. – else if some register R is available, it uses that register. – else if both the above options are not possible, it chooses a register that requires minimal number of load and store instructions. Function getReg()
  • 55. Example – To understand code generation process • T,U, and V are temporaries - not live at the end of the block – If it is not live, we need not store it at the end of the block. • W is a non-temporary - live at the end of the block – If it is live, we need to store it at the end of the block • Assume that there are 2 registers. • Initially, all the registers are free, when we process 1st instruction in the basic block.
  • 56. Example • Finally, code generation is completed for this basic block.
  • 57. DAG representation of basic blocks • DAG is useful data structures for implementing transformations on basic blocks • Gives a picture of how value computed by a statement is used in subsequent statements • Good way of determining common sub-expressions • A DAG for a basic block has following labels on the nodes – leaves are labeled by unique identifiers, either variable names or constants – interior nodes are labeled by an operator symbol – nodes are also optionally given a sequence of identifiers for labels
  • 60. Rearranging order of the code • Consider following basic block and its DAG.
  • 61. • Three address code for the DAG (assuming only two registers are available)
  • 62. • Rearranging the code as shown below, Rearranging order of the code
  • 63. Reference • A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman, Compilers Principles, Techniques and Tools, Pearson Edition, 2013. P. Kuppusamy - Lexical Analyzer