Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students.

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Operator Strength Reduction From Cooper, Simpson, & Vick, “Operator Strength Reduction”, ACM TOPLAS, 23(5), See also § of EaC2e. 1COMP 512,
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.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
SSA.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
SSA-Based Constant Propagation, SCP, SCCP, & the Issue of Combining Optimizations 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon,
Lazy Code Motion C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Loop Invariant Code Motion — classical approaches — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
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.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Improving Code Generation Honors Compilers April 16 th 2002.
Instruction Scheduling II: Beyond Basic Blocks Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Structural Data-flow Analysis Algorithms: Allen-Cocke Interval Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
First Principles (with examples from value numbering) C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Using SSA Dead Code Elimination & Constant Propagation C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Building SSA Form, III 1COMP 512, Rice University This lecture presents the problems inherent in out- of-SSA translation and some ways to solve them. Copyright.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Local Instruction Scheduling — A Primer for Lab 3 — Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Instruction Scheduling: Beyond Basic Blocks Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Eliminating Array Bounds Checks — and related problems — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved.
Introduction to Optimization
Parsing — Part II (Top-down parsing, left-recursion removal)
Local Instruction Scheduling
Introduction to Optimization
Factored Use-Def Chains and Static Single Assignment Forms
Code Generation Part III
Intermediate Representations
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Instruction Scheduling: Beyond Basic Blocks
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
Code Shape III Booleans, Relationals, & Control flow
Intermediate Representations
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
Code Generation Part III
Parsing — Part II (Top-down parsing, left-recursion removal)
Optimizations using SSA
Introduction to Optimization
Instruction Scheduling: Beyond Basic Blocks
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Algebraic Reassociation of Expressions COMP 512 Rice University Houston, Texas Fall 2003 P. Briggs & K.D. Cooper, “Effective Partial Redundancy Elimination,”
EECS 583 – Class 9 Classic and ILP Optimization
Code Generation Part II
The Partitioning Algorithm for Detecting Congruent Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Code Optimization.
Presentation transcript:

Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use.

COMP 512, Fall Operator Strength Reduction Consider the following loop What’s wrong with this picture Takes 3 operations to compute the address of a(i) On most machines, the integer multiply (t2) is slow sum = 0 do i = 1 to 100 sum = sum + a(i) dnd do loadI 0  r sum loadI 1  r i loadI 100  r 100 loop:subIr i, 1  r 1 multIr 1, 4  r 2 addIr a  r 3 loadr 3  r 4 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... } address of a(i)

COMP 512, Fall Operator Strength Reduction Consider the value sequences for the temporaries The only one we care about is r 3 We can compute it directly & cheaply loadI 0  r sum loadI 1  r i loadI 100  r 100 loop:subIr i, 1  r 1 multIr 1, 4  r 2 addIr a  r 3 loadr 3  r 4 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... r i = { 1, 2, 3, 4, … } r 1 = { 0, 1, 2, 3, … } r 2 = { 0, 4, 8, 12, … } r 3 = a+12, … }

COMP 512, Fall Operator Strength Reduction Computing r 3 directly yields From 8 operations in the loop to 6 operations No expensive multiply, just cheap adds loadI 0  r sum loadI 1  r i loadI 100  r 100 a  r 3 loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... r 3 = a+12, … } Still, we can do better... * address of a(i)

COMP 512, Fall Operator Strength Reduction Changing the loop’s exit test to use r 3 yields Address computation went from -,+,* to + Exit test went from +, cmp to cmp Loop body went from 8 operations to 5 operations Got rid of that expensive multiply, too loadI 0  r sum a  r 3 addIr 3, 396  r lim loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum cmp _LT r 3,r lim  r 5 cbr r 5  loop,exit exit:... r 3 = a+12, … } } Pretty good speed up for most machines 37.5% of ops in the loop

COMP 512, Fall Operator Strength Reduction Definition Strong form Replace series of multiplies with adds Weak form Replace single multiply with shifts and adds The Problem Its easy to see the transformation Its somewhat harder to automate the process Operator Strength Reduction is a transformation that replaces a strong (expensive) operator with a weaker (cheaper) operator

COMP 512, Fall Operator Strength Reduction Assumptions Low-level IR, such as ILOC, converted into SSA form Interpret SSA -form as a graph Terminology A strongly connected component ( SCC ) of a directed graph is a region where a path exists from each node to every other node A region constant ( RC ) of an SCC is an SCC -invariant value An induction variable ( IV ) of an SCC is one whose value only changes in the SCC when operations increment it by an RC or an IV, or when it is the destination of a COPY from another IV A candidate for reduction is an operation “x  y * z” where y, z  IV  RC and either y  IV or z  IV * Intuitively, we are interested in induction variables that are updated in a cyclic fashion. This creates the repetition from which OSR derives its benefits. The classic papers, however, define IVs this way. As you will see, our algorithm only finds IVs that form a cycle in the SSA graph

COMP 512, Fall Operator Strength Reduction loadI 0  r s0 loadI 1  r i0 loadI 100  r 100 loop:phir s0,r s2  r s1 phir i0,r i2  r i1 subIr i1, 1  r 1 multIr 1, 4  r 2 addIr a  r 3 loadr 3  r 4 add r 4,r s1  r s2 addIr i1, 1  r i2 cmp _LT r i2,r 100  r 5 cbr r 5  loop,exit exit:... Code in semi-pruned S SA FormS SA Form as a Graph { Short-lived temporary values { 0 Ø load Ø r4r4 r s0 r s1 r s2 r i0 r i1 r i2 r3r3 r2r2 r1r1 cmp _LT cbr r5r5 pc 100

COMP 512, Fall Operator Strength Reduction S SA form as a graph Each IV is an SCC Not every SCC is an IV x  RC if x is a constant or its definition dominates the SCC S SA simplifies O SR Find IV s with SCC finder Test operations in SCC Constant time test for RC > Constant or D OM Without SSA, need several passes 0 Ø load Ø r4r4 r s0 r s1 r s2 r i0 r i1 r i2 r3r3 r2r2 r1r1 cmp _LT cbr r5r5 pc 100

COMP 512, Fall Operator Strength Reduction Finding SCC s Use Tarjan’s algorithm Well-understood method Takes O(N+E ) time Useful property SCC popped only after all its external operands have been popped Reduce the SCC s as popped  | SCC | > 1  if its an IV, mark it  | SCC | = 1  try to reduce it DFS(n) n.DFSnum  nextDFSnum++ n.visited  true n.low  n.DFSnum push(n) for each o  { operands of n} if o.visited = false then DFS(o) n.low  min(n.low, o.low) if o.DFSnum < n.DFSnum and o  stack then n.low  min(n.low, o.DFSnum) if n.low = n.DFSnum then SCC  { } until x = n do x  pop() SCC  SCC  { x } We only need to add one line Process( SCC ) *

COMP 512, Fall Operator Strength Reduction What should Process(r) do? If r is one node, try to reduce it If r is a collection of nodes  Check to see if it is an IV  If so, reduce it & any ops that use it  If not, try to reduce the ops in r Process(r) if r has only one member, n then if n has the form x  IV x RC, x  RC x IV, x  IV ± RC, or x  RC + IV then Replace(n, IV, RC ) else n.header  NULL else ClassifyIV(r) Let’s tackle the easier problem first – ClassifyIV()

COMP 512, Fall Operator Strength Reduction ClassifyIV(r) header  first(r) for each node n  r if header  RPOnum > n.block  RPOnum then header  n.block for each node n  r if n.op is not one of { Ø, +, -, COPY } then r is not an induction variable else for each o  { operands of n } if o  r and not RCon(o,header) then r is not an induction variable if r is an induction variable then for each node n  r n.header  header else for each node n  r if n has the form x  IV x RC, x  RC x IV, x  IV ± RC, or x  RC + IV then Replace(n, IV, RC ) else n.header  NULL RCon(n,header) if n.op is loadI or n.block >> header then return true else return false { Find outer- most def { Reduce these ops >> means “strictly dominates”

COMP 512, Fall Operator Strength Reduction /* replace n with a COPY */ Replace(n,iv,rc) result  Reduce(n.op,iv,rc) Replace n with COPY from result n.header  iv.header /* create new IV & return its name */ Reduce(op,iv,rc) result  search(op,iv,rc) if result is not found then result  a new name add(op,iv,rc,result) newDef  copyDef(iv,result) for each operand o of newDef if o.header = iv.header then replace o with Reduce(op,o,rc) else if (opcode = x or newDef.op = Ø) then replace o with Apply(op,o,rc) return result * Returns name of op applied to iv and rc Clones the definition The Big Picture Reduce() creates a new IV, with appropriate range & increment For t3, in our example, would be range a a+396, with an increment of 4 Replace takes a candidate operation and rewrites it with a COPY from the new IV. It uses Reduce to create the IV. Net effect: replace a with a COPY from some new IV that runs a a+396 & increments by 4 on each iteration

COMP 512, Fall Operator Strength Reduction /* insert a new operation */ Apply(op,arg1,arg2) result  search(op,arg1,arg2) if result is not found then if (arg1.header ≠ NULL /*  IV */ & RCon(arg2,arg1.header) then result  Reduce(op,arg1,arg2) else if arg2.header ≠ NULL /*  IV */ & RCon(arg1,arg2.header) then result  Reduce(op,arg2,arg1) else result  a new name add(op,arg1,arg2,result) Choose a location to insert op Try constant folding Create newOp at the location newOper.header  NULL return result The Big Picture Apply takes an op & 2 args and inserts the corresponding operation into the code (if it isn’t already there). Uses >> on arg1 & arg2 to find a location Tries to reduce the operation Tries to simplify the operation Net effect: replace (i-1)*4+a with a COPY from some new IV that runs & increments by 4 on each iteration *

COMP 512, Fall Example And, most of this is dead... 0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 pc 1 + Ø 0 r a0 r a1 r a2 COPY r1r1 4 + Ø 0 r a3 r a4 r a5 COPY r2r2 4 + Ø r a6 r a7 r a8 COPY r3r3 100 *

COMP 512, Fall Example 0.0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 pc 4 + Ø r a6 r a7 r a8 COPY r3r3 This is dead, except for the comparison & branch. Need to reformulate them on r a8 100 The transformation to perform this simplification is called linear function test replacement *

COMP 512, Fall Linear Function Test Replacement Each time a new, reduced IV is created Add an LFTR edge from old IV to new IV Label edge with the opcode and RC of the reduction Walk the LFTR edges to accumulate the transformation Use transformation to rewrite the test

COMP 512, Fall Example pc 0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 1 + Ø 0 r a0 r a1 r a2 COPY r1r1 4 + Ø 0 r a3 r a4 r a5 COPY r2r2 4 + Ø r a6 r a7 r a8 COPY r3r3 100 x 4 a Follow the edges to find the right IV and to accumulate the transformation

COMP 512, Fall Example 1 + Ø 1 r i0 r i1 r i2 Now, this is dead, too Not dead ! r a8 0.0 Ø load + r4r4 r s0 r s1 r s2 4 + Ø r a6 r a7 COPY r3r3 cbr r5r5 pc a cmp _LT

COMP 512, Fall Example loadI 0  r sum a  r 3 addIr3, 396  r lim loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum cmp _LT r 3,r lim  r 5 cbr r 5  loop,exit exit:... And, we’re done.. r a8 0.0 Ø load + r4r4 r s0 r s1 r s2 4 + Ø r a6 r a7 COPY r3r3 cbr r5r5 pc a cmp _LT