You are on page 1of 15

Reaching Definitions and u-d

Chaining
M.B.Chandak
hodcs@rknec.edu
Reaching Definitions
• A definition of any variable is “killed” if between two points along the
path, there is a re-assignment.
• A definition “d” reaches point “p” if there is a path from the point
immediately following “d” to “p” such that “d” is not killed along the
path.
• For example: a=b+c in block B1 and a=d+e in block b4 and there is
path between B1 and B4, means definition of “a” in B1 does not
reaches B4.
• To compute reaching definitions, IN-OUT Analysis is performed.
• Reaching definition for block B1 is represented as IN[B1].
• Purpose: To perform optimization by computing loop invariant
Algorithm
Step 1:
For each block B do IN[B] =  and OUT[B] = GEN[B]
CHANGE=TRUE
While CHANGE do
{
CHANGE = FALSE
For each block B do
{
IN [B] = Superunion OUT[P] //P = predecessors
OLDOUT = OUT[B]
OUT[B] = GEN[B] union [IN[B] – KILL[B]]
If(OUT[B] ≠ OLDOUT)
CHANGE=TRUE
}
}
Example-1: IN-OUT analysis and Reaching Definitions
Label Code Block B1
i=0
101 i=0 N1=a+b
102 N1=a+b N2=a-b
103 N2=a-b
104 T2=i*4 Block B2
T2=i*4
105 T3 = add(a) T3=add(a)
T4=t3[t2] t5=n1*n2 if(t4 > t5) goto 110
106 T4=t3[t2]
107 T5=n1*n2
Block B3
108 If (t4 > t5) goto 110 Block B4
i=i+1
109 Goto 112 Goto 112
Goto 104
110 i=i+1
111 Goto 104
112 Return i Block B5
Return i
Example-1: IN-OUT analysis and Reaching Definitions
BLOCK PREDCE’R GEN[B] KILL[B] Block B1
i=0 (1)
B1 NIL 1,2,3 8 N1=a+b (2)
B2 B1,B3 4,5,6,7 NIL N2=a-b (3)

B3 B2 8 1
Block B2
B4 B2 Nil NIL T2=i*4 (4)
B5 B4 Nil NIL T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110

BLOCK IN[B] OUT[B] IN[B] OUT[B]


B1 Nil 1,2,3 Nil 1,2,3 Block B3
Block B4
B2 Nil 4,5,6,7 1,2,3,8 1,2,3,4, i=i+1 [8]
Goto 112
5,6,7,8 Goto 104
B3 Nil 8 4,5,6,7 4,5,6,7,
8
B4 Nil Nil 4,5,6,7 4,5,6,7 Block B5
Return i
B5 Nil Nil Nil Nil
Example-1: IN-OUT analysis and Reaching Definitions
BLOCK PREDCE’R GEN[B] KILL[B] Block B1
i=0 (1)
B1 NIL 1,2,3 8 N1=a+b (2)
B2 B1,B3 4,5,6,7 NIL N2=a-b (3)

B3 B2 8 1
Block B2
B4 B2 Nil NIL T2=i*4 (4)
B5 B4 Nil NIL T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110

BLOCK IN[B] OUT[B] IN[B] OUT[B]


B1 Nil 1,2,3 Nil 1,2,3 Block B3
Block B4
B2 1,2,3,8 1,2,3,4,5, 1,2,3,4, 1,2,3,4,5 i=i+1 [8]
Goto 112
6,7,8 5,6,7,8 ,6,7,8 Goto 104
B3 4,5,6,7 4,5,6,7,8 1,2,3,4, 2,3,4,5,6
5,6,7,8 7,8
B4 4,5,6,7 4,5,6,7 1,2,3,4, 1,2,3,4,5 Block B5
5,6,7,8 6,7,8 Return i
B5 Nil Nil 4,5,6,7 4,5,6,7
Example-1: IN-OUT analysis and Reaching Definitions
BLOCK PREDCE’R GEN[B] KILL[B] Block B1
B1 NIL 1,2,3 8 i=0 (1)
N1=a+b (2)
B2 B1,B3 4,5,6,7 NIL
N2=a-b (3)
B3 B2 8 1
B4 B2 Nil NIL
Block B2
B5 B4 Nil NIL
T2=i*4 (4)
T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110
BLOCK IN[B] OUT[B] IN[B] OUT[B]
B1 Nil 1,2,3 Nil 1,2,3
B2 1,2,3,4,5 1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5, Block B3
Block B4
,6,7,8 6,7,8 6,7,8 6,7,8 i=i+1 [8]
Goto 112
Goto 104
B3 1,2,3,4,5 2,3,4,5,6 1,2,3,4,5, 2,3,4,5,6,
,6,7,8 7,8 6,7,8 7,8
B4 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5, 1,2,3,4,5,
,6,7,8 6,7,8 6,7,8 6,7,8 Block B5
Return i
B5 4,5,6,7 4,5,6,7 1,2,3,4,5, 1,2,3,4,5,
6,7,8 6,7,8
Example-1: IN-OUT analysis and Reaching Definitions
BLOCK PREDCE’R GEN[B] KILL[B] Block B1
B1 NIL 1,2,3 8 i=0 (1)
N1=a+b (2)
B2 B1,B3 4,5,6,7 NIL
N2=a-b (3)
B3 B2 8 1
B4 B2 Nil NIL
Block B2
B5 B4 Nil NIL
T2=i*4 (4)
T3=add(a) [5]
BLOCK IN[B] OUT[B] IN[B] OUT[B]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110
B1 Nil 1,2,3 Nil 1,2,3
B2 1,2,3,4,5,6,7, 1,2,3,4,5, 1,2,3,4,5,6 1,2,3,4,5,
8 6,7,8 7,8 6,7,8 Block B3
Block B4
i=i+1 [8]
B3 1,2,3,4,5, 2,3,4,5,6,7 1,2,3,4,5,6 2,3,4,5,6,7 Goto 112
Goto 104
6,7,8 ,8 7,8 ,8
B4 1,2,3,4,5,6,7, 1,2,3,4,5,6 1,2,3,4,5,6 1,2,3,4,5,6
8 ,7,8 7,8 7,8
B5 1,2,3,4,5,6,7, 1,2,3,4,5,6 1,2,3,4,5,6 1,2,3,4,5,6 Block B5
8 ,7,8 7,8 7,8 Return i
Example-1: IN-OUT analysis and Reaching Definitions
BLOCK Reaching definitions Reaching Block B1
at beginning of Block definitions at i=0 (1)
end of Block N1=a+b (2)
N2=a-b (3)
B1 Nil 1,2,3
B2 1,2,3,4,5,6,7,8 1,2,3,4,5, 6,7,8 Block B2
T2=i*4 (4)
B3 1,2,3,4,5,6,7,8 2,3,4,5,6,7,8 T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110
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
Block B3
Block B4
i=i+1 [8]
Goto 112
Goto 104

Block B5
Return i
U-d Chain: Computation
• It is a data structure used to Quad Ud chain Remarks

store reaching definitions (4) t2 = i * 4 Udchain(4,i) =


{1,8}
Definition of “i“
IN[B2] are 1, 8
• It represents set of all definitions There is no
definition of “i“ in
reaching the quad. block B1
preceding quad 4,
• For example t5= n1*n2, then to hence ud_chain
(4,i) = 1,8
compute t5, it is necessary to get (5) t3 = add(a) None
access to variable n1 and n2. (8) i=i+1 Ud_chain(8,i) = Definitions of “i“
• It is expressed as: {1,8} IN[B3] are 1,8.
There is no
definition of “i“
• u-d chain(7,n1)={2} preceding quad 8
in block B3. So ud
• u-d chain(7,n2)={3} chain = [1,8]
B2 1,2,3,4,5,6,7, 1,2,3,4,5,
8 6,7,8
U-d Chain: Computation
Quad Ud chain Remarks
(7) t5 = n1*n2 Ud_chain(7,n1)=2 The definition of variable n1 is in block B1, and there is no definition of n1
Ud_chain(7,n2)=3 preceding to block B2 [in block B2]. Hence the definition of “n1” is from
quad 2, which is in Block B1.
Similar is true for variable “n2”

The loop invariant is computed using following procedure:


1. Draw the program flow graph from TAC
2. Find the Dominator information of each block
3. Compute back edges
4. Find the block involved in the back edge
5. Compute loop invariant from the blocks involved in back edges
6. Move the loop invariant after/before the loop [Refer rule base for creating new block for loop invariant]

Students can refer the class notes on computation of dominators and detection of back edge
The material in next slide is based on assumption: loop is already detected in program flow graph.
Computation of loop invariant
• From the information generated • Rule 1: All reaching definitions of
from computation of “y” and “z” at quad(3) are from
• Reaching definition outside the loop, which can be
checked using u-d chain process
• U-d chains
• Rule 2: The operand “y” and “z”
• And following the rules given are constant.
below: Loop invariant can be
computed if one of following
rule is true.
• (3) x=y+z is loop invariant if
Example: Loop invariant computation : Cont..
BLOCK Reaching definitions at beginning Reaching definitions at Block B1
of Block end of Block
i=0 (1)
B1 Nil 1,2,3
N1=a+b (2)
B2 1,2,3,4,5,6,7,8 1,2,3,4,5, 6,7,8 N2=a-b (3)
B3 1,2,3,4,5,6,7,8 2,3,4,5,6,7,8
Block B2
B4 1,2,3,4,5,6,7,8 1,2,3,4,5,6,7,8
T2=i*4 (4)
B5 1,2,3,4,5,6,7,8 1,2,3,4,5,6,7,8 T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110
Quad Ud chain Remarks

(5) t3 = None Constant


Block B3
add(a) Block B4
i=i+1 [8]
Goto 112
Goto 104

Since there is no ud-chain information for quad(5) and also Block B5


it is constant. It is considered as loop invariant. Return i
Example: Loop invariant computation : Cont..
BLOCK Reaching definitions at beginning Reaching definitions at Block B1
of Block end of Block
i=0 (1)
B1 Nil 1,2,3
N1=a+b (2)
B2 1,2,3,4,5,6,7,8 1,2,3,4,5, 6,7,8 N2=a-b (3)
B3 1,2,3,4,5,6,7,8 2,3,4,5,6,7,8
Block B2
B4 1,2,3,4,5,6,7,8 1,2,3,4,5,6,7,8
T2=i*4 (4)
B5 1,2,3,4,5,6,7,8 1,2,3,4,5,6,7,8 T3=add(a) [5]
T4=t3[t2] [6] t5=n1*n2 [7] if(t4 > t5) goto 110
Quad Ud chain Remarks

(7) t5 = Ud_chain(7,n1)=2 The definition of variable n1 is in block B1,


Block B3
n1*n2 Ud_chain(7,n2)=3 and there is no definition of n1 preceding Block B4
to block B2 [in block B2]. Hence the i=i+1 [8]
definition of “n1” is from quad 2, which is Goto 112
in Block B1.
Goto 104
Similar is true for variable “n2”

The statement is loop invariant, since the definition of “n1 and n2” are outside Block B5
Block B2 and there is no redefinition of variables. Return i
Next: How to create a new block to move the loop
invariant out of loop
M.B.Chandak

You might also like