SlideShare a Scribd company logo
1 of 58
Lecture 11: Code Optimization
CS 540
George Mason University
CS 540 Spring 2009 GMU 2
Code Optimization
REQUIREMENTS:
• Meaning must be preserved (correctness)
• Speedup must occur on average.
• Work done must be worth the effort.
OPPORTUNITIES:
• Programmer (algorithm, directives)
• Intermediate code
• Target code
CS 540 Spring 2009 GMU 3
Code Optimization
Scanner
(lexical
analysis)
Parser
(syntax
analysis)
Code
Optimizer
Semantic
Analysis
(IC generator)
Code
Generator
Symbol
Table
Source
language
tokens Syntactic
structure
Syntactic/semantic
structure
Target
language
CS 540 Spring 2009 GMU 4
Levels
• Window – peephole optimization
• Basic block
• Procedural – global (control flow graph)
• Program level – intraprocedural (program
dependence graph)
CS 540 Spring 2009 GMU 5
Peephole Optimizations
• Constant Folding
x := 32 becomes x := 64
x := x + 32
• Unreachable Code
goto L2
x := x + 1  unneeded
• Flow of control optimizations
goto L1 becomes goto L2
…
L1: goto L2
CS 540 Spring 2009 GMU 6
Peephole Optimizations
• Algebraic Simplification
x := x + 0  unneeded
• Dead code
x := 32  where x not used after statement
y := x + y  y := y + 32
• Reduction in strength
x := x * 2  x := x + x
CS 540 Spring 2009 GMU 7
Peephole Optimizations
• Local in nature
• Pattern driven
• Limited by the size of the window
CS 540 Spring 2009 GMU 8
Basic Block Level
• Common Subexpression elimination
• Constant Propagation
• Dead code elimination
• Plus many others such as copy propagation,
value numbering, partial redundancy
elimination, …
CS 540 Spring 2009 GMU 9
Simple example: a[i+1] = b[i+1]
• t1 = i+1
• t2 = b[t1]
• t3 = i + 1
• a[t3] = t2
• t1 = i + 1
• t2 = b[t1]
• t3 = i + 1  no longer live
• a[t1] = t2
Common expression can be eliminated
CS 540 Spring 2009 GMU 10
• i = 4
• t1 = i+1
• t2 = b[t1]
• a[t1] = t2
• i = 4
• t1 = 5
• t2 = b[t1]
• a[t1] = t2
Now, suppose i is a constant:
• i = 4
• t1 = 5
• t2 = b[5]
• a[5] = t2
• i = 4
• t2 = b[5]
• a[5] = t2
Final Code:
CS 540 Spring 2009 GMU 11
Control Flow Graph - CFG
CFG = < V, E, Entry >, where
V = vertices or nodes, representing an instruction or basic
block (group of statements).
E = (V x V) edges, potential flow of control
Entry is an element of V, the unique program entry
Two sets used in algorithms:
• Succ(v) = {x in V| exists e in E, e = v x}
• Pred(v) = {x in V| exists e in E, e = x v}
1 2 3 4 5
CS 540 Spring 2009 GMU 12
Definitions
• point - any location between adjacent statements
and before and after a basic block.
• A path in a CFG from point p1 to pn is a sequence
of points such that  j, 1 <= j < n, either pi is the
point immediately preceding a statement and pi+1
is the point immediately following that statement
in the same block, or pi is the end of some block
and pi+1 is the start of a successor block.
CS 540 Spring 2009 GMU 13
CFG
c = a + b
d = a * c
i = 1
f[i] = a + b
c = c * 2
if c > d
g = a * c g = d * d
i = i + 1
if i > 10
points
path
CS 540 Spring 2009 GMU 14
Optimizations on CFG
• Must take control flow into account
– Common Sub-expression Elimination
– Constant Propagation
– Dead Code Elimination
– Partial redundancy Elimination
– …
• Applying one optimization may create
opportunities for other optimizations.
CS 540 Spring 2009 GMU 15
Redundant Expressions
An expression x op y is redundant at a point p if it has
already been computed at some point(s) and no intervening
operations redefine x or y.
m = 2*y*z t0 = 2*y t0 = 2*y
m = t0*z m = t0*z
n = 3*y*z t1 = 3*y t1 = 3*y
n = t1*z n = t1*z
o = 2*y–z t2 = 2*y
o = t2-z o = t0-z
redundant
CS 540 Spring 2009 GMU 16
Redundant Expressions
c = a + b
d = a * c
i = 1
f[i] = a + b
c = c * 2
if c > d
g = a * c g = d * d
i = i + 1
if i > 10
Candidates:
a + b
a * c
d * d
c * 2
i + 1
Definition site
Since a + b is
available here,
 redundant!
CS 540 Spring 2009 GMU 17
Redundant Expressions
c = a + b
d = a * c
i = 1
f[i] = a + b
c = c * 2
if c > d
g = a * c g = d * d
i = i + 1
if i > 10
Candidates:
a + b
a * c
d * d
c * 2
i + 1
Definition site
Kill site
Not available
 Not redundant
CS 540 Spring 2009 GMU 18
Redundant Expressions
• An expression e is defined at some point p in the
CFG if its value is computed at p. (definition site)
• An expression e is killed at point p in the CFG if
one or more of its operands is defined at p. (kill
site)
• An expression is available at point p in a CFG if
every path leading to p contains a prior definition
of e and e is not killed between that definition and
p.
CS 540 Spring 2009 GMU 19
Removing Redundant Expressions
t1 = a + b
c = t1
d = a * c
i = 1
f[i] = t1
c = c * 2
if c > d
g = a * c g = d*d
i = i + 1
if i > 10
Candidates:
a + b
a * c
d * d
c * 2
i + 1
CS 540 Spring 2009 GMU 20
Constant Propagation
b = 5
c = 4*b
c > b
d = b + 2
e = a + b
b = 5
c = 20
c > 5
d = 7
e = a + 5
e = a + b
t
f
t
f
b = 5
c = 20
20 > 5
d = 7
e = a + 5
t
f
CS 540 Spring 2009 GMU 21
Constant Propagation
b = 5
c = 20
20 > 5
d = 7
e = a + 5
t
f
b = 5
c = 20
d = 7
e = a + 5
CS 540 Spring 2009 GMU 22
Copy Propagation
b = a
c = 4*b
c > b
d = b + 2
e = a + b
b = a
c = 4*a
c > a
d = a + 2
e = a + a
e = a + b
CS 540 Spring 2009 GMU 23
Simple Loop Optimizations: Code
Motion
while (i <= limit - 2)
t := limit - 2
while (i <= t)
L1:
t1 = limit – 2
if (i > t1) goto L2
body of loop
goto L1
L2:
t1 = limit – 2
L1:
if (i > t1) goto L2
body of loop
goto L1
L2:
CS 540 Spring 2009 GMU 24
Simple Loop Optimizations:
Strength Reduction
• Induction Variables control loop iterations
j = j – 1
t4 = 4 * j
t5 = a[t4]
if t5 > v
j = j – 1
t4 = t4 - 4
t5 = a[t4]
if t5 > v
t4 = 4*j
CS 540 Spring 2009 GMU 25
Simple Loop Optimizations
• Loop transformations are often used to expose
other optimization opportunities:
– Normalization
– Loop Interchange
– Loop Fusion
– Loop Reversal
– …
CS 540 Spring 2009 GMU 26
Consider Matrix Multiplication
for i = 1 to n do
for j = 1 to n do
for k = 1 to n do
C[i,j] = C[i,j] + A[i,k] + B[k,j]
end
end
end
= +
i i
j j
k
k
C B
A
CS 540 Spring 2009 GMU 27
Memory Usage
• For A: Elements are accessed across rows, spatial locality
is exploited for cache (assuming row major storage)
• For B: Elements are accessed along columns, unless
cache can hold all of B, cache will have problems.
• For C: Single element computed per loop – use register to
hold
= +
i i
j j
k
k
C B
A
CS 540 Spring 2009 GMU 28
Matrix Multiplication Version 2
for i = 1 to n do
for k = 1 to n do
for j = 1 to n do
C[i,j] = C[i,j] + A[i,k] + B[k,j]
end
end
end
= +
i i
j j
k
C B
A
loop interchange
k
CS 540 Spring 2009 GMU 29
Memory Usage
• For A: Single element loaded for loop body
• For B: Elements are accessed along rows to
exploit spatial locality.
• For C: Extra loading/storing, but across rows
= +
i i
j j
k
C B
A
k
CS 540 Spring 2009 GMU 30
Simple Loop Optimizations
• How to determine safety?
– Does the new multiply give the same answer?
– Can be reversed??
for (I=1 to N) a[I] = a[I+1] – can this loop be
safely reversed?
CS 540 Spring 2009 GMU 31
Data Dependencies
• Flow Dependencies - write/read
x := 4;
y := x + 1
• Output Dependencies - write/write
x := 4;
x := y + 1;
• Antidependencies - read/write
y := x + 1;
x := 4;
CS 540 Spring 2009 GMU 32
x := 4
y := 6
p := x + 2
z := y + p
x := z
y := p
x := 4 y := 6
p := x + 2
z := y + p
y := p x := z
Flow
Output
Anti
CS 540 Spring 2009 GMU 33
Global Data Flow Analysis
Collecting information about the way data is used in
a program.
• Takes control flow into account
• HL control constructs
– Simpler – syntax driven
– Useful for data flow analysis of source code
• General control constructs – arbitrary branching
Information needed for optimizations such as:
constant propagation, common sub-expressions,
partial redundancy elimination …
CS 540 Spring 2009 GMU 34
Dataflow Analysis: Iterative
Techniques
• First, compute local (block level)
information.
• Iterate until no changes
while change do
change = false
for each basic block
apply equations updating IN and OUT
if either IN or OUT changes, set change
to true
end
CS 540 Spring 2009 GMU 35
Live Variable Analysis
A variable x is live at a point p if there is
some path from p where x is used
before it is defined.
Want to determine for some variable x
and point p whether the value of x
could be used along some path starting
at p.
• Information flows backwards
• May – ‘along some path starting at p’
is x live
here?
CS 540 Spring 2009 GMU 36
Global Live Variable Analysis
Want to determine for some variable x and point p whether
the value of x could be used along some path starting at p.
• DEF[B] - set of variables assigned values in B prior to any
use of that variable
• USE[B] - set of variables used in B prior to any definition
of that variable
• OUT[B] - variables live immediately after the block
OUT[B] - IN[S] for all S in succ(B)
• IN[B] - variables live immediately before the block
IN[B] = USE[B] + (OUT[B] - DEF[B])
CS 540 Spring 2009 GMU 37
d1: a = 1
d2: b = 2
d3: c = a + b
d4: d = c - a
d8: b = a + b
d9: e = c - 1
d10: a = b * d
d22: b = a - d
d5: d = b * d
d6: d = a + b
d7: e = e + 1
B1
B2
B3
B4
B5
B6
DEF=a,b
USE =
DEF=c,d
USE = a,b
DEF=
USE = b,d
DEF=d
USE = a,b,e
DEF= e
USE = a,b,c
DEF= a
USE = b,d
CS 540 Spring 2009 GMU 38
Global Live Variable Analysis
Want to determine for some variable x and point p whether
the value of x could be used along some path starting at p.
• DEF[B] - set of variables assigned values in B prior to any
use of that variable
• USE[B] - set of variables used in B prior to any definition
of that variable
• OUT[B] - variables live immediately after the block
OUT[B] -  IN[S] for all S in succ(B)
• IN[B] - variables live immediately before the block
IN[B] = USE[B]  (OUT[B] - DEF[B])
CS 540 Spring 2009 GMU 39
IN OUT IN OUT IN OUT
B1  a,b  a,b e a,b,e
B2 a,b a,b,c,d a,b,e a,b,c,d ,e a,b,e a,b,c,d,e
B3 a,b,c,d e a,b,c,e a,b,c,d,e a,b,c,d,e a,b,c,d,e a,b,c,d,e
B4 a,b,c,e a,b,c,d,e a,b,c,e a,b,c,d,e a,b,c,e a,b,c,d,e
B5 a,b,c,d a,b,d a,b,c,d a,b,d,e a,b,c,d a,b,d,e
B6 b,d  b,d  b,d 
OUT[B] =  IN[S] for all S in succ(B)
IN[B] = USE[B] + (OUT[B] - DEF[B])
Block DEF USE
B1 {a,b} { }
B2 {c,d} {a,b}
B3 { } {b,d}
B4 {d} {a,b,e}
B5 {e} {a,b,c}
B6 {a} {b,d}
CS 540 Spring 2009 GMU 40
{ }
{b,d}
{e}
{a,b,e}
{a,b,e}
{a,b,c,d,e}
{a,b,c,d,e}
{a,b,c,d,e}
{a,b,c,d}
{a,b,d,e}
{a,b,c,e}
{a,b,c,d,e}
CS 540 Spring 2009 GMU 41
Dataflow Analysis Problem #2:
Reachability
• A definition of a variable x is a statement
that may assign a value to x.
• A definition may reach a program point p if
there exists some path from the point
immediately following the definition to p
such that the assignment is not killed along
that path.
• Concept: relationship between definitions
and uses
CS 540 Spring 2009 GMU 42
What blocks do definitions d2 and
d4 reach?
d1 i = m – 1
d2 j = n
d3 i = i + 1
d4 j = j - 1
B1
B2
B3
B4 B5
d2
d4
CS 540 Spring 2009 GMU 43
Reachability Analysis:
Unstructured Input
1. Compute GEN and KILL at block—level
2. Compute IN[B] and OUT[B] for B
IN[B] = U OUT[P] where P is a predecessor of B
OUT[B] = GEN[B] U (IN[B] - KILL[B])
3. Repeat step 2 until there are no changes to
OUT sets
CS 540 Spring 2009 GMU 44
Reachability Analysis: Step 1
For each block, compute local (block level)
information = GEN/KILL sets
– GEN[B] = set of definitions generated by B
– KILL[B] = set of definitions that can not reach
the end of B
This information does not take control flow
between blocks into account.
CS 540 Spring 2009 GMU 45
Reasoning about Basic Blocks
Effect of single statement: a = b + c
• Uses variables {b,c}
• Kills all definitions of {a}
• Generates new definition (i.e. assigns a value)
of {a}
Local Analysis:
• Analyze the effect of each instruction
• Compose these effects to derive information about
the entire block
CS 540 Spring 2009 GMU 46
Example
d1 i = m – 1
d2 j = n
d3 a = u1
B1
B2
B3
B4
d4 i = i + 1
d5 j = j - 1
d6 a = u2
d7 i = u2
Gen = 4,5
Kill = 1,2,7
Gen = 1,2,3
Kill = 4,5,6,7
Gen = 7
Kill = 1,4
Gen = 6
Kill = 3
CS 540 Spring 2009 GMU 47
Reachability Analysis: Step 2
Compute IN/OUT for each block in a forward
direction. Start with IN[B] = 
– IN[B] = set of defns reaching the start of B
=  (out[P]) for all predecessor blocks in the CFG
– OUT[B] = set of defns reaching the end of B
= GEN[B]  (IN[B] – KILL[B])
Keep computing IN/OUT sets until a fixed point is
reached.
CS 540 Spring 2009 GMU 48
Reaching Definitions Algorithm
• Input: Flow graph with GEN and KILL for each block
• Output: in[B] and out[B] for each block.
For each block B do out[B] = gen[B], (true if in[B] = emptyset)
change := true;
while change do begin
change := false;
for each block B do begin
in[B] := U out[P], where P is a predecessor of B;
oldout = out[B];
out[B] := gen[B] U (in[B] - kill [B])
if out[B] != oldout then change := true;
end
end
CS 540 Spring 2009 GMU 49
IN OUT
B1  1,2,3
B2  4,5
B3  6
B4  7
IN[B] = (out[P]) for all predecessor
blocks in the CFG
OUT[B] = GEN[B]  (IN[B] – KILL[B])
d1 i = m – 1
d2 j = n
d3 a = u1
B1
B2
B3
B4
d4 i = i + 1
d5 j = j - 1
d6 a = u2
d7 i = u2
Gen = 4,5
Kill = 1,2,7
Gen = 1,2,3
Kill = 4,5,6,7
Gen = 7
Kill = 1,4
Gen = 6
Kill = 3
CS 540 Spring 2009 GMU 50
I
N
OUT IN OUT
B1  1,2,3  1,2,3
B2  4,5 OUT[1]+OUT[4]
= 1,2,3,7
4,5 + (1,2,3,7
– 1,2,7)
= 3,4,5
B3  6 OUT[2] = 3,4,5 6 + (3,4,5 – 3)
= 4,5,6
B4  7 OUT[2]+OUT[3]
= 3,4,5,6
7 + (3,4,5,6 – 1,4) = 3,5,6,7
IN[B] = (out[P]) for all predecessor
blocks in the CFG
OUT[B] = GEN[B] + (IN[B] – KILL[B])
CS 540 Spring 2009 GMU 51
IN OUT IN OUT IN OUT
B1  1,2,3  1,2,3  1,2,3
B2  4,5 1,2,3,7 3,4,5 OUT[1] + OUT[4] =
1,2,3,5,6,7
4,5 + (1,2,3,5,6,7-1,2,7) =
3,4,5,6
B3  6 3,4,5 4,5,6 OUT[2] = 3,4,5,6 6 + (3,4,5,6 – 3)
= 4,5,6
B4  7 3,4,5,6 3,5,6,7 OUT[2] + OUT[3] =
3,4,5,6
7+(3,4,5,6 – 1,4)
= 3,5,6,7
IN[B] = (out[P]) for all predecessor
blocks in the CFG
OUT[B] = GEN[B] + (IN[B] – KILL[B])
CS 540 Spring 2009 GMU 52
Forward vs. Backward
Forward flow vs. Backward flow
Forward: Compute OUT for given IN,GEN,KILL
– Information propagates from the predecessors of a
vertex.
– Examples: Reachability, available expressions, constant
propagation
Backward: Compute IN for given OUT,GEN,KILL
– Information propagates from the successors of a vertex.
– Example: Live variable Analysis
CS 540 Spring 2009 GMU 53
Forward vs. Backward Equations
Forward vs. backward
– Forward:
• IN[B] - process OUT[P] for all P in
predecessors(B)
• OUT[B] = local U (IN[B] – local)
– Backward:
• OUT[B] - process IN[S] for all S in
successor(B)
• IN[B] = local U (OUT[B] – local)
CS 540 Spring 2009 GMU 54
May vs. Must
May vs. Must
Must – true on all paths
Ex: constant propagation – variable must provably
hold appropriate constant on all paths in order to do
a substitution
May – true on some path
Ex: Live variable analysis – a variable is live if it
could be used on some path; reachability – a
definition reaches a point if it can reach it on some
path
CS 540 Spring 2009 GMU 55
May vs. Must Equations
• May vs. Must
– May – IN[B] = (out[P]) for all
P in pred(B)
– Must – IN[B] = (out[P]) for
all P in pred(B)
CS 540 Spring 2009 GMU 56
• Reachability
– IN[B] = (out[P]) for all P in pred(B)
– OUT[B] = GEN[B] + (IN[B] – KILL[B])
• Live Variable Analysis
– OUT[B] = (IN[S]) for all S in succ(B)
– IN[B] = USE[B]  (OUT[B] - DEF[B])
• Constant Propagation
– IN[B] = (out[P]) for all P in pred(B)
– OUT[B] = DEF_CONST[B]  (IN[B] –
KILL_CONST[B])
CS 540 Spring 2009 GMU 57
Discussion
• Why does this work?
– Finite set – can be represented as bit vectors
– Theory of lattices
• Is this guaranteed to terminate?
– Sets only grow and since finite in size …
• Can we find ways to reduce the number of
iterations?
CS 540 Spring 2009 GMU 58
Choosing visit order for Dataflow
Analysis
In forward flow analysis situations, if we visit the blocks in
depth first order, we can reduce the number of iterations.
Suppose definition d follows block path 3  5  19  35 
16  23  45  4  10  17 where the block
numbering corresponds to the preorder depth-first
numbering.
Then we can compute the reach of this definition in 3
iterations of our algorithm.
3  5  19  35  16  23  45  4  10  17

More Related Content

Similar to Code Optimization.ppt

Similar to Code Optimization.ppt (20)

GCC
GCCGCC
GCC
 
Unit 4 dica
Unit 4 dicaUnit 4 dica
Unit 4 dica
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
 
Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
L7 pointers
L7 pointersL7 pointers
L7 pointers
 
Cs gate-2011
Cs gate-2011Cs gate-2011
Cs gate-2011
 
Cs gate-2011
Cs gate-2011Cs gate-2011
Cs gate-2011
 
ESL Anyone?
ESL Anyone? ESL Anyone?
ESL Anyone?
 
Lcdf4 chap 03_p2
Lcdf4 chap 03_p2Lcdf4 chap 03_p2
Lcdf4 chap 03_p2
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Apclass
ApclassApclass
Apclass
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
LalitBDA2015V3
LalitBDA2015V3LalitBDA2015V3
LalitBDA2015V3
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
sheet6.pdf
sheet6.pdfsheet6.pdf
sheet6.pdf
 

Recently uploaded

DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 

Recently uploaded (20)

DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

Code Optimization.ppt

  • 1. Lecture 11: Code Optimization CS 540 George Mason University
  • 2. CS 540 Spring 2009 GMU 2 Code Optimization REQUIREMENTS: • Meaning must be preserved (correctness) • Speedup must occur on average. • Work done must be worth the effort. OPPORTUNITIES: • Programmer (algorithm, directives) • Intermediate code • Target code
  • 3. CS 540 Spring 2009 GMU 3 Code Optimization Scanner (lexical analysis) Parser (syntax analysis) Code Optimizer Semantic Analysis (IC generator) Code Generator Symbol Table Source language tokens Syntactic structure Syntactic/semantic structure Target language
  • 4. CS 540 Spring 2009 GMU 4 Levels • Window – peephole optimization • Basic block • Procedural – global (control flow graph) • Program level – intraprocedural (program dependence graph)
  • 5. CS 540 Spring 2009 GMU 5 Peephole Optimizations • Constant Folding x := 32 becomes x := 64 x := x + 32 • Unreachable Code goto L2 x := x + 1  unneeded • Flow of control optimizations goto L1 becomes goto L2 … L1: goto L2
  • 6. CS 540 Spring 2009 GMU 6 Peephole Optimizations • Algebraic Simplification x := x + 0  unneeded • Dead code x := 32  where x not used after statement y := x + y  y := y + 32 • Reduction in strength x := x * 2  x := x + x
  • 7. CS 540 Spring 2009 GMU 7 Peephole Optimizations • Local in nature • Pattern driven • Limited by the size of the window
  • 8. CS 540 Spring 2009 GMU 8 Basic Block Level • Common Subexpression elimination • Constant Propagation • Dead code elimination • Plus many others such as copy propagation, value numbering, partial redundancy elimination, …
  • 9. CS 540 Spring 2009 GMU 9 Simple example: a[i+1] = b[i+1] • t1 = i+1 • t2 = b[t1] • t3 = i + 1 • a[t3] = t2 • t1 = i + 1 • t2 = b[t1] • t3 = i + 1  no longer live • a[t1] = t2 Common expression can be eliminated
  • 10. CS 540 Spring 2009 GMU 10 • i = 4 • t1 = i+1 • t2 = b[t1] • a[t1] = t2 • i = 4 • t1 = 5 • t2 = b[t1] • a[t1] = t2 Now, suppose i is a constant: • i = 4 • t1 = 5 • t2 = b[5] • a[5] = t2 • i = 4 • t2 = b[5] • a[5] = t2 Final Code:
  • 11. CS 540 Spring 2009 GMU 11 Control Flow Graph - CFG CFG = < V, E, Entry >, where V = vertices or nodes, representing an instruction or basic block (group of statements). E = (V x V) edges, potential flow of control Entry is an element of V, the unique program entry Two sets used in algorithms: • Succ(v) = {x in V| exists e in E, e = v x} • Pred(v) = {x in V| exists e in E, e = x v} 1 2 3 4 5
  • 12. CS 540 Spring 2009 GMU 12 Definitions • point - any location between adjacent statements and before and after a basic block. • A path in a CFG from point p1 to pn is a sequence of points such that  j, 1 <= j < n, either pi is the point immediately preceding a statement and pi+1 is the point immediately following that statement in the same block, or pi is the end of some block and pi+1 is the start of a successor block.
  • 13. CS 540 Spring 2009 GMU 13 CFG c = a + b d = a * c i = 1 f[i] = a + b c = c * 2 if c > d g = a * c g = d * d i = i + 1 if i > 10 points path
  • 14. CS 540 Spring 2009 GMU 14 Optimizations on CFG • Must take control flow into account – Common Sub-expression Elimination – Constant Propagation – Dead Code Elimination – Partial redundancy Elimination – … • Applying one optimization may create opportunities for other optimizations.
  • 15. CS 540 Spring 2009 GMU 15 Redundant Expressions An expression x op y is redundant at a point p if it has already been computed at some point(s) and no intervening operations redefine x or y. m = 2*y*z t0 = 2*y t0 = 2*y m = t0*z m = t0*z n = 3*y*z t1 = 3*y t1 = 3*y n = t1*z n = t1*z o = 2*y–z t2 = 2*y o = t2-z o = t0-z redundant
  • 16. CS 540 Spring 2009 GMU 16 Redundant Expressions c = a + b d = a * c i = 1 f[i] = a + b c = c * 2 if c > d g = a * c g = d * d i = i + 1 if i > 10 Candidates: a + b a * c d * d c * 2 i + 1 Definition site Since a + b is available here,  redundant!
  • 17. CS 540 Spring 2009 GMU 17 Redundant Expressions c = a + b d = a * c i = 1 f[i] = a + b c = c * 2 if c > d g = a * c g = d * d i = i + 1 if i > 10 Candidates: a + b a * c d * d c * 2 i + 1 Definition site Kill site Not available  Not redundant
  • 18. CS 540 Spring 2009 GMU 18 Redundant Expressions • An expression e is defined at some point p in the CFG if its value is computed at p. (definition site) • An expression e is killed at point p in the CFG if one or more of its operands is defined at p. (kill site) • An expression is available at point p in a CFG if every path leading to p contains a prior definition of e and e is not killed between that definition and p.
  • 19. CS 540 Spring 2009 GMU 19 Removing Redundant Expressions t1 = a + b c = t1 d = a * c i = 1 f[i] = t1 c = c * 2 if c > d g = a * c g = d*d i = i + 1 if i > 10 Candidates: a + b a * c d * d c * 2 i + 1
  • 20. CS 540 Spring 2009 GMU 20 Constant Propagation b = 5 c = 4*b c > b d = b + 2 e = a + b b = 5 c = 20 c > 5 d = 7 e = a + 5 e = a + b t f t f b = 5 c = 20 20 > 5 d = 7 e = a + 5 t f
  • 21. CS 540 Spring 2009 GMU 21 Constant Propagation b = 5 c = 20 20 > 5 d = 7 e = a + 5 t f b = 5 c = 20 d = 7 e = a + 5
  • 22. CS 540 Spring 2009 GMU 22 Copy Propagation b = a c = 4*b c > b d = b + 2 e = a + b b = a c = 4*a c > a d = a + 2 e = a + a e = a + b
  • 23. CS 540 Spring 2009 GMU 23 Simple Loop Optimizations: Code Motion while (i <= limit - 2) t := limit - 2 while (i <= t) L1: t1 = limit – 2 if (i > t1) goto L2 body of loop goto L1 L2: t1 = limit – 2 L1: if (i > t1) goto L2 body of loop goto L1 L2:
  • 24. CS 540 Spring 2009 GMU 24 Simple Loop Optimizations: Strength Reduction • Induction Variables control loop iterations j = j – 1 t4 = 4 * j t5 = a[t4] if t5 > v j = j – 1 t4 = t4 - 4 t5 = a[t4] if t5 > v t4 = 4*j
  • 25. CS 540 Spring 2009 GMU 25 Simple Loop Optimizations • Loop transformations are often used to expose other optimization opportunities: – Normalization – Loop Interchange – Loop Fusion – Loop Reversal – …
  • 26. CS 540 Spring 2009 GMU 26 Consider Matrix Multiplication for i = 1 to n do for j = 1 to n do for k = 1 to n do C[i,j] = C[i,j] + A[i,k] + B[k,j] end end end = + i i j j k k C B A
  • 27. CS 540 Spring 2009 GMU 27 Memory Usage • For A: Elements are accessed across rows, spatial locality is exploited for cache (assuming row major storage) • For B: Elements are accessed along columns, unless cache can hold all of B, cache will have problems. • For C: Single element computed per loop – use register to hold = + i i j j k k C B A
  • 28. CS 540 Spring 2009 GMU 28 Matrix Multiplication Version 2 for i = 1 to n do for k = 1 to n do for j = 1 to n do C[i,j] = C[i,j] + A[i,k] + B[k,j] end end end = + i i j j k C B A loop interchange k
  • 29. CS 540 Spring 2009 GMU 29 Memory Usage • For A: Single element loaded for loop body • For B: Elements are accessed along rows to exploit spatial locality. • For C: Extra loading/storing, but across rows = + i i j j k C B A k
  • 30. CS 540 Spring 2009 GMU 30 Simple Loop Optimizations • How to determine safety? – Does the new multiply give the same answer? – Can be reversed?? for (I=1 to N) a[I] = a[I+1] – can this loop be safely reversed?
  • 31. CS 540 Spring 2009 GMU 31 Data Dependencies • Flow Dependencies - write/read x := 4; y := x + 1 • Output Dependencies - write/write x := 4; x := y + 1; • Antidependencies - read/write y := x + 1; x := 4;
  • 32. CS 540 Spring 2009 GMU 32 x := 4 y := 6 p := x + 2 z := y + p x := z y := p x := 4 y := 6 p := x + 2 z := y + p y := p x := z Flow Output Anti
  • 33. CS 540 Spring 2009 GMU 33 Global Data Flow Analysis Collecting information about the way data is used in a program. • Takes control flow into account • HL control constructs – Simpler – syntax driven – Useful for data flow analysis of source code • General control constructs – arbitrary branching Information needed for optimizations such as: constant propagation, common sub-expressions, partial redundancy elimination …
  • 34. CS 540 Spring 2009 GMU 34 Dataflow Analysis: Iterative Techniques • First, compute local (block level) information. • Iterate until no changes while change do change = false for each basic block apply equations updating IN and OUT if either IN or OUT changes, set change to true end
  • 35. CS 540 Spring 2009 GMU 35 Live Variable Analysis A variable x is live at a point p if there is some path from p where x is used before it is defined. Want to determine for some variable x and point p whether the value of x could be used along some path starting at p. • Information flows backwards • May – ‘along some path starting at p’ is x live here?
  • 36. CS 540 Spring 2009 GMU 36 Global Live Variable Analysis Want to determine for some variable x and point p whether the value of x could be used along some path starting at p. • DEF[B] - set of variables assigned values in B prior to any use of that variable • USE[B] - set of variables used in B prior to any definition of that variable • OUT[B] - variables live immediately after the block OUT[B] - IN[S] for all S in succ(B) • IN[B] - variables live immediately before the block IN[B] = USE[B] + (OUT[B] - DEF[B])
  • 37. CS 540 Spring 2009 GMU 37 d1: a = 1 d2: b = 2 d3: c = a + b d4: d = c - a d8: b = a + b d9: e = c - 1 d10: a = b * d d22: b = a - d d5: d = b * d d6: d = a + b d7: e = e + 1 B1 B2 B3 B4 B5 B6 DEF=a,b USE = DEF=c,d USE = a,b DEF= USE = b,d DEF=d USE = a,b,e DEF= e USE = a,b,c DEF= a USE = b,d
  • 38. CS 540 Spring 2009 GMU 38 Global Live Variable Analysis Want to determine for some variable x and point p whether the value of x could be used along some path starting at p. • DEF[B] - set of variables assigned values in B prior to any use of that variable • USE[B] - set of variables used in B prior to any definition of that variable • OUT[B] - variables live immediately after the block OUT[B] -  IN[S] for all S in succ(B) • IN[B] - variables live immediately before the block IN[B] = USE[B]  (OUT[B] - DEF[B])
  • 39. CS 540 Spring 2009 GMU 39 IN OUT IN OUT IN OUT B1  a,b  a,b e a,b,e B2 a,b a,b,c,d a,b,e a,b,c,d ,e a,b,e a,b,c,d,e B3 a,b,c,d e a,b,c,e a,b,c,d,e a,b,c,d,e a,b,c,d,e a,b,c,d,e B4 a,b,c,e a,b,c,d,e a,b,c,e a,b,c,d,e a,b,c,e a,b,c,d,e B5 a,b,c,d a,b,d a,b,c,d a,b,d,e a,b,c,d a,b,d,e B6 b,d  b,d  b,d  OUT[B] =  IN[S] for all S in succ(B) IN[B] = USE[B] + (OUT[B] - DEF[B]) Block DEF USE B1 {a,b} { } B2 {c,d} {a,b} B3 { } {b,d} B4 {d} {a,b,e} B5 {e} {a,b,c} B6 {a} {b,d}
  • 40. CS 540 Spring 2009 GMU 40 { } {b,d} {e} {a,b,e} {a,b,e} {a,b,c,d,e} {a,b,c,d,e} {a,b,c,d,e} {a,b,c,d} {a,b,d,e} {a,b,c,e} {a,b,c,d,e}
  • 41. CS 540 Spring 2009 GMU 41 Dataflow Analysis Problem #2: Reachability • A definition of a variable x is a statement that may assign a value to x. • A definition may reach a program point p if there exists some path from the point immediately following the definition to p such that the assignment is not killed along that path. • Concept: relationship between definitions and uses
  • 42. CS 540 Spring 2009 GMU 42 What blocks do definitions d2 and d4 reach? d1 i = m – 1 d2 j = n d3 i = i + 1 d4 j = j - 1 B1 B2 B3 B4 B5 d2 d4
  • 43. CS 540 Spring 2009 GMU 43 Reachability Analysis: Unstructured Input 1. Compute GEN and KILL at block—level 2. Compute IN[B] and OUT[B] for B IN[B] = U OUT[P] where P is a predecessor of B OUT[B] = GEN[B] U (IN[B] - KILL[B]) 3. Repeat step 2 until there are no changes to OUT sets
  • 44. CS 540 Spring 2009 GMU 44 Reachability Analysis: Step 1 For each block, compute local (block level) information = GEN/KILL sets – GEN[B] = set of definitions generated by B – KILL[B] = set of definitions that can not reach the end of B This information does not take control flow between blocks into account.
  • 45. CS 540 Spring 2009 GMU 45 Reasoning about Basic Blocks Effect of single statement: a = b + c • Uses variables {b,c} • Kills all definitions of {a} • Generates new definition (i.e. assigns a value) of {a} Local Analysis: • Analyze the effect of each instruction • Compose these effects to derive information about the entire block
  • 46. CS 540 Spring 2009 GMU 46 Example d1 i = m – 1 d2 j = n d3 a = u1 B1 B2 B3 B4 d4 i = i + 1 d5 j = j - 1 d6 a = u2 d7 i = u2 Gen = 4,5 Kill = 1,2,7 Gen = 1,2,3 Kill = 4,5,6,7 Gen = 7 Kill = 1,4 Gen = 6 Kill = 3
  • 47. CS 540 Spring 2009 GMU 47 Reachability Analysis: Step 2 Compute IN/OUT for each block in a forward direction. Start with IN[B] =  – IN[B] = set of defns reaching the start of B =  (out[P]) for all predecessor blocks in the CFG – OUT[B] = set of defns reaching the end of B = GEN[B]  (IN[B] – KILL[B]) Keep computing IN/OUT sets until a fixed point is reached.
  • 48. CS 540 Spring 2009 GMU 48 Reaching Definitions Algorithm • Input: Flow graph with GEN and KILL for each block • Output: in[B] and out[B] for each block. For each block B do out[B] = gen[B], (true if in[B] = emptyset) change := true; while change do begin change := false; for each block B do begin in[B] := U out[P], where P is a predecessor of B; oldout = out[B]; out[B] := gen[B] U (in[B] - kill [B]) if out[B] != oldout then change := true; end end
  • 49. CS 540 Spring 2009 GMU 49 IN OUT B1  1,2,3 B2  4,5 B3  6 B4  7 IN[B] = (out[P]) for all predecessor blocks in the CFG OUT[B] = GEN[B]  (IN[B] – KILL[B]) d1 i = m – 1 d2 j = n d3 a = u1 B1 B2 B3 B4 d4 i = i + 1 d5 j = j - 1 d6 a = u2 d7 i = u2 Gen = 4,5 Kill = 1,2,7 Gen = 1,2,3 Kill = 4,5,6,7 Gen = 7 Kill = 1,4 Gen = 6 Kill = 3
  • 50. CS 540 Spring 2009 GMU 50 I N OUT IN OUT B1  1,2,3  1,2,3 B2  4,5 OUT[1]+OUT[4] = 1,2,3,7 4,5 + (1,2,3,7 – 1,2,7) = 3,4,5 B3  6 OUT[2] = 3,4,5 6 + (3,4,5 – 3) = 4,5,6 B4  7 OUT[2]+OUT[3] = 3,4,5,6 7 + (3,4,5,6 – 1,4) = 3,5,6,7 IN[B] = (out[P]) for all predecessor blocks in the CFG OUT[B] = GEN[B] + (IN[B] – KILL[B])
  • 51. CS 540 Spring 2009 GMU 51 IN OUT IN OUT IN OUT B1  1,2,3  1,2,3  1,2,3 B2  4,5 1,2,3,7 3,4,5 OUT[1] + OUT[4] = 1,2,3,5,6,7 4,5 + (1,2,3,5,6,7-1,2,7) = 3,4,5,6 B3  6 3,4,5 4,5,6 OUT[2] = 3,4,5,6 6 + (3,4,5,6 – 3) = 4,5,6 B4  7 3,4,5,6 3,5,6,7 OUT[2] + OUT[3] = 3,4,5,6 7+(3,4,5,6 – 1,4) = 3,5,6,7 IN[B] = (out[P]) for all predecessor blocks in the CFG OUT[B] = GEN[B] + (IN[B] – KILL[B])
  • 52. CS 540 Spring 2009 GMU 52 Forward vs. Backward Forward flow vs. Backward flow Forward: Compute OUT for given IN,GEN,KILL – Information propagates from the predecessors of a vertex. – Examples: Reachability, available expressions, constant propagation Backward: Compute IN for given OUT,GEN,KILL – Information propagates from the successors of a vertex. – Example: Live variable Analysis
  • 53. CS 540 Spring 2009 GMU 53 Forward vs. Backward Equations Forward vs. backward – Forward: • IN[B] - process OUT[P] for all P in predecessors(B) • OUT[B] = local U (IN[B] – local) – Backward: • OUT[B] - process IN[S] for all S in successor(B) • IN[B] = local U (OUT[B] – local)
  • 54. CS 540 Spring 2009 GMU 54 May vs. Must May vs. Must Must – true on all paths Ex: constant propagation – variable must provably hold appropriate constant on all paths in order to do a substitution May – true on some path Ex: Live variable analysis – a variable is live if it could be used on some path; reachability – a definition reaches a point if it can reach it on some path
  • 55. CS 540 Spring 2009 GMU 55 May vs. Must Equations • May vs. Must – May – IN[B] = (out[P]) for all P in pred(B) – Must – IN[B] = (out[P]) for all P in pred(B)
  • 56. CS 540 Spring 2009 GMU 56 • Reachability – IN[B] = (out[P]) for all P in pred(B) – OUT[B] = GEN[B] + (IN[B] – KILL[B]) • Live Variable Analysis – OUT[B] = (IN[S]) for all S in succ(B) – IN[B] = USE[B]  (OUT[B] - DEF[B]) • Constant Propagation – IN[B] = (out[P]) for all P in pred(B) – OUT[B] = DEF_CONST[B]  (IN[B] – KILL_CONST[B])
  • 57. CS 540 Spring 2009 GMU 57 Discussion • Why does this work? – Finite set – can be represented as bit vectors – Theory of lattices • Is this guaranteed to terminate? – Sets only grow and since finite in size … • Can we find ways to reduce the number of iterations?
  • 58. CS 540 Spring 2009 GMU 58 Choosing visit order for Dataflow Analysis In forward flow analysis situations, if we visit the blocks in depth first order, we can reduce the number of iterations. Suppose definition d follows block path 3  5  19  35  16  23  45  4  10  17 where the block numbering corresponds to the preorder depth-first numbering. Then we can compute the reach of this definition in 3 iterations of our algorithm. 3  5  19  35  16  23  45  4  10  17