Stanford University CS243 Winter 2006 Wei Li 1 Control Dependence.

Slides:



Advertisements
Similar presentations
Overview Structural Testing Introduction – General Concepts
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.
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.
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.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
I/O-Algorithms Lars Arge Fall 2014 September 25, 2014.
Stanford University CS243 Winter 2006 Wei Li 1 SSA.
Program Slicing. 2 CS510 S o f t w a r e E n g i n e e r i n g Outline What is slicing? Why use slicing? Static slicing of programs Dynamic Program Slicing.
Data Structures Using C++
CS 171: Introduction to Computer Science II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
Program Representations Xiangyu Zhang. CS590F Software Reliability Why Program Representations  Initial representations Source code (across languages).
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Program Representations Xiangyu Zhang. CS590Z Software Defect Analysis Program Representations  Static program representations Abstract syntax tree;
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
Data Flow Analysis Compiler Design Nov. 8, 2005.
1 Structures and Strategies for State Space Search 3 3.0Introduction 3.1Graph Theory 3.2Strategies for State Space Search 3.3Using the State Space to Represent.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Foundation of Computing Systems
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Graph Coverage (3). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section 2.2 ◦ Section
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 CMSC 341 Introduction to Trees Textbook sections:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Discrete Structures Li Tak Sing( 李德成 ) Lectures
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Fundamentals of Programming II Introduction to Trees
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Single-Source Shortest Paths
Distance and Routing Labeling Schemes in Graphs
Code Optimization Overview and Examples Control Flow Graph
CSE 421: Introduction to Algorithms
Interval Partitioning of a Flow Graph
Taken largely from University of Delaware Compiler Notes
Building SSA Harry Xu CS 142 (b) 04/22/2018.
Binary Trees.
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Stanford University CS243 Winter 2006 Wei Li 1 Control Dependence

CS243 Winter 2006 Stanford University 2 Control Dependence Introduction Introduction Formal Definition Formal Definition Optimal Control Dependence Computation Optimal Control Dependence Computation

CS243 Winter 2006 Stanford University 3 Example START a b c d END CFG b is control dependent on a c is control dependent on a a and d are control dependent on START

CS243 Winter 2006 Stanford University 4 Applications of CD Dead code elimination Dead code elimination Scheduling (hyper-block formation) Scheduling (hyper-block formation) Predication Predication …

CS243 Winter 2006 Stanford University 5 Simple Dead Code Elimination Mark inherently live statement live Mark inherently live statement live Store to memory, print, … Store to memory, print, … For each variable in these live statements, mark its definition statement live. For each variable in these live statements, mark its definition statement live. For each live statement, mark it live the node that it is control dependent on. For each live statement, mark it live the node that it is control dependent on. Remove everything that is not marked. Remove everything that is not marked.

CS243 Winter 2006 Stanford University 6 Example if (x > 0) { if (x > 0) { printf(“greater than zero”); printf(“greater than zero”); } The printf statement is inherently live. You also need to mark the “if (x>0)” live because the ‘print’ statement is control dependent on the ‘if’. The printf statement is inherently live. You also need to mark the “if (x>0)” live because the ‘print’ statement is control dependent on the ‘if’.

CS243 Winter 2006 Stanford University 7 Control Dependence Introduction Introduction Formal Definition Formal Definition Optimal Control Dependence Computation Optimal Control Dependence Computation

CS243 Winter 2006 Stanford University 8 Post-dominator Relation If X appears on every path from START to Y, then X dominates Y. If X appears on every path from START to Y, then X dominates Y. If X appears on every path from Y to END, then X postdominates Y. If X appears on every path from Y to END, then X postdominates Y. Postdominator Tree Postdominator Tree END is the root END is the root Any node Y other than END has ipdom(Y) as its parent Any node Y other than END has ipdom(Y) as its parent Parent, child, ancestor, descendant Parent, child, ancestor, descendant

CS243 Winter 2006 Stanford University 9 Control Dependence Relation There are two possible definitions. There are two possible definitions. Node w is control dependent on edge (u→v) if Node w is control dependent on edge (u→v) if w postdominates v w postdominates v If w ≠ u, w does not postdominate u If w ≠ u, w does not postdominate u Node w is control dependent on node u if there exists an edge u→v Node w is control dependent on node u if there exists an edge u→v w postdominates v w postdominates v If w ≠ u, w does not postdominate u If w ≠ u, w does not postdominate u

CS243 Winter 2006 Stanford University 10 Example S a b c d E CFG e E ? ? Pdom Tree abcde S→a b→c Control Dep Relation

CS243 Winter 2006 Stanford University 11 Example S a b c d E CFG e E e d S b c Pdom Tree a abcde S→a b→c Control Dep Relation

CS243 Winter 2006 Stanford University 12 Example S a b c d E CFG e E e d S b c Pdom Tree a abcde S→a √√√ b→c √√√ Control Dep Relation

CS243 Winter 2006 Stanford University 13 Control Dependence Queries CD(e): set of nodes control dependent on edge e CD(e): set of nodes control dependent on edge e CONDS(v): set of edges that node v is control dependent on CONDS(v): set of edges that node v is control dependent on

CS243 Winter 2006 Stanford University 14 Dominance Frontier Reverse control flow graph (RCFG) Reverse control flow graph (RCFG) Let X and Y be nodes in CFG. X in DF(Y) in CFG iff Y is control dependent on X in RCFG. Let X and Y be nodes in CFG. X in DF(Y) in CFG iff Y is control dependent on X in RCFG. DF(Y) in CFG = conds(Y) in RCFG, where conds(Y) is the set of nodes that Y is control dependent on. DF(Y) in CFG = conds(Y) in RCFG, where conds(Y) is the set of nodes that Y is control dependent on.

CS243 Winter 2006 Stanford University 15 Worst-case Size of CDR CFG abcd S→a d→a c→b Control Dependence Relation S a b c E d

CS243 Winter 2006 Stanford University 16 Worst-case Size of CDR CFG abcd S→a √√√√ d→a √√√√ c→b √√ Control Dependence Relation Size = O(n 2 ) S a b c E d

CS243 Winter 2006 Stanford University 17 Control Dependence Introduction Introduction Formal Definition Formal Definition Optimal Linear Control Dependence Computation Optimal Linear Control Dependence Computation

CS243 Winter 2006 Stanford University 18 APT APT: Augmented Postdominator Tree APT: Augmented Postdominator Tree which can be built in O(|E|) space and time which can be built in O(|E|) space and time which can be used to answer CD and CONDS queries in time proportional to output size which can be used to answer CD and CONDS queries in time proportional to output size Optimal control dependence computation Optimal control dependence computation Solution: reduced control computation to a graph problem called Roman Chariots Problem Solution: reduced control computation to a graph problem called Roman Chariots Problem

CS243 Winter 2006 Stanford University 19 Key Idea (I): Exploit Structure How to avoid building the entire control dependence relation (O(n 2 ))? How to avoid building the entire control dependence relation (O(n 2 ))? Nodes that are control dependent on an edge e form a simple path in the postdominator tree Nodes that are control dependent on an edge e form a simple path in the postdominator tree In a tree, a simple path is uniquely specified by its endpoints. In a tree, a simple path is uniquely specified by its endpoints. Pdom tree + endpoints of each control dependence path can be built in O(|E|) space and time Pdom tree + endpoints of each control dependence path can be built in O(|E|) space and time

CS243 Winter 2006 Stanford University 20 Example Epath S→a ? b→c ? S a b c d E CFG e Pdom Tree c E e d S b a Control Dep Relationabcde S→a √√√ b→c √√√ Path Array

CS243 Winter 2006 Stanford University 21 Example Epath S→a [a,e] b→c [c,b] S a b c d E CFG e Pdom Tree c E e d S b a Control Dep Relationabcde S→a √√√ b→c √√√ Path Array

CS243 Winter 2006 Stanford University 22 CD Queries How can we use the compact representation of the CDR (Control Dependence Relation) to answer queries for CD and CONDS sets in time proportional to output size? How can we use the compact representation of the CDR (Control Dependence Relation) to answer queries for CD and CONDS sets in time proportional to output size?

CS243 Winter 2006 Stanford University 23 Roman Chariots Problem CD(n): which cities are served by chariot n? CD(n): which cities are served by chariot n? CONDS(w): which chariots serve city w? CONDS(w): which chariots serve city w? Route # path I[Milano,Roma] II[Pompeii,Bologna] III[Venezia,Roma] Napoli Roma Milano Venezia Bologna Pompeii Verona Corleone

CS243 Winter 2006 Stanford University 24 CD(n): which cities are served by chariot n? Look up entry for chariot n in Route Array (say [x,y]) Look up entry for chariot n in Route Array (say [x,y]) Traverse nodes in tree T, starting at x and ending at y Traverse nodes in tree T, starting at x and ending at y Output all nodes encountered in traversal Output all nodes encountered in traversal Query time is proportional to output size Query time is proportional to output size

CS243 Winter 2006 Stanford University 25 CONDS(w): which chariots serve city w? For each chariot c in Route Array do For each chariot c in Route Array do Let route of c be [x,y]; Let route of c be [x,y]; If w is an ancestor of x and w is a descendant of y then If w is an ancestor of x and w is a descendant of y then Output c; Output c; Can we avoid examining all routes? Can we avoid examining all routes?

CS243 Winter 2006 Stanford University 26 Key Idea (II): Caching At each node in the tree, keep a list of chariot #’s whose bottom node is n. At each node in the tree, keep a list of chariot #’s whose bottom node is n. Chariot # route I[a,e] II[c,b] c E e d S b a {?}

CS243 Winter 2006 Stanford University 27 Key Idea (II): Caching At each node in the tree, keep a list of chariot #’s whose bottom node is n. At each node in the tree, keep a list of chariot #’s whose bottom node is n. Chariot # route I[a,e] II[c,b] c E e d S b a {I} {II}

CS243 Winter 2006 Stanford University 28 CONDS(w): which chariots serve city w? For each descendant d of w do For each descendant d of w do For each route c = [x,y] in list at d do For each route c = [x,y] in list at d do If w is a descendant of y then If w is a descendant of y then Output c; Output c; Query time is proportional to # of descendants + size of all lists at d Query time is proportional to # of descendants + size of all lists at d

CS243 Winter 2006 Stanford University 29 Sorting Lists Sort each list by decreasing length Sort each list by decreasing length Chariot # route I[e,d] II[e,c] III[e,b] IV[e,a] c E a d S b {IV,III,II,I} e

CS243 Winter 2006 Stanford University 30 CONDS(w): which chariots serve city w? For each descendant d of w do For each descendant d of w do For each route c = [x,y] in list at d do For each route c = [x,y] in list at d do If w is a descendant of y If w is a descendant of y then then Output c; Output c; else else break break Query time is proportional to size of output + # of descendants Query time is proportional to size of output + # of descendants

CS243 Winter 2006 Stanford University 31 Caching at All Nodes on Route Sort each list by decreasing length Sort each list by decreasing length c E a d b {IV,III,II,I} e c E a d b e {IV,III,II} {IV,III} {IV}

CS243 Winter 2006 Stanford University 32 Space Time Tradeoff Chariot # stored only at bottom node of the route Chariot # stored only at bottom node of the route Space: O(|V| + |A|) Space: O(|V| + |A|) Query Time: O(|V| + |Output|) Query Time: O(|V| + |Output|) Chariot # stored at all nodes on route Chariot # stored at all nodes on route Space: O(|V| * |A|) Space: O(|V| * |A|) Query Time: O(|Output|) Query Time: O(|Output|) V is the set of tree nodes, and A is the Route Array. V is the set of tree nodes, and A is the Route Array.

CS243 Winter 2006 Stanford University 33 Key idea (III): Caching Zones Divide tree into ZONES Divide tree into ZONES Nodes in Zone: Nodes in Zone: Boundary nodes: lowest nodes in zone Boundary nodes: lowest nodes in zone Interior nodes: all other nodes Interior nodes: all other nodes Query procedure: Query procedure: Visit only nodes below query node and in the same zone as query node Visit only nodes below query node and in the same zone as query node

CS243 Winter 2006 Stanford University 34 Caching Rule Boundary node: store all chariots serving node Boundary node: store all chariots serving node Interior node: store all chariots whose bottom node is that node Interior node: store all chariots whose bottom node is that node Algorithm: bottom-up, greedy zone construction Algorithm: bottom-up, greedy zone construction Query time: |A v | + |Z v | ≤ (α + 1) |A v | Query time: |A v | + |Z v | ≤ (α + 1) |A v | Space requirements ≤ |A| + |V| / α Space requirements ≤ |A| + |V| / α

CS243 Winter 2006 Stanford University 35 Constructing Zones (I) Invariant: for any node v, |Z v | ≤ α |A v | + 1, where α is a design parameter. Invariant: for any node v, |Z v | ≤ α |A v | + 1, where α is a design parameter. Query time for CONDS(v) Query time for CONDS(v) = O(|A v | + |Z v |) = O((α + 1)|A v | + 1) = O((α + 1)|A v | + 1) = O(|A v |) = O(|A v |)

CS243 Winter 2006 Stanford University 36 Constructing Zones (II) Build zones bottom-up, making them as large as possible without violating invariant Build zones bottom-up, making them as large as possible without violating invariant V is a leaf node, then make v a boundary node V is a leaf node, then make v a boundary node V is an interior node then V is an interior node then If (1 + ∑ u є children(v) |Z u |) > α |A v | + 1 If (1 + ∑ u є children(v) |Z u |) > α |A v | + 1 then make v a boundary node then make v a boundary node else make v an interior node else make v an interior node

CS243 Winter 2006 Stanford University 37 α = 1 (some caching) Zones: {a,b,c,d,e}, {f,g,h}, {E}, {S} Zones: {a,b,c,d,e}, {f,g,h}, {E}, {S} Boundary nodes: a, f, E, S Boundary nodes: a, f, E, S f c e b d {S→a, h→a} a g E h S α |Av| + 1 {g→b} {f→c} {e→d} {} {S→a, h→a, g→a, f→c} {}

CS243 Winter 2006 Stanford University 38 α = >>(no caching) Zones: {a,b,c,d,e,f,g,h}, {E}, {S} Zones: {a,b,c,d,e,f,g,h}, {E}, {S} Boundary nodes: a, E, S Boundary nodes: a, E, S ∞ {} f c e b d {S→a, h→a} a g E h S ∞ α |Av| + 1 {g→b} {f→c} {e→d} {} ∞ ∞ ∞ ∞ ∞ ∞

CS243 Winter 2006 Stanford University 39 α = << (full caching) Zones: every node Zones: every node Boundary nodes: every node Boundary nodes: every node f c e b d {S→a, h→a} a g E h S α |Av| + 1 {S→a, h→a, g→b} {S→a, h→a, g→b, f→c} {S→a, h→a, g→b, f→c, e→d} {S→a, h→a, g→a, f→c} {S→a, h→a, g→a} {S→a, h→a}

CS243 Winter 2006 Stanford University 40 APT (I) Postdominator tree with bidirectional edges Postdominator tree with bidirectional edges dfs-number[v]: integer dfs-number[v]: integer Used for ancestorship determination in CONDS query Used for ancestorship determination in CONDS query Boundary?[v]: boolean Boundary?[v]: boolean True if v is a boundary node, false otherwise True if v is a boundary node, false otherwise Used in CONDS query Used in CONDS query

CS243 Winter 2006 Stanford University 41 APT (II) L[v]: list pf chariots #’s/control dependences L[v]: list pf chariots #’s/control dependences Boundary node: all chariots serving v (all control dependences of v) Boundary node: all chariots serving v (all control dependences of v) Interior node: all chariots whose bottom node is v (all immediate control dependences of v) Interior node: all chariots whose bottom node is v (all immediate control dependences of v) Used in CONDS query Used in CONDS query

CS243 Winter 2006 Stanford University 42 Computational Complexity Query time: (α + 1) * output-size Query time: (α + 1) * output-size Space: |E| + |V|/α Space: |E| + |V|/α

CS243 Winter 2006 Stanford University 43 Reference “Optimal Control Dependence Computation and the Roman Chariots Problem”, Keshav Pingali, Gianfranco Bilardi, ACM Transactions on Programming Languages and Systems (TOPLAS), May pers/TOPLAS1997.pdf “Optimal Control Dependence Computation and the Roman Chariots Problem”, Keshav Pingali, Gianfranco Bilardi, ACM Transactions on Programming Languages and Systems (TOPLAS), May pers/TOPLAS1997.pdf