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 moreBasic 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsLogic Gates FundamentalsImplementing Boolean Functions with GatesKarnaugh Map SimplificationCombinational Circuit DesignFlip-Flops and LatchesFinite State Machines (FSMs)Deterministic Finite Automata (DFA)Nondeterministic Finite Automata (NFA)Two-Way Finite AutomataNFA to DFA Conversion (Subset Construction)DFA Properties and Minimization AlgorithmsRegular Languages: Definition and CharacterizationContext-Free Grammars (CFGs)Context-Free Grammar Properties and AmbiguityParse Trees, Derivations, and Ambiguity in CFGsContext-Free Grammars in Compiler DesignAbstract Syntax Trees (ASTs)Symbol Tables and Scope ResolutionSemantic Analysis PhaseIntermediate Code RepresentationControl Flow Graphs

Longest path: 94 steps · 505 total prerequisite topics

Prerequisites (3)

Leads To (9)