Representing the control flow of a program P3 / 2004 • Control forms a graph Loop Optimizations • A very large graph • Observation – lot of straight-line connections – simplify the graph by grouping some instructions Spring 2004 Kostis Sagonas 2 Loop Optimizations test: subu $fp, 16 sw zero, 0($fp) sw zero, 4($fp) sw zero, 8($fp) • Important because lots of execution time occurs lab1: mul $t0, $a0, 4 div $t1, $t0, $a1 in loops 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) ble $t0, $a3, lab1 – Induction variable elimination lw $v0, 0($fp) addu $fp, 16 b $ra Spring 2004 Spring 2004 3 4 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 Spring 2004 Spring 2004 Kostis Sagonas 5 Kostis Sagonas 6
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 ���� dominated – Lattice per basic block 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 – d 2 dominates d 1 (but not both – look at path from start) – gen = { b k | b k is the current basic block } – kill = { } • Immediate dominator of m – last dominator of m on – OUT = gen � (IN - kill) – IN = � � OUT � � any path from start node Spring 2004 Spring 2004 Kostis Sagonas 7 Kostis Sagonas 8 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 of n IN = {1} 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 Spring 2004 Spring 2004 9 10 Kostis Sagonas Kostis Sagonas Example Dominator Tree Identifying 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 Spring 2004 Spring 2004 Kostis Sagonas 11 Kostis Sagonas 12
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); Spring 2004 Spring 2004 Kostis Sagonas 13 Kostis Sagonas 14 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 they are unioned and treated as one loop 1 Two loops: {1,2} and {1, 3} 2 3 Unioned: {1,2,3} Spring 2004 Spring 2004 15 16 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 t1 = 100*N for i = 1 to N for i = 1 to N x = x + 1 x = x + 1 for j = 1 to N t2 = t1 + 10*i + x a[i,j] = 100*N + 10*i + j + x for j = 1 to N a[i,j] = t2 + j Spring 2004 Spring 2004 Kostis Sagonas 17 Kostis Sagonas 18
Loop Invariant Code Detection Detecting Loop Invariant Code Algorithm • A statement is loop-invariant if operands are for all statements in loop if operands are constant or have all reaching definitions – Constant, outside loop, mark statement as invariant – Have all reaching definitions outside loop, or do – Have exactly one reaching definition, and that for all statements in loop not already marked invariant definition comes from an invariant statement if operands are constant, have all reaching definitions outside • Concept of exit node of loop loop, or have exactly one reaching definition from invariant – node with successors outside loop statement then mark statement as invariant until there are no more invariant statements Spring 2004 Spring 2004 Kostis Sagonas 19 Kostis Sagonas 20 Loop Invariant Code Motion Order of Statements in Preheader Preserve data dependences from original program • Conditions for moving a statement s: x = y+z (can use order in which discovered by algorithm) into loop header: b = 2 – s dominates all exit nodes of loop b = 2 i = 0 i = 0 • If it does not, some use after loop might get wrong value • Alternate condition: definition of x from s reaches no use a = b * b outside loop (but moving s may increase run time) i < 80 c = a + a – No other statement in loop assigns to x • If one does, assignments might get reordered a = b * b i < 80 – No use of x in loop is reached by definition other c = a + a than s i = i + c i = i + c • If one is, movement may change value read by use Spring 2004 Spring 2004 21 22 Kostis Sagonas Kostis Sagonas Induction Variables Induction Variable Elimination Example: for j = 1 to 100 p = 0 i = 0 *(&A + 4*j) = 202 - 2*j p < 40 i < 10 Basic Induction variable: i = i + 1 J = 1, 2, 3, 4, ….. use of p p = p + 4 use of p p = 4 * i Induction variable &A+4*j: &A+4*j = &A+4, &A+8, &A+12, &A+16, …. Spring 2004 Spring 2004 Kostis Sagonas 23 Kostis Sagonas 24
What are induction variables? What is an Induction Variable? • x is an induction variable of a loop L if • Base induction variable – Only assignments in loop are of form i = i � c – variable changes its value every iteration of the loop – the value is a function of number of iterations of the • Derived induction variables loop – Value is a linear function of a base induction variable • In programs, this function is normally a linear – Within loop, j = c*i + d, where i is a base induction function variable – Very common in array index expressions – an Example: for loop index variable j, function d + c*j access to a[i] produces code like p = a + 4*i Spring 2004 Spring 2004 Kostis Sagonas 25 Kostis Sagonas 26 Strength Reduction for Derived Elimination of Superfluous Induction Variables Induction Variables i = 0 i = 0 p = 0 p = 0 i = 0 p = 0 i < 10 i < 10 p < 40 i < 10 i = i + 1 i = i + 1 use of p p = p + 4 use of p use of p i = i + 1 p = p + 4 p = 4 * i use of p p = p + 4 Spring 2004 Spring 2004 27 28 Kostis Sagonas Kostis Sagonas Output of Induction Variable Three Algorithms Detection Algorithm • Detection of induction variables • Set of induction variables – Find base induction variables – base induction variables – Each base induction variable has a family of derived – derived induction variables induction variables, each of which is a linear • For each induction variable j, a triple <i,c,d> function of base induction variable – i is a base induction variable • Strength reduction for derived induction – the value of j is i*c+d variables – j belongs to family of i • Elimination of superfluous induction variables Spring 2004 Spring 2004 Kostis Sagonas 29 Kostis Sagonas 30
Recommend
More recommend