Docsity

Compiler Essentials: Dataflow Analysis and Control Dependence, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Lecture 2: Compiler Essentials
ECE497MF
January 28, 2004
1 Dataflow Analysis: A Review
Any operation we want a compiler to perform must of course be expressed formally. Very often, we will use
the formal machinery of dataflow analysis. Dataflow analysis addresses the two main problems of consequence
to compilers:
1. Is there an execution path Pfrom statement Astatement Bhaving property C?
2. Do all execution paths Pfrom statement Ato statement Bhave property C?
Actually, item 1 and item 2 are equivalent. By DeMorgan’s law, item 1 can be re-cast in the form of
item 2 as follows: āˆ€P:C=¬(∃P:¬C).
Control Flow Graphs The control flow graph is a directed graph wherein the nodes are either program
statements or basic blocks and an edge A→Bexists if Bcan execute immediately after A. Special nodes
Sand Edenote the start and exit points respectively. By convention, all CFGs include an edge S→E
although this edge is not always shown. It is generally simpler to let each node in the flowgraph be a single
instruction, rather than a basic block. Using basic blocks has speed and memory advantages, but it isn’t
worthwhile in a research compiler. We’ll assume each node is a single instruction.
1.1 Foreshadowing: Control Dependence Graph
Last week’s paper by Lam and Wall demonstrated the importance of control-dependence in parallelization.
Ultimately, we need to define the control-dependence graph (CDG), but here we’ll give only an informal
explanation and then move on to do some other simple dataflow analysis problems before returning to the
CDG problem in a later lecture.
Intuitive definition of Control-dependence: Statement Ais control-dependent on a branch Bif the decision
at Bcan affect the number of times Aexecutes. In a block-structured programming language, each statement
is control-dependent on its enclosing conditionals. For example, in Figure 1, statement 2 is dependent on
statement 1, but neither 4, 6, 7, nor 8 are dependent on 1. In Figure 2, statements 1 and 2 are both
control-dependent on the loop conditional 3, but Eis not dependent on 3.
1.2 Dataflow Example 1: Reaching Definitions
Each assignment statement creates a new definition of its left-hand-side. The reaching definitions analysis
determines which definitions are visible to each statement in the program. Given an assignment statements
Aand another statement B,Areaches Biff there is a path from Ato Bwhere the left-hand-side of Ais
not overwritten. Using reaching definitions, one can build the def-use chains, which give information about
how data is produced and consumed in the program.
To perform the analysis, associate the sets gen(inst), pass(inst), and def s(inst) with each instruction
in the CFG. For all assignment statements, let gen(inst) contain the current statement and let pass(inst)
contain all assignment statements that do not define the same variable as the current statement. For all
other statements, let gen(inst) be the empty set and let pass(inst) be the set of all assignment statements.
1
pf3
pf4
pf5

Partial preview of the text

Download Compiler Essentials: Dataflow Analysis and Control Dependence and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Lecture 2: Compiler Essentials

ECE497MF

January 28, 2004

1 Dataflow Analysis: A Review

Any operation we want a compiler to perform must of course be expressed formally. Very often, we will use the formal machinery of dataflow analysis. Dataflow analysis addresses the two main problems of consequence to compilers:

  1. Is there an execution path P from statement A statement B having property C?
  2. Do all execution paths P from statement A to statement B have property C?

Actually, item 1 and item 2 are equivalent. By DeMorgan’s law, item 1 can be re-cast in the form of item 2 as follows: āˆ€P : C = ¬(∃P : ¬C).

Control Flow Graphs The control flow graph is a directed graph wherein the nodes are either program statements or basic blocks and an edge A → B exists if B can execute immediately after A. Special nodes S and E denote the start and exit points respectively. By convention, all CFGs include an edge S → E although this edge is not always shown. It is generally simpler to let each node in the flowgraph be a single instruction, rather than a basic block. Using basic blocks has speed and memory advantages, but it isn’t worthwhile in a research compiler. We’ll assume each node is a single instruction.

1.1 Foreshadowing: Control Dependence Graph

Last week’s paper by Lam and Wall demonstrated the importance of control-dependence in parallelization. Ultimately, we need to define the control-dependence graph (CDG), but here we’ll give only an informal explanation and then move on to do some other simple dataflow analysis problems before returning to the CDG problem in a later lecture. Intuitive definition of Control-dependence: Statement A is control-dependent on a branch B if the decision at B can affect the number of times A executes. In a block-structured programming language, each statement is control-dependent on its enclosing conditionals. For example, in Figure 1, statement 2 is dependent on statement 1, but neither 4, 6, 7, nor 8 are dependent on 1. In Figure 2, statements 1 and 2 are both control-dependent on the loop conditional 3, but E is not dependent on 3.

1.2 Dataflow Example 1: Reaching Definitions

Each assignment statement creates a new definition of its left-hand-side. The reaching definitions analysis determines which definitions are visible to each statement in the program. Given an assignment statements A and another statement B, A reaches B iff there is a path from A to B where the left-hand-side of A is not overwritten. Using reaching definitions, one can build the def-use chains, which give information about how data is produced and consumed in the program. To perform the analysis, associate the sets gen(inst), pass(inst), and def s(inst) with each instruction in the CFG. For all assignment statements, let gen(inst) contain the current statement and let pass(inst) contain all assignment statements that do not define the same variable as the current statement. For all other statements, let gen(inst) be the empty set and let pass(inst) be the set of all assignment statements.

E

1

2 3

4

5

6 7

8

Figure 1: An acyclic flowgraph.

S

1

2

E

3

Figure 2: A flowgraph with a loop.

1.3 Dataflow Example 2: Dominators

Given flowgraph nodes A and B, A dom B iff all paths from S to B flow through A. If we observe statement B execute in a program, we can say with certainty that A has executed at least once if A dom B. The dominator relation dom is a partial ordering of flowgraph nodes. In fact, that partial order is a tree called the ā€œdominator treeā€. Deriving the dominator tree is an intermediate step in many important transformations, including generating the control-dependence graph (CDG) and translating to single static assignment (SSA) form. Dominators find a simple application in detecting natural loops. An edge A → B is a natural loop back edge iff B dom A. For example, Figure 5 contains a natural loop with back-edge 3 → 1, while Figure 6 does not contain a natural loop (it results from unstructured code). The set of dominators dom(inst) for each instruction can be computed as follows. For each node except S, let dom(inst) initially be the set of all nodes. Let dom(S) = {S}. Iterate the following transfer function over each node until a fixed point is reached:

dom(inst) = 

i∈preds(inst)

doms(i) ∪ inst

Figure 4 shows a sample CFG where dges satisfying the natural loop edge criterion appear in bold. Table 2 shows the results of iterating the above equation to a fixed point (only one iteration). Again, more than one iteration will be necessary in the general case.

Node dom dom initial iter 1 S {S} {S} 1 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 } 2 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 2 } 3 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 } 4 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 } 5 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 5 } 6 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 6 } 7 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 7 } 8 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 7 , 8 } 9 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 7 , 9 } 10 {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 7 , 8 , 10 } E {S, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } {S, 1 , 3 , 4 , 7 , 9 , E}

Table 2: Propagating dominance sets through Figure 4 top-down.

2 Higher-Order Observations

  • The only tasks at which compilers are superior to runtime systems are those that deal with item 1 and item 2. This is because compilers can enumerate all possible execution paths while a runtime system knows only of those paths it observes executing.
  • Just as a compiler performs all of its transformations according to the rules of a formal system, human programmers also transform their programs using formal rules^1.
  • By paying attention to the rules we use as we design, we may learn to express them formally^2.

(^1) This is open to debate, of course (^2) The Tao of Matt pp.207 (paraphrased)

E

Figure 4: A flowgraph with 5 natural loop back-edges (bold).