Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures in C. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Control Structures if Statement if-else Statement switch.
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Whitebox Testing Fra: CS Fall Whitebox Testing AKA Structural, Basis Path Test Normally used at unit level Assumes errors at unit level are.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
1 CS 201 Compiler Construction Machine Code Generation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
8 Intermediate code generation
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Reduction in Strength CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k] * b[k,
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
1 CS 201 Compiler Construction Lecture 1 Introduction.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Improving Code Generation Honors Compilers April 16 th 2002.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.
Compositional correctness of IP-based system design: Translating C/C++ Models into SIGNAL Processes Rennes, November 04, 2005 Hamoudi Kalla and Jean-Pierre.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Compilers Modern Compiler Design
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
Basic Program Analysis
High-level optimization Jakub Yaghob
Lecture 5 Partial Redundancy Elimination
CS2100 Computer Organisation
Principle of Programming Lanugages 2: Imperative languages
Compiler Construction
Computer Architecture & Operations I
Data Flow Testing.
Flow of Control.
Flow of Control.
Control Flow Analysis CS 4501 Baishakhi Ray.
Intermediate Representations
Three-Address Implementation
Introduction to Programming
Chapter 6 Intermediate-Code Generation
CS 201 Compiler Construction
CS 201 Compiler Construction
TARGET CODE GENERATION
Intermediate Representations
Flow of Control.
Code Optimization Overview and Examples Control Flow Graph
SIGNAL FLOW GRAPH.
A graphing calculator is required for some problems or parts of problems 2000.
Sudipto Ghosh CS 406 Fall 99 November 16, 1999
Optimizations using SSA
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Line Graphs.
Translating Imperative Code into SQL
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

Loop invariant code removal CS 480

Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k] * b[k, j]

Translating for statement for i := 1 to n is translated as: temp := n i := 1 L1: if (i > temp) goto L2... i := i + 1 goto L1

Graph representation Each node has a DAG (series of assignments or function calls) and ends with either 1)unconditional transfer (goto a new node) 2)conditional transfer or a unconditional goto 3) function or procedure exit (basically a fancy type of branch).

Graph rep of our program Node 1 t1 <- n i <- 1 (implicit goto node 2) Node 2 If i > t1 goto node 10 Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4 Node 9 i <- i + 1 goto Node 2 Node 10 (procedure exit)

Multi step process Make a list of all variables that are potentially modified in the body of the loop Look for expressions that do NOT involve any of these variables Move any such expressions out of the body of the loop (may need to make new temps) Repeat until nothing new is found

What variables are changing? Find the list of all the variables that can potentially change Assignments Pass-by-reference parameters Global variables Targets of pointers

What is a loop? Can define a loop in terms of graph representation Loop header (can’t get into the loop without going through header) Loop test (way to get out of loop) Loop body (one or more nodes that go back to loop test) Can not get into loop body from anyplace else

Innermost loop Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

Values that are changing Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

Expressions that are not changing Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

Lets move them out of loop Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

Now have new common subexps Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (8 * m + t5)) k <- k + 1 goto node 6

Things have changed, so repeat Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

More new invariant expressions Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) <- deref(c+t7) + (t4 * p + t8)) + (8 * m + 5)) k <- k + 1 goto node 6

Move it out, find more common exps Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > te3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (8 * m + t5)) k <- k + 1 goto node 6

Do it again Do it once more This time nothing changes So then you back out to the next loop, find more values

Next innermost Loop, changing Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > te3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Invariant expressions Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Move them out Node 3 t2 <- m j <- 1 t4 <- i * Node 4 if j > t2 goto node 9 Node t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Do it again, find a new one Node 3 t2 <- m j <- 1 t4 <- i * 4 – 4 t6 <- t4 * p t9 <- t4 * m Node 4 if j > t2 goto node 9 Node t5 <- j * 4 – 4 t7 <- t9 + t5 (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Finally go to outermost loop Go to outermost loop But basically nothing changes here

So, are we done? Originally had +/-: 12 and */%: 7 in innermost loop Now have +/-: 8 and */%: 3 Are we done? Can we do better? Sure we can.. Next lecture