
Common-Subexpression Elimination Partial Redundancy Elimination, (Repetition) Loop Optimization An occurrence of an expression in a program is a common subexpression if there is another occurrence of the expression whose evaluation always precedes this one & Lazy Code Motion in execution order and if the operands of the expression remain unchanged between the two evaluations. Local Common Subexpression Elimination (CSE) keeps track of the set of available expressions within a basic block and replaces instances of them by references to Repetition: CSE Advanced Compiler Techniques new temporaries that keep their value. … … 2005 ttt=x+y; a=( x+y)+ z; Erik Stenman a=ttt+z; b=a-1; Virtutech b=a-1; c=x+y; c=ttt; … … Before CSE After CSE Advanced Compiler Techniques 4/22/2005 2 http://lamp.epfl.ch/teaching/advancedCompiler/ Partially Redundant Redundant Expressions Expressions An expression is redundant at a point p if, on every path to p An expression is partially redundant at p if it is redundant along 1. It is evaluated before reaching p, and some, but not all, paths reaching p. 2. None of its constituent values is redefined before p Example Example a←b+c b←b+1 a←b+c a←b+c b←b+1 a←b+c a←b+c a←b+c Redundant Expressions Some occurrences of a←b+c b+c are redundant Partially Redundant Expressions a←b+c a←b+c Not all occurrences ← ← a b+c b b+1 of b+c are redundant! Inserting a copy of “a←b+c” after the definition ← a b+c of b can make it redundant. Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 3 http://lamp.epfl.ch/teaching/advancedCompiler/ 4 http://lamp.epfl.ch/teaching/advancedCompiler/ Loop Invariant Expressions Loop Optimizations Another example: x←y*z x←y*z ♦ Loop Optimization is important because most x←y*z a←b+c a←b+c of the execution time occurs in loops. ♦ b + c is partially First, we will identify loops. a←b+c redundant here a←b+c ♦ We will study three optimizations: ♦ Loop-invariant code motion. Loop Optimizations ♦ Strength reduction. Partially Redundant Expressions ♦ Induction variable elimination. Loop invariant expressions are partially redundant. ♦ We will not talk about loop unrolling which ♦ Partial redundancy elimination performs code motion. ♦ Major part of the work is figuring out where to insert operations. is another important optimization technique. Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 5 http://lamp.epfl.ch/teaching/advancedCompiler/ 6 http://lamp.epfl.ch/teaching/advancedCompiler/ 1 What is a Loop? Anomalous Situations ♦Set of nodes ♦ Two back ♦ edges, two Loop header loops, one ♦ Single node header ♦ All iterations of ♦ Compiler Loop Optimizations Loop Optimizations loop go through merges loops header ♦ ♦Back edge No loop header, no loop Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 7 http://lamp.epfl.ch/teaching/advancedCompiler/ 8 http://lamp.epfl.ch/teaching/advancedCompiler/ Defining Loops With Identifying Loops Dominators Recall the concept of dominators: ♦ Node n dominates a node m if all paths from the ♦ A loop has a unique entry point – the header. start node to m go through n. ♦ At least one path back to header. ♦ The immediate dominator of m is the last dominator ♦ Find edges whose heads (>) dominate tails (-), of m on any path from start node. these edges are back edges of loops. ♦ A dominator tree is a tree rooted at the start node: ♦ Given a back edge n→d: ♦ Loop Optimizations: Dominators Nodes are nodes of control flow graph. ♦ The node d is the loop header. ♦ Loop Optimizations: Identifying loops Edge from d to n if d is the immediate dominator of n. ♦ The loop consists of n plus all nodes that can reach n without going through d (all nodes “between” d and n) Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 9 http://lamp.epfl.ch/teaching/advancedCompiler/ 10 http://lamp.epfl.ch/teaching/advancedCompiler/ Loop Construction Algorithm Nested Loops loop (d,n) ♦ If two loops do not have same header then loop = {d} ; stack = ∅; insert (n); ♦ Either one loop (inner loop) is contained in the while stack not empty do other (outer loop) ♦ Or the two loops are disjoint m = pop stack ; ♦ If two loops have same header, typically they for all p ∈ pred (m) do insert (p); are unioned and treated as one loop insert (m) 1 if m ∉ loop then Loop Optimizations: Identifying loops Loop Optimizations: Identifying loops 1 ∪ Two loops: loop = loop {m}; 2 3 {1,2} and {1, 3} push m onto stack ; 2 3 Unioned: {1,2,3} 4 Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 11 http://lamp.epfl.ch/teaching/advancedCompiler/ 12 http://lamp.epfl.ch/teaching/advancedCompiler/ 2 Loop Preheader Loop Optimizations ♦ Many optimizations stick code before loop. ♦Now that we have the loop, we can ♦ Put a special node (loop preheader ) before optimize it! loop to hold this code. ♦Loop invariant code motion: ♦ Move loop invariant code to the header. Loop Optimizations: Preheader Loop Optimizations: Loop invariant code motion Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 13 http://lamp.epfl.ch/teaching/advancedCompiler/ 14 http://lamp.epfl.ch/teaching/advancedCompiler/ Detecting Loop Invariant Loop Invariant Code Motion Code If a computation produces the same value in ♦ A statement is loop-invariant if operands are every loop iteration, move it out of the loop. ♦ Constant, ♦ Have all reaching definitions outside loop, or t1 = 100*N for i = 1 to N for i = 1 to N ♦ Have exactly one reaching definition, and that x = x + 1 definition comes from an invariant statement for j = 1 to N x = x + 1 a[i,j] = 100*N + 10*i + j + xxx t2 = t1 + 10*i + x ♦ Concept of exit node of loop for j = 1 to N ♦ node with successors outside loop a[i,j] = t2 + j Loop Optimizations: Loop invariant code motion Loop Optimizations: Loop invariant code motion Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 15 http://lamp.epfl.ch/teaching/advancedCompiler/ 16 http://lamp.epfl.ch/teaching/advancedCompiler/ Loop Invariant Code Detection Algorithm Loop Invariant Code Motion for all statements in loop if operands are constant or have all reaching definitions ♦ Conditions for moving a statement s: x = y+z outside loop, mark statement as invariant into loop header: do ♦ s dominates all exit nodes of loop for all statements in loop not already marked invariant ♦ If it does not, some use after loop might get wrong value if operands are constant, have all reaching definitions ♦ Alternate condition: definition of x from s reaches no use outside loop (but moving s may increase run time) outside loop, or have exactly one reaching definition from ♦ No other statement in loop assigns to x invariant statement ♦ If one does, assignments might get reordered then mark statement as invariant ♦ No use of x in loop is reached by definition other Loop Optimizations: Loop invariant code motion until there are no more invariant statements Loop Optimizations: Loop invariant code motion than s ♦ If one is, movement may change value read by use Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 17 http://lamp.epfl.ch/teaching/advancedCompiler/ 18 http://lamp.epfl.ch/teaching/advancedCompiler/ 3 Order of Statements in Induction Variables Preheader Preserve data dependences from original program (can use order in which discovered by algorithm) Example: b = 2 b = 2 for j = 1 to 100 i = 0 i = 0 *(&A + 4*j) = 202 - 2*j a = b * b i < 80 c = a + a Basic Induction variable: J = 1, 2, 3, 4, ….. a = b * b i < 80 c = a + a Loop Optimizations: Induction Variables Induction variable &A+4*j: Loop Optimizations: Loop invariant code motion i = i + c &A+4*j = &A+4, &A+8, &A+12, &A+16, …. i = i + c Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 19 http://lamp.epfl.ch/teaching/advancedCompiler/ 20 http://lamp.epfl.ch/teaching/advancedCompiler/ What are induction variables? Types of Induction Variables ♦ x is an induction variable of a loop L if ♦ Base induction variable: ♦ variable changes its value every iteration of the ♦ Only assignments in loop are of form i = i ± c loop ♦ Derived induction variables: ♦ the value is a function of number of iterations of ♦ Value is a linear function of a base induction the loop variable. ♦ Within loop, j = c*i + d, where i is a base induction ♦ In programs, this function is normally a linear variable. Loop Optimizations: Induction Variables Loop Optimizations: Induction Variables function ♦ Very common in array index expressions – Example: for loop index variable j, function d + c*j an access to a[i] produces code like p = a + 4*i. Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 21 http://lamp.epfl.ch/teaching/advancedCompiler/ 22 http://lamp.epfl.ch/teaching/advancedCompiler/ Strength Reduction for Elimination of Superfluous tion Derived Induction Variables Induction Variables i = 0 i = 0 i = 0 p = 0 p = 0 p = 0 i < 10 i < 10 p < 40 i < 10 i = i + 1 i = i + 1 use of p use of p i = i + 1 p = p + 4 use of p p = 4 * i p = p + 4 use of p p = p + 4 Loop Optimizations: Induction Variables – Elimination Loop Optimizations: Induction Variables – Strength Reduc Advanced Compiler Techniques 4/22/2005 Advanced Compiler Techniques 4/22/2005 23 http://lamp.epfl.ch/teaching/advancedCompiler/ 24 http://lamp.epfl.ch/teaching/advancedCompiler/ 4 Output of Induction Variable Three Algorithms Detection Algorithm ♦ Detection of induction variables: ♦Set of induction variables: ♦ Find base induction variables.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-