Algorithm Search⌘K

Definitionintro

Control flow graph

Also called flow graph of a procedure.

A directed graph whose nodes are the basic blocks of a procedure and whose edges are the possible transfers of control between them.

A control flow graph (CFG) is a directed graph that shows how control can move through one procedure. Its nodes are the basic blocks of the procedure, and it has an edge from block BB to block CC if CC can run right after BB. One node is the entry. Every run of the procedure follows a path of the graph from the entry, but some paths of the graph are never followed by any run. Almost every global analysis and optimization in a compiler, from dataflow analysis to the construction of static single assignment form, works on the control flow graph.

Definition

The graph

Let a procedure be cut into basic blocks B1,…,BnB_1, \ldots, B_n, with B1B_1 the block that holds the first instruction. Its control flow graph is G=(N,E,r)G = (N, E, r) with

  • N={B1,…,Bn}N = \{B_1, \ldots, B_n\},
  • an edge (B,C)∈E(B, C) \in E if the last instruction of BB is a jump or a branch with CC as a target, or if CC comes right after BB in the program text and the last instruction of BB is not an unconditional jump or a return (a fall-through),
  • the entry r=B1r = B_1.

The predecessors of CC are the blocks BB with (B,C)∈E(B, C) \in E, and the successors of BB are the blocks CC with (B,C)∈E(B, C) \in E. A path is a sequence of blocks in which each consecutive pair is an edge. A block is reachable if there is a path from rr to it. Most analyses first delete the unreachable blocks, and many assume that rr has no predecessors; a compiler can add an empty entry block to make this true.

Many analyses also want one exit. If several blocks end in a return, or none does (the procedure loops forever), the compiler adds a new exit node with an edge from every returning block. Backward analyses and post-dominance start from it.

In the toy IR of this site the blocks are written out, so the successors come from the terminator alone: jump L gives one successor, br a L1 L2 gives L1L_1 and L2L_2 (one edge if L1=L2L_1 = L_2), and ret a gives none. The helper toyir.py has succs(prog, L), preds(prog) and edges(prog) for this.

Size

Each block of the toy IR has at most two successors, so m≤2nm \le 2n for nn blocks and mm edges, and m=O(n)m = O(n). A switch lowered to a jump table can give one block many successors, so in general mm is not bounded by a constant times nn, and costs on CFGs are stated in both nn and mm.

Paths and runs

The CFG over-approximates the runs. Every run of the procedure that starts at the entry visits a sequence of blocks that is a path of GG from rr. The converse is false: a path of GG may be infeasible, because the conditions of its branches cannot all hold. Which paths are feasible is undecidable in general, by Rice's theorem, so analyses on the CFG treat every path as possible. This is why a must-analysis that is true on all paths of the CFG is sound, and why it can lose precision.

Terms used with CFGs

  • Dominance. dd dominates vv if every path from rr to vv contains dd (dominators problem, dominator tree). Post-dominance is the same notion on the reversed graph from the exit.
  • Depth-first orders. A depth-first search from rr gives preorder, postorder and reverse postorder numbers (depth-first orders). In reverse postorder, every edge that is not a retreating edge goes forward, so forward analyses visit most predecessors before a block.
  • Back edges and loops. An edge (t,h)(t, h) is a back edge if hh dominates tt; each back edge defines a natural loop. A CFG in which every cycle has a back edge is reducible.
  • Critical edge. An edge from a block with several successors to a block with several predecessors. Code cannot be placed on it without critical edge splitting.

Examples and non-examples

A loop with a branch inside

This toy IR program adds the odd numbers below nn:

B1:
  i = 0
  s = 0
  jump B2
B2:
  t1 = i < n
  br t1 B3 B6
B3:
  t2 = i % 2
  br t2 B4 B5
B4:
  s = s + i
  jump B5
B5:
  i = i + 1
  jump B2
B6:
  ret s

Its CFG has 6 nodes, the entry B1, and 7 edges:

B1→B2,  B2→B3,  B2→B6,  B3→B4,  B3→B5,  B4→B5,  B5→B2.B1 \to B2,\; B2 \to B3,\; B2 \to B6,\; B3 \to B4,\; B3 \to B5,\; B4 \to B5,\; B5 \to B2.

The predecessors are B2: {B1, B5}, B3: {B2}, B4: {B3}, B5: {B3, B4} and B6: {B2}. A depth-first search that tries successors in the order of the terminator visits B1, B2, B3, B4, B5 and then B6, so the postorder is B5, B4, B3, B6, B2, B1 and the reverse postorder is B1, B2, B6, B3, B4, B5. The only edge that goes backward in this order is B5 → B2. B2 dominates B5, so it is a back edge, and its natural loop is {B2, B3, B4, B5}. The edge B3 → B5 is critical: B3 has two successors and B5 has two predecessors. The immediate dominators are B1 for B2, B2 for B3 and B6, and B3 for B4 and B5.

The run on n=6n = 6 follows the path B1, B2, B3, B5, B2, B3, B4, B5, B2, ..., B6 and returns 9.

An infeasible path

B0:
  br a B1 B2
B1:
  x = 1
  jump B3
B2:
  x = 0
  jump B3
B3:
  br a B4 B5
B4:
  y = 10 / x
  ret y
B5:
  ret 0

The CFG has the path B0, B2, B3, B4, on which 10 / x divides by 0. No run takes it, because both branches test the same a. An analysis that looks at all CFG paths reports a possible trap at B4 anyway. Path-sensitive analyses and symbolic execution can rule such paths out, at a higher cost.

Variants

  • Instruction-level CFG. Some tools use one node per instruction. The graph is larger, but every node has one instruction, which makes some analyses simpler to state. The blocks of the block-level CFG are the maximal chains of the instruction-level CFG.
  • CFG with exception edges. In Java and C++ compilers, an instruction that can throw has an edge to the handler. Then a block can be left in the middle, or the blocks are cut after each throwing instruction.
  • Interprocedural CFG. The CFGs of all procedures joined by call and return edges, used by interprocedural analyses; it needs the call graph to know the targets.

Non-examples

  • The call graph is a graph of procedures, not of blocks; its edges are calls, not transfers of control inside a procedure.
  • An abstract syntax tree shows the nesting of statements. Control flow can be derived from it, but its edges are parent-child links.
  • A data dependence graph or a program dependence graph has edges for the flow of values and for control dependence, which is computed from the CFG but is a different relation.

Pitfalls

  • Keeping unreachable blocks. An unreachable block has no dominator in the usual sense, and its predecessors confuse many algorithms. Delete them, or skip them consistently.
  • Assuming one exit. A procedure may have several returns or none. Add a virtual exit before a backward analysis or post-dominance, and decide what to do with infinite loops that cannot reach it.
  • Edges with the same ends. br a L L is one edge in this site's convention. Some compilers keep two parallel edges; then the predecessor list of LL has BB twice, and phi-functions need one operand per edge.

References

  1. Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Section 8.4 (Basic Blocks and Flow Graphs), with 8.4.3 (flow graphs) and 8.4.5 (loops); Section 9.6 (Loops in Flow Graphs), with 9.6.1 (dominators) and 9.6.2 (depth-first ordering)
  2. Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 4 (Intermediate Representations), the part on control flow graphs; Chapter 9 (data-flow analysis on the control flow graph)
  3. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 7 (Control-Flow Analysis), flow graphs, depth-first search, dominators and loops
  4. Frances E. Allen, Control flow analysis, Proceedings of a Symposium on Compiler Optimization, SIGPLAN Notices 5(7), 1970, the definition of the control flow graph of a program, of dominance and of intervals

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/A4Y5XS/text.yaml