Cs 473: Compiler Design
Total Page:16
File Type:pdf, Size:1020Kb
CS 473: COMPILER DESIGN 1 LOOPS AND DOMINATORS 2 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 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 loop • Every node is reachable nodes from the header & the header is reachable from every node header – A loop is a strongly connected component exit node • No edges enter the loop except to the header • Nodes with outgoing edges are called loop exit nodes 4 Nested Loops • Control-flow graphs may contain many loops • Loops may contain other loops: Loop Nesting Tree: 5 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 Back Edge is a back edge if the target node dominates the source node • Every loop contains at least one back edge 6 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 8 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: 4 4 5 6 5 6 8 8 9 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: 1 1 2 2 3 4 3 4 5 6 5 6 7 8 7 8 9 0 9 0 10 Completing Loop Analysis • Dominator analysis identifies back edges: edges n → h where h dominates n • Each back edge has a natural loop: h – h is the header – All nodes reachable from h that also reach n n without going through h are in the loop 1 2 3 4 5 6 7 8 9 0 11 Completing Loop Analysis • Dominator analysis identifies back edges: edges n → h where h dominates n • Each back edge has a natural loop: h – h is the header – All nodes reachable from h that also reach n n without going through h are in the loop • It’s convenient for each loop to have a unique header, so if two loops share the same header, h we merge them n m • If all the nodes in one loop are also in another loop, the first loop is nested inside the second 12 Example Natural Loops 1 Loop Nesting Tree: 2 3 4 5 6 7 8 9 0 Natural Loops 13 14 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.