CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

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

Advanced Compiler Construction

Goals of Program Optimization (1 of 2)

Control-Flow Analysis

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

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

Regions and Other Concepts

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

Control Flow Analysis

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

Compiler Construction 2016/2017 Loop Optimizations

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

3/18/18. Program Analysis. CYSE 411/AIT 681 Secure Software Engineering. Learning Goal. Program Analysis on Security. Why Program Representations

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

Control Flow Analysis

CYSE 411/AIT 681 Secure Software Engineering Topic #14. Program Analysis

Compiler Construction 2010/2011 Loop Optimizations

Control Flow Analysis & Def-Use. Hwansoo Han

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

Intermediate representation

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

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

EECS 583 Class 2 Control Flow Analysis LLVM Introduction

1 Digraphs. Definition 1

Contents of Lecture 2

Lecture 22 Tuesday, April 10

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

ELEC 876: Software Reengineering

Lecture 23 CIS 341: COMPILERS

Lecture 2: Control Flow Analysis

Intermediate Representation (IR)

Introduction to LLVM compiler framework

CFG (Control flow graph)

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

Code Optimization Using Graph Mining

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

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

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

VRP in LLVM. The PredicateSimplifier pass. by Nick Lewycky

1 Matchings in Graphs

Control flow and loop detection. TDT4205 Lecture 29

Compiler Optimisation

Introduction to LLVM compiler framework

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

Lecture 3 Local Optimizations, Intro to SSA

CHAPTER 3 FUZZY RELATION and COMPOSITION

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

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

CS 510/12. Xiangyu Zhang

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

Graph Theory S 1 I 2 I 1 S 2 I 1 I 2

1 Connected components in undirected graphs

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Binary Relations McGraw-Hill Education

Math 776 Graph Theory Lecture Note 1 Basic concepts

LLVM and IR Construction

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

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

Network Design and Optimization course

Constraint Satisfaction Problems

Finding Strongly Connected Components

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

Lecture 19 Thursday, March 29. Examples of isomorphic, and non-isomorphic graphs will be given in class.

Reverse Engineering Techniques

Data Flow Analysis. Program Analysis

Lecture 8: Induction Variable Optimizations

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

Module 11. Directed Graphs. Contents

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

Data Structure - Binary Tree 1 -

A Simplified Correctness Proof for a Well-Known Algorithm Computing Strongly Connected Components

Introduction to Machine-Independent Optimizations - 4

Advanced Compiler Construction

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

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

NP-Completeness. Algorithms

Decompilation of LLVM IR

Lecture 21 CIS 341: COMPILERS

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Strongly Connected Components

CS 406/534 Compiler Construction Putting It All Together

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

BINTEST Binary Search-based Test Case Generation

Matching Theory. Figure 1: Is this graph bipartite?

LOGIC SYNTHESIS AND VERIFICATION ALGORITHMS. Gary D. Hachtel University of Colorado. Fabio Somenzi University of Colorado.

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Math 778S Spectral Graph Theory Handout #2: Basic graph theory

Decompilation of Binary Programs & Structuring Decompiled Graphs

Paths, Flowers and Vertex Cover

Foundations of Computer Science Spring Mathematical Preliminaries

CS 173 Lecture 18: Relations on a Set

Intermediate representations IR #1: CPS/L 3. Code example. Advanced Compiler Construction Michel Schinz

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

SSI Revisited. Benoit Boissinot, Philip Brisk, Alain Darte, Fabrice Rastello. HAL Id: inria

Simple and Efficient Construction of Static Single Assignment Form

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Chordal Graphs: Theory and Algorithms

Transcription:

CONTROL FLOW ANALYSIS The slides adapted from Vikram Adve

Flow Graphs Flow Graph: A triple G=(N,A,s), where (N,A) is a (finite) directed graph, s N is a designated initial node, and there is a path from node s to every node n N. An entry node in a flow graph has no predecessors. An exit node in a flow graph has no successors. There is exactly one entry node, s. We can modify a general DAG to ensure this. How? In a control flow graph, any node unreachable from s can be safely deleted. Why? Control flow graphs are usually sparse. I.e., A = O( N ). In fact, if only binary branching is allowed A 2 N.

Control Flow Graph (CFG) Basic Block is a sequence of statements S 1... S n such that execution control must reach S 1 before S 2, and, if S 1 is executed, then S 2... S n are all executed in that order Unless a statement causes the program to halt Leader is the first statement of a basic block Maximal Basic Block is a basic block with a maximum number of statements (n)

Control Flow Graph (CFG) CFG is a directed graph in which: Each node is a single basic block There is an edge b1 b2 if block b2 may be executed after block b1 in some execution We define it typically for a single procedure A CFG is a conservative approximation of the control flow! Why?

Example Source Code unsigned fib(unsigned n) { int i; int f0 = 0, f1 = 1, f2; } if (n <= 1) return n; for (i = 2; i <= n; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } return f2; LLVM bitcode define i32 @fib(i32) { %2 = icmp ult i32 %0, 2 br i1 %2, label %12, label %3 ; <label>:3: br label %4 ; <label>:4: %5 = phi i32 [ %8, %4 ], [ 1, %3 ] %6 = phi i32 [ %5, %4 ], [ 0, %3 ] %7 = phi i32 [ %9, %4 ], [ 2, %3 ] %8 = add i32 %5, %6 %9 = add i32 %7, 1 %10 = icmp ugt i32 %9, %0 br i1 %10, label %11, label %4 ; <label>:11: br label %12 ; <label>:12: %13 = phi i32 [ %0, %1 ], [ %8, %11 ] ret i32 %13 }

Dominance in Flow Graphs Let d, d1, d2, d3, n be nodes in G. d dominates n ( d dom n ) iff every path in G from s to n contains d d properly dominates n if d dominates n and d n d is the immediate dominator of n ( d idom n ) if d is the last proper dominator on any path from initial node to n, DOM(x) denotes the set of dominators of x.

Dominator Properties Lemma 1: DOM(s) = { s }. Lemma 2: s dom d, for all nodes d in G. Lemma 3: The dominance relation on nodes in a flow graph is a partial ordering Reflexive n dom n is true for all n. Antisymmetric If d dom n, then not n dom d Transitive d1 dom d2 d2 dom d3 d1 dom d3 Lemma 4: The dominators of a node form a list. Lemma 5: Every node except s has a unique immediate dominator.

Finding Dominators in a Flow Graph Input : A flow graph G = (N,A,s). Output : The sets DOM(node) for each node N.

Finding Dominators in a Flow Graph Input : A flow graph G = (N,A,s). Output : The sets DOM(node) for each node N. Initialize Iterate

Loops while (b) { }?

Loops The right definition of loop is not obvious. Obviously bad definitions Cycle: Not necessarily properly nested or disjoint Strongly Connected Components: Too coarse; no nesting information What properties of the loops do we want to extract from CFG?

Loops: Two Definitions Natural loop Defined using dominators Intervals Defined in terms of reachability in flow graph

Natural Loops Def. Back Edge: An edge n d where d dom n Def. Natural Loop: Given a back edge, n d, the natural loop corresponding to n d is the set of nodes {d + all nodes that can reach n without going through d} Def. Loop Header: A node d that dominates all nodes in the loop Header is unique for each natural loop Why? Implies d is the unique entry point into the loop Uniqueness is very useful for many optimizations

Natural Loops Pros: + Intuitive, and similar to SCC. + Single entry point: loop header. + Identifies nested loops (if different headers) Cons: - Nested loops are not disjoint. - Some nodes are not part of any natural loop. - Does not include some cycles in irreducible flow graphs.

Reducibility of Flow Graphs Def. Reducible* flow graph: a flow graph G is called reducible iff we can partition the edges into 2 disjoint sets: forward edges: should form a DAG in which every node is reachable from initial node s (or also header) remaining edges must be back edges: i.e., only those edges n d such that d dom n Idea: Every cycle has at least one back edge All cycles are natural loops Otherwise graph is called irreducible. * Well-structured

Loops: Two Definitions Natural loop Defined using dominators Intervals Defined in terms of reachability in flow graph

Interval Analysis* Idea: Partition flow graph into disjoint subgraphs so that each subgraph has a single entry (header). Definition: The interval with node h as header, denoted I(h), is the subset of nodes of G constructed as: * It s different from the interval analysis on numerical quantities

Transformation Rules T1 and T2 T1 : Reduce a self-loop x x to a single node X X T2 : If x y, and there is no other predecessor of y, then reduce x and y to a single node. X Y XY Important: If G is reducible, successive applications of T1 and T2 produce the trivial graph. Reducibility by T1 and T2 is equivalent to reducibility by intervals.

Node Splitting Claim: If a node has n > 1 predecessors and m > 1 successors, split the node into n copies: T2 is always applicable to a graph after a node is split Any graph can be reduced to the trivial graph by applying T1, T2, and splitting. Challenge: Finding a minimal splitting of a graph is not easy. Typically involves an NP-complete problem.

Interval Analysis* Idea: Partition flow graph into disjoint subgraphs so that each subgraph has a single entry (header). Definition: The interval with node h as header, denoted I(h), is the subset of nodes of G constructed as: * It s different from the interval analysis on numerical quantities

Derived Flow Graphs Def. Derived Flow Graph, I(g): If G is a flow graph, then its I(G) is: (a) The nodes of I(G) are the intervals of G (b) The initial node of I(G) is I(s) (c) There is an arc from node I(h) to I(k) in I(G) if there is any arc from a node in I(h) to node k in G. Def. Derived sequence: the sequence G = G 0,G 1,...,G k is derived iff G i+1 = I(G i ) for 0 i < k, G k 1 G k, I(G k ) = G k. G k is called the limit flow graph of G. Definition: A flow graph is reducible iff its limit flow graph is a single node with no arc. Otherwise it is called irreducible.

Intervals Properties Lemma 6. I(h) is unique: does not depend on order of node insertion. (See Hecht for proof) Lemma 7. The subgraph generated by I(h) is itself a flow graph. Lemma 8. (a) Every arc entering a node of the interval I(h) from the outside enters the header h. (b) h dominates every node in I(h) (c) every cycle in I(h) includes h

See You Next Time! Review in the next few weeks: Muchnick, Chapter 21: Case Studies of Compilers Review by next class: Sections from Muchnick Sections 4.1-4.5, 4.9: Intermediate Representations Section 7.1: Control Flow Graphs (or equivalent sections in Cooper & Torczon or Aho, Lam, Sethi & Ullman)