0% found this document useful (0 votes)
2 views58 pages

Code Optimization - Expert Talk

The document discusses code optimization, covering its necessity, components, and various techniques such as common subexpression elimination and loop-level optimizations. It highlights the evaluation criteria for optimization, including safety and profitability, and introduces concepts like data flow analysis and register allocation. Additionally, it explains the process of graph coloring for register assignment in optimization.

Uploaded by

22dce033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views58 pages

Code Optimization - Expert Talk

The document discusses code optimization, covering its necessity, components, and various techniques such as common subexpression elimination and loop-level optimizations. It highlights the evaluation criteria for optimization, including safety and profitability, and introduces concepts like data flow analysis and register allocation. Additionally, it explains the process of graph coloring for register assignment in optimization.

Uploaded by

22dce033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Code Optimization

Prof. (Dr.) Malay S Bhatt


Department of Computer Engineering
Sankalchand Patel College of Engineering
Sankalchand Patel University
Topics to be covered :
1. Need of Language Processing

2. Analysis – Synthesis Model

3. Language Processing

4. code optimization examples

5. Control Flow Graph

6. Reaching Definitions

7. Register Allocation
An optimization is a transformation that is expected
to improve program in some way.

• Program running time


• Memory utilization
• Power consumption

Basic components of optimization:


• Code Analysis
• Code Rewriting
Optimizers :
• Multiple passes are typical, each does particular
optimization task
• Work on Intermediate representation
Evaluation of Code Optimization :
• Safety

• Opportunity

• Profitability

• Overhead
Scope of Code Optimization :
Code Optimization Examples:
Common Subexpression Elimination:
Common Subexpression Elimination:
t :=d + e

a := t b := t

c := t
Loop level Optimizations:
Loop level Optimizations:
DATA FLOW ANALYSIS
EXAMPLE CALCULATION (ITERATION -1)
EXAMPLE CALCULATION (ITERATION -1)
EXAMPLE CALCULATION (ITERATION -1)

BLOCK REACH_IN REACH_OUT


Entry {} {}
B1 {} {1,2,3}
B2 {1,2,3} {1,2,3}
B3 {1,2,3} {1,2,3,4}
B4 {1,2,3,4} {1,2,3,4}
B5 {1,2,3,4} {1,2,3,4}
B6 {1,2,3,4} {1,5,6,7,8}
EXIT {1,2,3,4} {1,2,3,4}
EXAMPLE CALCULATION (ITERATION -2)

BLOCK REACH_IN REACH_OUT


Entry {} {}
B1 {} {1,2,3}
B2 {1,2,3} {1,2,3}
B3 {1,2,3} {1,2,3,4}
B4 {1,2,3,4,5,6,7,8} {1,2,3,4,5,6,7,8}
B5 {1,2,3,4,5,6,7,8} {1,2,3,4,5,6,7,8}
B6 {1,2,3,4,5,6,7,8} {1,5,6,7,8}
EXIT {1,2,3,4,5,6,7,8} {1,2,3,4,5,6,7,8}
Register Allocation :Interference graph

Instructions Live vars

b=a+2

c=b*b

b=c+1

return b * a
Register Allocation :Interference graph

Instructions Live vars

b=a+2

c=b*b

b=c+1
b,a
return b * a
Register Allocation :Interference graph

Instructions Live vars

b=a+2

c=b*b
a,c
b=c+1
b,a
return b * a
Register Allocation :Interference graph

Instructions Live vars

b=a+2
b,a
c=b*b
a,c
b=c+1
b,a
return b * a
Register Allocation :Interference graph

Instructions Live vars


a
b=a+2
b,a
c=b*b
a,c
b=c+1
b,a
return b * a
Register Allocation :Interference graph
color register
Instructions Live vars eax
a
b=a+2 ebx
a,b
c=b*b
a,c
a
b=c+1
a,b
return b * a
b c
Register Allocation :Interference graph
color register
Instructions Live vars eax
a
b=a+2 ebx
a,b
c=b*b
a,c
a
b=c+1
a,b
return b * a
b c
Register Allocation :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 can’t be next to one another in the graph
Coloring a graph
• Kempe’s algorithm [1879] for finding a K-coloring of a graph
• Assume K=3
• Step 1 (simplify): find a node with at most K-1 edges and cut it out of
the graph. (Remember this node on a stack for later stages.)
Coloring a graph
• 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 subgraph has been colored, add
back the node on the top of the stack and assign it a color not taken
by one of the adjacent nodes
Coloring a graph
color register

eax

ebx

stack:
b c

d e
Coloring a graph
color register

eax

ebx

stack:
b c

c
d e
Coloring a graph
color register

eax

ebx

stack:
b c
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b c
a
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b
b c
a
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
d
b c
b
a
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b c
b
a
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b c
a
e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b c

e
c
d e
Coloring a graph
color register

eax

ebx

stack:
b c

c
d e
Coloring a graph
color register

eax

ebx

stack:
b c

d e
Final Thought:

You might also like