CS 614: Theory and Construction of Compilers

CS 614: Theory and
Construction of Compilers
Lecture 17
Fall 2002
Department of
Computer Science
University of Alabama
Joel Jones
Overview

Control Flow Analysis
Basic Blocks
 Types of Analyses

• Control Flow Graphs
• Interval Analysis

Reading for Next Time
©2002 Joel Jones
Control Flow Analysis
Control flow analysis is the discovery of
the hierarchical flow of control within a
procedure
 Used in concert with data flow analysis in
performing optimizations

©2002 Joel Jones
Basic Blocks
All forms of control flow analysis begin
with basic blocks
 Basic blocks are formed from a linear
form of IR, such as MIR
 A basic block consists of a sequence of
instructions that can be entered only at
the beginning and exited only at the end

©2002 Joel Jones
Example of Basic Block
Formation
•
receive m (val)
•
f0 <- 0
•
f1 <- 1
•
if m <= 1 goto L3
•
i <- 2
• L1: if i <= m goto L2
•
return f2
• L2: f2 <- f0 + f1
•
f1 <- f2
•
i <- i + 1
•
goto L1
• L3: return m
• Pair Up:
• • Find the Basic
Blocks
©2002 Joel Jones
Control Flow Graphs





A control flow graph (CFG) is a rooted, directed
graph
The nodes of a CFG are basic blocks plus two
distinguished nodes, the entry and exit nodes
The edges of a CFG correspond to the flow of
control from one basic block (BB) the the next
Additionally, an edge is placed between the
entry node and the first node of the procedure
Also, edges are placed from each final BB (BBs
withh nosuccessors) to the exit
©2002 Joel Jones
Control Flow Graphs (cont.)
•
receive m (val)
•
f0 <- 0
•
f1 <- 1
•
if m <= 1 goto L3
•
i <- 2
• L1: if i <= m goto L2
•
return f2
• L2: f2 <- f0 + f1
•
f1 <- f2
•
i <- i + 1
•
goto L1
• L3: return m
• Pair Up:
• • Draw the Control
Flow Graph
©2002 Joel Jones
Structural Analysis

Interval analysis divides the flowgraph into
regions of various sorts, calculated by
transforming CFG, most common is T1-T2




The root of the control tree is an abstract graph
representing the original flow graph
The leaves of the control tree are individual BBs
The nodes between root and leaves represent
regions of the flowgraph (abstract nodes)
The edges represent the relationship between each
abstract node and the regions that are its
descendants
©2002 Joel Jones
Structural Analysis (cont.)
T1
B1
B1a
B1
B1a
T2
B2
©2002 Joel Jones
Structural Analysis (cont.)
• Pair Up:
• • Use the previous example and
transform it.
©2002 Joel Jones
Readings for Next Time

Chapter 7 of the textbook
©2002 Joel Jones