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

Similar documents
Control Flow Analysis

Computing Static Single Assignment (SSA) Form. Control Flow Analysis. Overview. Last Time. Constant propagation Dominator relationships

Static Single Assignment (SSA) Form

Calvin Lin The University of Texas at Austin

Induction Variable Identification (cont)

Control Flow Analysis

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

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

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

Control Flow Analysis & Def-Use. Hwansoo Han

Control-Flow Analysis

Control flow and loop detection. TDT4205 Lecture 29

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

Example. Example. Constant Propagation in SSA

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

Calvin Lin The University of Texas at Austin

Intermediate representation

Lecture 23 CIS 341: COMPILERS

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Example (cont.): Control Flow Graph. 2. Interval analysis (recursive) 3. Structural analysis (recursive)

Compiler Construction 2010/2011 Loop Optimizations

Lecture 8: Induction Variable Optimizations

Compiler Construction 2016/2017 Loop Optimizations

Review; questions Basic Analyses (2) Assign (see Schedule for links)

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

Topic 9: Control Flow

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

Introduction to Machine-Independent Optimizations - 6

Lecture 9: Loop Invariant Computation and Code Motion

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

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

Module 14: Approaches to Control Flow Analysis Lecture 27: Algorithm and Interval. The Lecture Contains: Algorithm to Find Dominators.

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

Using Static Single Assignment Form

Contents of Lecture 2

CS202 Compiler Construction

Static single assignment

SSA. Stanford University CS243 Winter 2006 Wei Li 1

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

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

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

Compiler Optimisation

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

CS153: Compilers Lecture 23: Static Single Assignment Form

4/1/15 LLVM AND SSA. Low-Level Virtual Machine (LLVM) LLVM Compiler Infrastructure. LL: A Subset of LLVM. Basic Blocks

Lecture Notes on Loop Optimizations

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

6. Intermediate Representation

6. Intermediate Representation!

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

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

CS 406/534 Compiler Construction Putting It All Together

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

Lecture 2: Control Flow Analysis

Control-Flow Analysis and Loop Detection

Intermediate Representation (IR)

CSC D70: Compiler Optimization Register Allocation

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

Data Flow Analysis and Computation of SSA

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

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

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

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

Lecture 6. Register Allocation. I. Introduction. II. Abstraction and the Problem III. Algorithm

Lecture 3 Local Optimizations, Intro to SSA

Combining Analyses, Combining Optimizations - Summary

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

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

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

Lecture 10 Notes Linked Lists

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

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

8. Static Single Assignment Form. Marcus Denker

Goals of Program Optimization (1 of 2)

EECS 583 Class 8 Classic Optimization

Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

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

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

Lecture Notes on Loop-Invariant Code Motion

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

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

Sta$c Single Assignment (SSA) Form

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

Introduction to Machine-Independent Optimizations - 4

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

Program Optimizations using Data-Flow Analysis

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions

Functional programming languages

CS553 Lecture Generalizing Data-flow Analysis 3

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Lecture 25 CIS 341: COMPILERS

Lecture 10 Notes Linked Lists

CS553 Lecture Dynamic Optimizations 2

Register Allocation. CS 502 Lecture 14 11/25/08

register allocation saves energy register allocation reduces memory accesses.

Transcription:

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 analysis Today Control-flow analysis Loops Identifying loops using dominators Converting to SSA using dominators Dominators and PA2 CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 2

Context Data-flow Flow of data values from defs to uses Could alternatively be represented as a data dependence Control-flow Sequencing of operations Could alternatively be represented as a control dependence e.g., Evaluation of then-code and else-code depends on if-test CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 3 Why study control flow analysis? Finding Loops most computation time is spent in loops to optimize them, we need to find them Loop Optimizations Loop-invariant code hoisting Induction variable elimination Array bounds check removal Loop unrolling Parallelization... Identifying structured control flow can be used to speed up data-flow analysis CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 4

Representing Control-Flow High-level representation Control flow is implicit in an AST Low-level representation: Use a Control-flow graph Nodes represent statements Edges represent explicit flow of control Other options Control dependences in program dependence graph (PDG) [Ferrante87] Dependences on explicit state in value dependence graph (VDG) [Weise 94] CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 5 What Is Control-Flow Analysis? Control-flow analysis discovers the flow of control within a procedure (e.g., builds a CFG, identifies loops) a := 0 Example b := a * b a := 0 2 b := a * b 3 L: c := b/d 4 if c < x goto L2 5 e := b / c 6 f := e + 7 L2: g := f 8 h := t - g 9 if e > 0 goto L3 0 goto L L3: return 3 c := b/d c < x? 7 g := f h := t - g e > 0? CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 6 No 5 e := b / c f := e + Yes 0 goto return

Loop Concepts Loop: Strongly connected subgraph of CFG with a single entry point (header) Loop entry edge: Source not in loop & target in loop Loop exit edge: Source in loop & target not in loop Loop header node: Target of loop entry edge. Dominates all nodes in loop. Back edge: Target is loop header & source is in the loop Natural loop: Associated with each back edge. Nodes dominated by header and with path to back edge without going through header Loop tail node: Source of back edge Loop preheader node: Single node that s source of the loop entry edge Nested loop: Loop whose header is inside another loop CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 7 Picturing Loop Terminology back edge preheader head entry edge loop tail exit edge CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 8

The Value of Preheader Nodes Not all loops have preheaders Sometimes it is useful to create them h Without preheader node There can be multiple entry edges With single preheader node There is only one entry edge t p h pre-header Useful when moving code outside the loop Don t have to replicate code for multiple entry edges t CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 9 Identifying Loops Why? Most execution time spent in loops, so optimizing loops will often give most benefit Many approaches Interval analysis Exploit the natural hierarchical structure of programs Decompose the program into nested regions called intervals Structural analysis: a generalization of interval analysis Identify dominators to discover loops We ll focus on the dominator-based approach CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 0

Dominator Terminology Dominators d dom i if all paths from entry to node i include d Strict dominators d sdom i if d dom i and d i entry d i d dom i entry Immediate dominators a idom b if a sdom b and there does not exist a node c such that c a, c b, a dom c, and c dom b not c, a sdom c and c sdom b Post dominators i p pdom i if every possible path from i to exit includes p (p dom i in the flow graph whose arcs are reversed and entry and exit are interchanged) p a idom b p pdom i a b CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA exit Identifying Natural Loops with Dominators Back edges A back edge of a natural loop is one whose target dominates its source Natural loop The natural loop of a back edge (m n), where n dominates m, is the set of nodes x such that n dominates x and there is a path from x to m not containing n Example SCC with c and d not a loop because has two entry points c a b d e CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 2 t s n m The target, c, of the edge (d c) does not dominate its source, d, so (d c) does not define a natural loop back edge natural loop a b c d e

Computing Dominators Input: Set of nodes N (in CFG) and an entry node s Output: Dom[i] = set of all nodes that dominate node i Dom[s] = {s} for each n N {s} Dom[n] = N repeat change = false for each n N {s} D = {n} ( p pred(n) Dom[p]) if D Dom[n] change = true Dom[n] = D until!change Key Idea If a node dominates all predecessors of node n, then it also dominates node n p p 2 p 3 CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 3 n pred[n] x Dom(p ) ^ x Dom(p 2 ) ^ x Dom(p 3 ) x Dom(n) Computing Dominators (example) Input: Set of nodes N and an entry node s Output: Dom[i] = set of all nodes that dominate node i s {s} Dom[s] = {s} for each n N {s} Dom[n] = N repeat change = false for each n N {s} D = {n} ( p pred(n) Dom[p]) if D Dom[n] change = true Dom[n] = D until!change {n, p, q, r, s} q r {n, p, q, r, s} n {n, p, q, r, s} Initially Dom[s] = {s} Dom[q] = {n, p, q, r, s}... Finally CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 4 p Dom[q] = {q, s} Dom[r] = {r, s} Dom[p] = {p, s} Dom[n] = {n, p, s} {n, p, q, r, s}

Recall SSA, Another use of dominator information Advantage Allow analyses and transformations to be simpler & more efficient/effective Disadvantage May not be executable (requires extra translations to and from) May be expensive (in terms of time or space) Process Original Code (RTL) Optimized Code (RTL) T T2 SSA Code SSA Code2 SSA Code3 CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 5 Static Single Assignment (SSA) Form Idea Each variable has only one static definition Makes it easier to reason about values instead of variables Similar to the notion of functional programming Transformation to SSA Rename each definition Rename all uses reached by that assignment Example v :=...... :=... v... v :=...... :=... v... What do we do when there s control flow? v 0 :=...... :=... v 0... v :=...... :=... v... CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 6

SSA and Control Flow Problem A use may be reached by several definitions 2 v :=... 3 v :=... 4...v... 2 v 0 :=... 3 v :=... 4...v?... CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 7 SSA and Control Flow (cont) Merging Definitions φ-functions merge multiple reaching definitions Example 2 v 0 :=... 3 v :=... 4 v 2 := φ(v 0,v )...v 2... CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 8

Another Example v := v 0 := 2 v := v+ 2 v := φ(v 0,v 2 ) v 2 := v + CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 9 Transformation to SSA Form Two steps Insert φ-functions Rename variables CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 20

Where Do We Place φ-functions? Basic Rule If two distinct (non-null) paths x z and y z converge at node z, and nodes x and y contain definitions of variable v, then a φ-function for v is inserted at z x v :=... y v 2 :=... z v 3 := φ(v,v 2 )...v 3... CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 2 Machinery for Placing φ-functions Recall Dominators d dom i if all paths from entry to node i include d d sdom i if d dom i and d i entry d d dom i Dominance Frontiers The dominance frontier of a node d is the set of nodes that are just barely not dominated by d; i.e., the set of nodes n, such that d dominates a predecessor p of n, and d does not strictly dominate n DF(d) = {n p pred(n), d dom p and d!sdom n} i Notational Convenience DF(S) = n S DF(n) CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 22

Dominance Frontier Example DF(d) = {n p pred(n), d dom p and d!sdom n} Dom(5) = {5, 6, 7, 8} DF(5) = {4, 5, 2, 3} Nodes in Dom(5) 2 5 9 3 6 7 0 4 8 2 3 What s significant about the Dominance Frontier? In SSA form, definitions must dominate uses CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 23 Dominance Frontier Example II DF(d) = {n p pred(n), d dom p and d!sdom n} Dom(5) = {5, 6, 7, 8} DF(5) = {4, 5, 3} Nodes in Dom(5) 2 5 3 6 7 4 8 3 In this graph, node 4 is the first point of convergence between the entry and node 5, so do we need a φ- function at node 3? CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 24

SSA Exercise v :=... 2 3 7 3 4 8 v :=... 9 v :=... 2 5 0 v 4 :=φ(v,v 2 ) 6 v 5 :=φ(v 3,v 4 ) DF(8) = DF(9) = DF(2) = DF({8,9}) = DF(0) = {0} {0} {6} {0} {6} DF(d) = {n p pred(n), d dom p and d!sdom n} DF({2,8,9,6,0}) = {6,0} See http://www.hipersoft.rice.edu/grads/publications/dom4.pdf for a more thorough description of DF. CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 25 Dominance Frontiers Revisited Suppose that node 3 defines variable x DF(3) = {5} x Def(3) 2 3 4 5 Do we need to insert a φ- function for x anywhere else? Yes. At node 6. Why? 6 CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 26

Dominance Frontiers and SSA Let DF (S) = DF(S) DF i+ (S) = DF(S DF i (S)) Iterated Dominance Frontier DF (S) Theorem If S is the set of CFG nodes that define variable v, then DF (S) is the set of nodes that require φ-functions for v CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 27 Dominance Tree Example The dominance tree shows the dominance relation 2 5 9 2 4 5 3 9 2 3 6 7 0 3 6 8 7 0 4 8 2 Dominance Tree 3 CFG CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 28

Inserting Phi Nodes Calculate the dominator tree a lot of research has gone into calculating this quickly Computing dominance frontier from dominator tree DF local [n]= successors of n (in CFG) that are not strictly dominated by n DF up [n]= nodes in the dominance frontier of n that are not strictly dominated by n s immediate dominator DF[n] = DF local [n] DF up [c] c children[n] CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 29 Algorithm for Inserting φ-functions for each variable v WorkList EverOnWorkList AlreadyHasPhiFunc for each node n containing an assignment to v Put all defs of v on the worklist WorkList WorkList {n} EverOnWorkList WorkList while WorkList Remove some node n for WorkList for each d DF(n) if d AlreadyHasPhiFunc Insert at most one φ function per node Insert a φ-function for v at d AlreadyHasPhiFunc AlreadyHasPhiFunc {d} if d EverOnWorkList Process each node at most once WorkList WorkList {d} EverOnWorkList EverOnWorkList {d} CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 30

Transformation to SSA Form Two steps Insert φ-functions Rename variables CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 3 Variable Renaming Basic idea When we see a variable on the LHS, create a new name for it When we see a variable on the RHS, use appropriate subscript Easy for straightline code x = = x x = = x x 0 = = x 0 x = = x Use a stack when there s control flow For each use of x, find the definition of x that dominates it x = x 0 = Traverse the dominance tree = x = x 0 CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 32

Variable Renaming (cont) Data Structures Stacks[v] v Holds the subscript of most recent definition of variable v, initially empty Counters[v] v Holds the current number of assignments to variable v; initially 0 Auxiliary Routine procedure GenName(variable v) i := Counters[v] push i onto Stacks[v] Counters[v] := i + 2 4 5 3 9 2 3 6 8 7 0 Use the Dominance Tree to remember the most recent definition of each variable CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 33 Variable Renaming Algorithm procedure Rename(block b) if b previously visited return Call Rename(entry-node) for each statement s in b (in order) for each variable v RHS(s) (except for φ-functions) replace v by v i, where i = Top(Stacks[v]) for each variable v LHS(s) GenName(v) and replace v with v i, where i=top(stack[v]) for each s succ(b) (in CFG) j position in s s φ-function corresponding to block b for each φ-function p in s replace the j th operand of RHS(p) by v i, where i = Top(Stack[v]) for each s child(b) (in DT) Rename(s) for each φ-function or statement t in b for each v i LHS(t) Pop(Stack[v]) Recurse using Depth First Search Φ(,, ) Unwind stack when done with this node CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 34

Transformation from SSA Form Proposal Restore original variable names (i.e., drop subscripts) Delete all φ-functions Complications (the proposal doesn t work!) What if versions get out of order? (simultaneously live ranges) x 0 = x = = x 0 = x Alternative Perform dead code elimination (to prune φ-functions) Replace φ-functions with copies in predecessors Rely on register allocation coalescing to remove unnecessary copies CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 35 PA2 and Dominators Why might you be getting Instruction does not dominate all uses! error? CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 36

Next Time Reading Advanced Compiler Optimizations for Supercomputers by Padua and Wolfe Lecture Dependencies in loops Parallelization and Performance Optimization of Applications CS553 Lecture Control-Flow, Dominators, Loop Detection, and SSA 37