Lesson 17.3 — Dead code elimination: mark–sweep, aggressive (control dependence), and dead stores¶
Techniques: mark–sweep "useful code" DCE (Kennedy 1981; Cooper–Torczon's Dead); aggressive DCE via control dependence (Cytron et al. 1991), including LLVM's handling of loops that may not terminate; dead-store elimination and its interplay with branch elimination · Pebble implements:
pebble-adce(exercise E2) · Prerequisites: worklist DCE of trivially dead instructions (Ch 13, Lesson 13.5); post-dominance and control dependence (Lesson 15.4) · Time: 4–6 hours
1. Problem and motivation¶
An instruction is dead if deleting it cannot change what the program does: its result is never used by anything observable, and it has no side effect. Ch 13's worklist DCE deletes trivially dead instructions — those with no uses — and repeats. It is sound and fast, but it cannot delete a cycle of dead values, and it never deletes a branch. The running example of this lesson (the work function of the §7 boxes, in the chapter's compact notation with LLVM's latch block for.inc merged into for.body) has both:
fn work(n, a):
entry:
br for.cond
for.cond:
s.0 = phi [0, entry], [add, for.body]
t.0 = phi [0, entry], [add1, for.body]
i.0 = phi [0, entry], [inc, for.body]
cmp = lt i.0, n
cbr cmp, for.body, for.end
for.body:
add = add s.0, i.0
mul = mul a, i.0
add1 = add t.0, mul
inc = add i.0, 1
br for.cond
for.end:
cmp2 = lt 5, a
cbr cmp2, if.then, if.end
if.then:
mul3 = mul t.0, 2
br if.end
if.end:
ret s.0
flowchart TD
entry([entry]) --> fc[for.cond]
fc -->|T| fb[for.body]
fc -->|F| fe[for.end]
fb --> fc
fe -->|T| it[if.then]
fe -->|F| ie[if.end]
it --> ie
t.0 and add1 use each other, so neither is trivially dead, although nothing observable depends on them; and the test a > 5 decides nothing that matters once mul3 is gone.
Mark–sweep DCE¶
The fix for cycles is to reverse the question: instead of proving instructions dead, prove them useful starting from the instructions that are obviously needed (returns, stores, calls with effects, branches) and following operands backwards; whatever is never reached is dead [EaC3, Ch. 10]. This "mark–sweep" formulation goes back to Kennedy's survey of global flow analysis and is the Dead algorithm of Engineering a Compiler. It removes t.0, add1, mul and mul3 from the running example but keeps every branch.
Aggressive DCE¶
Cytron, Ferrante, Rosen, Wegman and Zadeck made branches optional too: a branch is useful only if some useful instruction is control dependent on it [CFRWZ91, §7.1]. Everything starts dead, including branches; marking a useful instruction also marks the branches that decide whether it executes. A branch that remains unmarked is replaced by a jump to its nearest useful post-dominator. On the running example ADCE also removes cmp2 and the branch in for.end, and on a loop whose results are unused it removes the whole loop — which is only correct if the loop terminates. LLVM's adce [LLVM-ADCE] and GCC's cddce [GCC-DCE] both implement it and both answer the termination question explicitly; pebble-adce (E2) does the same.
Dead stores and dead branches¶
A store is an effect, so both algorithms treat it as a root. But a store whose value is overwritten before any read, or written to a local variable nobody reads, is dead as well; finding it needs memory reasoning (MemorySSA, alias analysis: Ch 19). Dead-store elimination (DSE) removes such stores [LLVM-DSE], and it interacts with ADCE: removing a dead store can make the branch around it dead, and only then can ADCE remove the branch.
2. Definitions and algorithms¶
Notation follows the chapter's Notation section. Throughout the lesson, let \(F\) be a function in SSA form with CFG \(G = (N, E, r)\) (NOTATION §3), let \(\mathrm{Inst}\) be its set of instructions, and for an instruction \(I \in \mathrm{Inst}\) write \(\mathrm{block}(I) \in N\) for the block that contains it; every block \(B \in N\) ends in exactly one terminator (br, cbr or ret), written \(\mathrm{term}(B)\). Post-dominance \(\mathrm{pdom}\), \(\mathrm{ipdom}\) and control dependence \(\mathrm{CD}\) are those of Ch 15 (NOTATION §4), computed on \(G\) with a virtual exit. Every numbered box restates the objects it uses.
Definition 17.3.1 (Roots, observable behavior, useful instructions)
Let \(F\) be a function in SSA form with instructions \(\mathrm{Inst}\). The observable behavior of a run of \(F\) is the sequence of its side effects (memory writes, calls, volatile accesses, I/O) with their operand values, followed by its returned value or by non-termination. An instruction \(I \in \mathrm{Inst}\) is a root if it can contribute to observable behavior directly: it returns, or it may have a side effect, or it may not return; write \(\mathrm{Roots} \subseteq \mathrm{Inst}\) for the set of roots. For a set \(R_b \subseteq \mathrm{Inst}\) of terminators, called the root branches, the useful instructions relative to \(R_b\) form the least set \(\mathcal{U} \subseteq \mathrm{Inst}\) such that (i) \(\mathrm{Roots} \cup R_b \subseteq \mathcal{U}\), and (ii) for every \(I \in \mathcal{U}\) and every operand \(v\) of \(I\) defined by an instruction, that instruction is in \(\mathcal{U}\). An instruction not in \(\mathcal{U}\) is dead relative to \(R_b\).
In LLVM IR the roots are ret, store, calls that may write memory or may not return, volatile loads and stores, and exception-handling pads; pebble-adce asks Instruction::mayHaveSideEffects() and isTerminator() (exercise E2).
Useful instructions of work
With every branch a root (\(R_b\) = all branches): \(\mathcal{U}\) = the branches, cmp, cmp2,
ret s.0, s.0, add, i.0, inc. Dead: t.0, add1, mul, mul3.
Algorithm 17.3.2 (Mark–sweep DCE)
- Input: a function \(F\) in SSA form with instructions \(\mathrm{Inst}\) and roots \(\mathrm{Roots}\) (Definition 17.3.1).
- Output: \(F\) without the instructions that are dead relative to \(R_b\) = all terminators.
- Precondition: \(\mathrm{Roots}\) contains every instruction that may have a side effect.
- Postcondition: exactly the useful set \(\mathcal{U}\) for \(R_b\) = all terminators survives; the CFG is unchanged (Theorem 17.3.9).
- State: the marked set \(M \subseteq \mathrm{Inst}\), initially \(\emptyset\), and the worklist \(W\), a stack of instructions, initially empty.
- Operations: marking an instruction \(I\) is the update \(M \gets M \cup \{I\}\) followed by pushing \(I\) on \(W\); it asserts \(I \in \mathcal{U}\). Popping removes and returns the top of \(W\).
- Invariant: \(M \subseteq \mathcal{U}\); every instruction of \(M\) whose operands' definitions are not all in \(M\) is on \(W\).
function MarkSweepDCE(F):
M ← ∅; W ← []
for each instruction I ∈ Inst:
if I ∈ Roots or I is a terminator: Mark(I)
while W ≠ []:
I ← pop W
for each operand v of I that is defined by an instruction J:
if J ∉ M: Mark(J)
for each instruction I ∉ M: delete I # sweep
function Mark(I): M ← M ∪ {I}; push I on W
Definition 17.3.3 (Control dependence, recalled)
Let \(G = (N, E, r)\) be a flowgraph in which every block reaches an exit, and let \(\mathrm{pdom}\) be post-dominance on \(G\) with a virtual exit (Lesson 15.4); \(\mathrm{ipdom}(X)\) is the immediate post-dominator of a block \(X \in N\). A block \(Y \in N\) is control dependent on a block \(X \in N\), written \(X \in \mathrm{CD}(Y)\), if \(X\) has a successor \(S \in \mathrm{succs}(X)\) with \(Y \mathrel{\mathrm{pdom}} S\) and \(Y\) does not strictly post-dominate \(X\). Equivalently, \(\mathrm{CD}(Y)\) is the post-dominance frontier of \(Y\) (Ch 15, Theorem 15.4.6).
Definition 17.3.4 (ADCE live set, useful blocks, marking)
Let \(F\) be a function in SSA form with CFG \(G = (N, E, r)\), instructions \(\mathrm{Inst}\), roots \(\mathrm{Roots}\) (Definition 17.3.1) and control dependences \(\mathrm{CD}\) (Definition 17.3.3). Fix a depth-first search of \(G\) from \(r\) and let \(T, H \in N\) be blocks; the edge \((T, H) \in E\) is a retreating edge of that search if \(H\) is an ancestor of \(T\) in its DFS tree (equivalently, \(H\) is still on the DFS stack when the edge is explored). The ADCE live set \(\mathcal{L} \subseteq \mathrm{Inst}\) and the useful blocks \(\mathcal{B} \subseteq N\) are the least sets such that:
- \(\mathrm{Roots} \subseteq \mathcal{L}\) (a branch is not a root; branches enter \(\mathcal{L}\) only through rules (5) and (6));
- \(r \in \mathcal{B}\);
- for every \(I \in \mathcal{L}\): \(\mathrm{block}(I) \in \mathcal{B}\), and the defining instruction of every operand of \(I\) is in \(\mathcal{L}\);
- for every phi \(I \in \mathcal{L}\) and every incoming block \(P\) of \(I\): \(P \in \mathcal{B}\);
- for every \(B \in \mathcal{B}\): if \(\mathrm{term}(B)\) is an unconditional branch then \(\mathrm{term}(B) \in \mathcal{L}\), and for every \(X \in \mathrm{CD}(B)\), \(\mathrm{term}(X) \in \mathcal{L}\);
- optionally (the keep-loops rule): for every retreating edge \((T, H)\) of the fixed depth-first search, \(\mathrm{term}(T) \in \mathcal{L}\).
A marking state is a pair \((\mathit{Live}, \mathit{Useful})\) with \(\mathit{Live} \subseteq \mathrm{Inst}\) and \(\mathit{Useful} \subseteq N\), initially \((\emptyset, \emptyset)\). Marking an instruction \(I\) live is the update \(\mathit{Live} \gets \mathit{Live} \cup \{I\}\); it asserts \(I \in \mathcal{L}\), i.e. that \(I\) is an instruction the transformation must keep because it cannot prove that removing \(I\) leaves the observable behavior unchanged. Marking a block \(B\) useful is the update \(\mathit{Useful} \gets \mathit{Useful} \cup \{B\}\); it asserts \(B \in \mathcal{B}\). An instruction not in \(\mathcal{L}\) is dead; a conditional branch not in \(\mathcal{L}\) is a dead branch.
Rule (6) keeps loops that may not terminate (§4). On a reducible CFG the retreating edges are exactly the back edges \(T \to H\) with \(H \mathrel{\mathrm{dom}} T\); on an irreducible CFG a cycle may have no such back edge, but every cycle contains a retreating edge of any DFS, which is what the termination argument of Theorem 17.3.11 needs.
Algorithm 17.3.5 (Aggressive dead code elimination, Cytron et al.)
- Input: a function \(F\) in SSA form with CFG \(G = (N, E, r)\), instructions \(\mathrm{Inst}\) and roots
\(\mathrm{Roots}\), in which every block reaches an exit; its post-dominator function \(\mathrm{ipdom}\)
and control dependences \(\mathrm{CD}\) (Definition 17.3.3); a fixed depth-first search of \(G\) with its
retreating edges, and the flag
keep loopsselecting rule (6) (Definition 17.3.4). - Output: \(F\) without its dead instructions, with every dead branch replaced by an unconditional branch.
- Precondition: every block reaches an exit; if
keep loopsis off, every loop of \(F\) terminates; \(\mathrm{Roots}\) contains every instruction that may have a side effect. - Postcondition: at the end of the marking phase, \(\mathit{Live} = \mathcal{L}\) and \(\mathit{Useful} = \mathcal{B}\) (Lemma 17.3.8); the output has the same observable behavior as \(F\) on every terminating run (Theorem 17.3.11).
- State: the marking state \((\mathit{Live}, \mathit{Useful})\) of Definition 17.3.4 and the worklist \(W\), a stack of instructions, initially empty.
- Operations: for an instruction \(I \in \mathrm{Inst}\) and blocks \(B, X, Y \in N\):
Mark\((I)\) marks \(I\) live (Definition 17.3.4), pushes \(I\) on \(W\) and marks \(\mathrm{block}(I)\) useful;MarkBlock\((B)\) marks \(B\) useful and applies rule (5) to \(B\). Retargeting a block \(X\) with a dead branch to a block \(Y\) replaces \(\mathrm{term}(X)\) bybr\(Y\), adding apoisonentry for \(X\) to every phi of \(Y\) if \((X, Y) \notin E\). - Invariant: \(\mathit{Live} \subseteq \mathcal{L}\) and \(\mathit{Useful} \subseteq \mathcal{B}\); every instruction of \(\mathit{Live}\) to which rules (3) and (4) have not yet been applied is on \(W\); rule (5) has been applied to every block of \(\mathit{Useful}\).
function ADCE(F):
Live ← ∅; Useful ← ∅; W ← []
for each root I ∈ Roots: Mark(I) # rule (1)
if keep loops: for each retreating edge (T → H): Mark(term(T)) # rule (6)
MarkBlock(r) # rule (2)
while W ≠ []:
I ← pop W
for each operand v of I defined by an instruction J: Mark(J) # rule (3)
if I is a phi: for each incoming block P of I: MarkBlock(P) # rule (4)
for each block X whose terminator is a conditional branch ∉ Live: # sweep
Y ← ipdom(X)
while Y ∉ Useful: Y ← ipdom(Y) # nearest useful post-dominator
retarget X to Y
delete every instruction ∉ Live, except the terminators of blocks in Useful
delete the blocks that became unreachable from r
function Mark(I):
if I ∉ Live: Live ← Live ∪ {I}; push I on W; MarkBlock(block(I)) # rule (3), first half
function MarkBlock(B):
if B ∉ Useful:
Useful ← Useful ∪ {B}
if term(B) is an unconditional branch: Mark(term(B)) # rule (5)
for each X ∈ CD(B): Mark(term(X)) # rule (5)
scalaropt.adce in tools/course/lib is this pseudo-code (with aggressive=False it becomes Algorithm 17.3.2, with remove_loops=False rule (6) is on); the drill adce-marking traces it. In LLVM's ADCE.cpp the marking state is the Live bit of each InstInfo and BlockInfo, markLive is Mark, and the retreating edges are found by markLiveLoopBranches from the DFS of BlockInfo::PostOrder (§7).
Definition 17.3.6 (Dead store)
Let \(F\) be a function with a set \(\mathrm{Loc}\) of memory locations (sets of bytes). A store instruction \(s\) of \(F\) to a location \(\ell \in \mathrm{Loc}\) is dead if on every path from \(s\) to an exit of \(F\), either \(\ell\) is overwritten, by a store that writes every byte of \(\ell\), before any instruction that may read a byte of \(\ell\), or no instruction that may read a byte of \(\ell\) occurs before the lifetime of \(\ell\) ends.
The second case is the local variable nobody reads: a non-escaping alloca whose lifetime ends at ret.
Algorithm 17.3.7 (Dead-store elimination, two classic cases)
- Input: a function \(F\) in SSA form with loads and stores over locations \(\mathrm{Loc}\) (Definition 17.3.6), and an alias oracle: two predicates \(\mathrm{mayAlias}, \mathrm{mustOverwrite} : \mathrm{Loc} \times \mathrm{Loc} \to \{\text{true}, \text{false}\}\), where \(\mathrm{mayAlias}(\ell, \ell')\) is true unless \(\ell\) and \(\ell'\) surely share no byte, and \(\mathrm{mustOverwrite}(\ell, \ell')\) is true only if a store to \(\ell\) writes every byte of \(\ell'\).
- Output: \(F\) without the stores that the two rules below prove dead.
- Precondition: the alias oracle is sound: \(\mathrm{mayAlias}\) never answers false for two locations that share a byte, and \(\mathrm{mustOverwrite}\) never answers true wrongly.
- Postcondition: every deleted store is dead (Definition 17.3.6; Theorem 17.3.12).
- State: while scanning a block backwards, the set \(\mathit{Covered} \subseteq \mathrm{Loc}\) of locations stored later in the block.
- Invariant: every \(\ell \in \mathit{Covered}\) is surely overwritten, by a store later in the block, before any instruction later in the block that may read it.
function DSE(F):
for each block B ∈ N: # rule 1: overwritten later in the block
Covered ← ∅
for each instruction I of B, last to first:
if I is `store v, ℓ`:
if some c ∈ Covered with mustOverwrite(c, ℓ): delete I
else: Covered ← Covered ∪ {ℓ}
else if I may read memory at a location ℓ' (load, call):
Covered ← { c ∈ Covered : not mayAlias(c, ℓ') }
for each alloca A whose address never escapes: # rule 2: never read at all
if no load or call can read A: delete every store to A (and A)
LLVM's DSE generalizes rule 1 across blocks by walking MemorySSA from each store to its possible readers and killers (Ch 19).
3. Worked example¶
Mark–sweep DCE¶
Algorithm 17.3.2 on work (scalaropt.adce(fn, aggressive=False)); the seeds are the six terminators, then the worklist proceeds in FIFO order:
| step | action | because | worklist after |
|---|---|---|---|
| 1–6 | mark entry.term, for.cond.term, for.body.term, for.end.term, if.then.term, if.end.term |
roots: every branch, and the return | the six terminators |
| 7 | pop entry.term |
no operands | 5 terminators |
| 8 | pop for.cond.term; mark cmp |
operand of the branch | … cmp |
| 9 | pop for.body.term, for.end.term; mark cmp2 |
operand of the branch | if.then.term if.end.term cmp cmp2 |
| 10 | pop if.then.term, if.end.term; mark s.0 |
operand of ret |
cmp cmp2 s.0 |
| 11 | pop cmp; mark i.0 |
operand | cmp2 s.0 i.0 |
| 12 | pop cmp2 (operands 5, a: nothing to mark), s.0; mark add |
phi operand | i.0 add |
| 13 | pop i.0; mark inc |
phi operand | add inc |
| 14 | pop add, inc |
operands already marked | — |
Dead: t.0, mul, add1, mul3. The dead cycle t.0 ↔ add1 is found because nobody marks it; Ch 13's trivially-dead worklist cannot delete either (each has a use). for.end still tests a > 5 and branches to an empty if.then.
Aggressive DCE¶
Algorithm 17.3.5 on work, generated by scalaropt.adce(fn) (control dependences: \(\mathrm{CD}(\text{for.cond}) = \mathrm{CD}(\text{for.body}) = \{\text{for.cond}\}\), \(\mathrm{CD}(\text{if.then}) = \{\text{for.end}\}\), all others empty):
| step | action | because | worklist after |
|---|---|---|---|
| 1 | mark if.end.term |
root: return | if.end.term |
| 2 | block entry useful |
the entry block is always useful | if.end.term |
| 3 | mark entry.term |
unconditional branch of useful block entry |
if.end.term entry.term |
| 4 | block if.end useful |
contains live if.end.term |
entry.term |
| 5 | mark s.0 |
operand of if.end.term |
entry.term s.0 |
| 6 | block for.cond useful |
contains live s.0 |
— |
| 7 | mark for.cond.term |
for.cond is control dependent on for.cond |
for.cond.term |
| 8 | mark add |
operand of s.0 |
for.cond.term add |
| 9 | block for.body useful |
incoming block of live phi s.0 |
for.cond.term add |
| 10 | mark for.body.term |
unconditional branch of useful block for.body |
for.cond.term add for.body.term |
| 11 | mark cmp |
operand of for.cond.term |
add for.body.term cmp |
| 12 | mark i.0 |
operand of add |
for.body.term cmp i.0 |
| 13 | mark inc |
operand of i.0 |
inc |
(Rows 5 and 6 each come from popping one instruction; the table shows every change of the live sets.) Useful blocks: entry, for.cond, for.body, if.end. for.end is not useful: nothing live is in it and no useful block is control dependent on it. Its dead branch is rewritten to its nearest useful post-dominator, \(\mathrm{ipdom}(\text{for.end}) = \text{if.end}\); if.then becomes unreachable and is deleted:
Dead: t.0, mul, add1, cmp2, for.end.term (rewritten), mul3, if.then.term. The loop survives because s.0 is live and for.cond is control dependent on itself. Loops: if work returned a instead of s.0, nothing in the loop would be live, and ADCE would rewrite for.cond's branch to if.end — deleting the whole loop (scalaropt.adce reports 14 dead instructions); with remove_loops=False (rule (6)), for.body's back-edge branch is a root and the loop control (i.0, inc, cmp) stays.
Try it
./course drill adce-marking --seed 2 --difficulty hard --solution: dead instructions, branch
retargets, and what ADCE removes beyond mark–sweep DCE.
Dead stores and dead branches¶
In @inter of the §7 box, block a computes %y and stores it into a local alloca that is never loaded. ADCE alone treats the store as a root, marks %y, marks block a useful, and so marks the branch in entry (block a is control dependent on entry): nothing is removed. DSE's rule 2 deletes the store (the alloca never escapes and is never read). Now a contains only br, is not useful, and ADCE rewrites entry's branch to br label %j. The order matters: DSE → ADCE removes everything, ADCE → DSE leaves the empty diamond for SimplifyCFG.
4. Invariants and correctness¶
Lemma 17.3.8 (The marking computes the least sets)
Let \(F\) be a function in SSA form with CFG \(G = (N, E, r)\), instructions \(\mathrm{Inst}\) and \(U\) operand uses, satisfying the preconditions of Algorithms 17.3.2 and 17.3.5, with control dependences \(\mathrm{CD}\) as in Definition 17.3.3. Then Algorithm 17.3.2 terminates after \(O(\lvert \mathrm{Inst} \rvert + U)\) steps with \(M = \mathcal{U}\), the useful set of Definition 17.3.1 for \(R_b\) = all terminators; and Algorithm 17.3.5 terminates after \(O(\lvert \mathrm{Inst} \rvert + U + \sum_{B \in N} \lvert \mathrm{CD}(B) \rvert)\) steps with \(\mathit{Live} = \mathcal{L}\) and \(\mathit{Useful} = \mathcal{B}\), the sets of Definition 17.3.4.
Proof
Every instruction and block is marked at most once (the membership test guards each marking) and pushed
at most once; popping an instruction scans its operands (total \(U\)) and, for a phi, its incoming
blocks; marking a block \(B\) scans \(\mathrm{CD}(B)\) once. Soundness of the marks: by induction on steps,
each marking applies one closure rule of Definition 17.3.1 or 17.3.4 to already-marked objects (or marks
a root or the entry), so every marked object is in the least sets: \(M \subseteq \mathcal{U}\),
\(\mathit{Live} \subseteq \mathcal{L}\) and \(\mathit{Useful} \subseteq \mathcal{B}\). Completeness: at
termination the worklist is empty, so every marked instruction has had rule (3) and, if a phi, rule (4)
applied, and every marked block has had rule (5) applied (inside MarkBlock); the marked sets are closed
under all rules and contain the roots and the entry, so by leastness they contain \(\mathcal{U}\),
respectively \(\mathcal{L}\) and \(\mathcal{B}\).
Theorem 17.3.9 (Mark–sweep DCE preserves behavior)
Let \(F\) be a function in SSA form with instructions \(\mathrm{Inst}\), roots as in Definition 17.3.1 (every instruction with a side effect is a root), and let \(\mathcal{U}\) be its useful set for \(R_b\) = all terminators. Then the function obtained by deleting every instruction of \(\mathrm{Inst} \setminus \mathcal{U}\) (the output of Algorithm 17.3.2) has the same observable behavior as \(F\) on every input.
Proof
The CFG is unchanged because every terminator is kept. Compare a run of the original and of the new program on the same input: by induction on the number of executed steps, both follow the same block sequence and every kept instruction computes the same value. The step for a kept instruction \(I\): its operands are definitions in \(\mathcal{U}\) (closure under rule "operands of a member"), which by the induction hypothesis hold equal values; a deleted instruction has no side effect (it is not a root) and its value is used only by deleted instructions (if a kept one used it, closure would have kept it). Branches are kept with equal conditions, so the block sequences agree; roots are kept with equal operands, so the observable behavior agrees.
Lemma 17.3.10 (Post-dominators are met in chain order)
Let \(G = (N, E, r)\) be a flowgraph in which every block reaches an exit, with \(\mathrm{ipdom}\) its immediate post-dominator function with respect to a virtual exit (Definition 17.3.3). Let \(X \in N\) and define the chain \(p_0 = X\), \(p_{k+1} = \mathrm{ipdom}(p_k)\). Then for every \(j \ge 0\) and every path from \(X\) to an exit, the first occurrence of \(p_j\) on the path precedes the first occurrence of \(p_{j+1}\).
Proof
Every \(p_j\) post-dominates \(X\), so each occurs on the path. Suppose on some path the first \(p_{j+1}\) comes before the first \(p_j\). The prefix up to that \(p_{j+1}\) avoids \(p_j\). The suffix from \(p_{j+1}\) must contain \(p_j\) (the whole path does, and the prefix does not). If some path from \(p_{j+1}\) to the exit avoided \(p_j\), gluing it to the prefix would give a path from \(X\) to the exit avoiding \(p_j\), contradicting \(p_j \mathrel{\mathrm{pdom}} X\). So \(p_j \mathrel{\mathrm{pdom}} p_{j+1}\); also \(p_{j+1} \mathrel{\mathrm{pdom}} p_j\) by definition of the chain, and post-dominance is antisymmetric (Ch 15, Theorem 15.1.3 on the reverse CFG): \(p_j = p_{j+1}\), impossible for an immediate post-dominator.
Theorem 17.3.11 (ADCE preserves behavior of terminating runs)
Let \(F\) be a function in SSA form with CFG \(G = (N, E, r)\) in which every block reaches an exit, with roots as in Definition 17.3.1 (every instruction with a side effect is a root), control dependences \(\mathrm{CD}\) as in Definition 17.3.3, and live set \(\mathcal{L}\) and useful blocks \(\mathcal{B}\) as in Definition 17.3.4. Let \(F'\) be the output of Algorithm 17.3.5 on \(F\). Then every run of \(F\) that terminates has a run of \(F'\) on the same input with the same observable behavior; if moreover rule (6) is on, or every loop of \(F\) terminates, the runs of \(F\) and \(F'\) correspond one to one.
Proof
Key claim: from a block \(X\) whose branch is dead, the next useful block after \(X\) on any terminating path is \(Y\), the nearest useful strict post-dominator of \(X\) (the target the sweep chooses). Take a path \(X = w_0 \to w_1 \to \dots\) and let \(Z = w_k\) (\(k \geq 1\)) be its first useful block after \(X\). Suppose \(Z\) does not post-dominate \(X\). Let \(i < k\) be the largest index such that \(Z\) does not post-dominate \(w_i\) (\(i = 0\) qualifies). Then \(Z \mathrel{\mathrm{pdom}} w_{i+1}\) (\(w_{i+1} = Z\) when \(i + 1 = k\)), and \(Z\) does not post-dominate \(w_i\), so \(w_i \in \mathrm{CD}(Z)\) (Definition 17.3.3). Since \(Z \in \mathcal{B}\), rule (5) put \(w_i\)'s terminator in \(\mathcal{L}\), so \(w_i \in \mathcal{B}\) (rule (3)); for \(i \geq 1\) this contradicts the choice of \(Z\), and for \(i = 0\) it contradicts "\(X\)'s branch is dead". So \(Z\) post-dominates \(X\) and is useful; by Lemma 17.3.10 the first useful post-dominator reached on the path is the nearest one in the chain: \(Z = Y\).
Phis. If \(Y\) has a live phi, rule (4) made every predecessor of \(Y\) useful; the block before \(Y\) on the path is then useful, so by the key claim it is \(X\) itself: the path uses the edge \(X \to Y\), which the rewrite keeps, and the phi selects the same operand. If \(Y\) has no live phi, the new edge's phi entries are irrelevant (dead phis are deleted).
Simulation. Consider a terminating run of \(F\). Restrict it to the useful blocks it visits,
\(B_1, B_2, \dots\). The run of \(F'\) visits the same useful blocks in the same order: between consecutive
useful blocks the original passes only non-useful blocks, whose branches are dead (a live branch would
make its block useful by rule (3)); in \(F'\), a useful block with a live branch goes to the same successor
(same condition value, by the data argument below), and one with a dead branch jumps to \(Y\), which is the
next useful block by the key claim. Live instructions in useful blocks compute equal values by induction
on the run, since their operands are live (rule (3)) and phis select the same incoming operand. Roots are
live, so observable behavior agrees. Termination: the key claim used that the original path reaches
\(Y\). If the original run loops forever in non-useful blocks after \(X\), \(F'\) jumps to \(Y\) and continues: a
non-terminating run became terminating, a behavior change unless the language allows it (C++ forward
progress, LLVM's mustprogress). Rule (6) prevents it: an infinite run repeats some cycle of the CFG,
every cycle contains a retreating edge of the DFS (Definition 17.3.4), and rule (6) put that edge's
terminator in \(\mathcal{L}\), so its source is in \(\mathcal{B}\). So an infinite run visits useful blocks
infinitely often, the next useful block \(Z\) after \(X\) always exists, and the key claim (which did not use
termination) applies to it. (With dominance back edges instead of retreating edges this step fails on
irreducible loops, which is why LLVM's adce finds the edges with a DFS.)
Theorem 17.3.12 (Dead-store elimination is sound)
Let \(F\) be a function in SSA form with locations \(\mathrm{Loc}\), and let \(\mathrm{mayAlias}\) and \(\mathrm{mustOverwrite}\) be a sound alias oracle as in Algorithm 17.3.7. Then every store deleted by Algorithm 17.3.7 on \(F\) is dead in the sense of Definition 17.3.6, and deleting a dead store preserves the observable behavior of \(F\) on every input.
Proof
Rule 1: when a store \(s\) to a location \(\ell\) is deleted, a later store \(c\) in the same block with
\(\mathrm{mustOverwrite}(c, \ell)\) is in Covered, and by the invariant no instruction between them may
read a location aliasing \(c\) (such an instruction would have removed \(c\) from Covered), hence none may
read \(\ell\) (a read of \(\ell\) reads bytes that \(c\) overwrites, which alias \(c\)); every path from \(s\)
passes the block's next instructions in order, so \(\ell\) is overwritten before any read. Rule 2: no
instruction can read the alloca (its address does not escape, and no load or call reads it), and its
lifetime ends at return. Behavior: a load returns the value of the last store to its bytes; for a dead
store that is never the dead store on any path, so every load and every observable effect sees the same
values.
What breaks it. Without the "every block reaches an exit" precondition the post-dominator tree needs a virtual exit and infinite loops have no post-dominators; LLVM marks their branches live up front (the §7 box on adce), and pebble-adce treats blocks that cannot reach an exit the same way (tests/ch17/lit/adce.ll, @forever). Without a sound side-effect model (a call assumed pure that writes memory) every DCE deletes observable behavior.
5. Complexity¶
Write \(I\) for the number of instructions, \(U\) for the number of operand uses, \(n\) for the number of blocks and \(e\) for the number of edges.
| Technique | Time (worst) | Time (typical) | Space | Justification |
|---|---|---|---|---|
| Worklist DCE (Ch 13) | \(O(I + U)\) | linear | \(O(I)\) | each instruction deleted once, each deletion re-queues its operands |
| Mark–sweep DCE (17.3.2) | \(O(I + U)\) | linear | \(O(I)\) | Lemma 17.3.8 with no CD scans |
| ADCE (17.3.5) | \(O(I + U + n^2)\): \(\sum_B \lvert\mathrm{CD}(B)\rvert\) can be \(\Theta(n^2)\) | near-linear: LLVM computes only the needed part of the post-dominance frontier with an IDF calculator per batch of new useful blocks | \(O(I + n)\) | Lemma 17.3.8; the post-dominator tree costs \(O(e\,\alpha(e, n))\) (Ch 15) |
| DSE rule 1 (block-local) / MemorySSA DSE | \(O(I \cdot k)\) with \(k\) live Covered entries / \(O(I \cdot s)\) with a scan limit \(s\) |
linear | \(O(I)\) | one backward scan per block; LLVM caps its walks (-dse-memoryssa-scanlimit) |
Pathological family for control dependence. Nest \(n\) bottom-tested loops: \(r \to H_1 \to H_2 \to \dots \to H_n \to T_n\), where latch \(T_k\) branches back to \(H_k\) or on to \(T_{k-1}\), and \(T_1\) branches to \(H_1\) or to the exit \(X\). The innermost header \(H_n\) is control dependent on every latch \(T_1, \dots, T_n\) (each decides whether \(H_n\) runs again), and in general \(H_k\) and \(T_k\) depend on \(T_1, \dots, T_k\): \(\sum \lvert \mathrm{CD} \rvert = n(n+1)\), i.e. 6, 20, 72 and 272 pairs for \(n = 2, 4, 8, 16\) (computed with cfa.control_dependence). An ADCE that materializes \(\mathrm{CD}\) pays \(\Theta(n^2)\) on this family, which is why LLVM never builds it and asks an IDF calculator only about blocks that just became useful. At scale, ADCE and DCE are cheap enough that LLVM runs adce in every function simplification pipeline and GCC 14 runs dce/cddce eleven times at -O2 (gcc-14 -O2 -fdump-passes).
6. Variants and refinements¶
- Kill cycles only (partial aggressiveness) — LLVM's
adce-remove-control-flow=falsekeeps every branch: exactly Algorithm 17.3.2 [LLVM-ADCE]. Trade-off: no CFG changes, so dominator trees stay valid. - Loops that may not terminate — LLVM keeps back-edge branches unless
-adce-remove-loops(§7 box); GCC'scddcefirst asks whether each loop is finite (finite_loop_p) and removes only finite ones [GCC-DCE]. Trade-off: soundness for C (no forward-progress guarantee in some cases) against deleting more code;mustprogress(C++, Rust) gives the license. - Partial dead code elimination — sink a computation into the branches that use it so it becomes dead on the others (Knoop–Rüthing–Steffen 1994, the dual of PRE; GVN-sink, Lesson 17.5 §6). Trade-off: code motion machinery.
- Bit-level DCE — demanded bits (Lesson 17.2) deletes computations whose bits do not matter.
- Dead argument and dead global elimination — the interprocedural versions (Ch 20).
- Clean — the companion of Dead in EaC: fold redundant branches, remove empty blocks, merge and hoist after DCE [EaC3, Ch. 10]; Lesson 17.7 covers it as SimplifyCFG.
7. In real compilers¶
Mark–sweep DCE¶
LLVM's adce with -adce-remove-control-flow=false is mark–sweep DCE; GCC's plain dce passes are the same algorithm without control dependence (perform_tree_ssa_dce with aggressive = false, find_obviously_necessary_stmts for the roots) [GCC-DCE]. LLVM's dce pass is the trivially-dead worklist [LLVM-DCE].
Cycles: worklist DCE vs mark–sweep
Reproduce (clang 23.1.2, opt 23.1.2):
cat > dce.c <<'EOF'
int work(int n, int a) {
int s = 0, t = 0;
for (int i = 0; i < n; i++) {
s = s + i; // live: returned
t = t + a * i; // dead: never used after the loop
}
if (a > 5)
t = t * 2; // dead branch arm
return s;
}
EOF
clang-23 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -S -emit-llvm dce.c -o dce.O0.ll
opt -passes=mem2reg -S dce.O0.ll -o dce.ll
opt -passes=dce -S dce.ll | grep -E 't\.0 =|add1 =|mul ='
opt -passes=adce -adce-remove-control-flow=false -S dce.ll | sed -n '/^for.end:/,/^}/p'
Output:
%t.0 = phi i32 [ 0, %entry ], [ %add1, %for.inc ]
%mul = mul nsw i32 %a, %i.0
%add1 = add nsw i32 %t.0, %mul
for.end: ; preds = %for.cond
%cmp2 = icmp sgt i32 %a, 5
br i1 %cmp2, label %if.then, label %if.end
if.then: ; preds = %for.end
br label %if.end
if.end: ; preds = %if.then, %for.end
ret i32 %s.0
}
What to notice: the worklist pass (dce) keeps the cycle %t.0/%add1 and %mul (each has a use);
mark–sweep deletes them and %mul3, but keeps the branch on %cmp2 around the now empty if.then —
the §3 mark–sweep trace.
Aggressive DCE¶
LLVM: llvm/lib/Transforms/Scalar/ADCE.cpp — AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences asks a ReverseIDFCalculator (the post-dominance frontier) for the dead terminators controlling newly live blocks; initialize marks back-edge branches live unless adce-remove-loops, and marks every block that cannot reach a return live; updateDeadRegions rewrites a dead branch to its successor with the largest post-order number of the reverse CFG, a cheap stand-in for "nearest post-dominator" [LLVM-ADCE]. GCC: gcc/tree-ssa-dce.cc — mark_control_dependent_edges_necessary in the cddce passes [GCC-DCE].
LLVM adce removes the dead branch; loops need permission
Reproduce (opt 23.1.2; dce.ll from the previous box):
opt -passes=adce -S dce.ll | sed -n '/^for.end:/,/^}/p'
cat > spin.ll <<'EOF'
define i32 @spin(i32 %n) {
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i1, %loop ]
%i1 = add i32 %i, 1
%c = icmp ne i32 %i1, %n ; never false if n <= 0: may not terminate
br i1 %c, label %loop, label %exit
exit:
ret i32 0
}
EOF
opt -passes=adce -S spin.ll | sed -n '/^loop:/,/^exit:/p'
opt -passes=adce -adce-remove-loops -S spin.ll | sed -n '/^loop:/,/^exit:/p'
Output:
for.end: ; preds = %for.cond
br label %if.end
if.then: ; No predecessors!
br label %if.end
if.end: ; preds = %for.end, %if.then
ret i32 %s.0
}
loop: ; preds = %loop, %entry
%i = phi i32 [ 0, %entry ], [ %i1, %loop ]
%i1 = add i32 %i, 1
%c = icmp ne i32 %i1, %n
br i1 %c, label %loop, label %exit
exit: ; preds = %loop
loop: ; preds = %entry
br label %exit
exit: ; preds = %loop
What to notice: the first output is the §3 ADCE result (LLVM leaves the unreachable if.then for
SimplifyCFG). @spin computes nothing, but %c is never false when n ≤ 0, so deleting the loop could
make a hanging program terminate: by default the back-edge branch is a root (rule (6)). With
-adce-remove-loops the loop is removed — Theorem 17.3.11's termination assumption made explicit.
GCC cddce: marking through control dependence
Reproduce (gcc 14.2.0; dce.c from the first box):
gcc-14 -O2 -fdump-tree-cddce1-details -S dce.c -o /dev/null
grep -E 'Marking useful|Found loop|Deleting :|Removing basic' dce.c.*t.cddce1
Output:
Marking useful stmt: return s_11;
Found loop 1 to be finite: upper bound found.
Marking useful stmt: if (i_4 < n_8(D))
Deleting : t_14 = _1 + t_3;
Deleting : _1 = i_4 * a_9(D);
Deleting : if (a_9(D) > 5)
Deleting : t_3 = PHI <0(2), _14(3)>
Removing basic block 6
What to notice: the only root is the return; the loop test becomes useful because s is computed
under it (control dependence), after GCC proved the loop finite — its version of rule (6). The dead cycle
and the dead if (a > 5) are deleted, as in the ADCE trace of §3.
Dead stores and dead branches¶
LLVM: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp — eliminateDeadStores walks MemorySSA from each store to find a killing store or prove no read follows [LLVM-DSE]; in the -O2 pipeline DSE runs right after ADCE [LLVM-Pipelines]. GCC has tree-ssa-dse.cc.
DSE unlocks ADCE
Reproduce (opt 23.1.2):
cat > inter.ll <<'EOF'
define i32 @inter(i32 %x, i1 %c) {
entry:
%tmp = alloca i32
br i1 %c, label %a, label %j
a:
%y = mul i32 %x, 3
store i32 %y, ptr %tmp ; never loaded: a dead store
br label %j
j:
ret i32 %x
}
EOF
opt -passes=adce -S inter.ll | sed -n '/^entry:/,/^}/p'
opt -passes='dse,adce' -S inter.ll | sed -n '/^entry:/,/^}/p'
Output:
entry:
%tmp = alloca i32, align 4
br i1 %c, label %a, label %j
a: ; preds = %entry
%y = mul i32 %x, 3
store i32 %y, ptr %tmp, align 4
br label %j
j: ; preds = %a, %entry
ret i32 %x
}
entry:
br label %j
a: ; No predecessors!
br label %j
j: ; preds = %entry, %a
ret i32 %x
}
What to notice: to ADCE the store is a root, which keeps %y and the branch; after DSE's rule 2
(a non-escaping alloca that is never read) deletes it, ADCE removes %y, the alloca and the branch.
The two analyses are one fixed point that the pipeline approximates by ordering (Lesson 17.8).
Find where LLVM does it. In llvm/lib/Transforms/Scalar/ADCE.cpp, which command-line option decides whether branches that end a back edge are marked live at the start, and what is its default? (Quiz llvm-where-adce-loops.)
8. Comparison¶
| Technique | Power / precision | Speed (asymptotic · practical) | Output / error quality | Implementation effort | Typical use |
|---|---|---|---|---|---|
| Mark–sweep DCE | Removes dead cycles; keeps every branch | \(O(I + U)\) · linear | CFG unchanged | Low | Cheap cleanup; LLVM adce without control flow, GCC dce |
| Aggressive DCE | Also removes dead branches and dead loops (Theorem 17.3.11) | \(O(I + U + \lvert\mathrm{CD}\rvert)\) · near-linear with IDF | Rewrites branches to post-dominators | Medium (post-dominators, control dependence, termination policy) | LLVM adce, GCC cddce, pebble-adce |
| Dead-store elimination | Removes overwritten / never-read stores; enables ADCE | \(O(I \cdot s)\) with MemorySSA scan limits | Deletes memory operations | High (alias analysis, partial overwrites) | LLVM dse, GCC dse |
Choose mark–sweep DCE when the CFG must not change (between passes that keep dominator trees) and dead cycles are the target. Choose ADCE when you want dead control flow gone too, and you have post-dominators and a termination policy. Choose DSE when memory operations are the dead code; run it before ADCE so the branches it frees can go.
9. Assessment¶
- Quiz:
marksweep-dead,marksweep-cycles(tagmarksweep-dce);adce-dead,adce-retarget,llvm-where-adce-loops(tagadce);dse-interplay,dse-rule(tagdse). - Drill:
./course drill adce-marking(hardcompares with mark–sweep DCE). Dead-store elimination has no drill: its difficulty is alias reasoning, which Ch 19's drills cover; the quiz has an instance. - Flashcards: tags
marksweep-dce,adce,dse. - Exercises: E2
pebble-adce.
References¶
See the chapter references.