loops
DESCRIPTION
Loops. Guo, Yao. Content. Concepts: Dominators Depth-First Ordering Back edges Graph depth Reducibility Natural Loops Efficiency of Iterative Algorithms. Loops are Important!. Loops dominate program execution time Needs special treatment during optimization - PowerPoint PPT PresentationTRANSCRIPT
School of EECS, Peking University
“Advanced Compiler Techniques” (Fall 2011)
LoopsLoops
Guo, YaoGuo, Yao
2Fall 2011“Advanced Compiler
Techniques”
ContentContent Concepts:Concepts:
DominatorsDominators Depth-First OrderingDepth-First Ordering Back edgesBack edges Graph depthGraph depth ReducibilityReducibility
Natural LoopsNatural Loops Efficiency of Iterative AlgorithmsEfficiency of Iterative Algorithms
3Fall 2011“Advanced Compiler
Techniques”
Loops are Important!Loops are Important! Loops dominate program execution Loops dominate program execution
timetime Needs special treatment during Needs special treatment during
optimizationoptimization Loops also affect the running time of Loops also affect the running time of
program analysesprogram analyses e.g., A dataflow problem can be solved e.g., A dataflow problem can be solved
in just a single pass if a program has no in just a single pass if a program has no loopsloops
4Fall 2011“Advanced Compiler
Techniques”
DominatorsDominators Node Node dd dominatesdominates node node nn if every path if every path
from the entry to from the entry to nn goes through goes through dd.. written as: written as: d dom nd dom n
Quick observations:Quick observations: Every node dominates itself.Every node dominates itself. The entry dominates every node.The entry dominates every node.
Common Cases:Common Cases: The test of a The test of a whilewhile loop dominates all blocks loop dominates all blocks
in the loop body.in the loop body. The test of an The test of an if-then-elseif-then-else dominates all dominates all
blocks in either branch.blocks in either branch.
5Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Dominators: Dominators
1
35
24
6Fall 2011“Advanced Compiler
Techniques”
Dominator TreeDominator Tree Immediate dominance: d idom n
d dom n, d n, no m s.t. d dom m and m dom n
Immediate dominance relationships form a tree
1
35
24
1
35
24
7Fall 2011“Advanced Compiler
Techniques”
Finding DominatorsFinding Dominators A dataflow analysis problem: For A dataflow analysis problem: For
each node, find all of its dominators.each node, find all of its dominators. Direction: forwardDirection: forward Confluence: set intersectionConfluence: set intersection Boundary: OUT[Entry] = {Entry}Boundary: OUT[Entry] = {Entry} Initialization: OUT[B] = All nodesInitialization: OUT[B] = All nodes Equations:Equations:
OUT[B] = IN[B] U {B}OUT[B] = IN[B] U {B} IN[B] = IN[B] = p is a predecessor of Bp is a predecessor of B OUT[p] OUT[p]
8Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Dominators: Dominators
1
35
24
{1,5}
{1,4}
{1,2,3}
{1,2}
{1}
9Fall 2011“Advanced Compiler
Techniques”
Depth-First SearchDepth-First Search Start at entry.Start at entry. If you can follow an edge to an If you can follow an edge to an
unvisited node, do so.unvisited node, do so. If not, backtrack to your If not, backtrack to your parentparent
(node from which you were visited).(node from which you were visited).
10Fall 2011“Advanced Compiler
Techniques”
Depth-First Spanning Depth-First Spanning TreeTree
Root = entry.Root = entry. Tree edges are the edges along Tree edges are the edges along
which we first visit the node at the which we first visit the node at the head.head.
1
53
42
11Fall 2011“Advanced Compiler
Techniques”
Depth-First Node OrderDepth-First Node Order The reverse of the order in which a The reverse of the order in which a
DFS DFS retreatsretreats from the nodes. from the nodes. Alternatively, reverse of postorder Alternatively, reverse of postorder
traversal of the tree.traversal of the tree.
12Fall 2011“Advanced Compiler
Techniques”
ExampleExample: DF Order: DF Order
1
35
24
13Fall 2011“Advanced Compiler
Techniques”
Four Kinds of EdgesFour Kinds of Edges
1.1. Tree edges.Tree edges.
2.2. Advancing edgesAdvancing edges (node to proper (node to proper descendant).descendant).
3.3. Retreating edgesRetreating edges (node to (node to ancestor, including edges to self).ancestor, including edges to self).
4.4. Cross edgesCross edges (between two nodes, (between two nodes, neither of which is an ancestor of neither of which is an ancestor of the other.the other.
14Fall 2011“Advanced Compiler
Techniques”
A Little MagicA Little Magic Of these edges, only retreating Of these edges, only retreating
edges go from high to low in DF edges go from high to low in DF order.order.
Most surprising: all cross edges go Most surprising: all cross edges go right to left in the DFST.right to left in the DFST. Assuming we add children of any node Assuming we add children of any node
from the left.from the left.
15Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Non-Tree : Non-Tree EdgesEdges
1
35
24
Retreating
Forward
Cross
16Fall 2011“Advanced Compiler
Techniques”
Back EdgesBack Edges An edge is a An edge is a back edgeback edge if its head if its head
dominates its tail.dominates its tail. TheoremTheorem: Every back edge is a : Every back edge is a
retreating edge in every DFST of retreating edge in every DFST of every flow graph.every flow graph. Converse almost always true, but not Converse almost always true, but not
always.always.
17Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Back Edges: Back Edges
1
35
24
{1,5}
{1,4}
{1,2,3}
{1,2}
{1}
18Fall 2011“Advanced Compiler
Techniques”
Reducible Flow GraphsReducible Flow Graphs A flow graph is A flow graph is reduciblereducible if every if every
retreating edge in any DFST for that retreating edge in any DFST for that flow graph is a back edge.flow graph is a back edge.
Testing reducibilityTesting reducibility: Take any DFST : Take any DFST for the flow graph, remove the back for the flow graph, remove the back edges, and check that the result is edges, and check that the result is acyclic.acyclic.
19Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Remove Back : Remove Back EdgesEdges
1
35
24
20Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Remove Back : Remove Back EdgesEdges
1
35
24
Remaining graph is acyclic.
21Fall 2011“Advanced Compiler
Techniques”
Why Reducibility?Why Reducibility? Folk theoremFolk theorem: All flow graphs in : All flow graphs in
practice are reducible.practice are reducible.
FactFact: If you use only while-loops, for-: If you use only while-loops, for-loops, repeat-loops, if-then(-else), loops, repeat-loops, if-then(-else), break, and continue, then your flow break, and continue, then your flow graph graph isis reducible. reducible.
22Fall 2011“Advanced Compiler
Techniques”
Example: Nonreducible Example: Nonreducible GraphGraph
A
CB
In any DFST, oneof these edges willbe a retreating edge.
A
B
C
A
B
C
23Fall 2011“Advanced Compiler
Techniques”
Why Care About Why Care About Back/Retreating Edges?Back/Retreating Edges?
1.1. Proper ordering of nodes during Proper ordering of nodes during iterative algorithm assures number iterative algorithm assures number of passes limited by the number of of passes limited by the number of ““nestednested”” back edges. back edges.
2.2. Depth of nested loops upper-Depth of nested loops upper-bounds the number of nested back bounds the number of nested back edges.edges.
24Fall 2011“Advanced Compiler
Techniques”
DF Order and Retreating DF Order and Retreating EdgesEdges
Suppose that for a Reaching Suppose that for a Reaching Definitions analysis, we visit nodes Definitions analysis, we visit nodes during each iteration in DF order.during each iteration in DF order.
The fact that a definition The fact that a definition dd reaches a reaches a block will propagate in one pass along block will propagate in one pass along any increasing sequence of blocks.any increasing sequence of blocks.
When When dd arrives along a retreating arrives along a retreating edge, it is too late to propagate edge, it is too late to propagate dd from from OUT to IN.OUT to IN.
25Fall 2011“Advanced Compiler
Techniques”
ExampleExample: DF Order: DF Order
1
35
24
d d
d
d
d
d
d d
d
d
Suppose there is a definition of
d in Block 2.
26Fall 2011“Advanced Compiler
Techniques”
Depth of a Flow GraphDepth of a Flow Graph The The depth depth of a flow graph is the of a flow graph is the
greatest number of retreating edges greatest number of retreating edges along any acyclic path.along any acyclic path.
For RD, if we use DF order to visit For RD, if we use DF order to visit nodes, we converge in depth+2 nodes, we converge in depth+2 passes.passes. Depth+1 passes to follow that number Depth+1 passes to follow that number
of increasing segments.of increasing segments. 1 more pass to realize we converged.1 more pass to realize we converged.
27Fall 2011“Advanced Compiler
Techniques”
Example: Depth = 2Example: Depth = 2
increasing
retreating
increasingincreasing
retreating
1 -> 4 ->7 - - -> 3 -> 10 ->17 - - -> 6 -> 18 -> 20
28Fall 2011“Advanced Compiler
Techniques”
Similarly . . .Similarly . . . AE also works in depth+2 passes.AE also works in depth+2 passes.
UnavailabilityUnavailability propagates along retreat- propagates along retreat-free node sequences in one pass.free node sequences in one pass.
So does LV if we use So does LV if we use reversereverse of DF of DF order.order. A use propagates backward along paths A use propagates backward along paths
that do not use a retreating edge in one that do not use a retreating edge in one pass.pass.
29Fall 2011“Advanced Compiler
Techniques”
In General . . .In General . . . The depth+2 bound works for any The depth+2 bound works for any
monotone framework, as long as monotone framework, as long as information only needs to propagate information only needs to propagate along acyclic paths.along acyclic paths. ExampleExample: if a definition reaches a point, : if a definition reaches a point,
it does so along an acyclic path.it does so along an acyclic path.
30Fall 2011“Advanced Compiler
Techniques”
However . . .However . . . Constant propagation does not have this Constant propagation does not have this
property. property.
a = b
b = c
c = 1
L: a = b
b = c
c = 1
goto L
31Fall 2011“Advanced Compiler
Techniques”
Why Depth+2 is GoodWhy Depth+2 is Good Normal control-flow constructs Normal control-flow constructs
produce reducible flow graphs with produce reducible flow graphs with the number of back edges at most the number of back edges at most the nesting depth of loops.the nesting depth of loops. Nesting depth tends to be small.Nesting depth tends to be small.
A study by Knuth has shown that A study by Knuth has shown that average depth of typical flow graphs average depth of typical flow graphs =~2.75.=~2.75.
32Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Nested Loops: Nested Loops
3 nested while-loops; depth = 3.
3 nested repeat-loops; depth = 1
33Fall 2011“Advanced Compiler
Techniques”
Natural LoopsNatural Loops A A natural loop natural loop is defined by:is defined by:
A single entry-point called header a header dominates all nodes in the loop
A back edge that enters the loop header Otherwise, it is not possible for the flow of
control to return to the header directly from the "loop" ; i.e., there really is no loop.
34Fall 2011“Advanced Compiler
Techniques”
Find Natural LoopsFind Natural Loops The The natural loopnatural loop of a back edge of a back edge aa--
>>bb is { is {bb} plus the set of nodes that } plus the set of nodes that can reach can reach aa without going through without going through bb..
Remove b from the flow graph, find all predecessors of a
TheoremTheorem: two natural loops are : two natural loops are either disjoint, identical, or nested.either disjoint, identical, or nested.
35Fall 2011“Advanced Compiler
Techniques”
ExampleExample: Natural Loops: Natural Loops
1
35
24
Natural loopof 3 -> 2
Natural loopof 5 -> 1
36Fall 2011“Advanced Compiler
Techniques”
Relationship b/w LoopsRelationship b/w Loops If two loops do not have the same
header they are either disjoint, or one is entirely contained (nested within) the
other innermost loop: one that contains no other
loop. If two loops share the same header
Hard to tell which is the inner loop Combine as one
1
2
3 4
37Fall 2011“Advanced Compiler
Techniques”
Next TimeNext Time Single Static Assignment (SSA)Single Static Assignment (SSA)
Readings: Cytron'91, Chow'97 Readings: Cytron'91, Chow'97