1
play

1 What Is Control-Flow Analysis? Loop Concepts Control-flow - PowerPoint PPT Presentation

Control-Flow Analysis and Loop Detection Context Last time Data-flow Speeding up data-flow analysis Flow of data values from defs to uses Could alternatively be represented as a data dependence Today Control-flow analysis


  1. Control-Flow Analysis and Loop Detection Context Last time Data-flow – Speeding up data-flow analysis – Flow of data values from defs to uses – Could alternatively be represented as a data dependence Today – Control-flow analysis Control-flow – Loops – Sequencing of operations – Identifying loops using dominators – Could alternatively be represented as a control dependence – Reducibility – e.g., Evaluation of then-code and else-code depends on if-test, CS553 Lecture Control-Flow and Loop Detection 2 CS553 Lecture Control-Flow and Loop Detection 3 Why study control flow analysis? Representing Control-Flow Finding Loops High-level representation – most computation time is spent in loops – Control flow is implicit in an AST – to optimize them, we need to find them Low-level representation: Loop Optimizations – Use a Control-flow graph – Loop-invariant code hoisting – Nodes represent statements – Induction variable elimination – Edges represent explicit flow of control – Array bounds check removal – Loop unrolling Other options – Parallelization – Control dependences in program dependence graph (PDG) [Ferrante87] – ... – Dependences on explicit state in value dependence graph (VDG) [Weise 94] Identifying structured control flow – can be used to speed up data-flow analysis CS553 Lecture Control-Flow and Loop Detection 4 CS553 Lecture Control-Flow and Loop Detection 5 1

  2. What Is Control-Flow Analysis? Loop Concepts Control-flow analysis discovers the flow of control within a procedure Loop : Strongly connected component of CFG with a single entry point (header) ( e.g., builds a CFG, identifies loops) 1 a := 0 Loop entry edge : Source not in loop & target in loop Example b := a * b Loop exit edge : Source in loop & target not in loop a := 0 1 3 c := b/d Loop header node : Target of loop entry edge b := a * b 2 c < x? Natural loop : Nodes with path to backedge without going through header. L1: c := b/d 3 if c < x goto L2 Back edge : Target is loop header & source is in the loop 4 5 e := b / c e := b / c 5 f := e + 1 Loop tail node : Source of back edge f := e + 1 6 Loop preheader node : Single node that’s source of the loop entry edge L2: g := f 7 7 g := f h := t - g Nested loop : Loop whose header is inside another loop 8 h := t - g if e > 0 goto L3 9 e > 0? 10 goto L1 No Yes 11 L3: return 10 goto 11 return CS553 Lecture Control-Flow and Loop Detection 6 CS553 Lecture Control-Flow and Loop Detection 7 Picturing Loop Terminology The Value of Preheader Nodes Not all loops have preheaders h – Sometimes it is useful to create them preheader back edge entry edge Without preheader node t head – There can be multiple entry edges p pre-header With single preheader node – There is only one entry edge h loop Useful when moving code outside the loop t tail – Don’t have to replicate code for multiple entry edges exit edge CS553 Lecture Control-Flow and Loop Detection 8 CS553 Lecture Control-Flow and Loop Detection 9 2

  3. Identifying Loops Dominator Terminology entry Dominators Why? d dom i if all paths from entry to node i include d – Most execution time spent in loops, so optimizing loops will often give d dom i d most benefit Strict dominators d sdom i if d dom i and d ≠ i entry i Many approaches – Interval analysis Immediate dominators a idom b a – Exploit the natural hierarchical structure of programs a idom b if a sdom b and there does not exist a node c such that c ≠ a, c ≠ b, a dom c, and c dom b – Decompose the program into nested regions called intervals not ∃ c, a sdom c and c sdom b – Structural analysis: a generalization of interval analysis b Post dominators – Identify dominators to discover loops i p pdom i if every possible path from i to exit includes p (p dom i in the flow graph whose arcs are reversed We’ll look at the dominator-based approach and entry and exit are interchanged) p pdom i p exit CS553 Lecture Control-Flow and Loop Detection 10 CS553 Lecture Control-Flow and Loop Detection 11 Identifying Natural Loops with Dominators Computing Dominators t Back edges Input : Set of nodes N (in CFG) and an entry node s A back edge of a natural loop is one whose target back edge Output : Dom[i] = set of all nodes that dominate node i dominates its source s Dom[s] = {s} Natural loop Key Idea for each n ∈ N – {s} The natural loop of a back edge (m → n), where n If a node dominates all n dominates m, is the set of nodes x such that n Dom[n] = N predecessors of node n, then it natural dominates x and there is a path from x to m not also dominates node n loop repeat containing n m change = false a for each n ∈ N – {s} p 1 p 2 p 3 pred[n] D = {n} ∪ ( ∩ p ∈ pred(n) Dom[p]) a The target, c, of the b Example if D ≠ Dom[n] edge (d → c) does not n b This loop has two c dominate its source, d, change = true entry points, so (d → c) does not Dom[n] = D c d d c and d define a natural loop until !change x ∈ Dom(p 1 ) ^ x ∈ Dom(p 2 ) ^ x ∈ Dom(p 3 ) ⇒ x ∈ Dom(n) e e CS553 Lecture Control-Flow and Loop Detection 12 CS553 Lecture Control-Flow and Loop Detection 13 3

  4. Computing Dominators (example) Reducibility Input : Set of nodes N and an entry node s Definition Output : Dom[i] = set of all nodes that dominate node i s {s} – A CFG is reducible (well-structured) if we can partition its edges into two disjoint sets, the forward edges and the back edges, such that {n, p, q, r, s} {n, p, q, r, s} {n, p, q, r, s} {n, p, q, r, s} q r – The forward edges form an acyclic graph in which every node can be Dom[s] = {s} reached from the entry node for each n ∈ N – {s} p {n, p, q, r, s} {n, p, q, r, s} – The back edges consist only of edges whose targets dominate their Dom[n] = N sources n {n, p, q, r, s} {n, p, q, r, s} repeat – A CFG is reducible if it can be converted into a single node using T1 and Initially change = false T2 transformations. Dom[s] = {s} for each n ∈ N – {s} Structured control-flow constructs give rise to reducible CFGs Dom[q] = {n, p, q, r, s} . . . D = {n} ∪ ( ∩ p ∈ pred(n) Dom[p]) Value of reducibility Finally if D ≠ Dom[n] – Dominance useful in identifying loops Dom[q] = {q, s} change = true – Simplifies code transformations (every loop has a single header) Dom[n] = D Dom[r] = {r, s} – Permits interval analysis and it is easy to calculate the CFG depth until !change Dom[p] = {p, s} {n, p, s} Dom[n] = CS553 Lecture Control-Flow and Loop Detection 14 CS553 Lecture Control-Flow and Loop Detection 15 T1 and T2 transformations Handling Irreducible CFG’s T1 transformation Node splitting a a – remove self-cycles – Can turn irreducible CFGs into reducible CFGs a a T2 transformation b b – if node n has a unique predecessor p, then remove n and make all the successors for n be successors for p c d c d a ab e e d ′ b e ′ c d c d CS553 Lecture Control-Flow and Loop Detection 16 CS553 Lecture Control-Flow and Loop Detection 17 4

  5. Why Go To All This Trouble? Concepts Modern languages provide structured control flow Control-flow analysis – Shouldn’t the compiler remember this information rather than throw it Control-flow graph (CFG) away and then re-compute it? Loop terminology Identifying loops Answers? Dominators – We may want to work on the binary code in which case such information Reducibility is unavailable – Most modern languages still provide a goto statement – Languages typically provide multiple types of loops. This analysis lets us treat them all uniformly – We may want a compiler with multiple front ends for multiple languages; rather than translate each language to a CFG, translate each language to a canonical LIR, and translate that representation once to a CFG CS553 Lecture Control-Flow and Loop Detection 18 CS553 Lecture Control-Flow and Loop Detection 19 Next Time Assignments – Project 2 due: the writeup is IMPORTANT Reading – Ch 18.2 Lecture – Loop invariant code motion CS553 Lecture Control-Flow and Loop Detection 20 5

Recommend


More recommend