CS 473: COMPILER DESIGN

15
CS 473: COMPILER DESIGN 1

Transcript of CS 473: COMPILER DESIGN

Page 1: CS 473: COMPILER DESIGN

CS 473: COMPILER DESIGN

1

Page 2: CS 473: COMPILER DESIGN

LOOPS AND DOMINATORS

2

Page 3: CS 473: COMPILER DESIGN

Loops in Control-flow Graphs

• Most programs spend most of their time in loops, so if we want to optimize, loops are a good place to look

• Like other optimizations, loop optimizations are best applied to a control flow graph IR

– Other opts may create opportunities for loop opts and vice versa, so it makes sense to alternate between them

• Loops may be hard to recognize at the CFG IR level

– Many kinds of loops: while, do/while, for, loops with break/continue, goto… all of which turn into some combination of comparisons, labels, jumps, and body code

• Problem: How do we identify loops in the control flow graph?

3

Page 4: CS 473: COMPILER DESIGN

Definition of a Loop

• A loop is a set of nodes in the control flow graph such that:

• There is a single distinguished entry point called the header

• Every node is reachable from the header &the header is reachable from every node

– A loop is a strongly connected component

• No edges enter the loop except to the header

• Nodes with outgoing edges are called loop exit nodes

4

header

exit node

loopnodes

Page 5: CS 473: COMPILER DESIGN

Nested Loops

• Control-flow graphs may contain many loops

• Loops may contain other loops:

5

Loop Nesting Tree:

Page 6: CS 473: COMPILER DESIGN

Loop Analysis

• Goal: Identify the loops and nesting structure of the CFG

• Loop analysis is based on the idea of dominators: node A dominates node B if the only way to reach B from the start node is through node A– The header of a loop dominates all the

nodes in the loop

• An edge in the graph is a back edge if the target node dominatesthe source node

• Every loop contains at leastone back edge

6

Back Edge

Page 7: CS 473: COMPILER DESIGN

Dominator Dataflow Analysis

• We can define Dom[n] as a forward dataflow analysis.

• Using our usual framework:

• “A node B is dominated by another node A if A dominates all of the predecessors of B.”

– in[n] := ∩n’∈pred[n]out[n’]

• “Every node dominates itself.”

– out[n] := in[n] ∪ {n}

• We start with every node in all of the sets, and in each iteration remove those that don’t dominate all of the node’s predecessors

• At the end, out[n] will be the set of nodes that dominate n

7

Page 8: CS 473: COMPILER DESIGN

8

Page 9: CS 473: COMPILER DESIGN

Dominator Trees

• Result: for each node, a set of nodes that dominate it

• Each node has one immediate dominator, the closest node that dominates it

• We can draw a tree where each node’s parent is its immediate dominator

CFG: Dominator Tree:

9

4

5 6

8

4

5 6

8

Page 10: CS 473: COMPILER DESIGN

Dominator Trees

• Result: for each node, a set of nodes that dominate it

• Each node has one immediate dominator, the closest node that dominates it

CFG: Dominator Tree:

10

1

2

3 4

5 6

7 8

9 0

1

2

3 4

5 6

7 8

9 0

Page 11: CS 473: COMPILER DESIGN

• Dominator analysis identifies back edges: edges n → h where h dominates n

• Each back edge has a natural loop:

– h is the header

– All nodes reachable from h that also reachn without going through h are in the loop

Completing Loop Analysis

11

h

n

1

2

3 4

5 6

7 8

9 0

Page 12: CS 473: COMPILER DESIGN

• Dominator analysis identifies back edges: edges n → h where h dominates n

• Each back edge has a natural loop:

– h is the header

– All nodes reachable from h that also reachn without going through h are in the loop

• It’s convenient for each loop to have a uniqueheader, so if two loops share the same header, we merge them

• If all the nodes in one loop are also in another loop, the first loop is nested inside the second

Completing Loop Analysis

12

h

n

h

n m

Page 13: CS 473: COMPILER DESIGN

Example Natural Loops

13

Loop Nesting Tree:

Natural Loops

1

2

3 4

5 6

7 8

9 0

Page 14: CS 473: COMPILER DESIGN

14

Page 15: CS 473: COMPILER DESIGN

Using Loop Information

• Loop nesting depth plays an important role in optimization heuristics

– Deeply nested loops pay off the most for optimization

• Need to know loop headers / back edges for doing:

– loop invariant code motion (remove code from loop if it’s the same every time)

– loop unrolling (execute n iterations of the loop at once)

– and lots more!

• Dominance information also plays a role in converting to Static Single Assignment form

15