0% found this document useful (0 votes)
304 views

Dominators Global Data Flow Analysis

This document discusses dominators in control flow graphs and their applications. It defines that a node d dominates a node n if every path from the initial node to n goes through d. The entry node of a loop dominates all nodes in the loop. A dominator tree can represent these relationships, with each node's parent being its immediate dominator. Global data flow analysis examines the entire program to determine how data is defined and used, which enables certain compiler optimizations.

Uploaded by

Mahi Mahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
304 views

Dominators Global Data Flow Analysis

This document discusses dominators in control flow graphs and their applications. It defines that a node d dominates a node n if every path from the initial node to n goes through d. The entry node of a loop dominates all nodes in the loop. A dominator tree can represent these relationships, with each node's parent being its immediate dominator. Global data flow analysis examines the entire program to determine how data is defined and used, which enables certain compiler optimizations.

Uploaded by

Mahi Mahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

DOMINATORS

4/8/22 SCS1303-Compiler Design 1


Loops in Flow graph

• A graph representation of three-address statements, called a


flow graph, is useful for understanding code-generation
algorithms.
• Nodes in the flow graph represent computations, and the
edges represent the flow of control.

• In order to define what constitutes a loop in a flow graph may


require to define:
– A node dominating another to define “natural loops”,
– Reducible flow graphs.

4/8/22 SCS1303-Compiler Design 2


Dominators
• In  control flow graphs, a node d dominates a node n  , if every
path from initial node of the flow graph to n goes through d.
• Notationally, this is written as d dom n 
• By definition, every node dominates itself.
• The entry of a loop dominates all nodes in the loop.

4/8/22 SCS1303-Compiler Design 3


Example: Consider the flow graph,

• Initial node, node 1 dominates


every node.
• node 2 dominates itself .
• node 3 dominates all but 1 and 2.
• node 4 dominates all but 1,2 and 3.
• node 5 and 6 dominates only
themselves, since flow of control
can skip around either by going
through the other. 
• node 7 dominates 7,8 ,9 and 10.
• node 8 dominates 8,9 and 10.
• node 9 and 10 dominates only
Flow graph
themselves.

4/8/22 SCS1303-Compiler Design 4


A useful way of presenting dominator information is in a tree,
called the dominator tree, in which
– The initial node is the root.
– The parent of each other node is its immediate dominator.
– Each node d dominates only its descendents in the tree.
– All and only dominators of a node n will be the ancestors
of n in the tree.

4/8/22 SCS1303-Compiler Design 5


 
Properties of DOM
• The existence of dominator tree follows from a property of
dominators;
• Each node n has a unique immediate dominator m that is the
last dominator of n on any path from the initial node to n.
• In terms of the dom relation, the immediate dominator m has
that property that if d≠n and d dom n, then d dom m.
D(1)={1}
D(2)={1,2}
D(3)={1,3}
D(4)={1,3,4}
D(5)={1,3,4,5}
D(6)={1,3,4,6}
D(7)={1,3,4,7}
D(8)={1,3,4,7,8}
D(9)={1,3,4,7,8,9}
D(10)={1,3,4,7,8,10}
4/8/22 SCS1303-Compiler Design 6
 
Properties of DOM
• Algebraic Properties regarding the dominance relation are:
1. Dominance is a reflexive partial order.That is,dominance
is reflexive.
a DOM a for all a
2. Antisymmetric
a DOM b and b DOM a implies that a=b

3. Transitive
a DOM b and b DOM c implies that a DOM c

4. It is possible that there may be node a and b such that


neither a DOM b nor b DOM a.

4/8/22 SCS1303-Compiler Design 7


Loop Detection
• A good way to look for loops is to search for edges in the flow
graph whose heads dominate their tail.
– i.e If a→b is an edge , b is the head and a is the tail.
– Such edges are back edges.

•The edge 7 →4 is a back edge and 4 DOM 7


•The edge 10 →7 is a back edge and 7 DOM 10
• similarly 4 →3, 8 →3 and 9 → 1

•They represent the loops.

4/8/22 SCS1303-Compiler Design 8


Natural Loops
• An useful application of dominator information is in
determining the loops of a flow graph suitable for
improvement. There are two essential properties of loops:
– A loop must have a single entry point, called the header.
This entry point-dominates all nodes in the loop.
– There must be at least one way to iterate the loop(i.e.)at
least one path back to the header.
• A good way to find all the loops in a flow graph is to search for
edges in the flow graph whose heads dominate their tails.
• Given a back edge n → d, we can find the natural loop of the
edge to be d plus the set of nodes that can reach n without
going through d.
• Node d is the header of the loop.
 
4/8/22 SCS1303-Compiler Design 9
Algorithm: Constructing the natural loop of a back edge.

Input: A flow graph G and a back edge n→d.


Output: The set loop consisting of all nodes in the natural loop
n→d.
Method:
– Use stacking algorithm to consider each node m in loop.
– Other than d to make sure that m’s predecessors are also
placed in loop.
– Each node in loop, except for d, is placed once on stack, so
its predecessors will be examined.
 

4/8/22 SCS1303-Compiler Design 10


Algorithm:Constructing the natural loop
Procedure insert(m);
 if m is not in loop then
begin
loop := loop U {m};
  push m onto stack
end;
/* main program follows*/
stack : = empty;
loop : = {d};
insert(n);
while stack is not empty do
begin
pop m, the first element of stack, off stack; 
for each predecessor p of m do insert(p)
end
4/8/22 SCS1303-Compiler Design 11
Finding Dominators
Input: A flow graph G with set of nodes N, set of edges E and
initial node n0 .
Output: The Relation DOM
Method: Compute D(n),set of dominators for n.

4/8/22 SCS1303-Compiler Design 12


Dominator Computing Algorithm
begin
D(n0)={n0}
for n in N - {n0} do D(n)=N D(1)= {1}
CHANGE=true D(2)= {1,2}
while CHANGE do D(3)= {{3} ꓴ ({1} ꓵ {1,2} ꓵ {1,2…,10}}
begin ={1,3}
CHANGE=false Compute all?
for n in N - {n0} do
begin NEWD  { n }   D (n)
P a pred  D(1)={1}
ecessor of n
D(2)={1,2}
D(3)={1,3}
if D(n) ≠ NEWD then CHANGE=true D(4)={1,3,4}
D(n) = NEWD D(5)={1,3,4,5}
end D(6)={1,3,4,6}
end D(7)={1,3,4,7}
end D(8)={1,3,4,7,8}
D(9)={1,3,4,7,8,9}
4/8/22 SCS1303-Compiler Design 13
D(10)={1,3,4,7,8,10}
Reducible flow graphs
• Reducible flow graphs are special flow graphs, for which several
code optimization transformations are especially easy to
perform,
• Loops are unambiguously defined, dominators can be easily
calculated, data flow analysis problems can also be solved
efficiently.
• Exclusive use of structured flow-of-control statements such as if-
then-else, while-do, continue, and break statements produces
programs whose flow graphs are always reducible.
 
The most important properties of reducible flow graphs are that
1.     There are no jumps into the middle of loops from outside;
2.     The only entry to a loop is through its header
 
4/8/22 SCS1303-Compiler Design 14
Definition
• A flow graph G is reducible if and only if we can partition the
edges into two disjoint groups, forward edges and back edges
with the following properties:
– The forward edges form an acyclic graph in which every
node can be reached from the initial node of G.
– The back edges consist only the edges whose heads
dominate their tails.

4/8/22 SCS1303-Compiler Design 15


Applications
• Dominators have applications in compilers for
computing static single assignment form.
• A number of compiler optimizations can also benefit from
dominators. The flow graph in this case comprises basic
blocks.
• Memory usage analysis can benefit from the dominator tree
to easily find leaks and identify high memory usage.
• In hardware systems, dominators are used for computing
signal probabilities for test generation, estimating switching
activities for power and noise analysis, and selecting cut
points in equivalence checking.
• In software systems, they are used for reducing the size of the
test set in structural testing techniques such as statement and
branch coverage.
4/8/22 SCS1303-Compiler Design 16
GLOBAL DATA FLOW ANALYSIS

4/8/22 SCS1303-Compiler Design 17


Global data flow analysis

• To efficiently optimize the code compiler collects all the


information about the program and distribute this
information to each block of the flow graph. This process is
known as data-flow graph analysis.

• Certain optimization can only be achieved by examining the


entire program. It can't be achieve by examining just a portion
of the program.

Friday, April 8, 2022 SCS1303 Compiler Design 18


• It is the analysis of flow of data in control flow graph,
– i.e., the analysis that determines the information regarding
the definition and use of data in program.
• With the help of this analysis optimization can be done.

Definition Point: a point in a program


containing some definition.
Reference Point: a point in a program
containing a reference to a data item.
Evaluation Point: a point in a program
containing evaluation of expression.

Friday, April 8, 2022 SCS1303 Compiler Design 19


Global data flow analysis
• use-definition (or ud-) chaining is one particular problem.
– Given that variable A is used at point p,at which points
could the value of A used at p have been defined?
– By a use of variable A means any occurrence of A as an
operand.
– By a definition of A means either an assignment to A or the
reading of a value for a.
– By a point in a program means the position before or after
any intermediate language statement.
• Control reaches a point just before a statement when
that statement is about to be executed.
• Control reaches a point after a statement when that
statement has just been executed.

Friday, April 8, 2022 SCS1303 Compiler Design 20


Reaching Definitions
• A definition of a variable A reaches a point p ,if there is a path in
the flow graph from that definition to p, such that no other
definitions of A appear on the path.
• To determine the definitions that can reach a given point in a
program requires assigning distinct number to each definition.
• To compute two sets for each basic block B:
– GEN[B]-set of generated definitions within block B.
– KILL[B]- set of definitions outside of B that define identifiers
that also have definition within B.
• To compute the sets IN[B] and OUT[B]:
– IN[B]-set of all definition reaching the point just before
the first statement of block B.
– OUT[B]-set of definitions reaching the point just after the last
statement of B

Friday, April 8, 2022 SCS1303 Compiler Design 21


Data-Flow Equations
• There are two sets of equations called data-flow equations, that
relate IN and OUT. They are for all blocks B,
1. OUT[B]  IN[B]  KILL[B]  GEN[B]
2. IN [ B ]  OUT [ P ]
P a pred 
IN [ B]   KILL[ B]
ecessor of B

• The rule (1) says that a definition d reaches the end of the block B
if and only if either
i. d is in IN[B], i.e d reaches the beginning of B and is not killed by B ,
or
ii. d is generated within B i.e., it appears in B and its identifier is not
subsequently redefined within B.
• The rule (2) says that a definition reaches the beginning of the
block B if and only if it reaches the end of one of its predecessors.

Friday, April 8, 2022 SCS1303 Compiler Design 22


Algorithm for Reaching definition

Input: A flow graph for which GEN[B] and KILL[B] have been
computed for each block B.

Output: IN[B] and OUT[B] for each block B.

Method: An iterative approach, initializing with IN[B]=Ø for all B


and converging to the desired values of IN and OUT.

Friday, April 8, 2022 SCS1303 Compiler Design 23


Algorithm to compute IN and OUT
begin
for each block B do
begin
IN[B]=Ø
OUT[B]=GEN[B]
end
CHANGE=true
while CHANGE do
begin
CHANGE=false
for each block B do
begin
NEWIN   OUT
P a pred 
[P]
ecessor of B

if NEWIN≠IN[B] then CHANGE=true


IN[B]=NEWIN
OUT[B]  IN[B]  KILL[B]  GEN[B]
end
end
end
Friday, April 8, 2022 SCS1303 Compiler Design 24
Gen and KILL
Block GEN[B] Bit KILL[B] Bit
B vector vector
B1 {d1,d2} 11000 {d3,d4 ,d5} 00111
B2 {d3} 00100 {d1} 10000
B3 {d4} 00010 {d2,d5} 01001
B4 {d5} 00001 {d2,d4} 01010
B5 Ø 00000 Ø 00000

A flow Graph

Friday, April 8, 2022 SCS1303 Compiler Design 25


Computation

For block B1 :
NEWIN=OUT[B2] , since B2 is the only predecessor of B1
NEWIN=GEN[B2]=00100 = IN[B1] IN [ B]   KILL[ B]
OUT[B1] = IN[B1] – KILL[B1] + GEN[B1] 00100
= 00100 – 00111 + 11000 11000
00000
= 11000
For block B2 :
NEWIN=OUT[B1] + OUT[B5] , since B1 and B5 are the predecessor of B2
IN[B2] = 11000 + 00000 = 11000
OUT[B2] = IN[B2] – KILL[B2] + GEN[B2]
= 11000 – 10000 + 00100
= 01100

Friday, April 8, 2022 SCS1303 Compiler Design 26


Computation of IN and OUT
Initial pass Pass-1
Block B IN[B] OUT[B] Block B IN[B] OUT[B]

B1 00000 11000 B1 00100 11000


B2 00000 00100 B2 11000 01100
B3 00000 00010 B3 01100 00110
B4 00000 00001 B4 00110 00101
B5 00000 00000
B5 00111 00111

Pass-2 Pass-3
Block B IN[B] OUT[B] Block B IN[B] OUT[B]
B1 01100 11000 B1 01111 11000
B2 11111 01111 B2 11111 01111
B3 01111 00110 B3 01111 00110
B4 00110 00101 B4 00110 00101
B5 00111 00111 B5 00111 00111
Friday, April 8, 2022 SCS1303 Compiler Design 27
Computing ud-chains
• From reaching definitions information we can compute ud-
chains.
• If a use of variable A is preceded in its block by a definition of
A, then only the last definition of A in the block prior to this
use reaches the use.
– i.e. ud-chain for this use contains only one definition.
• If a use of A is preceded in its block B by no definition of A,
then the ud-chain for this use consists of all definitions of A in
IN[B].
• Since ud-chain take up much space, it is important for an
optimizing compiler to format them compactly.

Friday, April 8, 2022 SCS1303 Compiler Design 28


• A Use-Definition Chain (UD Chain) is a data structure that
consists of a use, U, of a variable, and all the definitions, D, of
that variable that can reach that use without any other
intervening definitions.
• A UD Chain generally means the assignment of some value to
a variable.
• A counterpart of a UD Chain is a Definition-Use Chain (DU
Chain), which consists of a definition, D, of a variable and all
the uses, U, reachable from that definition without any other
intervening definitions.
• Both UD and DU chains are created by using a form of static
code analysis known as data flow analysis.

Friday, April 8, 2022 SCS1303 Compiler Design 29


Applications
• Knowing the use-def and def-use chains are helpful in
compiler optimizations including constant propagation and
common sub-expression elimination.
• Helpful in dead-code elimination.
• Instruction reordering.
• (Implementation of ) scoping/shadowing.

Friday, April 8, 2022 SCS1303 Compiler Design 30

You might also like