code optimization dominatorloop detection in code •useful in loop optimization process....

19
Code Optimization M.B.Chandak Lecture notes on Language Processor

Upload: others

Post on 24-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Code OptimizationM.B.Chandak

Lecture notes on Language Processor

Page 2: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Approaches:

• Local Optimization

• Loop Optimization

• Peep Hole optimization

Basis:

• Optimized code should generate same results as original code.

Trade off:

• Time spent in optimization and outcome should be balanced

Page 3: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Local optimization: 1. Elimination of Common Sub-expression

• Common Sub-expressions

• Repeated in code and executed more than once

• Example

A = B+C*D

X = Y+C*D

• Common expression can be found from TAC

T1 = C*D

T2 = B+T1

T3 = C*D

T4 = Y + T3

Idea: To identify and replace

Page 4: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Implementation: Removal of Common SE

DAG: Directed Acyclic Graph

• Construction: From leaf node to root node

• Root node = Operator, Leaf node=Generally operands

• Left hand side rule is used to construct DAG

STEPS

• Given Program Segment – Construct TAC – Construct DAG – identify Common Sub Expressions – Remove Common SE – Rewrite TAC [optimized]

Page 5: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Examples: DAGLabel Code

100 T1 = 4 * I

101 T2 = ADD(A) – 4

102 T3 = T2[T1]

103 T4 = 4 * I

104 T5 = ADD[B] – 4

105 T6 = T5[T4]

106 T7 = T3*T6

107 T8=PROD + T7

108 T9 = I + 1

109 I = T9

110 IF (I<=20) GOTO 100

i4

* T1

ADD

(a)

- T2

[] T3

T4

ADD

(b)

-T5

[] T6

* T7

Prod

+ T8

1

+T9

i

20

<=

100

Page 6: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

ExampleLabel Code

100 T1 = I * 4

102 T2 = ADD(A) - 4

103 T3 = T2[T1]

104 X = T3

105 T4 = J * 4

106 T5 = ADD(P) – 4

107 T6 = T5[T4]

108 T7 = I * 4

109 T8 = ADD(A) – 4

110 T9 = T8[T7]

111 Z = T9

Page 7: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Conversion of TAC into PFG

• Program flow graph is partitioned graph of Three Address Code.

• For construction of PFG: Leader statements are identified, basic blocks designed and connected.

• Leader statement rules:

• First Statement of TAC is leader

• Target of conditional and unconditional goto statement is leader.

• Statement immediately following conditional goto statement is leader.

Page 8: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Basic Blocks of PFG and connection rules

• Once leader statements are identified, TAC is divided into basic blocks depending upon the boundaries of TAC

• Connection rules:

• If B1 and B2 are two blocks, then add an edge from B1 to B2; if B2 follow B1 in execution.

• B2 follows B1 in execution if:

• 1) First statement of Block B2 immediately follows the last statement of block B1 in TAC and last statement of B1 is not an unconditional goto.

• 2) Last statement of block B1 is either a conditional or unconditional Goto and first statement of block B2 is target of goto statement.

Page 9: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Loop detection in code

• Useful in loop optimization process.

• Process:

• Given Program Segment

• Design program flow graph (PFG)

• In PFG, two types of edges: forward edge and backward edge

• Detect Backward edge, to find loops

• Process of detecting backward edge, involves calculation of Dominators between blocks of PFG.

Page 10: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Example:• Program Segment

Int demo(int t)

{

int a, b

amt=a

rate=a

while(a>10)

{

amt=amt*rate

amt=amt+100

a=a+1;

}

}

Three Address Code

10: amt=a

20: rate=a

30: if(a>10) goto 50

40: goto 120

50: t1=amt*rate

60: amt=t1

70: t2=amt+100

80: amt=t2

90: t3=a+1

100: a=t3

110: goto 30

120: Exit

L1

L2

L3

L4

L5

Three Address Code

10: amt=a

20: rate=a

30: if(a>10) goto 50

40: goto 120

50: t1=amt*rate

60: amt=t1

70: t2=amt+100

80: amt=t2

90: t3=a+1

100: a=t3

110: goto 30

120: Exit

B0

B1

B2

B3

B4

Page 11: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Connectivity between blocks

Block 0

amt=a

Rate=a

Block 1

If(a>10) goto 50

Block 2

Goto 120

Block 3

t1=amt*rate

:

Goto 30

Block 4

Exit

Page 12: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Example: PFG and Block

int func(int a, int b)

{

int i,tot;

int n1,n2;

i =0;

n1 = a * b

n2 = a – b

while (a[i] > n1*n2)

i = i + 1

retrun i;

}

Label Code Leader Block

100 i=0 L1 B1

101 T1 = a*b

102 N1=t1

103 T2 = a-b

104 N2=t2

105 T3 = i*4 L2 B2

106 T4 = add(a) – c

107 T5 = t4[t5]

108 T6=t1*t2

109 If(t5 > t6) goto 111

110 Goto 114 L3 B3

111 T7=i+1 L4 B4

112 i=t7

113 Goto 105

114 Return i L5 B5

115 Exit

Page 13: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Example: Perform local optimization, convert to PFG, draw block structure, detect loop

int func(int a, int b)

{

int i, j, k;

i = 45;

j = a + b;

While(j < 25)

{

if(a+i > 100)

k = a + j;

else

k = b + j;

j = j + 4

}

return (k)

}

Page 14: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Dominators• Dominators allows to detect loops within program flow graph.

• A node “d” of flow graph dominates node “n” if every path from the initial node to node “n” goes/flow through node “d”.

• It is represented as d dom n

• Every node dominates itself

Block / node Dominator

Block 0 {0}

Block 1 {0,1}

Block 2 {0,1,2}

Block 3 {0,1,3}

Block 4 {0,1,2,4}

Page 15: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Finding back edge

• In the program flow graph every edge flows from Tail to Head.

• If block 0 is connected to block 1, then Tail=Block 0 and Head=Block=1

• Steps:

• Compute edges in PFG

• Compute Head and Tail information from edges

• Find dominator of head and dominator of tail

• Rule: If in the dominator of tail, head node is present, then the edge is denoted as back edge.

Page 16: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Edges and dominator tableEdge Head Tail Dominator[H] Dominator[T] Remarks

0 � 1 1 0 {0,1} {0}

1 � 2 2 1 {0,1,2} {0,1}

1 � 3 3 1 {0,1,3} {0,1}

3 � 1 1 3 {0,1} {0,1,3} Back Edge

2 � 4 4 2 {0,1,2,4} {0,1,2}

Block / node Dominator

Block 0 {0}

Block 1 {0,1}

Block 2 {0,1,2}

Block 3 {0,1,3}

Block 4 {0,1,2,4}

The back edge 3����1 indicates presence of loop between block 3 and 1. All the blocks

present between 3 and 1 path and path from 1 to 3 will be part of loop.

For example: loop will be 3-1-3

Page 17: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Example-2

Page 18: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Solution

Page 19: Code Optimization DOMINATORLoop detection in code •Useful in loop optimization process. •Process: • Given Program Segment • Design program flow graph (PFG) • In PFG, two

Example-3