Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Similar documents
Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Lecture 9: Loop Invariant Computation and Code Motion

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Goals of Program Optimization (1 of 2)

Compiler Construction 2016/2017 Loop Optimizations

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

Compiler Construction 2010/2011 Loop Optimizations

Induction Variable Identification (cont)

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

Simone Campanoni Loop transformations

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Control flow and loop detection. TDT4205 Lecture 29

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

CS202 Compiler Construction

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Lecture Notes on Loop Optimizations

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

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

Intermediate representation

Lecture 8: Induction Variable Optimizations

Lecture Notes on Loop-Invariant Code Motion

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

Why Global Dataflow Analysis?

Overview of a Compiler

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

CSE P 501 Compilers. Loops Hal Perkins Spring UW CSE P 501 Spring 2018 U-1

Introduction to Optimization Local Value Numbering

Using Static Single Assignment Form

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Code Merge. Flow Analysis. bookkeeping

EECS 583 Class 8 Classic Optimization

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

Calvin Lin The University of Texas at Austin

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

CSCI Compiler Design

Tour of common optimizations

Optimization Prof. James L. Frankel Harvard University

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

Languages and Compiler Design II IR Code Optimization

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

COMPILER DESIGN - CODE OPTIMIZATION

Loop Transformations! Part II!

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

Topic 9: Control Flow

Machine-Independent Optimizations

Code Optimization. Code Optimization

Introduction to Machine-Independent Optimizations - 6

The View from 35,000 Feet

Intra-procedural Data Flow Analysis Introduction

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

CSC D70: Compiler Optimization Register Allocation

Compiler Optimization and Code Generation

A main goal is to achieve a better performance. Code Optimization. Chapter 9

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

Calvin Lin The University of Texas at Austin

Compiler Optimization

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

Overview of a Compiler

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

Lecture 3 Local Optimizations, Intro to SSA

Overview of a Compiler

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

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

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

Introduction to Machine-Independent Optimizations - 1

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

Syntactic Directed Translation

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Compiler Optimization Techniques

Register Allocation. Introduction Local Register Allocators

CS202 Compiler Construction

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

Control Flow Analysis & Def-Use. Hwansoo Han

Compilers. Optimization. Yannis Smaragdakis, U. Athens

CODE GENERATION Monday, May 31, 2010

Example. Example. Constant Propagation in SSA

Code Optimization Using Graph Mining

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Intermediate Representation (IR)

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

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

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Compiler Design Spring 2017

Autotuning. John Cavazos. University of Delaware UNIVERSITY OF DELAWARE COMPUTER & INFORMATION SCIENCES DEPARTMENT

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

COP5621 Exam 4 - Spring 2005

CSC D70: Compiler Optimization

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

Compiler Code Generation COMP360

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

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

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

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

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

More Loop Optimizations

Transcription:

Outline Loop Optimizations Induction Variables Recognition Induction Variables Combination of Analyses Copyright 2010, Pedro C Diniz, all rights reserved Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use 2 a(i,j) = 100*N + 10*i + j + x 3 4 a(i,j) = 100*N + 10*i + j + x a(i,j) = 100*N + 10*i + j + x 5 6 1

a(i,j) = t1 + 10*i + j + x a(i,j) = t1 + 10*i + j + x 7 8 a(i,j) = t1 + 10*i + j + x t2 = 10*i + x a(i,j) = t1 + 10*i + j + x 9 10 t2 = 10*i + x a(i,j) = t1 + t2 + j t2 = 10*i + x a(i,j) = t1 + t2 + j 11 12 2

loop iteration, move it out of the loop t2 = 10*i + x a(i,j) = t1 + t2 + j Opportunities for Loop Invariant Code Motion In User Code Complex Expressions Easily readable code, reduce # of variables After Compiler Optimizations Copy Propagation, Algebraic simplification Correctness and Profitability? Loop Should Execute at Least Once! 13 14 Usefulness of Loop Invariant Code Motion In many programs most of the execution happens in loops Reducing work inside a loop nest is very beneficial CSE of expression x instructions become x/2 LICM of expression x instructions become x/n Implementing Loop Invariant Code Motion An expression can be moved out of the loop if all its operands are invariant in the loop 15 16 Constants are invariant DUH!!! Constants are invariant All the definitions are outside the loop 17 18 3

All the definitions are outside the loop All the definitions are outside the loop x = f() x = f() y = g() z = z + x*y y = g() t = x * y z = z + t 19 20 Operand has only one reaching definition and that definition is loop invariant Operand has only one reaching definition and that definition is loop invariant 21 22 Operand has only one reaching definition and that definition is loop invariant Operand has only one reaching definition and that definition is loop invariant y = 100 * 5 Definition outside the loop, in a basic blocks that dominates the loop header 23 24 4

Operand has only one reaching definition and that definition is loop invariant if(i > p)then x = 10 else x = 5 Move Or Not To Move Statement can be moved only if All the Uses are Dominated by the Statement x = 5 i = i + x Clearly a single definition is a safe restrictions There could be many definition with the same value 25 26 Move Or Not To Move Move Or Not To Move Statement can be moved only if All the Uses are Dominated by the Statement Statement can be moved only if All the Uses are Dominated by the Statement The Exit of the Loop is Dominated by the Statement x = 5 x = f(i) x = 5 i = i + x Reaching Definitions (RD) Analysis computes all the definitions of x and y which may reach t = x OP y 27 28 Conditions for Code Motion Example: Correctness: Movement doesn t change semantics of the program Performance: Code is not slowed down A = B +C A = B +C E = 2 E = 3 Basic Ideas: Defines once and for all Control flow Other definitions Other uses 29 30 5

Example: Example: Conditions: Conditions: Defs of B and C outside the Loop Defs of B and C outside the Loop Uses of A dominated by Statement Uses of A dominated by Statement Exit Dominated by Statement Exit Dominated by Statement E = 2 E = 3 E = 2 E = 3 31 32 Example: Example: Conditions: Conditions: Defs of B and C outside the Loop Defs of B and C outside the Loop Uses of A dominated by Statement Exit Dominated by Statement Uses of A dominated by Statement Exit Dominated by Statement E = 2 E = 3 E = 2 E = 3 33 34 Other Issues Preserve dependencies between loop-invariant instructions when hoisting code out of the loop for (i=0; i<n; i++) { x = y+z; a[i] = 10*i + x*x; x = y+z; t = x*x; for(i=0; i<n; i++) } a[i] = 10*i + t; Nested loops: apply loop invariant code motion algorithm multiple times for (i=0; i<n; i++) for (j=0; j<m; j++) a[i][j] = x*x + 10*i + 100*j; t1 = x*x; for (i=0; i<n; i++) { t2 = t1+ 10*i; for (j=0; j<m; j++) a[i][j] = t2 + 100*j; } 35 36 6

a = p*q j = j + a*i a = p*q j = j + a*i 37 38 t1 = p*q t1 = p*q a = p*q j = j + a*i 39 40 t1 = p*q t1 = p*q 41 42 7

t3 = p*q t3 = p*q t1 = p*q t1 = t3 43 44 t3 = p*q t1 = t3 Algorithm for Loop Invariant Code Motion Observations Loop Invariant Operands are defined outside loop or invariant themselves Code Motion Not all loop invariant instructions can be moved to pre-header Why? Algorithm Find Invariant Expression Check Conditions for Code Motion Apply Code Transformation 45 46 Detecting Loop Invariant Computation Input: Basic Blocks and CFG, Dominator Relation, Loop Information Output: Instructions that are Loop Invariant InvSet = repeat for each instruction i InvSet if operands are constants, or have definitions outside the loop, or have exactly one definition d InvSet; then InvSet = InvSet U {i}; until no changes in InvSet; Code Motion Algorithm Given: a set of nodes in a loop Compute Reaching Definitions Compute Loop Invariant Computation Compute Dominators Find the exits of the loop, nodes with successors outside the loop Candidate Statement for Code Motion: Loop Invariant In blocks that dominate all the Exits of the Loop Assign to variable not assigned to elsewhere in the loop In blocks that dominate all blocks in the loop that use the variable assigned Perform a depth-first search of the blocks Move candidate to pre-header if all the invariant operations it depends on have been moved 47 48 8

More Examples More Aggressive Optimizations Gamble On: Most loops get executed Can we relax the constraint of dominating all exits? E = 2 E = 3 E = 3 E = A + D E = A + D D = exit D = exit Landing Pads While p do s ----> if p { } pre-header repeat statements until not p; 49 50 Outline Redundancy Elimination Important and Profitable Transformation Precise Definition and Algorithm for Loop Invariant Computation Precise Algorithm for Code Motion Combination of Several Analyses Use of Reaching Definitions Use Dominators Next: Combination with Loop Induction Variables Two Optimizations Common Sub-Expression Elimination (CSE) (LICM) Dead Code Elimination Many Others Value Numbering Partial Redundancy Elimination (PRE) Focus: Induction Variable Recognition & Elimination 51 52 Induction Variables in Loops What is an Induction Variable? For a given loop variable v is an induction variable iff Its value Changes at Every Iteration Is either Incremented or Decremented by a Constant Amount Either Compile-time Known or Symbolically Constant Classification: Basic Induction Variables A single assignment in the loop of the form x = x + constant Example: variable i in for i = 1 to 10 Derived Induction Variables A linear function of a basic induction variable Variable j in the loop assigned j = c 1 * i + c 2 Why Are Induction Variables Important? Pervasive in Computations that Manipulate Arrays Allow for Understanding of Data Access Patterns in Memory Access Support Transformations Tailored to Memory Hierarchy Can Be Eliminated with Strength Reduction Substantially reduce the weight of address calculations Combination with CSE Example: a(i,j) = b(i,j) t1 = @a(i,1) t2 = &b(i,1) *t1 = *t2 t1 += 8 t2 += 8? 53 54 9

Detection of Induction Variables Algorithm: Inputs: Loop L with Reaching Definitions and Loop Invariant Output: For each Induction Variable k the triple (i,c,d) st the value of k = i * c + d Find the Basic Induction Variables by Scanning the Loop L such that each Basic Induction Variable has (i,1,0) Search for variables k with a single assignment to k of the form: k = i * b, k = b*i, k = i/bwith b a constant and i is a Basic Induction Variable Check if the Assignment to k is dominated by the definitions for i If so, k is a Derived Induction Variable of i Strength Reduction & Induction Variables Idea of the Transformation Replace the Induction Variable in each Family by references to a common induction variable, the basic induction variable Exploit the Algebraic Properties for the update to the basic variable Algorithm outline foreach Basic Induction variable i do foreach Induction variable k: (i,c,d) in the family of i do create a new variable s replace the assignment to k by k = s after each assignment i = i + n where n is a constant append s = s + c * n place s in the family of induction variables of i end foreach initialize s to c*i + d on loop entry as either s = c * i followed by s = s + d (simplify if d = 0 or c = 1) end foreach 55 56 Detection of Induction Variables Example Detection of Induction Variables Example i = m - 1 j = n i = m - 1 j = n Basic Induction Variables: i in : single increment, (i,1,1) j in : single decrement (j,1,-1) t2 = 4 * i t2 = 4 * i t3 = a[t2] t3 = a[t2] j = j - 1 t4 = 4 * j j = j - 1 t4 = 4 * j t5 = a[t4] t5 = a[t4] if i < j goto B4 58 if i < j goto B4 57 Detection of Induction Variables Example Strength Reduction of Induction Variables i = m - 1 j = n t2 = 4 * i t3 = a[t2] j = j - 1 t4 = 4 * j t5 = a[t4] if i < j goto B4 Basic Induction Variables: i in : single increment, (i,1,1) j in : single decrement (j,1,-1) Derived Induction Variables t2 in : single assign (i,4,0) t4 in : single assign (j,4,0) i = m - 1 j = n s2 = 4 * i s4 = 4 * j s2 = s2 + 4 t2 = s2 t3 = a[t2] j = j - 1 s4 = s4-4 t4 = s4 t5 = a[t4] if i < j goto B4 Basic Induction Variables: i in : single increment, (i,1,1) j in : single decrement (j,1,-1) Derived Induction Variables t2 in : single assign (i,4,0) t4 in : single assign (j,4,0) Strength Reduction (for t4 in ) create s4 for the expression 4*j replace t4 = 4*j with t4 = s4 replace induction step for j with s4 = s4-4 where -4 comes from -1*c create initialization of s4 in pre-header 59 60 10

Eliminating Induction Variables After all the tricks we might be left with Code that uses the basic induction variables just for conditional including the loop control Given the linear relation between induction variables we can remove the basic induction variable by rewording the tests with a derived induction variable that is used in the code Check out dead statements (trivial is you use SSA) Check the initialization and remove induction variables Strength Reduction of Induction Variables i = m - 1 j = n s2 = 4 * i s4 = 4 * j s2 = s2 + 4 t2 = s2 t3 = a[s2] j = j - 1 s4 = s4-4 t4 = s4 t5 = a[s4] B4 if s2 < s4 goto Basic Induction Variables: i in : single increment, (i,1,1) j in : single decrement (j,1,-1) Derived Induction Variables t2 in : single assign (i,4,0) t4 in : single assign (j,4,0) Strength Reduction (for t4 in ) create s4 for the expression 4*j replace t4 = 4*j with t4 = s4 replace induction step for j with s4 = s4-4 where -4 comes from -1*c create initialization of s4 in pre-header Elimination of Induction Variables replace i < j with s2 < s4 copy propagate s2 and s4 61 62 Symbolic Propagation Strength Reduction of Induction Variables s2 = 4 * (m-1) s4 = 4 * n s2 = s2 + 4 t2 = s2 t3 = a[s2] s4 = s4-4 t4 = s4 t5 = a[s4] B4 if s2 < s4 goto Basic Induction Variables: i in : single increment, (i,1,1) j in : single decrement (j,1,-1) Derived Induction Variables t2 in : single assign (i,4,0) t4 in : single assign (j,4,0) Strength Reduction (for t4 in ) create s4 for the expression 4*j replace t4 = 4*j with t4 = s4 replace induction step for j with s4 = s4-4 where -4 comes from -1*c create initialization of s4 in pre-header Elimination of Induction Variables replace i < j with s2 < s4 copy propagate s2 and s4 Summary Important and Profitable Transformation Precise Definition and Algorithm for Loop Invariant Computation Precise Algorithm for Code Motion Induction Variables Change Values at Every Iteration of a Loop by a Constant amount Basic and Derived Induction Variables with Affine Relation Combination of Various Analyses and Transformations Dominators, Reaching Definitions Strength Reduction, Dead Code Elimination and Copy Propagation and Common Sub-Expression Elimination 63 64 11