representing the control flow of a program
play

Representing the control flow of a program P3 / 2003 Control - PDF document

Representing the control flow of a program P3 / 2003 Control forms a graph Loop Optimizations A very large graph Observation lot of straight-line connections simplify the graph by grouping some instructions Kostis


  1. Representing the control flow of a program P3 / 2003 • Control forms a graph Loop Optimizations • A very large graph • Observation – lot of straight-line connections – simplify the graph by grouping some instructions Kostis Sagonas 2 Spring 2003 Loop Optimizations test: subu $fp, 16 sw zero, 0($fp) sw zero, 4($fp) sw zero, 8($fp) • Important because lots of computation occurs in lab1: mul $t0, $a0, 4 loops div $t1, $t0, $a1 lw $t2, 8($fp) mul $t3, $t1, $t2 lw $t4, 8($fp) • First, we will identify loops addui $t4, $t4, 1 lw $t5, 8($fp) addui $t5, $t5, 1 mul $t6, $t4, $t5 • We will study three optimizations addu $t7, $t3, $t6 lw $t8, 0($fp) add $t8, $t7, $t8 sw $t8, 0($fp) – Loop-invariant code motion lw $t0, 4($fp) mul $t1, $t0, a1 sw $t2, 0($fp) – Strength reduction lw $t0, 8($fp) addui $t0, $t0, 1 sw $t0, 8($fp) – Induction variable elimination ble $t0, $a3, lab1 lw $v0, 0($fp) addu $fp, 16 b $ra 3 Spring 2003 4 Spring 2003 Kostis Sagonas Kostis Sagonas What is a Loop? Anomalous Situations • Set of nodes • Two back edges, two loops, one • Loop header header – Single node • Compiler merges – All iterations of loop loops go through header • Back edge • No loop header, no loop Kostis Sagonas 5 Spring 2003 Kostis Sagonas 6 Spring 2003 1

  2. Defining Loops With Dominators Dominator Problem Formulation • Concept of dominator • A cross product of the lattice for each basic block: – Node n dominates a node m if all paths from start node to m – Lattice per basic block ���� dominated go through n ⊥ = not dominated • Flow direction: Forward Flow • If d 1 and d 2 both dominate m, then either • Flow Functions: – d 1 dominates d 2 , or – gen = { b k | b k is the current basic block } – d 2 dominates d 1 (but not both – look at path from start) – kill = { } – OUT = gen ∪ (IN - kill) • Immediate dominator of m – last dominator of m on any path from start node – IN = ∩ OUT Kostis Sagonas 7 Spring 2003 Kostis Sagonas 8 Spring 2003 Computing Dominators Dominator Tree OUT = gen ∪ (IN - kill) IN = { } IN = ∩ OUT • Nodes are nodes of control flow graph bb1 • Edge from d to n if d is the immediate dominator IN = {1} of n bb2 • This structure is a tree IN = {1,2} IN = {1,2} • Rooted at start node bb3 bb4 IN = {1,2} bb5 IN = {1,2,5} bb6 9 Spring 2003 10 Spring 2003 Kostis Sagonas Kostis Sagonas Example Dominator Tree Defining Loops 1 • Unique entry point – header 1 • At least one path back to header 2 • Find edges whose heads dominate tails 3 2 3 – These edges are back edges of loops 4 – Given a back edge n → d 4 – Loop consists of n plus all nodes that can reach n 5 6 5 6 7 without going through d 7 (all nodes “between” d and n) – d is loop header Kostis Sagonas 11 Spring 2003 Kostis Sagonas 12 Spring 2003 2

  3. Two Loops In Example Loop Construction Algorithm insert(m) 1 1 if m ∉ loop then 2 loop = loop ∪ {m} 3 2 3 push m onto stack loop(d,n) 4 4 loop = ∅ ; stack = ∅ ; insert(n); 5 6 5 6 7 while stack not empty do 7 m = pop stack; for all p ∈ pred(m) do insert(p) Kostis Sagonas 13 Spring 2003 Kostis Sagonas 14 Spring 2003 Nested Loops Loop Preheader • If two loops do not have same header then • Many optimizations stick code before loop – Either one loop (inner loop) is contained in the • Put a special node (loop preheader) before loop other (outer loop) to hold this code – Or the two loops are disjoint • If two loops have same header, typically unioned and treated as one loop 1 Two loops: {1,2} and {1, 3} 2 3 Unioned: {1,2,3} 15 Spring 2003 16 Spring 2003 Kostis Sagonas Kostis Sagonas Loop Optimizations Loop Invariant Code Motion • Now that we have the loop, we can optimize it! If a computation produces the same value in every loop iteration, move it out of the loop • Loop invariant code motion – Stick loop invariant code in the header for i = 1 to N x = x + 1 for j = 1 to N a(i,j) = 100*N + 10*i + j + x Kostis Sagonas 17 Spring 2003 Kostis Sagonas 18 Spring 2003 3

  4. Loop Invariant Code Motion Detecting Loop Invariant Code If a computation produces the same value in every • A statement is loop-invariant if operands are loop iteration, move it out of the loop – Constant, – Have all reaching definitions outside loop, or t1 = 100*N – Have exactly one reaching definition, and that for i = 1 to N definition comes from an invariant statement x = x + 1 • Concept of exit node of loop t2 = t1 + 10*i + x – node with successors outside loop for j = 1 to N a(i,j) = t2 + j Kostis Sagonas 19 Spring 2003 Kostis Sagonas 20 Spring 2003 Loop Invariant Code Detection Loop Invariant Code Motion Algorithm for all statements in loop • Conditions for moving a statement s: x:=y+z into loop header: if operands are constant or have all reaching definitions outside loop, mark statement as invariant – s dominates all exit nodes of loop • If it does not, some use after loop might get wrong value do • Alternate condition: definition of x from s reaches no use for all statements in loop not already marked invariant outside loop (but moving s may increase run time) if operands are constant, have all reaching definitions outside – No other statement in loop assigns to x loop, or have exactly one reaching definition from invariant • If one does, assignments might get reordered statement – No use of x in loop is reached by definition other then mark statement as invariant than s until there are no more invariant statements • If one is, movement may change value read by use 21 Spring 2003 22 Spring 2003 Kostis Sagonas Kostis Sagonas Order of Statements in Preheader Induction Variables Preserve data dependences from original program Example: (can use order in which discovered by algorithm) for j = 1 to 100 b = 2 b = 2 *(&A + 4*j) = 202 - 2*j i = 0 i = 0 a = b * b i < 80 Basic Induction variable: c = a + a J = 1, 2, 3, 4, ….. a = b * b i < 80 Induction variable &A+4*j: c = a + a &A+4*j = &A+4, &A+8, &A+12, &A+16, …. i = i + c i = i + c Kostis Sagonas 23 Spring 2003 Kostis Sagonas 24 Spring 2003 4

  5. Induction Variable Elimination What are induction variables? • x is an induction variable of a loop L if – variable changes its value every iteration of the loop i = 0 p = 0 – the value is a function of number of iterations of the i < 10 p < 40 loop i = i + 1 • In programs, this function is normally a linear p = p + 4 use of p use of p p = 4 * i function Example: for loop index variable j, function d + c*j Kostis Sagonas 25 Spring 2003 Kostis Sagonas 26 Spring 2003 Strength Reduction for Derived What is an Induction Variable? Induction Variables • Base induction variable i = 0 – Only assignments in loop are of form i = i ± c p = 0 i = 0 • Derived induction variables i < 10 i < 10 – Value is a linear function of a base induction variable i = i + 1 i = i + 1 use of p – Within loop, j = c*i + d, where i is a base induction use of p p = p + 4 p = 4 * i variable – Very common in array index expressions – an access to a[i] produces code like p = a + 4*i 27 Spring 2003 28 Spring 2003 Kostis Sagonas Kostis Sagonas Elimination of Superfluous Three Algorithms Induction Variables • Detection of induction variables – Find base induction variables i = 0 p = 0 p = 0 – Each base induction variable has a family of derived p < 40 induction variables, each of which is a linear i < 10 function of base induction variable • Strength reduction for derived induction p = p + 4 use of p i = i + 1 use of p variables p = p + 4 • Elimination of superfluous induction variables Kostis Sagonas 29 Spring 2003 Kostis Sagonas 30 Spring 2003 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend