Fall Compiler Principles Lecture 10: Loop Optimizations

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Lecture 11: Code Optimization CS 540 George Mason University.
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.
Compiler Principles Winter Compiler Principles Loop Optimizations and Register Allocation Mayer Goldberg and Roman Manevich Ben-Gurion University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Program Representations. Representing programs Goals.
Compilation (Semester A, 2013/14) Lecture 12: Abstract Interpretation Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv and Eran Yahav.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
CS 536 Spring Global Optimizations Lecture 23.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Program Representations. Representing programs Goals.
Compilation Lecture 8 Abstract Interpretation Noam Rinetzky 1.
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Compiler Principles Fall Compiler Principles Lecture 9: Dataflow & Optimizations 2 Roman Manevich Ben-Gurion University of the Negev.
Compiler Principles Fall Compiler Principles Lecture 8: Dataflow & Optimizations 1 Roman Manevich Ben-Gurion University of the Negev.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Data Flow Analysis Suman Jana
Fall Compiler Principles Lecture 7: Dataflow & Optimizations 2
Iterative Dataflow Problems
Fall Compiler Principles Lecture 4: Parsing part 3
Fall Compiler Principles Lecture 6: Dataflow & Optimizations 1
Fall Compiler Principles Lecture 8: Loop Optimizations
IR + Optimizations Noam Rinetzky
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Global optimizations.
University Of Virginia
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Abstract Interpretation Noam Rinetzky
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Fall Compiler Principles Lecture 10: Global Optimizations
Data Flow Analysis Compiler Design
Fall Compiler Principles Lecture 6: Dataflow & Optimizations 1
Fall Compiler Principles Lecture 4b: Scanning/Parsing recap
Fall Compiler Principles Lecture 13: Summary
Live Variables – Basic Block
Presentation transcript:

Fall 2015-2016 Compiler Principles Lecture 10: Loop Optimizations Roman Manevich Ben-Gurion University of the Negev

Tentative syllabus Front End Intermediate Representation Optimizations Scanning Top-down Parsing (LL) Bottom-up Parsing (LR) Intermediate Representation Lowering Operational Semantics Lowering Correctness Optimizations Dataflow Analysis Loop Optimizations Code Generation Register Allocation

previously Dataflow analysis framework More optimizations Join semilattices Chaotic iteration Monotone transformers More optimizations Reminder: Available expressions  common sub-expression elimination + copy propagation Constant propagation  constant folding

agenda Loop optimizations Dataflow summary Reaching definitions  loop code motion + strength reduction via induction variables Dataflow summary

Loop optimizations Most of a program’s computations are done inside loops Focus optimizations effort on loops The optimizations we’ve seen so far are independent of the control structure Some optimizations are specialized to loops Loop-invariant code motion (Strength reduction via induction variables) Require another type of analysis to find out where expressions get their values from Reaching definitions

Loop invariant computation y = … t = … z = … start y = t * 4 x < y + z x = x + 1 end

Loop invariant computation y = … t = … z = … start t*4 and y+z have same value on each iteration y = t * 4 x < y + z x = x + 1 end

Code hoisting y = … t = … z = … y = t * 4 w = y + z start x < w x = x + 1 end

What reasoning did we use? Both t and z are defined only outside of loop y = … t = … z = … start constants are trivially loop-invariant y = t * 4 x < y + z y is defined inside loop but it is loop invariant since t*4 is loop-invariant x = x + 1 end

What about now? y = … t = … z = … start Now t is not loop-invariant and so are t*4 and y y = t * 4 x < y + z x = x + 1 t = t + 1 end

Loop-invariant code motion d: t = a1 op a2 d is a program location a1 op a2 loop-invariant (for a loop L) if computes the same value in each iteration Hard to know in general Conservative approximation Each ai is a constant, or All definitions of ai that reach d are outside L, or Only one definition of of ai reaches d, and is loop-invariant itself Transformation: hoist the loop-invariant code outside of the loop

Reaching definitions analysis

Reaching definitions analysis A definition d: t = … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Let’s define the corresponding dataflow analysis…

Reaching definitions analysis A definition d: t = … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Direction: ? Domain: ? Join operator: ? Transfer function: fd: a=b op c(RD) = ? fd: not-a-def(RD) = ? Where defs(a) is the set of locations defining a (statements of the form a=...) Initial value: ?

Reaching definitions analysis A definition d: t := … reaches a program location if there is a path from the definition to the program location, along which the defined variable is never redefined Direction: Forward Domain: sets of program locations that are definitions Join operator: union Transfer function: F[d: x:=R](RD) = (RD \ defs(x))  {d} F[d: not-a-def](RD) = RD Where defs(x) is the set of locations defining x (statements of the form x:=R) Initial value: {}

Reaching definitions analysis d1: y = … d2: t = … d3: z = … start d4: y = t * 4 d4:x < y + z end {} d6: x = x + 1

Reaching definitions analysis d1: y = … d2: t = … d3: z = … start d4: y = t * 4 d4:x < y + z end {} d5: x = x + 1

Initialization d1: y = … d2: t = … d3: z = … start {} {} d4: y = t * 4 d4:x < y + z end {} {} d5: x = x + 1 {}

Iteration 1 d1: y = … d2: t = … d3: z = … {} start {} {} d4: y = t * 4 d4:x < y + z end {} {} d5: x = x + 1 {}

Iteration 1 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} d4: y = t * 4 d4:x < y + z end {} {} d5: x = x + 1 {}

Iteration 2 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {} d5: x = x + 1 {}

Iteration 2 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {} d5: x = x + 1 {}

Iteration 2 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {} d5: x = x + 1 {}

Iteration 2 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {d2, d3, d4} d5: x = x + 1 {}

Iteration 3 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {d2, d3, d4} d5: x = x + 1 {d2, d3, d4} {}

Iteration 3 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {d2, d3, d4} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5}

Iteration 4 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {d2, d3, d4} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5}

Iteration 4 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z end {} {d2, d3, d4} {d2, d3, d4} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5}

Iteration 4 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z end {} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5}

Iteration 5 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4, d5} end {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4} {d2, d3, d4, d5}

Iteration 6 d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4, d5} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

Which expressions are loop invariant d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} t is defined only in d2 – outside of loop {d1, d2, d3} y is defined only in d4 – inside of loop but depends on t and 4, both loop-invariant {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4, d5} end {d2, d3, d4, d5} x is defined only in d5 – inside of loop so is not a loop-invariant {d2, d3, d4, d5} z is defined only in d3 – outside of loop {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

Inferring loop invariants

Inferring loop-invariant expressions For a command c of the form t := a1 op a2 A variable ai is immediately loop-invariant if all reaching definitions IN[c]={d1,…,dk} for ai are outside of the loop LOOP-INV = immediately loop-invariant variables and constants LOOP-INV = LOOP-INV  {x | d: x := a1 op a2, d is in the loop, and both a1 and a2 are in LOOP-INV} Iterate until fixed-point An expression is loop-invariant if all operands are loop-invariants

Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

(immediately) LOOP-INV = {t} Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} (immediately) LOOP-INV = {t} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

(immediately) LOOP-INV = {t, z} Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} (immediately) LOOP-INV = {t, z} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

(immediately) LOOP-INV = {t, z} Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} (immediately) LOOP-INV = {t, z} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

(immediately) LOOP-INV = {t, z} Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} (immediately) LOOP-INV = {t, z} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} LOOP-INV = {t, z, 4} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

Computing LOOP-INV d1: y = … d2: t = … d3: z = … {} {d1} start {} {d1, d2} {d1, d2, d3} LOOP-INV = {t, z, 4, y} {d1, d2, d3, d4, d5} d4: y = t * 4 x < y + z {d2, d3, d4} end {d2, d3, d4, d5} {d2, d3, d4, d5} {d2, d3, d4, d5} d5: x = x + 1 {d2, d3, d4, d5}

Strength reduction via induction variables

Induction variables j is a linear function of the induction variable with multiplier 4 while (i < x) { j = a + 4 * i a[j] = j i = i + 1 } i is incremented by a loop-invariant expression on each iteration – this is called an induction variable

Strength-reduction Prepare initial value j = a + 4 * (i-1) while (i < x) { j = j + 4 a[j] = j i = i + 1 } Increment by multiplier

Dataflow summary

Summary of optimizations All of the analyses below use monotone transformers Enabled Optimizations Analysis Common-subexpression elimination Copy Propagation Available Expressions Constant folding Constant Propagation Dead code elimination Live Variables Strongly-live variables Loop-invariant code motion Induction variable strength reduction Reaching Definitions

Analysis direction Forward Backward In the CFG, information is propagated from predecessors of a statement to its output Properties depend on past computations Examples: available expressions, constant propagating, reaching definitions Backward In the CFG, information is propagated from successors of a statement to its output Properties depend on future computations Examples: Liveness analysis

Types of dataflow analysis May vs. Must Must analysis – properties hold on all paths (join is set intersection) Examples: available expressions, constant propagation May analysis – properties hold on some path (join is set union) Examples: liveness, reaching definitions

Next lecture: Register allocation