Loops • Most execution time in most programs is spent in loops: 90/10 is typical CS 4120 • Most important targets of optimization: loops Introduction to Compilers • Loop optimizations: – loop-invariant code motion Andrew Myers – loop unrolling Cornell University – loop peeling – strength reduction of expressions containing induction Lecture 26: Control-flow analysis variables 28 Oct 11 – removal of bounds checks – loop tiling • When to apply loop optimizations? CS 4120 Introduction to Compilers 2 High-level optimization? Definition of a loop • A loop is a set of nodes in the control flow graph, • Loops may be hard to recognize in IR or with one distinguished node called the header (entry quadruple form -- should we apply loop point) optimizations to source code or high-level • Every node is reachable IR? from header, header reachable from every – Many kinds of loops: while, do/while, continue header node: strongly-connected – loop optimizations benefit from other IR-level component loop exit optimizations and vice-versa -- want to be • No entering edges from able to interleave outside except to header • Problem: identifying loops in CFG • nodes with outgoing edges: loop exit nodes 3 4 CS 4120 Introduction to Compilers CS 4120 Introduction to Compilers
Nested loops Dominators • Control-flow graph may contain many loops, • CFA based on idea of dominators and loops may contain each other • Node A dominates node B if the only way • Control-flow analysis : identify the loops to reach B from start node is through A and nesting structure: control 1 • Edge in flowgraph is a tree back edge if destination 2 back edge dominates source 5 4 3 • A loop contains at least one back edge inner loop CS 4120 Introduction to Compilers 5 CS 4120 Introduction to Compilers 6 Dominator tree Dominator dataflow analysis • Domination is transitive; if A dominates B and B dominates C, then A dominates C • Forward analysis; out[ n ] is set of nodes dominating n • Domination is anti-symmetric • “A node B is dominated by another node A if A • Every flowgraph has dominator tree (Hasse diagram dominates all of the predecessors of B ” of domination relation) in[ n ] = ∩ n ’ ! pred[ n ] out[ n ’] 1 1 • “Every node dominates itself” 2 2 3 4 3 4 out[ n ] = in[ n ] " { n } 5 6 5 6 • Formally: L = sets of nodes ordered by # , flow functions 8 7 8 7 F n ( x ) = x " { n }, ⊑ = ⊆ , ⊤ = {all n } 9 10 9 10 $ Standard iterative analysis gives best soln 7 8 CS 4120 Introduction to Compilers CS 4120 Introduction to Compilers
Completing control-flow analysis • Dominator analysis gives all back edges Each back edge n % h has an associated natural loop with h • as its header: all nodes reachable from h that reach n without going through h • For each back edge, find natural loop 1 • Nest loops based on subset 2 relationship between natural loops 3 4 • Exception: natural loops may share 5 6 same header; merge them into larger loop. 8 7 • Control tree built using nesting 9 10 relationship CS 4120 Introduction to Compilers 9
Recommend
More recommend