Example for Basic Blocks
Transcription
Example for Basic Blocks
C-9 Basic Blocks Basic Block (dt. Grundblock): Maximal sequence of instructions that can be entered only at the first of them and exited only from the last of them. Begin of a basic block: • procedure entry • target of a branch • instruction after a branch or return Call instructions: are usually not considered as a branch Local optimization considers the context of one single basic block (or part of it) at a time. © 2000 bei Prof. Dr. Uwe Kastens Global optimization: Basic blocks are the nodes of control-flow graphs. Vorlesung Übersetzer II SS 2000 / Folie 09 Ziele: Understand the notion of basic blocks in der Vorlesung: The topics on the slide are explained. Examples are given. • The definition is explained. • The construction is explained using the example of C-10. • The consequences of having calls in a basic block are discussed. Verständnisfragen: • Explain the decomposition of intermediate code into basic blocks for C-10 and for further examples. C-10 Example for Basic Blocks A C function that computes Fibonacci numbers: Intermediate code with basic blocks: [Muchnick, p. 170] © 2000 bei Prof. Dr. Uwe Kastens int fib (int m) { 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 2 3 4 receive m f0 <- 0 f1 <- 1 if m <= 1 goto L3 B1 5 i <- 2 B3 6 L1: if i <= m goto L2 B4 7 B5 return f2 8 L2: f2 <- f0 + f1 9 f0 <- f1 10 f1 <- f2 11 i <- i + 1 12 goto L1 B6 13 L3: return m B2 Vorlesung Übersetzer II SS 2000 / Folie 10 Ziele: Example for the construction of basic blocks in der Vorlesung: The decomposition into basic blocks is explained according C-9 using the example. C-11 Control-Flow Graph A control-flow graph (dt. Ablaufgraph) represents the control structure of a procedure. Nodes: basic blocks and 2 unique nodes entry and exit. Edges: branches from the end of one basic block to the beginning of a basic block. Fundamental data structure for • structural transformations • data-flow analysis • code motion © 2000 bei Prof. Dr. Uwe Kastens Graph traversal: depth-first search (DFS), breadth-first search (BFS) Vorlesung Übersetzer II SS 2000 / Folie 11 Ziele: Understand the notion of control-flow graphs in der Vorlesung: Examples are given. • The definition is explained. • The example of C-12 is explained. • The representation of loops in control-flow graphs is compared to source language representation. • Algorithms that recognize loops in control-flow graphs are presented at the end of this chapter. Verständnisfragen: • Why is the loop structure of source programs not preserved on the level of intermediate languages? C-12 Example for a Control-flow Graph Intermediate code with basic blocks: Control-flow graph: © 2000 bei Prof. Dr. Uwe Kastens [Muchnick, p. 172] 1 2 3 4 receive m f0 <- 0 f1 <- 1 if m <= 1 goto L3 B1 5 i <- 2 B3 entry B1 6 L1: if i <= m goto L2 B4 7 B5 return f2 8 L2: f2 <- f0 + f1 9 f0 <- f1 10 f1 <- f2 11 i <- i + 1 12 goto L1 B6 13 L3: return m B2 B2 B3 B4 B5 exit Vorlesung Übersetzer II SS 2000 / Folie 12 Ziele: Example for a control-flow graph in der Vorlesung: The control-flow graph represents the basic blocks and their branches, as defined in C-11. Verständnisfragen: B6 C-13 Data-Flow Analysis Data-flow analysis (DFA) provides information about how the execution of a program may manipulate its data. Many different problems can be formulated as data-flow problems, for example: • Which assignments to variable v may influence a use of v at a certain program position? • Is a variable v used on any path from a program position p to the exit node? • The values of which expressions are available at program position p? Data-flow problems are stated in terms of • paths through the control-flow graph and • properties of basic blocks. Data-flow analysis provides information for global optimization. Data-flow analysis does not know © 2000 bei Prof. Dr. Uwe Kastens • input values provided at run-time, • branches taken at run-time. Its results are to be interpreted pessimistic. Vorlesung Übersetzer II SS 2000 / Folie 13 Ziele: Goals and ability of data-flow analysis in der Vorlesung: • The topics on the slide are explained. • Examples for the use of DFA information are given. • Examples for pessimistic information are given. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.4 Verständnisfragen: • What’s wrong about optimistic information? • Why can pessimistic information be useful? C-14 Data-Flow Equations A data-flow problem is stated as a system of equations for a control-flow graph. System of Equations for forward problems (propagate information along control-flow edges): 2 equations for each basic block: Out (B) = Gen (B) ∪ (In (B) - Kill (B)) In (B) = Example Reaching definitions: A definiton d of a variable v reaches the beginning of a block B if there is a path from d to B on which v is not assigned again. This problem is stated over sets of the assignments in a procedure: Θ Out (h) h ∈pred(B) B . pred (B) . (In - Kill) ∪ Gen = Out . © 2000 bei Prof. Dr. Uwe Kastens In, Out, Gen, Kill In, Out variables of the system of equations for each block Gen, Kill a pair of constant sets that characterize a block w.r.t. the DFA problem Θ meet operator, here: Θ = ∪ union of the Out sets of the predecessor blocks. Vorlesung Übersetzer II SS 2000 / Folie 14 Ziele: A DFA problem is modeled by a system of equations in der Vorlesung: • The equation pattern is explained. • Equations are defined over sets. • In this example: sets of assignment statements at certain program positions. • The meet operator being the union operator is correlated to "there is a path" in the problem statement. • Note: In this context a "definition of a variable" means an "assignment of a variable". nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.4 Verständnisfragen: • Explain the meaning of In(B)= {d1: x=5, d4: x=7, d6: y=a+1} for a particular block B. . . . C-15 Specification of a DFA Problem Specification of reaching definitions: • Description: A definiton d of a variable v reaches the beginning of a block B if there is a path from d to B on which v is not assigned again. • It is a forward problem. • The meet operator is union. • The optimization information in the sets are assignments at certain program positions. • Gen (B): contains all definitions d: v = e; in B, such that v is not defined after d in B. • Kill (B): contains all definitions d: v = e; in blocks different from B, such that B has a definition of v. © 2000 bei Prof. Dr. Uwe Kastens Variants of DFA problems: • forward problem ; backward problem () (cf. C-17) • union problem: Θ = ∪; „there is a path“; solution: minimal sets that solve the equations intersect problem: Θ = ∩; „for all paths“; solution: maximal sets that solve the equations • optimization information: sets of certain statements, of variables, of expressions. Further classes of DFA problems over general lattices instead of sets, not considered here. Vorlesung Übersetzer II SS 2000 / Folie 15 Ziele: Specify a DFA problem systematically in der Vorlesung: The topics on the slide are explained. Examples are given. • The items that characterize a DFA problem are explained. • The definition of Gen and Kill is explained. • The variants of DFA problems are summarized. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.4 Verständnisfragen: • Why does this definition of Gen and Kill serves the purpose of the description in the first item? • Explain the relation of the meet operator, the paths in the graph, and the maximal/minimal solutions. C-16 Example Reaching Definitions B1 Gen (B): contains all definitions d: v = e; in B, such that v is not defined after d in B. Kill (B): contains all definitions d: v = e; in blocks different from B, such that B has a definition of v. d1 : a := d2 : b := d3 : c := B2 B3 d4 : b := B4 © 2000 bei Prof. Dr. Uwe Kastens B5 d5 : c := d6 : b := d7 : c := d8 : a := Gen Kill In Out B1 d1, d2, d3 d4, d5, d6, d7, d8 ∅ d1, d2, d3 B2 d4 d2, d6 d1, d2, d3 d1, d3, d4 B3 d5 d3, d7 d1, d2, d3, d6, d7 d1, d2, d5, d6 B4 d6, d7 d2, d3, d4, d5 d1, d2, d5, d6 d1, d6, d7 B5 d8 d1 d1, d2, d3, d4, d5, d6 d2, d3, d4, d5, d6, d8 Vorlesung Übersetzer II SS 2000 / Folie 16 Ziele: Understand the meaning of DFA sets in der Vorlesung: The topics on the slide are explained. Examples are given. • The example for C-15 is explained. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.4 Verständnisfragen: • Check that the In and Out sets solve the equations for the CFG. • How can you argue that the solution is minimal? • Add some elements to the solution such that it still solves the equations. Explain what such non-maximal solutions mean. C-17 Backward Problems System of Equations for backward problems propagate information against control-flow edges: 2 equations for each basic block: In (B) = Gen (B) ∪ (Out (B) - Kill (B)) Out (B) = Example Live variables: • Description: Is variable v alive at a given point p in the program, i. e. is there a path from p to the exit where v is used but not defined before the use? • backward problem • optimization information: sets of variables Θ In (h) h ∈succ(B) B control-flow . . . In = Gen ∪ (Out - Kill) . . succ (B) . optimization information • meet operator: Θ = ∪ union © 2000 bei Prof. Dr. Uwe Kastens • Gen (B): variables that are used in B, but not defined before they are used there. • Kill (B): variables that are defined in B, but not used before they are defined there. Vorlesung Übersetzer II SS 2000 / Folie 17 Ziele: Symmetry of forward and backward schemes in der Vorlesung: The topics on the slide are explained. Examples are given. • The equation pattern is explained. • The DFA problem "live variables" is explained. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.4 Verständnisfragen: • How do determine the live variables within a basic block? C-18 Important Data-Flow Problems • Reaching definitions: A definiton d of a variable v reaches the beginning of a block B if there is a path from d to B on which v is not assigned again. DFA variant: forward; union; set of assignments Transformations: use-def-chains, constant propagation, loop invariant computations • Live variables: Is variable v alive at a given point p in the program, i. e. there is a path from p to the exit where v is used but not defined before the use. DFA variant: backward; union; set of variables Transformations: eliminate redundant assignments • Available expressions: Is expression e computed on every path from the entry to a program position p and none of its variables is defined after the last computation before p. DFA variant: forward; intersect; set of expressions Transformations: eliminate redundant computations © 2000 bei Prof. Dr. Uwe Kastens • Copy propagation: Is a copy assignment c: x = y redundant, i.e. on every path from c to a use of x there is no assignment to y? DFA variant: forward; intersect; set of copy assignments Transformations: remove copy assignments and rename use • Constant propagation: Has variable x at position p a known value, i.e. on every path from the entry to p the last definition of x is an assignment of the same known value. DFA variant: forward; combine function; vector of values Transformations: substitution of variable uses by constants Vorlesung Übersetzer II SS 2000 / Folie 18 Ziele: Recognize the DFA problem scheme in der Vorlesung: • The DFA problems and their purpose are explained. • The DFA classification is derived from the description. • Examples are given. • Problems like copy propagation oftem match to code that results from other optimizing transformations. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.3 Verständnisfragen: • Explain the classification of the DFA problems. • Construct an example for each of the DFA problems. C-19 Iterative Solution of Data-Flow Equations Input: Output: the CFG; the sets Gen(B) and Kill(B) for each basic block B the sets In(B) and Out(B) Algorithm: © 2000 bei Prof. Dr. Uwe Kastens repeat stable := true; for all B ≠ entry {*} do begin for all V ∈ pred(B) do In(B):= In(B) Θ Out(V); oldout:= Out(B); Out(B):= Gen(B) ∪ (In(B)-Kill(B)); stable:= stable and Out(B)=oldout end until stable Complexity: Initialization Union: empty sets for all B do begin In(B):=∅; Out(B):=Gen(B) end; Intersect: full sets for all B do begin In(B) := U; Out(B):= Gen(B)∪ (U - Kill(B)) end; O(n3) with n number of basic blocks O(n2) if pred (B) ≤ k << n for all B Vorlesung Übersetzer II SS 2000 / Folie 19 Ziele: Understand the iterative DFA algorithm in der Vorlesung: The topics on the slide are explained. Examples are given. • Initialization variants are explained. • The algorithm is explained. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.5, 8.2.6 Verständnisfragen: • How is the initialization related to the size of the solution for the two variants union and intersect? • Why does the algorithm terminate? C-20 Variants of DFA Algorithms Heuristic improvement: Goal: propagate changes in the In and Out sets as fast as possible. Technique: visit CFG nodes in topological order in the outer for-loop {*}. Then the number of iterations of the outer repeat-loop is only determined by back edges in the CFG Algorithm for backward problems: Exchange In and Out sets symmetrically in the algorithm of C-19. The nodes should be visited in topological order as if the directions of edges were flipped. Hierarchical algorithms, interval analysis: © 2000 bei Prof. Dr. Uwe Kastens Regions of the CFG are considered nodes of a CFG on a higher level. That abstraction is recursively applied until a single root node is reached. The Gen, Kill sets are combined in upward direction; the In, Out sets are refined downward. Vorlesung Übersetzer II SS 2000 / Folie 20 Ziele: Overview on DFA algorithms in der Vorlesung: The topics on the slide are explained. Examples are given. • The variants of the algorithm of C-19 is explained. • The improvement is discussed. • The idea of hierarchical approaches is explained. nachlesen: Kastens / Übersetzerbau, Abschnitt 8.2.5, 8.2.6 Verständnisfragen: • For a backward problem the blocks could be considered in reversed topological order. Why is that not a good idea?