A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Control Flow Graphs

Graduate Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
25topics build on this
505prerequisites beneath it
See this on the map →
Intermediate Code RepresentationDirected Graphs and Digraphs+1 more→→Basic Block AnalysisDataflow Analysis+7 more
cfg program-analysis graph-representation

Core Idea

A control flow graph (CFG) represents a program's control structure as a directed graph where nodes are basic blocks (straight-line code with one entry/exit) and edges represent jumps. CFGs are the foundation for program analysis: dominance, loops, and dataflow properties are computed on CFGs. Building and analyzing CFGs is essential for optimization and verification.

Explainer

When a compiler translates source code into intermediate representation (IR), it produces a flat list of three-address instructions. But a flat list hides a crucial dimension: not every instruction always executes. Branches, loops, and function returns mean that execution can take many paths through the code. A control flow graph makes this structure explicit by turning the flat instruction list into a directed graph that mirrors all the ways the program can actually run.

The first step in building a CFG is identifying basic blocks. A basic block is a maximal run of instructions with a single entry point (no jumps land in the middle) and a single exit point (only the last instruction may be a branch). Within a basic block, control flow is perfectly sequential: if the first instruction executes, all of them do. This is a powerful guarantee for optimization — you can propagate constants, eliminate dead code, and allocate registers within a block using only local information, without worrying about branching.

The second step is adding edges. After each basic block, execution either falls through to the next block, jumps unconditionally to some target, or branches conditionally to one of two targets. Each possibility becomes a directed edge in the CFG. A conditional if-else creates two outgoing edges from the block containing the branch: one to the "then" block and one to the "else" block. Loops create back edges — edges that point backward to an earlier block — which are the graph-theoretic signature of a loop. Finding all back edges lets the compiler identify natural loops and apply loop-specific optimizations like loop-invariant code motion.

With the CFG in hand, the compiler can compute global properties across all blocks. Dominator analysis asks: for each basic block B, which blocks must every execution path pass through before reaching B? The dominator tree organizes this information and enables structured optimizations like partial redundancy elimination. Liveness analysis uses the CFG edges in reverse to determine which variables are still needed at each program point, enabling efficient register allocation. All of these analyses — which you will study in depth as dataflow analysis — are defined as fixed-point computations on the CFG structure.

The CFG is not just an internal compiler data structure; it is also the foundation for static analysis tools, test coverage measurement (branch coverage counts CFG edges), and program verification. When a security scanner checks for use-after-free errors or null-pointer dereferences, it is walking paths through the program's CFG. Understanding the CFG therefore unlocks not just compiler optimizations but the broader field of program analysis.

Practice Questions 3 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Two-Way Finite Automata → NFA to DFA Conversion (Subset Construction) → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Intermediate Code Representation → Control Flow Graphs

Longest path: 94 steps · 505 total prerequisite topics

Prerequisites (3)

Leads To (9)