1 / 62

Control Flow Analysis (Chapter 7)

Control Flow Analysis (Chapter 7). Outline. What is Control Flow Analysis? Structure of an optimizing compiler Constructing basic blocks Depth first search Finding dominators Reducibility Interval and Structural Analysis Conclusions. Control Flow Analysis. Input : A sequence of IR

calvin
Download Presentation

Control Flow Analysis (Chapter 7)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Flow Analysis(Chapter 7)

  2. Outline • What is Control Flow Analysis? • Structure of an optimizing compiler • Constructing basic blocks • Depth first search • Finding dominators • Reducibility • Interval and Structural Analysis • Conclusions

  3. Control Flow Analysis • Input: A sequence of IR • Output: • A partition of the IR into basic blocks • A control flow graph • The loop structure

  4. Compiler Structure String of characters Scanner tokens Parser Symbol table and access routines OS Interface AST Semantic analyzer IR Code Generator Object code

  5. Optimizing Compiler Structure String of characters Front-End IR Control Flow Analysis CFG Data Flow Analysis CFG+information Program Transformations Object code IR instruction selection

  6. An Example Reaching Definitions • A definition --- an assignment to variable • An assignment d reaches a program point block if there exists an execution path to the this point in which the value assigned at d is still active

  7. Running Example MIR intermediate code for C routine 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m unsigned int fib(unsigned int m) {unsigned int f0=0, f1=1, f2, i; if (m <= 1) { return m; } else { for (i=2, i <=m, i++) { f2=f0+f1; f0=f1; f1 =f2;} return f2; } } 1 1, 2 1, 2, 3 1, 2, 3, 5 1, 2, 3, 5, 8, 9, 10, 11 1, 2, 3, 5, 8, 9, 10, 11 1, 3, 5, 8, 9, 10, 11 1, 5, 8, 9, 10, 11 1, 8, 9, 10, 11

  8. Our first task is in analysing program to discover its control structure. • Control structure is obvious in source code but difficult in case of Intermediate code, so we create visual representation, namely flow chart as shown in the following figure. • Flow chart is in the following diagram

  9. Identify basic blocks: where each basic block is informally a straight-line sequence of code that can be entered only at the beginning and exited only at the end. • Node 1-4 basic block B1 • Node 8-11 form Block B6 • Node 12 into block B2 • Node 5 into B3 • Node 6 into B4 • Node 7 into B5

  10. Approaches for Control Flow Analysis • Iterative • Compute natural loops and iterate on CFG • Interval Based • Reduce the CFG to single node • Inductively define the data flow solution • Structural • Identify control flow structures in the CFG • Inductively define the data flow solution

  11. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m   2, 3 2, 3, 5 ,8, 9, 10, 11 2, 3, 5, 8,9, 10, 11 2, 3, 5 , 8, 9, 10, 11 2, 3, 5, 8,9, 10, 11 2,3 exit

  12. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m {9, 10}, {1, 2, 3} {11}, {5} {2, 3, 5}, {8, 9, 10, 11} exit

  13. entry {11}, {5} , {8, 9, 10, 11} {9, 10}, {1, 2, 3} exit

  14. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m   2, 3 2, 3, 5,8,9, 10, 11 2, 3, 5, 8,9, 10, 11 2, 3, 5, 8,9, 10, 11 2, 3, 5,8,9, 10, 11 2,3 exit

  15. entry {9, 10}, {1, 2, 3} , {8, 9, 10, 11, 5} exit

  16. entry {9, 10}, {1, 2, 3} , {8, 9, 10, 11, 5} exit

  17. entry {9, 10}, {1, 2, 3} , {8, 9, 10, 11, 5} exit

  18. entry , {8, 9, 10, 11, 5} {9, 10}, {1, 2, 3} exit

  19. entry , {1, 2, 3, 8, 9, 10, 11, 5} exit

  20. entry , {1, 2, 3, 8, 9, 10, 11, 5} exit

  21. Finding Basic Blocks • A basic block is the maximal sequence of straight-line IR instructions • no fork-join • A leader IR instruction • the entry of a routine • a target of a branch • instruction immediately following branch

  22. Constructing basic blocks Input:a sequence of MIR instructions Output:a list of basic blocks where each MIR instruction occurs in exactly one block Method: determine the leaders of the basic blocks: - the first instruction in the procedure is a leader - any instruction that is the target of a jump is a leader - any instruction after branch is aleader for each leader its basic block consists of - the leader and - all instructions up to but not including the next leader or the end of the program

  23. Running Example unsigned int fib(unsigned int m) {unsigned int f0=0, f1=1, f2, i; if (m <= 1) { return m; } else { for (i=2, i <=m, i++) { f2=f0+f1; f0=f1; f1 =f2;} return f2; } } 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m

  24. Running Example unsigned int fib(unsigned int m) {unsigned int f0=0, f1=1, f2, i; if (m <= 1) { return m; } else { for (i=2, i <=m, i++) { f2=f0+f1; f0=f1; f1 =f2;} return f2; } } 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m

  25. Running Example 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m unsigned int fib(unsigned int m) {unsigned int f0=0, f1=1, f2, i; if (m <= 1) { return m; } else { for (i=2, i <=m, i++) { f2=f0+f1; f0=f1; f1 =f2;} return f2; } } B1 B2 B3 B4 B5 B6

  26. Constructing Control Flow Graph (CFG) • Special entry block r without successors • Special exit block without predecessors • There is an edge m  n • m= entry and the first instruction in n begins the procedure • n=exit and the last instruction in m is return or the last instruction in the procedure • there is a branch from the last instruction in m into the first instruction in n • the first instruction in n immediately follows the lastnon-branchinstruction in m

  27. 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m Running Example B1 B2 B3 B4 B5 B6

  28. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m exit

  29. How to treat call instructions? • A call is an atomic instruction • A call ends a basic block • Replace the call by the procedure body (inline) • A call is a “goto” into the procedure • A call is handled in a special way

  30. Potential Difficulties • Gotos outside procedure boundaries • Exit/Trap calls • Exception handling • Computed gotos • setjump(), lonjump() calls

  31. DFS, Pre order Traversal & Post Order Traversal • All these four apply to rooted, directed graph and to flow graph. • DFS:- visits the descendants of a node in the graph before visiting any of its siblings that are not also its descendants.

  32. Rooted directed graph with Depth-First presentation of it.

  33. DFS algorithm

  34. The Depth First presentation includes all the graph’s nodes and the edges that make up the depth first order displayed as a tree. • The edges that are part of the DFS are called tree edges and that are not part of Depth First spanning tree is divide into 3 classes: • 1. forward edges – F • 2. Back edges – B • 3. Cross Edge - C

  35. To get DFS for a given graph, an instance of DFS that computes both a Depth First Spanning tree and pre order and post order traversal of the graph G = <N, E> with root r. • DFS for the given graph is : 1,2,3,4,5,6,7,8 • BFS for the given graph is :1,2,6,3,4,5,7,8

  36. Dominators and Post dominators • To determine the loops in a flow graph, we first define a binary relation called dominance on flow graph nodes. • Node d dominates node i, written d dom i, if every possible execution path from entry to i includes d. • dom is reflexive (Every node dominates itself), transitive (if a dom b and b dom c, then a dom c) and antisymmetric (if a dom b and b dom a , then b=a)

  37. Immediate dominance • idom : such that for a ≠ b , a idom b iff, a dom b and there does not exist a node c such that c ≠ b for which a dom c and c dom b, and we write idom(b) to denote the immediate dominance of b. • Clearly, the immediate dominator of a node is unique.

  38. The immediate dominance relation forms a tree of the nodes of a flow graph whose root is the entry node, whose edges are the immediate dominances, and whose paths display all the dominance relationships. • d strictly dominates i, written d sdomi, if d dominates i and d ≠ i • Node p post dominates node i, written p pdomi, if every possible execution path from i to exit includes p, i.eidom p in flow graph with all the edges reversal and entry and exit interchanged.

  39. Loops and strongly connected components • Back edge shows loops: back edge d to c which shows loop

  40. Given a back edge mn, the natural loop of mn is the subgraph consisting of the set of nodes containing n and all the nodes from which m can be reached in the flowgraph without passing through n and the edge set connecting all the nodes in its node set. Node n is the loop header • Algorithm is used to compute set of nodes and the set of edges of the loop it needed.

  41. Many optimizations require moving code from inside a loop to just before its header. To get this, we introduced the concept of pre header. • Pre header is initially an empty block place just before the header of a loop, such that all the edges that previously went to the header from outside the loop now go to pre header and there is a single new edge from pre header to the header

  42. Loop without and with pre header

  43. If two loops have the same header then they are either nested loop or disjoint. • The most general looping structure that may occur is a strongly connected components (SCC) of a flow graph, which is a sub graph Gs = <Ns, Es> such that every node by a path that includes only edges in Es. • A strongly connected component is maximal if every strongly connected component containing it is the component itself.

  44. Reducibility • It results from several kinds of transformations that can be applied to flow graphs that collapse sub graphs into single nodes and hence reduce the flow graph. • The pattern make flow graphs irreducible are called improper regions and they are multiple entry strongly connected components of a flow graph.

  45. The simplest improper region is the two entry loop & three entry loop as in the above fig. It is easy to show how to produce an infinite sequence of distinct improper regions beginning with these two.

  46. Practical approaches to deal with irreducibility • 1. Iterative data flow analysis on irreducible regions and to plug the results into the data flow equations for the rest of the flow graphs. • 2. Use technique called node splitting that transforms irreducible regions into reducible regions. • 3. To perform an induced iteration on the lattice of monotone functions from the lattice to itself (chap 8).

More Related