# Compiler Construction 2016/2017 Loop Optimizations

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Compiler Construction 2016/2017 Loop Optimizations Peter Thiemann January 16, 2017

2 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

3 Loops Loops are everywhere worthwhile target for optimization

4 Loops Definition: Loop A loop with header h is a set L of nodes in a CFG such that h L ( s L) exists path from h to s ( s L) exists path from s to h ( t / L) ( s L) if there is an edge from t to s, then s = h Special loop nodes A loop entry node has a predecessor outside the loop. A loop exit node has a successor outside the loop.

5 Example Loops 1 2 3

6 Example Loops 18-1a 1 2 3

7 Example Loops 18-1b

8 Example Loops 18-1c 1 2 3

9 Example Loops 18-1d

10 Example Loops 18-1e

11 Program for 18-1e 1 int isprime (int n) { 2 i = 2; 3 do { 4 j = 2; 5 do { 6 if (i*j==n) { 7 return 0; 8 } else { 9 j = j+1; 10 } 11 } while (j<n); 12 i = i+1; 13 } while (i<n); 14 return 1; 15 }

12 Reducible Flow Graphs Arbitrary flow graphs: Spaghetti code Reducible flow graphs arise from structured control if-then-else while-do repeat-until for break (multi-level)

13 Irreducible Flow Graphs 18-2a: Not a loop 1 2 3

14 Irreducible Flow Graphs 18-2b: Not a loop

15 Irreducible Flow Graphs 18-2c: Not a loop Reduces to 18-2a: collapse edges (x, y) where x is the only predecessor of y A flow graph is irreducible if exhaustive collapsing leads to a subgraph like 18-2a.

16 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

17 Dominators Objective Find all loops in flow graph Assumption Each CFG has unique entry node s 0 without predecessors Domination relation A node d dominates a node n if every path from s 0 to n must go through d. Remark Domination is reflexive

18 Algorithm for Finding Dominators Lemma Let n be a node with predecessors p 1,..., p k and d n a node. d dominates n iff ( 1 i k) d dominates p i Domination equation Let D[n] be the set of nodes that dominate n. D[n] = {n} p pred[n] D[p] Solve by fixed point iteration Start with ( n N) D[n] = N (all nodes in the CFG) Observe that D[s 0 ] = {s 0 } because pred(s 0 ) = Watch out for unreachable nodes

19 Immediate Dominators Theorem Let G be a connected, rooted graph. If d dominates n and e dominates n, then either d dominates e or e dominates d. Proof: by contradiction Consequence: Each node n s 0 has one immediate dominator idom(n) such that 1 idom(n) n 2 idom(n) dominates n 3 idom(n) does not dominate another dominator of n

20 Dominator Tree Dominator Tree The dominator tree is a directed graph where the nodes are the nodes of the CFG and there is an edge (x, y) if x = idom(y). back edge in CFG: from n to h so that h dominates n

21 Finding Loops Natural Loop The natural loop of a back edge (n, h) where h dominates n is the set of nodes x such that h dominates x exists path from x to n not containing h h is the header of this natural loop.

22 Nested Loops Nested Loop If A and B are loops with headers a b and b A, then B A. Loop B is nested within A. B is the inner loop. Algorithm: Loop-nest Tree 1 Compute the dominators of the CFG 2 Compute the dominator tree 3 Find all natural loops with their headers 4 For each loop header h merge all natural loops of h into a single loop loop[h] 5 Construct the tree of loop headers such that h 1 is above h 2 if h 2 loop[h 1 ] Leaves are innermost loops Procedure body is pseudo-loop at root of loop-nest tree

23 A Loop-Nest Tree

24 Adding a Loop Preheader P exit 8 exit 8 loop optimizations need a CFG node before the loop as a target to move code out of the loop add preheader node like P in example

25 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

26 Loop-Invariant Computations Suppose t a b occurs in a loop. If a and b have the same value for each iteration of the loop, then t always gets the same value. t s definition is loop-invariant, but its computation is repeated on each iteration Goals Detect such loop-invariant definitions Hoist them out of the loop

27 Approximation to Loop-Invariance Loop-Invariance The definition d : t a 1 a 2 is loop-invariant for loop L if d L and, for each a i, one of the following conditions holds: 1 a i is a constant, 2 all definitions of a i that reach d are outside of L, or 3 only one definition of a i reaches d and that definition is loop-invariant. Algorithm: Loop-Invariance 1 Identify all definitions whose operands are constant or defined outside the loop 2 Add loop-invariant definitions until a fixed point is reached

28 Hoisting Suppose t a b is loop-invariant. Can we hoist it out of the loop? L 0 t 0 L 1 i i + 1 t a b M[i] t if i < N goto L 1 L 2 x t L 0 t 0 L 1 if i N goto L 2 i i + 1 t a b M[i] t goto L 1 L 2 x t L 0 t 0 L 1 i i + 1 t a b M[i] t t 0 M[j] t if i < N goto L 1 L 2 L 0 t 0 L 1 M[j] t i i + 1 t a b M[i] t if i < N goto L 1 L 2 x t

29 Hoisting Suppose t a b is loop-invariant. Can we hoist it out of the loop? L 0 t 0 L 0 t 0 L 1 L 1 L 1 L 1 i i + 1 if i N goto L 2 i i + 1 M[j] t t a b i i + 1 t a b i i + 1 M[i] t t a b M[i] t t a b if i < N goto L 1 M[i] t t 0 M[i] t L 2 goto L 1 M[j] t if i < N goto L 1 x t L 2 if i < N goto L 1 L 2 x t L 2 x t yes no no no L 0 t 0 L 0 t 0

30 Hoisting Criteria for hoisting A loop-invariant definition d : t a b can be hoisted to the end of its loop s preheader if all of the following hold 1 d dominates all loop exits at which t is live-out 2 there is only one definition of t in the loop 3 t is not live-out at the loop preheader Attention: arithmetic exceptions, side effects of Condition 1 often prevents hoisting from while loops: transform into repeat-until loops.

31 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

32 Induction Variables C-code for summation of a long array 1 long sum(long a[], int n) { 2 long s = 0; 3 int i = 0; 4 while (i<n) { 5 s += a[i]; 6 i ++; 7 } 8 return s; 9 }

33 Induction Variables and Strength Reduction Consider the corresponding IR s 0 i 0 L 1 : if i n goto L 2 j i 4 k j + a x M[k] s s + x i i + 1 goto L 1 L 2

34 Induction Variables and Strength Reduction Consider the corresponding IR s 0 i 0 L 1 : if i n goto L 2 j i 4 k j + a x M[k] s s + x i i + 1 goto L 1 L 2 before s 0 k a b n 4 c a + b L 1 : if k c goto L 2 x M[k ] s s + x k k + 4 goto L 1 L 2 after

35 Induction Variables Induction-variable analysis: identify induction variables and relations among them Strength reduction: replace expensive operation (e.g., multiplication) by cheap operation (e.g., addition) Induction-variable elimination: remove dependent induction variables

36 Induction Variables A basic induction variable is directly incremented A derived induction variable is computed from other induction variables Describe an induction variable b by a triple (b, o, f ), where b is a basic induction variable o is an offset f is a factor so that b = o + f b. A linear induction variable changes by the same amount in every iteration.

37 Induction Variables in the Example i is a basic induction variable described by (i, 0, 1) j is a derived induction variable: after j i 4, it is described by (i, 0, 4) k is a derived induction variable: after k j + a, it is described by (i, a, 4)

38 Non-linear Induction Variables s 0 L 1 : if s > 0 goto L 2 i i + b j i 4 x M[j] s s x goto L 1 L 2 : i i + 1 s s + j if i < n goto L 1

39 Non-linear Induction Variables s 0 L 1 : if s > 0 goto L 2 i i + b j i 4 x M[j] s s x goto L 1 L 2 : i i + 1 s s + j if i < n goto L 1 s 0 j i 4 b b 4 n n 4 L 1 : if s > 0 goto L 2 j j + b j j x M[j] s s x goto L 1 L 2 : j j + 4 s s + j if j < n goto L 1 before after

40 Detection of Induction Variables Basic Induction Variable (in the family of i) Variable i is a basic induction variable if all definitions of i in loop L have the form i i ± c where c is loop-invariant. Derived Induction Variable Variable k is a derived ind. var. in the family of i in loop L if 1 there is exactly one definition of k in L of the form k j c or k j + d where j is an induction variable in the family of i and c, d are loop-invariant 2 if j is a derived induction variable in the family of i, then only the definition of j in L reaches (the definition of) k there is no definition of i on any path between the definition of j and the definition of k 3 If j is described by (i, a, b), then k is described by (i, a c, b c) or (i, a + d, b), respectively.

41 Strength Reduction Often multiplication is more expensive than addition Replace the definition j i c of a derived induction variable by an addition Procedure For each derived induction variable j (i, a, b) create new variable j After each assignment i i + c to a basic induction variable, create an assignment j j + c b Replace assignment to j with j j Initialize j a + i b at end of preheader

42 Example Strength Reduction Induction Variables j (i, 0, 4) and k (i, a, 4) s 0 i 0 L 1 : if i n goto L 2 j i 4 k j + a x M[k] s s + x i i + 1 L 2 goto L 1 before s 0 i 0 j 0 k a L 1 : if i n goto L 2 j j k k x M[k] s s + x i i + 1 j j + 4 k k + 4 goto L 1 L 2 after

43 Elimination Apply constant propagation, copy propagation, and dead code elimination Special case: elimination of induction variables that are not used in the loop only used in comparisons with loop-invariant variables useless Useless variable A variable is useless in a loop L if it is dead at all exits from L it is only used in its own definitions Example After removal of j, j is useless

44 Rewriting Comparisons Almost useless variable A variable is almost useless in loop L if it is only used in comparisons against loop-invariant values and in definitions of itself and there is another induction variable in the same family that is not useless. An almost useless variable can be made useless by rewriting the comparisons to use the related induction variable

45 Rewriting Comparisons Coordinated induction variables Let x (i, a x, b x ) and y (i, a y, b y ) be induction variables. x and y are coordinated if (x a x )/b x = (y a y )/b y throughout the execution of the loop, except during a sequence of statements of the form z i z i + c i where c i is loop-invariant.

46 Rewriting Comparisons Let j (i, a j, b j ) and k (i, a k, b k ) be coordinated induction variables. Consider the comparison k < n with n loop-invariant. Using (j a j )/b j = (k a k )/b k the comparison can be rewritten as follows b k (j a j )/b j + a k < n b k (j a j )/b j < n a k j < (n a k )b j /b k + a j if b j /b k > 0 j > (n a k )b j /b k + a j if b j /b k < 0 where the right-hand sides are loop-invariant and their computation can be hoisted to the preheader.

47 Rewriting Comparisons Restrictions 1 (n a k )b j must be a multiple of b k 2 b j and b k must both be constants or loop invariants of known sign

48 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

49 Array-Bounds Checks Safe programming languages check that the subscript is within the array bounds at each array operation. Bounds for an array have the form 0 i < N where N > 0 is the size of the array. Implemented by i < u N (unsigned comparison). Bounds checks redundant in well-written programs slowdown For better performance: let the compiler prove which checks are redundant! In general, this problem is undecidable.

50 Assumptions for Bounds Check Elimination in Loop L 1 There is an induction variable j and loop-invariant u used in statement s 1 of either of the forms if j < u goto L 1 else goto L 2 if j u goto L 2 else goto L 1 if u > j goto L 1 else goto L 2 if u j goto L 2 else goto L 1 where L 2 is out of the loop L. 2 There is a statement s 2 of the form if k < u n goto L 3 else goto L 4 where k is an induction variable coordinated with j, n is loop-invariant, and s 1 dominates s 2. 3 No loop nested within L contains a definition of k. 4 k increases when j does: b j /b k > 0.

51 Array-Bounds Checking Objective Insert test in preheader so that 0 k < n in the loop. Lower Bound Let k 1,..., k m be the loop-invariant values added to k inside the loop k 0 everywhere in the loop if k 0 in the loop preheader k k m 0

52 Array-Bounds Checking Upper Bound Let k 1,..., k p be the set of loop-invariant values added to k on any path between s 1 and s 2 that does not go through s 1. k < n at s 2 if k < n ( k k p ) at s 1 From (k a k )/b k = (j a j )/b j this test can be rewritten to j < b j /b k (n ( k k p ) a k ) + a j It is sufficient that u b j /b k (n ( k k p ) a k ) + a j because the test j < u dominates the test k < n All parts of this test are loop-invariant!

53 Array-Bounds Checking Transformation Hoist loop-invariants out of the loop Copy the loop L to a new loop L with header label L h Replace the statement if k < u n goto L 3 else goto L 4 by goto L 3 At the end of L s preheader put statements equivalent to if k 0 k 1 0 k m 0 and u b j /b k (n ( k k p ) a k ) + a j goto L h else goto L h

54 Array-Bounds Checking Transformation This condition can be evaluated at compile time if 1 all loop-invariants in the condition are constants; or 2 n and u are the same temporary, a k = a j, b k = b j and no k s are added to k between s 1 and s 2. The second case arises for instance with code like this: 1 int u = a.length; 2 int i = 0; 3 while (i<u) { 4 sum += a[i]; 5 i++; 6 } assuming common subexpression elimination for a.length Compile-time evaluation of the condition means to unconditionally use L or L and delete the other loop Clean up with elimination of unreachable and dead code

55 Array-Bounds Checking Generalization Comparison of j u instead of j < u Loop exit test at end of loop body: A test s 2 : if j < u goto L 1 else goto L 2 where L 2 is out of the loop and s 2 dominates all loop back edges; the k i are between s 2 and any back edge and between the loop header and s 1 Handle the case b j /b k < 0 Handle the case where j counts downward and the loop exit tests for j l (a loop-invariant lower bound) The increments to the induction variable may be undisciplined with non-obvious increment: 1 while (i<n-1) { 2 if (sum<0) { i++; sum += i; i++ } else { i += 2; } 3 sum += a[i]; 4 }

56 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

57 Loop Unrolling For loops with small body, some time is spent incrementing the loop counter and testing the exit condition Loop unrolling optimizes this situation by putting more than one copy of the loop body in the loop To unroll a loop L with header h and back edges s i h: 1 Copy L to a new loop L with header h and back edges s i h 2 Change the back edges in L from s i h to s i h 3 Change the back edges in L from s i h to s i h

58 Loop Unrolling Example (Still Useless) L 1 : L 2 x M[i] s s + x i i + 4 if i < n goto L 1 else L 2 L 1 : x M[i] s s + x i i + 4 if i < n goto L 1 else L 2 1 x M[i] s s + x i i + 4 if i < n goto L 1 else L 2 L 2 before after

59 Loop Unrolling Improved No gain, yet Needed: induction variable i such that every increment i i + dominates every back edge of the loop each iteration increments i by the sum of the s increments and tests can be moved to the back edges of loop In general, a separate epilogue is needed to cover the remaining iterations because a loop that is unrolled K times can only do multiple-of-k iterations.

60 Loop Unrolling Example L 1 : x M[i] s s + x x M[i + 4] s s + x i i + 8 if i < n goto L 1 else L 2 L 2 only even numbers if i < n 4 goto L 1 else L 2 L 1 : x M[i] s s + x x M[i + 4] s s + x i i + 8 if i < n 4 goto L 1 else L 2 L 2 : if i < n goto L 2 else L 3 L 2 : x M[i] s s + x i i + 4 L 3 with epilogue

### Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

Loops or Lather, Rinse, Repeat CS4410: Spring 2013 Program Loops Reading: Appel Ch. 18 Loop = a computation repeatedly executed until a terminating condition is reached High-level loop constructs: While

### Lecture Notes on Loop Optimizations

Lecture Notes on Loop Optimizations 15-411: Compiler Design Frank Pfenning Lecture 17 October 22, 2013 1 Introduction Optimizing loops is particularly important in compilation, since loops (and in particular

### Control flow graphs and loop optimizations. Thursday, October 24, 13

Control flow graphs and loop optimizations Agenda Building control flow graphs Low level loop optimizations Code motion Strength reduction Unrolling High level loop optimizations Loop fusion Loop interchange

### Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 2 Local vs. Global Optimizations Local: inside a single basic block Simple forms of common subexpression elimination, dead code elimination,

### Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Compiler Optimizations Chapter 8, Section 8.5 Chapter 9, Section 9.1.7 2 Local vs. Global Optimizations Local: inside a single basic block Simple forms of common subexpression elimination, dead code elimination,

### Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Traditional Three-pass Compiler Source Code Front End IR Middle End IR Back End Machine code Errors Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce

### An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

An Overview of GCC Architecture (source: wikipedia) CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA Control-Flow Analysis and Loop Detection Last time Lattice-theoretic framework for data-flow

### Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Intermediate Representations CS2210 Lecture 11 Reading & Topics Muchnick: chapter 6 Topics today: Intermediate representations Automatic code generation with pattern matching Optimization Overview Control

### Lecture Notes on Loop-Invariant Code Motion

Lecture Notes on Loop-Invariant Code Motion 15-411: Compiler Design André Platzer Lecture 17 1 Introduction In this lecture we discuss an important instance of Partial Redundancy Elimination (PRE) and

CONTROL FLOW ANALYSIS The slides adapted from Vikram Adve Flow Graphs Flow Graph: A triple G=(N,A,s), where (N,A) is a (finite) directed graph, s N is a designated initial node, and there is a path from

### Intermediate representation

Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.

### Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Control Flow Analysis CS2210 Lecture 11 Reading & Topics Muchnick: chapter 7 Optimization Overview Control Flow Analysis Maybe start data flow analysis Optimization Overview Two step process Analyze program

### Loop Unrolling. Source: for i := 1 to 100 by 1 A[i] := A[i] + B[i]; endfor

Source: for i := 1 to 100 by 1 A[i] := A[i] + B[i]; Loop Unrolling Transformed Code: for i := 1 to 100 by 4 A[i ] := A[i ] + B[i ]; A[i+1] := A[i+1] + B[i+1]; A[i+2] := A[i+2] + B[i+2]; A[i+3] := A[i+3]

### Calvin Lin The University of Texas at Austin

Loop Invariant Code Motion Last Time SSA Today Loop invariant code motion Reuse optimization Next Time More reuse optimization Common subexpression elimination Partial redundancy elimination February 23,

### Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen Melikyan 1 Course Overview Introduction: Overview of Optimizations 1 lecture Intermediate-Code Generation 2 lectures Machine-Independent

### COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

1 COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013 Lecture Outline 1. Code optimization strategies 2. Peephole optimization 3. Common subexpression elimination

### COMS W4115 Programming Languages and Translators Lecture 24: Code Optimization April 21, 2014

1 COMS W4115 Programming Languages and Translators Lecture 24: Code Optimization April 21, 2014 Lecture Outline 1. Code optimization strategies 2. Peephole optimization 3. Common subexpression elimination

### Machine-Independent Optimizations

Chapter 9 Machine-Independent Optimizations High-level language constructs can introduce substantial run-time overhead if we naively translate each construct independently into machine code. This chapter

### Induction-variable Optimizations in GCC

Induction-variable Optimizations in GCC 程斌 bin.cheng@arm.com 2013.11 Outline Background Implementation of GCC Learned points Shortcomings Improvements Question & Answer References Background Induction

### We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places

### Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Register allocation IR instruction selection register allocation machine code errors Register allocation: have value in a register when used limited resources changes instruction choices can move loads

### Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

### Control Flow Graphs. (From Matthew S. Hetch. Flow Analysis of Computer Programs. North Holland ).

Control Flow Graphs (From Matthew S. Hetch. Flow Analysis of Computer Programs. North Holland. 1977. ). 38 Control Flow Graphs We will now discuss flow graphs. These are used for global optimizations (as

### USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

CS553 Compiler Construction Instructor: URL: Michelle Strout mstrout@cs.colostate.edu USC 227 Office hours: 3-4 Monday and Wednesday http://www.cs.colostate.edu/~cs553 CS553 Lecture 1 Introduction 3 Plan

### Control Flow Analysis & Def-Use. Hwansoo Han

Control Flow Analysis & Def-Use Hwansoo Han Control Flow Graph What is CFG? Represents program structure for internal use of compilers Used in various program analyses Generated from AST or a sequential

### Lecture Compiler Middle-End

Lecture 16-18 18 Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 What We Have Done A lot! Compiler Frontend Defining language Generating

### Control Flow Analysis

COMP 6 Program Analysis and Transformations These slides have been adapted from http://cs.gmu.edu/~white/cs60/slides/cs60--0.ppt by Professor Liz White. How to represent the structure of the program? Based

### Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

Challenges in the back end CS3300 - Compiler Design Basic Blocks and CFG V. Krishna Nandivada IIT Madras The input to the backend (What?). The target program instruction set, constraints, relocatable or

### CS553 Lecture Dynamic Optimizations 2

Dynamic Optimizations Last time Predication and speculation Today Dynamic compilation CS553 Lecture Dynamic Optimizations 2 Motivation Limitations of static analysis Programs can have values and invariants

### The Encoding Complexity of Network Coding

The Encoding Complexity of Network Coding Michael Langberg Alexander Sprintson Jehoshua Bruck California Institute of Technology Email: mikel,spalex,bruck @caltech.edu Abstract In the multicast network

### Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology

### ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

ECE 5775 (Fall 17) High-Level Digital Design Automation Static Single Assignment Announcements HW 1 released (due Friday) Student-led discussions on Tuesday 9/26 Sign up on Piazza: 3 students / group Meet

### Introduction to Machine-Independent Optimizations - 6

Introduction to Machine-Independent Optimizations - 6 Machine-Independent Optimization Algorithms Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course

### MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Program Analysis Compile-time reasoning about run-time behavior

### Compiler Design and Construction Optimization

Compiler Design and Construction Optimization Generating Code via Macro Expansion Macroexpand each IR tuple or subtree A := B+C; D := A * C; lw \$t0, B, lw \$t1, C, add \$t2, \$t0, \$t1 sw \$t2, A lw \$t0, A

### Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

Register allocation CS3300 - Compiler Design Liveness analysis and Register allocation V. Krishna Nandivada IIT Madras Copyright c 2014 by Antony L. Hosking. Permission to make digital or hard copies of

### Compiler Construction SMD163. Understanding Optimization: Optimization is not Magic: Goals of Optimization: Lecture 11: Introduction to optimization

Compiler Construction SMD163 Understanding Optimization: Lecture 11: Introduction to optimization Viktor Leijon & Peter Jonsson with slides by Johan Nordlander Contains material generously provided by

### Topic I (d): Static Single Assignment Form (SSA)

Topic I (d): Static Single Assignment Form (SSA) 621-10F/Topic-1d-SSA 1 Reading List Slides: Topic Ix Other readings as assigned in class 621-10F/Topic-1d-SSA 2 ABET Outcome Ability to apply knowledge

### Dataflow analysis (ctd.)

Dataflow analysis (ctd.) Available expressions Determine which expressions have already been evaluated at each point. A expression x+y is available at point p if every path from the entry to p evaluates

### CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2] 1 his Lecture 2 1 Remember: Phases of a Compiler his lecture: Code Optimization means floating point 3 What

### IR Optimization. May 15th, Tuesday, May 14, 13

IR Optimization May 15th, 2013 Tuesday, May 14, 13 But before we talk about IR optimization... Wrapping up IR generation Tuesday, May 14, 13 Three-Address Code Or TAC The IR that you will be using for

### Computing intersections in a set of line segments: the Bentley-Ottmann algorithm

Computing intersections in a set of line segments: the Bentley-Ottmann algorithm Michiel Smid October 14, 2003 1 Introduction In these notes, we introduce a powerful technique for solving geometric problems.

### Program Static Analysis. Overview

Program Static Analysis Overview Program static analysis Abstract interpretation Data flow analysis Intra-procedural Inter-procedural 2 1 What is static analysis? The analysis to understand computer software

### Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

### Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

LLVM Compiler Infrastructure Source: LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation by Lattner and Adve Reuse Optimization Eliminate redundant operations in the dynamic execution

### Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Lecture Outline Global Optimization Global flow analysis Global constant propagation Liveness analysis Compiler Design I (2011) 2 Local Optimization Recall the simple basic-block optimizations Constant

### Chapter 4 C Program Control

1 Chapter 4 C Program Control Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 4 C Program Control Outline 4.1 Introduction 4.2 The Essentials of Repetition

### Searching Algorithms/Time Analysis

Searching Algorithms/Time Analysis CSE21 Fall 2017, Day 8 Oct 16, 2017 https://sites.google.com/a/eng.ucsd.edu/cse21-fall-2017-miles-jones/ (MinSort) loop invariant induction Loop invariant: After the

### CprE 488 Embedded Systems Design. Lecture 6 Software Optimization

CprE 488 Embedded Systems Design Lecture 6 Software Optimization Joseph Zambreno Electrical and Computer Engineering Iowa State University www.ece.iastate.edu/~zambreno rcl.ece.iastate.edu If you lie to

### Static Single Assignment Form in the COINS Compiler Infrastructure

Static Single Assignment Form in the COINS Compiler Infrastructure Masataka Sassa (Tokyo Institute of Technology) Background Static single assignment (SSA) form facilitates compiler optimizations. Compiler

### Example of Global Data-Flow Analysis

Example of Global Data-Flow Analysis Reaching Definitions We will illustrate global data-flow analysis with the computation of reaching definition for a very simple and structured programming language.

### We assume uniform hashing (UH):

We assume uniform hashing (UH): the probe sequence of each key is equally likely to be any of the! permutations of 0,1,, 1 UH generalizes the notion of SUH that produces not just a single number, but a

### Partial Redundancy Analysis

Partial Redundancy Analysis Partial Redundancy Analysis is a boolean-valued data flow analysis that generalizes available expression analysis. Ordinary available expression analysis tells us if an expression

### Lecture 6 Foundations of Data Flow Analysis

Review: Reaching Definitions Lecture 6 Foundations of Data Flow Analysis I. Meet operator II. Transfer functions III. Correctness, Precision, Convergence IV. Efficiency [ALSU 9.3] Phillip B. Gibbons 15-745:

### Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

COMP 412 FALL 2017 Local Optimization: Value Numbering The Desert Island Optimization Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon,

### Having a BLAST with SLAM

Announcements Having a BLAST with SLAM Meetings -, CSCI 7, Fall 00 Moodle problems? Blog problems? Looked at the syllabus on the website? in program analysis Microsoft uses and distributes the Static Driver

### Hardware-Software Codesign. 9. Worst Case Execution Time Analysis

Hardware-Software Codesign 9. Worst Case Execution Time Analysis Lothar Thiele 9-1 System Design Specification System Synthesis Estimation SW-Compilation Intellectual Prop. Code Instruction Set HW-Synthesis

### 6. Intermediate Representation

6. Intermediate Representation Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/

### TR ON OPERATOR STRENGTH REDUCTION. John H. Crawford Mehd i Jazayer i

TR 80-003 ON OPERATOR STRENGTH REDUCTION John H. Crawford Mehd i Jazayer i l. ON OPERATOR STRENGTH REDUCTION John H. Crawford Intel Corporation 3065 Bowers Avenue Santa Clara, California Mehdi Jazayeri

### TRANSLATING BETWEEN PEGS AND CFGS

TRANSLATING BETWEEN PEGS AND CFGS ROSS TATE, MICHAEL STEPP, ZACHARY TATLOCK, AND SORIN LERNER Department of Computer Science and Engineering, University of California, San Diego e-mail address: {rtate,mstepp,ztatlock,lerner}@cs.ucsd.edu

### Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009 Note that I change the name of the functions slightly in these notes from what I used in class, to be consistent with

### Soot A Java Bytecode Optimization Framework. Sable Research Group School of Computer Science McGill University

Soot A Java Bytecode Optimization Framework Sable Research Group School of Computer Science McGill University Goal Provide a Java framework for optimizing and annotating bytecode provide a set of API s

### CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

CSE 501: Compiler Construction Course outline Main focus: program analysis and transformation how to represent programs? how to analyze programs? what to analyze? how to transform programs? what transformations

### Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

topological ordering, minimum spanning tree, Union-Find problem Jiří Vyskočil, Radek Mařík 2012 Subgraph subgraph A graph H is a subgraph of a graph G, if the following two inclusions are satisfied: 2

### Automatic Translation of Fortran Programs to Vector Form. Randy Allen and Ken Kennedy

Automatic Translation of Fortran Programs to Vector Form Randy Allen and Ken Kennedy The problem New (as of 1987) vector machines such as the Cray-1 have proven successful Most Fortran code is written

### Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

### Abstract Interpretation

Abstract Interpretation Ranjit Jhala, UC San Diego April 22, 2013 Fundamental Challenge of Program Analysis How to infer (loop) invariants? Fundamental Challenge of Program Analysis Key issue for any analysis

### Lecture 15 : Review DRAFT

CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.

### INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

### HW/SW Codesign. WCET Analysis

HW/SW Codesign WCET Analysis 29 November 2017 Andres Gomez gomeza@tik.ee.ethz.ch 1 Outline Today s exercise is one long question with several parts: Basic blocks of a program Static value analysis WCET

### DSP Mapping, Coding, Optimization

DSP Mapping, Coding, Optimization On TMS320C6000 Family using CCS (Code Composer Studio) ver 3.3 Started with writing a simple C code in the class, from scratch Project called First, written for C6713

### A Framework for Space and Time Efficient Scheduling of Parallelism

A Framework for Space and Time Efficient Scheduling of Parallelism Girija J. Narlikar Guy E. Blelloch December 996 CMU-CS-96-97 School of Computer Science Carnegie Mellon University Pittsburgh, PA 523

### Principles of Compiler Design

Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

### Review: Hoare Logic Rules

Review: Hoare Logic Rules wp(x := E, P) = [E/x] P wp(s;t, Q) = wp(s, wp(t, Q)) wp(if B then S else T, Q) = B wp(s,q) && B wp(t,q) Proving loops correct First consider partial correctness The loop may not

### Assignment 1 (concept): Solutions

CS10b Data Structures and Algorithms Due: Thursday, January 0th Assignment 1 (concept): Solutions Note, throughout Exercises 1 to 4, n denotes the input size of a problem. 1. (10%) Rank the following functions

### Having a BLAST with SLAM

Having a BLAST with SLAM Meeting, CSCI 555, Fall 20 Announcements Homework 0 due Sat Questions? Move Tue office hours to -5pm 2 Software Model Checking via Counterexample Guided Abstraction Refinement

### Parallel Program Graphs and their. (fvivek dependence graphs, including the Control Flow Graph (CFG) which

Parallel Program Graphs and their Classication Vivek Sarkar Barbara Simons IBM Santa Teresa Laboratory, 555 Bailey Avenue, San Jose, CA 95141 (fvivek sarkar,simonsg@vnet.ibm.com) Abstract. We categorize

### Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow

### CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

### Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

600.363 Introduction to Algorithms / 600.463 Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 14.1 Introduction Today we re going to talk about algorithms for computing shortest

### Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority

### Handling Loops in Bounded Model Checking of C Programs via k-induction

Software Tools for Technology Transfer manuscript No. (will be inserted by the editor) Handling Loops in Bounded Model Checking of C Programs via k-induction Mikhail Y. R. Gadelha, Hussama I. Ismail, and

### C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

### C: How to Program. Week /Mar/05

1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

### We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places

### Reading Assignment. Symbolic Evaluation/Execution. Move from Dynamic Analysis to Static Analysis. Move from Dynamic Analysis to Static Analysis

Reading Assignment Symbolic Evaluation/Execution *R.W. Floyd, "Assigning Meaning to Programs, Symposium on Applied Mathematics, 1967, pp. 19-32 (Appeared as volume 19 of Mathematical Aspects of Computer

### Data Flow Analysis and Computation of SSA

Compiler Design 1 Data Flow Analysis and Computation of SSA Compiler Design 2 Definitions A basic block is the longest sequence of three-address codes with the following properties. The control flows to

### (How Not To Do) Global Optimizations

(How Not To Do) Global Optimizations #1 One-Slide Summary A global optimization changes an entire method (consisting of multiple basic blocks). We must be conservative and only apply global optimizations

### Iteration. Side effects

Computer programming Iteration. Side effects Marius Minea marius@cs.upt.ro 17 October 2017 Assignment operators We ve used the simple assignment: lvalue = expression lvalue = what can be on the left of

### Compiler Optimization Intermediate Representation

Compiler Optimization Intermediate Representation Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology

### 1 Undirected Vertex Geography UVG

Geography Start with a chip sitting on a vertex v of a graph or digraph G. A move consists of moving the chip to a neighbouring vertex. In edge geography, moving the chip from x to y deletes the edge (x,

### Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

.. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar.. Shortest Path Problems Algorithms on Graphs: Part III Path in a graph. Let G = V,E be a graph. A path p = e 1,...,e k, e i E,

### 6.046 Recitation 1: Proving Correctness Bill Thies, Fall 2004 Outline

6.046 Recitation 1: Proving Correctness Bill Thies, Fall 2004 Outline Acknowledgement Much of these notes is adapted from David Liben Nowell s notes for the same subject (thanks David!) My contact information:

### 3 No-Wait Job Shops with Variable Processing Times

3 No-Wait Job Shops with Variable Processing Times In this chapter we assume that, on top of the classical no-wait job shop setting, we are given a set of processing times for each operation. We may select

### Lecture 4 Introduction to Data Flow Analysis

Lecture 4 Introduction to Data Flow Analysis I. Structure of data flow analysis II. Example 1: Reaching definition analysis III. Example 2: Liveness analysis IV. Generalization What is Data Flow Analysis?

### Chapter 2 The Language PCF

Chapter 2 The Language PCF We will illustrate the various styles of semantics of programming languages with an example: the language PCF Programming language for computable functions, also called Mini-ML.

### Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

### Lecture 4: Primal Dual Matching Algorithm and Non-Bipartite Matching. 1 Primal/Dual Algorithm for weighted matchings in Bipartite Graphs

CMPUT 675: Topics in Algorithms and Combinatorial Optimization (Fall 009) Lecture 4: Primal Dual Matching Algorithm and Non-Bipartite Matching Lecturer: Mohammad R. Salavatipour Date: Sept 15 and 17, 009