1 / 66

Optimization & Code Generation

Optimization & Code Generation. Objectives. To learn about Issues In The Design Of Code Generators Basic Blocks And Flow Graphs Next-use Information A Simple Code Generator Register Allocation & Assignment The Dag Representation Of Basic Blocks. Source Code. Symbol Table. Front End.

liseli
Download Presentation

Optimization & Code Generation

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. Optimization & Code Generation www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  2. Objectives • To learn about • Issues In The Design Of Code Generators • Basic Blocks And Flow Graphs • Next-use Information • A Simple Code Generator • Register Allocation & Assignment • The Dag Representation Of Basic Blocks www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  3. Source Code Symbol Table Front End IR Code IR Code Target Code Code Generator Code Generator • The final phase of the compiler • Code Optimization is the optional preceding phase • Code generator should • Effectively use the features of target machine and • Itself should run efficiently Code Optimizer www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  4. Optimization versus Generation • Code generation: • usually transforms from one representation to another • However, in as much as a code generator selects among possibilities, it is also doing a restricted form of optimization. • Optimization: • transforms a computation to an equivalent but ‘‘better’’ computation in the same representation language or • annotates the representation with additional information about the computation. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  5. Optimization through Transformation • Optimization process must have following properties • It must preserve the meaning of programs • Must improve the performance by measurable amount • It must be worth the effort spent by compiler • Best program transformation must yield the most benefit for the least effort • A fast and non-optimizing compiler may be more useful during the debugging phase and optimized one at the actual runtime. • Available options include: • Improving loops • Improving Procedure calls • Instruction selection • Efficient use of registers • Address calculations Control Flow Analysis Data Flow Analysis www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  6. Organization of Optimizing Compiler Code Optimizer • Intermediate code can be relatively independent of the target machine, • So the optimizer can perform freely and does not have to change even if code generator is changed Control Flow Analysis Data Flow Analysis Transformations www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  7. Basic Blocks • A sequence of consecutive intermediate language statements in which flow of control can only enter at the beginning and leave at the end. • A maximal length segment of straight-line code (i.e., no branches) • Basic Block terminology • The first statement in the block is a LABEL / LEADER. • The last statement in the block is a JUMP or a CJUMP. • There are no other LABELs, JUMPs, or CJUMPs in the block. • Question : Why do we need to look at Blocks ? www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  8. Basic Block • Significance • If any statement executes, they all execute • Barring exceptions or other unusual circumstances • Execution totally ordered • Many techniques for improving basic blocks – simplest and strongest methods • How to partition the IR code into Basic blocks ? • Identify LEADER statements • The basic block corresponding to a leader consists of the leader, plus all statements up to but not including the next leader or up to the end of the program www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  9. How to Identify the leader ? • Using the following rules • The first statement in the program is a leader • Any statement that is the target of a branchstatement is a leader (for most intermediate languages these are statements with an associated label) • Any statement that immediately follows a branch or return statement is a leader www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  10. Source Code begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i]; i = i+ 1; end while i <= 20 end (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 • if i <= 20 goto (3) • ………. Three Addr Code Example: Finding Leaders • The following Code computes the product of a matrix RULES • The first statement in the program is a leader • Any statement that is the target of a branch statement is a leader • Any statement that immediately follows a branch or return statement is a leader www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  11. B2 B3 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) B1 (1) prod := 0 (2) i := 1 (13) … Example : Forming Basic Blocks • The basic block corresponding to a leader consists of the leader, plus all statements up to but not including the next leader or up to the end of the program. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  12. Flow Analysis • Blocks are used as basic units in Flow Analysis • Control Flow Analysis: determine the control structure of a program and build a Control Flow Graph. • Data Flow Analysis: determine the flow of scalar values and build Data Flow Graphs. Control flow analysis Inter-procedural Program Intra-procedural Procedure Flow analysis Local Basic block Data flow analysis www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  13. (1) prod := 0 (2) i := 1 B1 B1 B2 B3 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) B2 (13) … B3 Building CFG Rule (1) There is a directed edge from basic block Bx to basic block By in the CFG if: (1) There is a branch from the last statement of Bx to the first statement of By, or (2) Control flow can fall through from Bx to By because: (i) By immediately follows Bx, and (ii) Bx does not end with an unconditional branch Rule (2 i ) Rule (2 ii ) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  14. Control Flow Graph • A control flow graph (CFG), or simply a flow graph, is a directed multi-graph in which: • the nodes are basic blocks; and • the edges represent flow of control (branches/ fall-through ) • The basic block whose leader is the first intermediate language statement is called the start node • No information about the data; • Therefore an edge in the CFG means that the program may take that path • How to build CFG ? www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  15. Use of CFG’s • Used to identify loops in code with several blocks, with two concepts • Strongly Connected nodes • a collection of N nodes where there is a path ( of length one or more ) from any node in N to any ther node in N • Dominance • A node a in a CFG dominatesanode b if every path from the start node to node b goes through a. We say that node a is a dominator of node b • Motivation: • Programs spend most of the execution time in loops, therefore there is a larger payoff for optimizations that exploit loop structure www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  16. Basic Block Transformations • Basic Block Computes a set of expressions • Number of transformations can be applied to a basic block without changing the expressions computed by the block • Such transformations are useful in Code optimization since they can help in • Improving the running time and • Saving the memory occupied by the block ( program) • Two types of transformations are possible • Structure preserving & • Algebraic Transformations www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  17. Quick Sort void quicksort (m,n) int m,n; { int i, j; int v, z; if ( n <= m ) return; /* fragment begins here */ i := m-1; j := n; v := a [n] ; while (1) { do i := I + 1; while ( a[ i ] < v ); do j := j - 1; while ( a[ j ] > v ); if ( I >= j) break; x := a[ i ]; a[ i ] = a[ j ]; a[ j ] := x; } x := a[ i ]; a[ i ] := a[n]; a[n] := x; quicksort (m,j); quicksort ( i +1,n); } www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  18. i := n-1 • j := n • t1 := 4*n • v := a[t1] • i := i + 1 • t2 := 4* i • t3 := a[ t2] • if t1 < v goto(5) • j := j-1 • t4 := 4 *j • t5 := a [t4] • if t5 > v goto (9) • if i >= j goto (23) • t6 := 4 * i • x := a [t6[ • t7 := 4 * i • t8 := 4* j • T9 := a [ t8 ] • a[t7] := t9 • t10 := 4 * j • a[t10] :=x • goto (5) • t11 := 4 * i • x := a[t11] • t12 := 4 * i • t13 := 4 * n • t14 := a[t13] • a[t11] := t14 • t15 := 4 * n • a[t13] := x Corr.Three address code www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  19. i := n-1 • j := n • t1 := 4*n • v := a[t1] • t6 := 4 * i • x := a [t6] • t7 := 4 * i • t8 := 4* j • t9 := a [ t8 ] • a[t7] := t9 • t10 := 4 * j • a[t10] :=x • goto (5) Three address code B1 B5 • i := i + 1 • t2 := 4* i • t3 := a[ t2] • if t1 < v goto(5) B2 • t11 := 4 * i • x := a[t11] • t12 := 4 * i • t13 := 4 * n • t14 := a[t13] • a[t11] := t14 • t15 := 4 * n • a[t13] := x • j := j-1 • t4 := 4 *j • t5 := a [t4] • if t5 > v goto (9) B6 B3 B4 • if i >= j goto (23) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  20. B1 i := n-1 j := n t1 := 4*n v := a[t1] FLOW GRAPH B2 Loops i := i + 1 t2 := 4* i t3 := a[ t2] if t1 < v gotoB2 B5 B6 t6 := 4 * i x := a [t6] t7 := 4 * i t8 := 4* j T9 := a [ t8 ] a[t7] := t9 t10 := 4 * j a[t10] :=x goto B2 B3 t11 := 4 * i x := a[t11] t12 := 4 * i t13 := 4 * n t14 := a[t13] a[t12] := t14 t15 := 4 * n a[t15] := x j := j-1 t4 := 4 *j t5 := a [t4] if t5 > v goto B3 B4 if i >= j goto B6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers Try Transformations on this as an exercise

  21. Principal Sources of Code Optimization • Function Preserving Transformations • Common Sub-expressions • Copy Propagation • Dead Code Elimination • Loop Optimization • Code Motion • Induction Variables & • reduction in Strength www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  22. 1. Structure Preserving Transformations 1.1 Common Sub-expression Elimination • Consider the code block • The 2nd & 4th statement compute the same expression b + c - d • Hence it can be replaced by a := b + c b := a – d c := b + c d := a – d a := b + c b := a – d c := b + c d := b www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  23. B1 i := n-1 j := n t1 := 4*n v := a[t1] Elimination of Common Sub-ex t2 := 4* i t3 := a[ t2] B2 t2 := 4* i t4 := 4 *j t5 := a [t4] i := i + 1 t2 := 4* i t3 := a[ t2] if t1 < v gotoB2 t4 := 4 *j t1 := 4 *n B5 B6 t6 := 4 * i x := a [t6] t7 := 4 * i t8 := 4* j T9 := a [ t8 ] a[t7] := t9 t10 := 4 * j a[t10] :=x goto B2 x := t3 B3 t11 := 4 * i x := a[t11] t12 := 4 * i t13 := 4 * n t14 := a[t13] a[t11] := t14 t15 := 4 * n a[t13] := x x := t3 j := j-1 t4 := 4 *j t5 := a [t4] if t5 > v goto B3 a[t7] := t5 ] a[t4] := x 2] B4 if i >= j goto B6 ] www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  24. 1. Structure Preserving Transformations 1.2 Dead Code Elimination • Eliminating code that computes a value which is never used later ! • Can be identified through: • Liveness analysis & • Def-Use Chains If ( debug ) print is usually preceded by an assignment debug := false. If copy propagation replaces debug by false, then the print statement is dead since it can’t be reached. We can eliminate both test and print statements More generally, deducing at compile time that the value of an expression is a constant and using the constant instead is known as constant folding. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  25. Copy Propagation f := g is copy statement or simply a copy. In fact, elimination of common sub-expressions introduces some of these copy statements. It would be incorrect to replace c:= d+e with c := a or c := b as one does not know the flow of control at the run time. The idea is to use g for f wherever possible after the copy statement f := g in B5 of the example, x := t3 a[t2] := t5 a[t4] := x goto B2 which can be replaced by x := t3 a[t2] := t5 a[t4] := t3 goto B2This may not appear to be an improvement, but it gives an opportunity to eliminate the assignment to x www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  26. 1. Structure Preserving Transformations 1.3Constant propagation ( Constant folding) • Constant expressions are calculated and used • Consider the following Code: const NN = 4; …. i:=2+NN; j:=i*5+a; 1.4 Loop Optimization • Aims to minimise the time spent in the loop, often by reducing the number of operations in the loop • Can be done in several ways i := 6 j := 30 + a www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  27. 1. Structure Preserving Transformations 1.4.1Code Motion • Moving the invariant code out of the loop: • The following Code: • Taking an expression and placing it outside the loop that yields the result independent of the number of times a loop is executed. • while ( j < limit-2 ) do … can be written as • t := limit-2; while j < t do… • Can be rewritten as t := b/c; for i := 1 to 10 do begin z := i + t; . end: for i := 1 to 10 do begin z := i + b/c . . end; www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  28. 1. Structure Preserving Transformations 1.4.2Loop Unrolling • Reducing number of iterations • The following Code: Can be rewritten as i:=1; while (i<=50) do begin a[i]:= b[i]; i:= i + 1; a[i]:= b[i]; i:= i + 1; end; i:=1; while (i<=50) do begin a[i]:= b[i]; i:= i + 1 end; www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  29. B1 i := n-1 j := n t1 := 4*n v := a[t1]; ; Strength Reduction t4 := 4 *j B2 i := i + 1 t2 := 4* i t3 := a[ t2] if t1 < v gotoB2 The variables j and t4 in B3 are in the lock-step, in each iteration, j is decremented by 1 and t4 by 4. B3 j := j-1 t4 := 4 *j t5 := a [t4] if t5 > v goto B3 j := j-1 t4 := 4 *j j := j-1 t4 := 4 *j B6 B5 t4 := t4 - 4 B4 if i >= j goto B6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  30. 1. Structure Preserving Transformations • Consider the Following Code; • Certain variables, in a loop derive their values from the looping variable ( i or j ) and hence their values form an arithmetic progression as we go around the loop. • All such induction variables must be related by a linear function at each point in the loop. • The calculation of each induction variable, frequently involving multiplication, may be replaced by addition to its former value. • Such improvements, replacing an expensive operation by a cheaper one, are called reduction in strength. 1.4.3Induction variable Elimination • j := j-1 • t4 := 4 *j • t5 := a [t4] • if t5 > v goto (1) • It can be replaced by • t4 := t4 - 4 • t5 := a [t4] • if t5 > v goto (1) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  31. B1 i := n-1 j := n t1 := 4*n v := a[t1]; t4 := 4 *j ; t2 := 4*i FLOW GRAPH AFTER LOOP OPTIMIZATION B2 t2 := t2 + 4 t3 := a[ t2] if t1 < v gotoB2 B3 t4 := t4 - 4 t5 := a [t4] if t5 > v goto B3 B6 B5 a[t2] := t5 a[t4] := t3 goto B2 t14 := a[t1] a[t2] := t14 a[t1] := t3 B4 if t2 >= t4 goto B6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  32. 2. Algebraic Transformation • Can be used to change the set of expressions by its algebraic equivalent • For Example statements like • x := x = 0 or • x := X * 1 can be eliminated. • Statement • x := y ** 2 can be changed to cheaper x := y * y • X := y * 2 can be changed to cheaper x := y + y ( Strength Reduction ) www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  33. Data Flow Analysis • A collection of techniques for compile-time reasoning about the run-time flow of values • Almost always involves building a graph • Usually formulated as a set of simultaneous equations attached to nodes and edges • Useful in code optimization tasks like • dead-code elimination • redundant computation elimination • code motion etc… • One such technique is Liveness Analysis www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  34. Liveness Analysis – Another use of FG • Consider the following code : a := 0; L1: b := a +1; c := c + b; a := b * 2; if a < N go to L1; return c • Corresponding CFG Would be: a := 0; 1 2 b := a +1; 3 c := c + b; A variable is live if its current value will be used in future. So we analyze live-ness from future to present to past 4 a := b * 2; a < N 5 6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  35. Liveness Analysis • The variable b is used in statement 4 & is therefore live at 4. • It is automatically live from 3 to 4 and since 3 does not assign a value to b it is also live from 2 to 3. • But 2 assigns b; Further, b is not needed by anyone from 1 to 2 and is therefore dead from 1 to 2, • but live from 2 to 3 a := 0; 1 2 b := a +1; 3 c := c + b; 4 a := b * 2; a < N 5 6 and 3 to 4. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  36. Liveness Analysis • The variable a is live from 1 to 2 and again is live from 4 to 5 to 2 but not live from 2 to 3 to 4. • The variable c is live on entry. • If it is a parameter then it is live throughout this segment. • If it is a local variable, then the compiler would detect an un-initialized variable and give a warning signal ! a := 0; 1 2 b := a +1; 3 c := c + b; 4 a := b * 2; a < N 5 6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  37. Predecessor & Successor • A flow graph node has out-edges that lead to successor nodes, and • in-edges that come from predecessor nodes. • The set pred [n] is all the predecessors of node n and • succ [n] is the set of successors a := 0; 1 2 b := a +1; 3 c := c + b; • The out-edges of (5) are 6 and 2. therefore, the succ [5] is {2,6}. • Similarly the in-edges of 2 are1 and 5 therefore pred [ 2 ] are {1,5} 4 a := b * 2; a < N 5 6 return c www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  38. def & uses of Variables in FG • An assignment to a variable or a temporary defines it. • An occurrence of a variable on RHS of an assignment or inany other expression uses the variable. • def [ 3 ] is {c} & • uses [ 3 ] = {b,c} a := 0; 1 2 b := a +1; 3 c := c + b; 4 a := b * 2; a < N 5 6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  39. Live-in, Live-out & Live -on • A variable is : • live-in at a node if it is live on any of the in-edges of that node • live-out at a node if it is live on any of the out-edges of that node • live on an edge if there is a directed path from the edge to a use of the variable that does not go through any def. a := 0; 1 2 b := a +1; 3 c := c + b; 4 a := b * 2; a < N 5 6 www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  40. Liveness – Observations & equations • If a variable is in Use [n] then it is live at node n (i.e. if a statement uses a variable, then the variable is live on entry to that statement) • If a variable is live-in at a node n then it is live-out at all nodes in pred [n]. • If a variable is live-out at node n, and not def [n] then the variable is also live-in at n.( i.e. if someone needs the value a at the end of the statement n and n does not provide that value, then a’s value is needed on entry to n) • In [n] = use [n] (out [n] – def [n]) • Out [n] = s Єsucc [n] in [s] a := 0; 1 2 b := a +1; 3 c := c + b; 4 a := b * 2; a < N ∩ 5 6 ∩ www.Bookspar.com | Website for Students | VTU - Notes - Question Papers return c

  41. Liveness Computation Algorithm for eachn in [n] ← ( ) out [n] ← ( ) ; repeat for each n in’ [n]← in [n] out’ [n] ← out [n] in [n] ← use [n] (out [n] – def [n]) out [n] ← s Є succ [n] in [s] Until in’ [n] = in [n] and out’ [n ] = out [n] a := 0; 1 2 b := a +1; 3 c := c + b; ∩ 4 a := b * 2; ∩ a < N 5 6 return c www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  42. Second Third O I O I 6 5 4 3 2 1 Liveness Computation Algorithm First a := 0; 1 use def O I c c c c 2 b := a +1; a c ac ac ac ac ac 3 c := c + b; b a ac bc ac bc ac bc bc c bc bc bc bc bc bc 4 a := b * 2; a b bc ac bc ac bc ac a < N 5 6 a ac c ac c ac c return c

  43. Live-in and live-out of Basic Blocks Variables in the module are a, b, c, d, e, f b, c, d, f b, c, d, e, f a:= b + c d:= d – b e := a + f b, c, d, f B1 b, c, f a c d e a c d f a, c, d, e, f are live f := a - d B2 b := d + f e := a –c B3 c, d, e, f are live c d e f b, c, d, e, f are live B4 b := d + c b, c, d, e, f are live www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  44. Live-in and live-out of Basic Blocks • Variables in the module are a, b, c, d, e, f b c d f a:= b + c d:= d – b e := a + f B1 a c d e a c d f a, c, d, e, f are live f := a - d B2 b := d + f e := a –c B3 c, d, e, f are live c d e f b, c, d, e, f are live B4 b := d + c b, c, d, e, f are live www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  45. Usage of live-ness property • Live-ness property is useful in usage counts which is useful for Register allocation • Live-ness property helps to determine sharing of registers by variables which are not live at the same time. • Give warning message for uninitialized variables. • Helps in data flow analysis. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  46. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  47. Code Generator Issues • Input to code generator –Intermediate Representations • Several choices including graphical ones • Assumption is prior stage has done type checking & job to be done is allocating the target machine variable types • Further assumption is error checking is done thing • Target Code • Could be absolute machine code or relocatable code or assembly language code • Memory management • Mapping of variables to memory locations ( absolutely or relatively www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  48. Code Generator Issues (Contd.) • Instruction Selection • Decided by the nature of the instruction set, supported data types, instruction speeds machine idioms etc.... of the target machine • Each three address code line can be individually translated into target machine code ; but this could result in poor code • Register Allocation • Instructions involving registers (RR) tend to go fast • Effective use of variables in registers speeds up the program execution • Limited number of registers in the system calls for efficient use of these register locations www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  49. www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

  50. Code Generation – Phases • Instruction Selection • Mapping IR into assembly code • Assumes a fixed storage mapping & code shape • Combining operations, using address modes • Instruction scheduling • Reordering operations to hide latencies • Assumes a fixed program (set of operations) • Changes demand for registers • Register allocation • Deciding which values will reside in registers • Changes the storage mapping, may add false sharing • Concerns about placement of data & memory operations www.Bookspar.com | Website for Students | VTU - Notes - Question Papers

More Related