Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes.

Slides:



Advertisements
Similar presentations
Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
Advertisements

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.
1 SSA review Each definition has a unique name Each use refers to a single definition The compiler inserts  -functions at points where different control.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Static Single Assignment (SSA) Form Jaeho Shin :00 ROPAS Weekly Show & Tell.
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.
Jeffrey D. Ullman Stanford University. 2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If.
- 1 - Dominator Tree BB0 BB1 BB2BB3 BB4 BB6 BB7 BB5 BB0 BB1 BB2BB3 BB4 BB6 BB5 BB7 BBDOM0 10,1 20,1,2 30,1,3 BBDOM 40,1,3,4 50,1,3,5 60,1,3,6 70,1,7 Dom.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Computer Science 313 – Advanced Programming Topics.
Stanford University CS243 Winter 2006 Wei Li 1 SSA.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Advanced Compiler Design – Assignment 1 SSA Construction & Destruction Michael Fäs (Original Slides by Luca Della Toffola)
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
CS745: SSA© Seth Copen Goldstein & Todd C. Mowry Static Single Assignment.
CS 201 Compiler Construction
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
Building SSA Form (A mildly abridged account) For the full story, see the lecture notes for COMP 512 (lecture 8) and.
Final Code Generation and Code Optimization.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Static Single Assignment
Building SSA Form (A mildly abridged account)
Efficiently Computing SSA
Machine-Independent Optimization
Factored Use-Def Chains and Static Single Assignment Forms
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
CS 201 Compiler Construction
CSC D70: Compiler Optimization Static Single Assignment (SSA)
Optimizations using SSA
Dominator Tree First BB is the root node, each node
Interval Partitioning of a Flow Graph
Final Code Generation and Code Optimization
EECS 583 – Class 7 Static Single Assignment Form
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/4/2019 CPEG421-05S/Topic5.
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/17/2019 CPEG421-05S/Topic5.
EECS 583 – Class 7 Static Single Assignment Form
Building SSA Harry Xu CS 142 (b) 04/22/2018.
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes

Assignments Reminders –project #3 is Now due Friday, June 8, 2007 at 5:00 PM In order to get the course graded, there will be no extensions –Final exam will be Tuesday June 12, 3:30pm to 5:20 pm Normal place FAB 150

Grading Project 1 Grading of Project 1 I ran 11 tests, where I observed the output of your code, looking for key values. This was worth 56 points I ran 22 coverage tests, where I was trying to cause your code to cause an error. These were worth 44 points. I added the two scores. I have included the transcript of the first 11 tests, each test tells what I was looking for, and then prints out the code you generated.

Static Single Assignment The SSA form of a program combines elements of both data-flow and control flow into a single analysis. Every variable is assigned only once, so the scope or reach of an assignment is the whole program. We do this by renaming (or numbering) every variable to the left of an assignment, then renaming uses appropriately We also need to add phi functions, that combine the mulitple possible values of (of the original variables before renaming) that reach each basic block. This is best seen by example:

Example x <- … y <- … while (x <100) x <- x+1 y <- y +x x 0 <- … y 0 <- … if (x 0 >= 100) goto next loop: x 1 <-  (x 0,x 2 ) y 1 <-  (y 0,y 2 ) x 2 <- x y 2 <- y 1 + x 2 if (x 2 <= 100) goto loop next: x 3 <-  (x 0,x 2 ) y 3 <-  (y 0,y 2 ) each  (x i ) chooses exactly one if the x i corresponding to the actual flow of control. we split the loop into two tests because in the test (x<100) the x could refer to either x 0 or x 2

Building SSA Algorithm –Walk over the code, for each assignment renumber the variables. –Do this basic-block by basic block, in a depth first manner. –Inside each block, keep a counter, k, and walk from top to bottom. when we reach x := e produce x k := e renamed To rename, replace all x’s with x k, until the next assignment. –For each basic block, add an n-way phi function if there n-predecessors to the block.

Example for 1 Basic Block x <- 0 x <- x + 3 y <- x – 5 z <- x-y x 0 <- 0 x 1 <- x y <- x – 5 z <- x-y next counter = 0 next counter = 1 x 0 <- 0 x 1 <- x y 2 <- x 1 – 5 z <- x - y next counter = 2 x 0 <- 0 x 1 <- x y 2 <- x 1 – 5 z 3 <- x 1 - y 2 next counter = 3

Adding phi nodes loop: x 1 <-  (x 0,x 2 ) y 1 <-  (y 0,y 2 ) x 2 <- x y 2 <- y 1 + x 2 if x 2 <= 100) goto loop x 0 <- … y 0 <- … if x 0 >= 100 goto next next: x3 <-  (x0,x2) y3 <-  (y0,y2) Note that both the second and third BB have two flow paths, so they get phi functions with two inputs

Problems This algorithm works But it tends to add too many phi nodes. Why? Because we add phi-nodes with n-inputs to the beginning of each basic block. We add one for each variable used in the block. But, not every path redefines every variable in the block. So we tend to have too many phi-nodes. We need an analysis that determines which paths define which variables.

Dominators In a control flow graph –(node i) `dominates` (node j) –if every path to j must also go through i This is useful in many optimizations such as SSA construction B0B0 B1B1 B2B2 B3B3 B4B4 B5B5 B6B6 B7B7

Computing Dominators We can compute dominators by using dataflow equations. Dom(n) = {n}  ( ∩ m  preds(n) Dom(m) ) This computes the complete set of blocks that dominates each block.

B0B0 B1B1 B2B2 B3B3 B4B4 B5B5 B6B6 B7B7 preds(0) = {} preds(1) = {0,7} preds(2) = {1} preds(3) = {1} preds(4) = {3} preds(5) ={3} preds(6) ={4,5} preds(7) = {6,2} Dom(n) = {n}  ( ∩m  preds(n) Dom(m) ) Dom(0) = {0} Dom(1) = {1} Dom(2) = {2} Dom(3) = {3} Dom(4) = {4} Dom(5) ={5} Dom(6) ={6} Dom(7) = {7} Dom(0) = {0} Dom(1) = {1,0} Dom(2) = {2,1,0} Dom(3) = {3,1,0} Dom(4) = {4,3,1,0} Dom(5) ={5,3,1,0} Dom(6) ={6,3,1,0} Dom(7) = {7,1,0}

Dominance Frontiers The dominance frontier of a node n, the DF(n). DF(n) includes the first nodes m i reachable from n that are not dominated by n. I.e. there is another path through the graph that could reach each m i, that does not flow through node n B0B0 B1B1 B2B2 B3B3 B4B4 B5B5 B6B6 B7B7

Domnance Frontier info to & phi nodes When inserting phi functions in SSA form. A definition of x ( x <- … ) in block B, forces a phi function at the start of every node in the DF(B) Blocks reachable from B, but not in the DF(B) have no other realizable paths, so phi functions are unnecessary. B0B0 B1B1 B2B2 B3B3 B4B4 B5B5 B6B6 B7B7