Repository navigation
IR 10 dominance
Relevant source files
The following files were used as context for generating this wiki page:
Block A dominates block B if every path from the entry block to B passes through A. Dominance answers "is this value guaranteed to have been computed here?", which is what SSA, loop detection, code motion and phi placement all need. LLVM computes it once per function as a dominator tree and keeps it up to date as passes change the CFG.
This page defines dominance, immediate dominators, the dominator tree, the dominance frontier and post-dominance, works through an example, and shows the LLVM classes and printer passes. Use of these in mem2reg is on SSA and Phi.
| Term | Definition |
|---|---|
| A dominates B | Every path from entry to B goes through A. Every block dominates itself. |
| A strictly (properly) dominates B | A dominates B and A ≠ B |
| Immediate dominator idom(B) | The strict dominator of B closest to B. Every block except entry has exactly one. |
| Dominator tree | Tree with an edge idom(B) → B for every B. The entry block is the root. |
| Dominance frontier DF(A) | Blocks B such that A dominates a predecessor of B but does not strictly dominate B: where A's dominance "ends" |
| A post-dominates B | Every path from B to a function exit goes through A |
(illustrative CFG)
flowchart TD
E["entry"] --> A["A"]
A --> B["B"]
A --> C["C"]
B --> D["D"]
C --> D
D --> A
D --> X["exit"]
| Block | Dominators | idom | Dominance frontier |
|---|---|---|---|
| entry | entry | none | {} |
| A | entry, A | entry | {A} (via the back edge from D) |
| B | entry, A, B | A | {D} |
| C | entry, A, C | A | {D} |
| D | entry, A, D | A | {A} |
| exit | entry, A, D, exit | D | {} |
Neither B nor C dominates D, because D can be reached through the other one. So D's idom is A. The dominator tree is:
flowchart TD
E["entry"] --> A["A"]
A --> B["B"]
A --> C["C"]
A --> D["D"]
D --> X["exit"]
Reading the frontier: a variable assigned in B needs a phi in D, where B's value meets C's. A variable assigned in D needs a phi in A, where the value from the previous iteration meets the value from entry. That is exactly the rule mem2reg uses, with the frontier iterated because each new phi is itself a definition.
Since D → A goes to a block that dominates its source, it is a back edge, and A is a loop header; see CFG and Loops.
DominatorTree is a DominatorTreeBase<BasicBlock, false> (the false means "not post-dominators"). Each node is a DomTreeNode with getIDom() and its children. The main queries:
| Call | Answers |
|---|---|
DT.dominates(A, B) on blocks |
Does block A dominate block B |
DT.dominates(Def, UseInst) |
Is Def available at instruction UseInst (handles instructions in the same block by order) |
DT.dominates(Def, U) on a Use
|
Is Def available at this use; for a phi use, checks the end of the incoming block |
DT.properlyDominates(A, B) |
Strict dominance |
DT.getNode(BB)->getIDom() |
Immediate dominator |
DT.findNearestCommonDominator(A, B) |
Lowest block dominating both |
A pass obtains it from the analysis manager:
// Illustrative
DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
if (DT.dominates(Def, UserInst)) { /* safe to use Def here */ }LLVM builds the tree with the Semi-NCA algorithm and can update it incrementally (DomTreeUpdater) when a pass adds or removes edges, rather than recomputing.
Sources: llvm/include/llvm/IR/Dominators.h:122, llvm/include/llvm/IR/Dominators.h:140-175, llvm/include/llvm/IR/Dominators.h:241, llvm/include/llvm/Support/GenericDomTree.h:112, llvm/include/llvm/Support/GenericDomTree.h:440-449
opt -disable-output -passes='print<domtree>' example.ll
The output lists the tree with indentation by depth and DFS numbers in braces. For the example above it has this shape (illustrative):
DominatorTree for function: example
=============================--------------------------------
Inorder Dominator Tree: DFSNumbers invalid: 0 slow queries.
[1] %entry {4294967295,4294967295} [0]
[2] %A {4294967295,4294967295} [1]
[3] %B {4294967295,4294967295} [2]
[3] %C {4294967295,4294967295} [2]
[3] %D {4294967295,4294967295} [2]
[4] %exit {4294967295,4294967295} [3]
Roots: %entry
The number in the first brackets is the depth. print<postdomtree> prints the post-dominator tree the same way.
Sources: llvm/include/llvm/IR/Dominators.h:254, llvm/lib/Passes/PassRegistry.def:522, llvm/lib/Passes/PassRegistry.def:530
DominanceFrontier stores DF(B) for every block. Most passes do not need the full table: mem2reg and SSA updating use IDFCalculator, which computes the iterated frontier of a set of blocks directly from the dominator tree (see SSA and Phi).
Sources: llvm/include/llvm/Analysis/DominanceFrontier.h:43, llvm/include/llvm/Analysis/DominanceFrontier.h:94, llvm/include/llvm/Analysis/DominanceFrontier.h:134
Post-dominance is dominance on the reversed CFG, with the function exits as roots. In the example, D post-dominates B and C (both must pass through D to exit), and exit post-dominates everything.
It answers "if B runs, is A certain to run afterwards?". Uses include:
-
Control dependence: B is control dependent on a branch if B post-dominates one successor of the branch but not the branch itself. Aggressive dead code elimination (
adce) uses this to delete branches whose outcome does not matter. - Sinking code to a point that is executed whenever the original was.
PostDominatorTree and PostDominatorTreeAnalysis are in PostDominators.h.
Sources: llvm/include/llvm/Analysis/PostDominators.h:29, llvm/include/llvm/Analysis/PostDominators.h:48-64
- A dominates B if every entry-to-B path goes through A; idom links form the dominator tree.
- The dominance frontier is where a block's dominance ends;
phis go at the iterated frontier of definitions. - Post-dominance is dominance on the reversed CFG and gives control dependence.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look