Code Optimization 2

Code Optimization 2

University of Amsterdam Introduction to Compiler Design: optimization and backend issues Andy Pimentel Computer Systems Architecture group [email protected] CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 1/127 Compilers: Organization Revisited University of Amsterdam Source IR Optimizer IR Machine Frontend Backend Code Optimizer Independent part of compiler Different optimizations possible IR to IR translation CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 2/127 Intermediate Representation (IR) University of Amsterdam Flow graph Nodes are basic blocks Basic blocks are single entry and single exit Edges represent control-flow Abstract Machine Code Including the notion of functions and procedures Symbol table(s) keep track of scope and binding information about names CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 3/127 Partitioning into basic blocks University of Amsterdam 1. Determine the leaders, which are: The first statement Any statement that is the target of a jump Any statement that immediately follows a jump 2. For each leader its basic block consists of the leader and all statements up to but not including the next leader CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 4/127 Partitioning into basic blocks (cont’d) University of Amsterdam d1 :a=1 B1 if read() <= 0 goto B4 B2 d2 :b=a d3 :a=243 B3 goto B2 B4 CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 5/127 Intermediate Representation (cont’d) University of Amsterdam Structure within a basic block: Abstract Syntax Tree (AST) Leaves are labeled by variable names or constants Interior nodes are labeled by an operator Directed Acyclic Graph (DAG) C-like 3addressstatements(likewehavealreadyseen) CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 6/127 Directed Acyclic Graph University of Amsterdam Like ASTs: Leaves are labeled by variable names or constants Interior nodes are labeled by an operator Nodes can have variable names attached that contain the value of that expression Common subexpressions are represented by multiple edges to the same expression CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 7/127 DAG creation University of Amsterdam Suppose the following three address statements: 1. x = y op z 2. x = op y 3. x = y if(i <= 20) ... will be treated like case 1 with x undefined CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 8/127 DAG creation (cont’d) University of Amsterdam If node(y) is undefined, create leaf labeled y,sameforz if applicable Find node n labeled op with children node(y) and node(z) if applicable. When not found, create node n.Incase3letn be node(y) Make node(x) point to n and update the attached identifiers for x CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 9/127 DAG example University of Amsterdam 1t1=4*i 2t2=a[t1] + t6, prod 3t3=4*i prod * t5 4t4=b[t3] [ ]t2 [ ] t4 <= 5t5=t2*t4 t1, t3+ t7, i 6t6=prod+t5 * 20 7prod=t6 a b 4 i 1 8t7=i+1 9i=t7 CSACSA 10 if (i <= 20) goto 1 Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 10/127 Local optimizations University of Amsterdam On basic blocks in the intermediate representation Machine independent optimizations As a post code-generation step (often called peephole optimization) On a small “instruction window” (often a basic block) Includes machine specific optimizations CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 11/127 Transformations on basic blocks University of Amsterdam Examples Function-preserving transformations Common subexpression elimination Constant folding Copy propagation Dead-code elimination Temporary variable renaming Interchange of independent statements CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 12/127 Transformations on basic blocks (cont’d) University of Amsterdam Algebraic transformations Machine dependent eliminations/transformations Removal of redundant loads/stores Use of machine idioms CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 13/127 Common subexpression elimination University of Amsterdam If the same expression is computed more than once it is called a common subexpression If the result of the expression is stored, we don’t have to recompute it Moving to a DAG as IR, common subexpressions are automatically detected! x = a + bx= a + b ... ⇒ ... y = a + by= x CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 14/127 Constant folding University of Amsterdam Compute constant expression at compile time May require some emulation support x = 3 + 5 x = 8 ... ⇒ ... y = x ∗ 2 y = 16 CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 15/127 Copy propagation University of Amsterdam Propagate original values when copied Target for dead-code elimination x = yx= y ... ⇒ ... z = x ∗ 2 z = y ∗ 2 CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 16/127 Dead-code elimination University of Amsterdam Avariablex is dead at a statement if it is not used after that statement An assignment x = y + z where x is dead can be safely eliminated Requires live-variable analysis (discussed later on) CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 17/127 Temporary variable renaming University of Amsterdam t1 = a + bt1 = a + b t2 = t1 ∗ 2 t2 = t1 ∗ 2 ... ⇒ ... t1 = d − et3 = d − e c = t1 + 1 c = t3 + 1 If each statement that defines a temporary defines a new temporary, then the basic block is in normal-form Makes some optimizations at BB level a lot simpler (e.g. common subexpression elimination, copy CSACSA propagation, etc.) Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 18/127 Algebraic transformations University of Amsterdam There are many possible algebraic transformations Usually only the common ones are implemented x = x + 0 x = x ∗ 1 x = x ∗ 2 ⇒ x = x << 1 x = x2 ⇒ x = x ∗ x CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 19/127 Machine dependent eliminations/transformations University of Amsterdam Removal of redundant loads/stores 1movR0,a 2mova,R0 //canberemoved Removal of redundant jumps, for example 1beq...,$Lxbne...,$Ly 2j$Ly⇒ $Lx: ... 3$Lx:... Use of machine idioms, e.g., Auto increment/decrement addressing modes CSACSA SIMD instructions Computer Etc., etc. (see practical assignment) Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 20/127 Other sources of optimizations University of Amsterdam Global optimizations Global common subexpression elimination Global constant folding Global copy propagation, etc. Loop optimizations They all need some dataflow analysis on the flow graph CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 21/127 Loop optimizations University of Amsterdam Code motion Decrease amount of code inside loop Take a loop-invariant expression and place it before the loop while (i <= limit − 2) ⇒ t = limit − 2 while (i <= t) CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 22/127 Loop optimizations (cont’d) University of Amsterdam Induction variable elimination Variables that are locked to the iteration of the loop are called induction variables Example: in for (i = 0; i < 10; i++) i is an induction variable Loops can contain more than one induction variable, for example, hidden in an array lookup computation Often, we can eliminate these extra induction variables CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 23/127 Loop optimizations (cont’d) University of Amsterdam Strength reduction Strength reduction is the replacement of expensive operations by cheaper ones (algebraic transformation) Its use is not limited to loops but can be helpful for induction variable elimination i = i + 1 i = i + 1 t1 = i ∗ 4 ⇒ t1 = t1 + 4 t2 = a[t1] t2 = a[t1] if (i < 10) goto top if (i < 10) goto top CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 24/127 Loop optimizations (cont’d) University of Amsterdam Induction variable elimination (2) Note that in the previous strength reduction we have to initialize t1beforetheloop After such strength reductions we can eliminate an induction variable i = i + 1 t1 = t1 + 4 t1 = t1 + 4 ⇒ t2 = a[t1] t2 = a[t1] if (t1 < 40) goto top if (i < 10) goto top CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 25/127 Finding loops in flow graphs University of Amsterdam Dominator relation Node A dominates node B if all paths to node B go through node A Anodealwaysdominatesitself We can construct a tree using this relation: the Dominator tree CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 26/127 Dominator tree example University of Amsterdam Flow graph Dominator tree 1 1 2 23 3 4 4 5 7 5 6 6 7 8 9 10 8 9 10 CSACSA Computer Systems Architecture Introduction to Compiler Design – A. Pimentel – p. 27/127 Natural loops University of Amsterdam Aloophasasingleentrypoint,the header,which dominates the loop There must be a path back to the header Loops can be found by searching for edges of which

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    128 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us