Skip to content

Lesson 11.3 — Switch lowering: jump tables, binary search, bit tests, and LLVM's switch

Techniques: jump tables — one range check and an indirect jump through a table indexed by the value (Sale 1981; Bernstein 1985); binary search trees of comparisons over the sorted cases (Hennessy & Mendelsohn 1982; Bernstein 1985); bit tests — one shift and one mask test per destination when the cases fit in a machine word (LLVM, GCC); LLVM's switch instruction plus back-end lowering, which partitions the cases into clusters of each kind (Kannan & Proebsting 1994) and searches over the clusters · Pebble implements: PIR's switch terminator becomes an LLVM switch in the code generator (E5); all four strategies in the comparison lab (labs/ch11-lowering, part B) · Prerequisites: Lesson 11.2 (branch costs) · Time: 4–5 hours

A switch (C's switch, Rust's match on integers, PIR's switch terminator) selects one of several destinations by the value of an integer. Pebble's surface language has no switch, but PIR does (pir-spec §11) — a Rust front end lowers match to it (Ch 24) — and the shared back end (E5) must compile it. The question is how: for a switch with \(n\) cases, \(n\) comparisons in a row cost \(O(n)\) per dispatch; the alternatives trade table space or word-sized masks for \(O(1)\) or \(O(\log n)\) dispatch. This lesson uses three running switches, all measured in the lab and compiled by Clang and GCC in §7:

name cases (value → destination) what LLVM picks
chars 9, 10, 13, 32 → A (space); 48…57 → B (digit); 43, 45 → C (sign) bit tests
op 0, 1 → A; 2, 3 → B; 4 → C; 6 → D one jump table
sparse 3 → A, 70 → B, 911 → C, 1200 → D, 5000 → E a binary tree

1. Problem and motivation

Jump tables

A table with one code address per value between the least and the greatest case value turns dispatch into a bounds check and an indirect jump, whatever the number of cases. Pascal compilers used it for dense case statements; Sale discussed when a table is worth its space [Sal81], and Bernstein gave the classic heuristic of splitting a sparse switch into dense tables joined by comparisons [Ber85]. It is the fastest dispatch for dense switches, but its size is the range, not the number of cases, and an indirect branch is harder to predict than a conditional one. The lab's JumpTable builds it in IR with blockaddress constants and indirectbr.

Binary search trees

Sorting the case values and comparing against the median halves the candidates with each comparison, so a sparse switch of \(n\) cases costs \(O(\log n)\) comparisons and \(O(n)\) code, independent of the range [HM82, Ber85]. It is the fallback when no dense range exists (sparse above), and LLVM uses it to combine the other kinds: its tree is built over clusters, not single values.

Bit tests

When all case values fit in a word (greatest minus least value below 64) and there are few destinations, the test "does the scrutinee go to this destination?" is one AND of a single bit, shifted by the scrutinee's offset from the least case value, with a precomputed mask per destination. Three destinations with 16 values cost one range check and at most three tests: chars classifies characters this way in both Clang and GCC (§7). LLVM builds them in buildBitTests [LLVM-SwitchLowering]; GCC in its bit_test_cluster [GCC-SwitchConv].

LLVM's switch and back-end lowering

LLVM IR keeps a switch as one instruction until instruction selection, where SelectionDAGBuilder::visitSwitch partitions the sorted cases into clusters — ranges, jump tables and bit tests — and emits a balanced binary tree over the clusters [LLVM-SDB]. The partition into the fewest jump-table-dense pieces is Kannan and Proebsting's dynamic program [KP94]. A front end should therefore emit switch and let the back end choose; the lab's LLVMSwitch strategy is ten lines, and the lab measures how close each hand-written strategy gets to it. Pebble's code generator emits switch for PIR's terminator (FunctionLowering::emitTerminator).

2. Definitions and algorithms

Notation follows the chapter's Notation section. Throughout the lesson, let \(w \ge 1\) be the word width in bits, \(\mathbb{Z}_w \triangleq [-2^{w-1}, 2^{w-1})\) the set of \(w\)-bit two's-complement integers, \(x \in \mathbb{Z}_w\) the scrutinee (the value a switch dispatches on), \(N\) the set of blocks of the flowgraph, and \(S\) a switch with cases \((v_1, d_1), \dots, (v_n, d_n)\), default \(d_0\), least case value \(v_{\min}\) and greatest case value \(v_{\max}\), \(R\) its range and \(\delta\) its dispatch function (Definition 11.3.1). Every numbered box restates the objects it uses.

Definition 11.3.1 (Switch, range, density, cluster)

Let \(w \ge 1\) and let \(N\) be the set of blocks of a flowgraph. A switch on a scrutinee \(x \in \mathbb{Z}_w\) is a pair \(S = (\{(v_1, d_1), \dots, (v_n, d_n)\}, d_0)\) of a finite set of cases, \(n \ge 1\), with pairwise distinct case values \(v_i \in \mathbb{Z}_w\) and destinations \(d_i \in N\), and a default destination \(d_0 \in N\). Its dispatch function \(\delta : \mathbb{Z}_w \to N\) is \(\delta(x) \triangleq d_i\) if \(x = v_i\) for some \(i\), and \(\delta(x) \triangleq d_0\) otherwise. Let \(v_{\min} \triangleq \min_{1 \le i \le n} v_i\) and \(v_{\max} \triangleq \max_{1 \le i \le n} v_i\); the range of \(S\) is \(R \triangleq v_{\max} - v_{\min} + 1\) and its density is \(n / R\).

Number the cases so that \(v_1 < v_2 < \dots < v_n\). A cluster is a non-empty set of cases \(C = \{(v_i, d_i), \dots, (v_j, d_j)\}\) with \(1 \le i \le j \le n\) (consecutive in sorted order), together with a kind in \(\{\mathrm{range}, \mathrm{jump\text{-}table}, \mathrm{bit\text{-}test}\}\), which says how it is dispatched; write \(\mathrm{lo}(C) \triangleq v_i\), \(\mathrm{hi}(C) \triangleq v_j\) and \(\mathrm{cases}(C) \triangleq j - i + 1\). A range cluster is a cluster of kind range whose values are consecutive integers (\(v_{k+1} = v_k + 1\) for \(i \le k < j\)) and whose destinations are all equal. A clustering of \(S\) is a sequence \(C_0, \dots, C_{M-1}\) of clusters, \(M \ge 1\), that partitions the cases in sorted order (\(\mathrm{hi}(C_k) < \mathrm{lo}(C_{k+1})\) for \(0 \le k < M - 1\)). For a run \(C_i, \dots, C_j\) of a clustering, \(0 \le i \le j < M\), its case count is \(\mathrm{cases}(i, j) \triangleq \sum_{k=i}^{j} \mathrm{cases}(C_k)\), its span is \(\mathrm{span}(i, j) \triangleq \mathrm{hi}(C_j) - \mathrm{lo}(C_i) + 1\) and its density is \(\mathrm{cases}(i, j) / \mathrm{span}(i, j)\).

Lemma 11.3.2 (One unsigned comparison is a range check)

Let \(w \ge 1\) and let \(x, v_{\min}, v_{\max} \in \mathbb{Z}_w\) with \(v_{\min} \le v_{\max}\). Let \(\mathit{idx} \triangleq (x - v_{\min}) \bmod 2^w \in [0, 2^w)\) (the result of a wrapping sub, read as an unsigned integer). Then \(v_{\min} \le x \le v_{\max}\) if and only if \(\mathit{idx} \le v_{\max} - v_{\min}\).

Proof

Write values as mathematical integers with \(-2^{w-1} \le x, v_{\min}, v_{\max} < 2^{w-1}\). If \(v_{\min} \le x \le v_{\max}\), then \(0 \le x - v_{\min} \le v_{\max} - v_{\min} \le 2^w - 1\), so the subtraction does not wrap and \(\mathit{idx} = x - v_{\min} \le v_{\max} - v_{\min}\). If \(x > v_{\max}\), then \(v_{\max} - v_{\min} < x - v_{\min} \le 2^w - 1\), so again no wrap and \(\mathit{idx} > v_{\max} - v_{\min}\). If \(x < v_{\min}\), then \(x - v_{\min} < 0\) and \(\mathit{idx} = x - v_{\min} + 2^w\); since \(x \ge -2^{w-1}\) and \(v_{\max} < 2^{w-1}\), \(\mathit{idx} - (v_{\max} - v_{\min}) = x + 2^w - v_{\max} > -2^{w-1} + 2^w - 2^{w-1} = 0\), so \(\mathit{idx} > v_{\max} - v_{\min}\).

Algorithm 11.3.3 (Jump table)

  • Input: a switch \(S = (\{(v_1, d_1), \dots, (v_n, d_n)\}, d_0)\) on a scrutinee \(x \in \mathbb{Z}_w\), with \(n \ge 1\), least value \(v_{\min}\), range \(R\) and dispatch function \(\delta\) (Definition 11.3.1), where \(N\) is the set of blocks of the flowgraph.
  • Output: code that transfers control to \(\delta(x)\).
  • Precondition: \(R\) is small enough to allocate (LLVM: density \(\ge 10\,\%\); the lab: \(R < 4096\)).
  • Postcondition: control reaches \(d_i\) iff \(x = v_i\), and \(d_0\) for every other \(x\).
  • State: a table \(\mathit{table} : [0, R) \to N\) of destinations, one entry per value of \([v_{\min}, v_{\max}]\).
  • Invariant: for every \(k\) with \(0 \le k < R\), \(\mathit{table}[k] = \delta(v_{\min} + k)\): the destination of the case with value \(v_{\min} + k\), or \(d_0\) if there is none.
function JumpTable(S = (cases, d0)):
    table[0 .. R-1] ← d0
    for (v, d) in cases: table[v - vmin] ← d
    emit:  idx ← x - vmin                      # wrapping
           if idx >u R - 1: goto d0             # Lemma 11.3.2
           goto *table[idx]                     # indirectbr / jmpq *

Algorithm 11.3.4 (Balanced binary search)

  • Input: a switch \(S\) on a scrutinee \(x \in \mathbb{Z}_w\) with cases \((v_1, d_1), \dots, (v_n, d_n)\) numbered so that \(v_1 < \dots < v_n\), and default \(d_0\) (Definition 11.3.1); two indices \(\mathit{lo}\), \(\mathit{hi}\) with \(1 \le \mathit{lo} < \mathit{hi} \le n + 1\).
  • Output: a tree of comparisons whose leaves are equality tests; write \(\mathrm{Search}(\mathit{lo}, \mathit{hi})\) for the call that emits the tree for the cases \(\mathit{lo}, \dots, \mathit{hi} - 1\). The whole switch is \(\mathrm{Search}(1, n + 1)\).
  • Precondition: the case values are distinct and sorted.
  • Postcondition: control reaches \(d_i\) iff \(x = v_i\), else \(d_0\); at most \(\lceil \log_2 n \rceil + 1\) comparisons on any path (Theorem 11.3.5).
  • Invariant: when \(\mathrm{Search}(\mathit{lo}, \mathit{hi})\) runs, every case that \(x\) could still equal is among the cases \(\mathit{lo}, \dots, \mathit{hi} - 1\).
function Search(lo, hi):
    if hi - lo = 1:  emit  if x = v[lo]: goto d[lo] else goto d0
    else:
        mid ← lo + ⌊(hi - lo) / 2⌋
        emit  if x <s v[mid]: goto L else goto G
        in L: Search(lo, mid);   in G: Search(mid, hi)

Theorem 11.3.5 (Correctness and depth of the search)

Let \(S\) be a switch on a scrutinee \(x \in \mathbb{Z}_w\) with \(n \ge 1\) cases, default \(d_0\) and dispatch function \(\delta\) (Definition 11.3.1), and write \(\mathrm{Search}\) for the procedure of Algorithm 11.3.4. The code emitted by \(\mathrm{Search}(1, n + 1)\) transfers control to \(\delta(x)\) for every \(x \in \mathbb{Z}_w\), and executes at most \(\lceil \log_2 n \rceil + 1\) comparisons on every path.

Proof

Correctness by induction on \(\mathit{hi} - \mathit{lo}\), using the invariant of Algorithm 11.3.4. One case: the equality test is exact. More cases: if \(x < v_{\mathit{mid}}\) then, the values being sorted, \(x\) can only equal a case with index in \([\mathit{lo}, \mathit{mid})\); otherwise only one with index in \([\mathit{mid}, \mathit{hi})\); both halves are non-empty since \(\mathit{lo} < \mathit{mid} < \mathit{hi}\), and the hypothesis applies. Depth: let \(C(m)\) be the worst-case number of comparisons for \(m \ge 1\) cases: \(C(1) = 1\) and \(C(m) = 1 + C(\lceil m / 2 \rceil)\) for \(m \ge 2\), the larger half having \(\lceil m/2 \rceil\) cases. By induction \(C(m) = \lceil \log_2 m \rceil + 1\): for \(m \ge 2\), \(\lceil \log_2 \lceil m/2 \rceil \rceil = \lceil \log_2 m \rceil - 1\).

Algorithm 11.3.6 (Bit tests)

  • Input: a switch \(S = (\{(v_1, d_1), \dots, (v_n, d_n)\}, d_0)\) on a scrutinee \(x \in \mathbb{Z}_w\) with \(n \ge 1\) cases, its least value \(v_{\min}\) and its greatest value \(v_{\max}\) (Definition 11.3.1), whose set of destinations \(\mathrm{Dests}(S) \triangleq \{d_1, \dots, d_n\}\) has \(D \triangleq \lvert \mathrm{Dests}(S) \rvert\) elements (LLVM: \(D \le 3\)).
  • Output: a range check and \(D\) mask tests.
  • Precondition: \(v_{\max} - v_{\min} < w\) (the masks fit in a word).
  • Postcondition: control reaches \(d\) iff \(x\) is a case value with destination \(d\), else \(d_0\).
  • State: for every destination \(d \in \mathrm{Dests}(S)\), write \(\mathit{mask}_d\) for its \(w\)-bit mask, \(\mathit{mask}_d \triangleq \sum_{i \in I_d} 2^{v_i - v_{\min}}\) where \(I_d \triangleq \{\, i \mid 1 \le i \le n,\ d_i = d \,\}\) is the set of cases going to \(d\).
  • Invariant: for every \(k\) with \(0 \le k < w\), bit \(k\) of \(\mathit{mask}_d\) is 1 iff \(v_{\min} + k\) is a case value with destination \(d\).
function BitTests(S = (cases, d0)):
    for each destination d: mask_d ← Σ { 2^(v - vmin) : (v, d) ∈ cases }
    emit:  idx ← x - vmin;  if idx >u vmax - vmin: goto d0     # Lemma 11.3.2
           bit ← 1 << idx
           for d in Dests(S), most bits first:
               if bit & mask_d ≠ 0: goto d
           goto d0

Algorithm 11.3.7 (LLVM's switch lowering: clusters, then a tree)

  • Input: a switch \(S\) (Definition 11.3.1) on a 64-bit target (\(w = 64\)), at -O1 or above.
  • Output: a clustering \(C_0, \dots, C_{M-1}\) of \(S\) whose clusters are of kind range, jump-table or bit-test, and a binary tree over them.
  • Precondition: no profile (equal case probabilities); not optimizing for size.
  • Postcondition: the clusters partition the cases in sorted order; each jump-table cluster is a run of \(\ge 4\) range clusters with density \(\ge 10\,\%\) that is not suitable for bit tests; each bit-test cluster is a run of \(\ge 2\) range clusters with span \(\le 64\) and \(\le 3\) destinations that is suitable for bit tests.
  • State: the current clustering \(C_0, \dots, C_{M-1}\) with \(M\) its length, and three arrays \(\mathit{MinPart}, \mathit{Last}, \mathit{Score} : [0, M) \to \mathbb{Z}\), indexed by a cluster index \(i\).
  • Suitability: a run \(C_i, \dots, C_j\) (\(0 \le i \le j < M\)) is suitable for a jump table if \(100 \cdot \mathrm{cases}(i, j) \ge 10 \cdot \mathrm{span}(i, j)\) (density at least \(10\,\%\)). For a destination \(d\) let \(\mathrm{cmps}_d(i, j)\) be the sum over the range clusters of the run with destination \(d\) of 1 for a single-value cluster and 2 for a multi-value one, and let \(D(i, j)\) be the number of distinct destinations of the run; the run is suitable for bit tests if \(j > i\), \(\mathrm{span}(i, j) \le 64\), \(\max_d \mathrm{cmps}_d(i, j) \ge 2\) and, with \(\Sigma \triangleq \sum_d \mathrm{cmps}_d(i, j)\): \(D(i, j) = 1\) and \(\Sigma \ge 3\), or \(D(i, j) = 2\) and \(\Sigma \ge 5\), or \(D(i, j) = 3\) and \(\Sigma \ge 6\).
  • Invariant (dynamic program): \(\mathit{MinPart}[i]\) is the least number of pieces in a partition of the suffix \(C_i, \dots, C_{M-1}\) into runs that are single clusters or suitable for a jump table; \(\mathit{Last}[i]\) is the last index of the first piece of such a partition, and \(\mathit{Score}[i]\) its score, where a piece scores 2 if it is a single cluster, 1 if it has \(\le 2\) or \(\ge 4\) clusters and 0 otherwise; among partitions with the same number of pieces the one with the higher score is kept.
function LowerSwitch(S):
    C ← SortAndRangeify(S)                     # sort the cases; merge adjacent values with one
                                               # destination into range clusters
    C ← FindJumpTables(C)
    C ← FindBitTestClusters(C)
    EmitTree(C)                                # balanced by probability, ≤ 3 clusters per leaf

function Suitable(i, j):                       # TargetLoweringBase::isSuitableForJumpTable
    return cases(i, j) · 100 ≥ span(i, j) · 10

function BitsSuit(i, j):                       # TargetLowering::isSuitableForBitTests
    return j > i and span(i, j) ≤ 64 and max_d cmps_d(i, j) ≥ 2 and
           (D(i, j) = 1 and Σ ≥ 3 or D(i, j) = 2 and Σ ≥ 5 or D(i, j) = 3 and Σ ≥ 6)

function FindJumpTables(C):                    # M = |C|
    if M < 4: return C
    if Suitable(0, M-1) and not BitsSuit(0, M-1): return [table C[0..M-1]]
    MinPart[M-1] ← 1; Last[M-1] ← M-1; Score[M-1] ← 2
    for i from M-2 down to 0:
        MinPart[i] ← MinPart[i+1] + 1; Last[i] ← i; Score[i] ← Score[i+1] + 2
        for j from M-1 down to i+1:
            if not Suitable(i, j): continue
            parts ← 1 + (0 if j = M-1 else MinPart[j+1])
            score ← (0 if j = M-1 else Score[j+1]) + (1 if j-i+1 ≤ 2 or j-i+1 ≥ 4 else 0)
            if parts < MinPart[i] or (parts = MinPart[i] and score > Score[i]):
                MinPart[i] ← parts; Last[i] ← j; Score[i] ← score
    result ← []; i ← 0
    while i < M:
        j ← Last[i]
        if j - i + 1 ≥ 4 and not BitsSuit(i, j): result.add(table C[i..j])
        else: result.add(C[i..j] as they are)
        i ← j + 1
    return result

function FindBitTestClusters(C):               # the same DP, pieces of span ≤ 64, ≤ 3 destinations
    (fewest pieces of range clusters with span ≤ 64 and ≤ 3 destinations, scanning j downward
     from min(M-1, i+63) and stopping at the first piece that contains a non-range cluster or a
     4th destination; each piece for which BitsSuit holds becomes one bit-test cluster)

Theorem 11.3.8 (The partition is minimal)

Let \(C_0, \dots, C_{M-1}\) be a clustering of a switch \(S\) into range clusters with \(M \ge 4\) (Definition 11.3.1). FindJumpTables of Algorithm 11.3.7 returns a partition of \(C_0, \dots, C_{M-1}\) into runs with the fewest pieces, where a piece is either one cluster or a run that is suitable for a jump table.

Proof

If the whole run \(C_0, \dots, C_{M-1}\) is suitable and not suitable for bit tests, one piece is returned, which is minimal. Otherwise, by induction on \(i\) from \(M - 1\) down to 0, \(\mathit{MinPart}[i]\) is the minimum over all such partitions of the suffix \(C_i, \dots, C_{M-1}\). Base: one cluster, one piece. Step: an optimal partition of the suffix starts with a first piece \(C_i, \dots, C_j\); if \(j = i\) it costs \(1 + \mathit{MinPart}[i+1]\) (the baseline), otherwise the piece must be suitable and the rest is optimally partitioned in \(\mathit{MinPart}[j+1]\) pieces by the hypothesis (optimal substructure: replacing the rest by a better partition would improve the whole). The loop tries every \(j\), so it finds the minimum; the score only breaks ties. Following \(\mathit{Last}\) from 0 reconstructs one minimal partition. [KP94] proves the same recurrence for Bernstein's heuristic.

The table-building step of FindJumpTables (the final while loop) replaces a piece by a jump-table cluster only if it has \(\ge 4\) clusters and is not suitable for bit tests; a piece that fails either test stays as separate range clusters, so the lowering may use more pieces than the partition the theorem counts. A clustering of fewer than 4 clusters gets no jump table at all (FindJumpTables returns it unchanged before the whole-run test).

3. Worked example

Jump tables

op: clusters \([0, 1] \to \texttt{A}\), \([2, 3] \to \texttt{B}\), \([4] \to \texttt{C}\), \([6] \to \texttt{D}\) (4 clusters, 6 cases, range 7). Suitable(0, 3): \(6 \cdot 100 = 600 \ge 7 \cdot 10 = 70\), and bit tests do not apply (4 destinations), so the whole switch is one table of 7 entries: A A B B C d0 D. At run time: idx = x - 0; idx >u 6 → default; else jmp *table[idx]. The PIR box in §7 shows exactly this code from pebblec.

Binary search trees

sparse: 5 clusters, 5 cases, range 4998: \(500 < 49\,980\), not suitable; every sub-run is sparse too (e.g. 3..70: \(2 \cdot 100 < 68 \cdot 10\)), so FindJumpTables returns the 5 clusters and no piece suits bit tests. The tree (equal probabilities) splits 2 | 3: test \(x < 911\); left leaf: compare 3, then 70; right leaf: compare 911, 1200, 5000 — at most 1 + 3 = 4 comparisons. The lab's BinarySearch (Algorithm 11.3.4) on 8 cases needs at most \(\lceil \log_2 8 \rceil + 1 = 4\) (the lab's sparse, maxcmp = 4).

Bit tests

chars: SortAndRangeify gives 6 clusters \([9, 10] \to \texttt{A}\), \([13] \to \texttt{A}\), \([32] \to \texttt{A}\), \([43] \to \texttt{C}\), \([45] \to \texttt{C}\), \([48, 57] \to \texttt{B}\): 16 cases over a range of 49. A table would be dense enough (\(1600 \ge 490\)), but BitsSuit holds for the whole run: \(57 - 9 < 64\), \(D = 3\), comparisons \(A: 2 + 1 + 1 = 4\), \(C: 1 + 1 = 2\), \(B: 2\), total \(8 \ge 6\). So buildJumpTable declines and the six clusters become one bit-test cluster. Since \(0 < 9\) and \(57 < 64\), LLVM skips the subtraction (LowBound = 0) and tests 1 << x directly:

destination values mask (hex)
B 48–57 0x3FF000000000000
A 9, 10, 13, 32 0x100002600
C 43, 45 0x280000000000

These are the three movabsq constants of the Clang box in §7, tested most-cases-first. GCC reports the same cluster: BT(values:16 comparisons:8 range:49 …).

LLVM's switch and back-end lowering

A switch where the dynamic program matters — 16 cases, 9 clusters, range 1006, so no single table:

\(i\) cluster \(\mathit{MinPart}[i]\) \(\mathit{Last}[i]\) \(\mathit{Score}[i]\) why
8 1005 → H 1 8 2 base
7 1003 → G 1 8 1 7..8: 2 cases over 3 values, suitable, \(\le 2\) clusters
6 1002 → F 1 8 0 6..8: 3 clusters (score +0)
5 1001 → E 1 8 1 5..8: 4 clusters, a table
4 1000 → D 1 8 1 4..8: 5 cases over 6 values
3 103 → A 2 3 3 alone; 3..8 spans 903 values for 6 cases
2 100..101 → C 2 3 2 2..3: 3 cases over 4 values
1 4..7 → B 3 1 4 1..3 spans 100 values for 7 cases: too sparse
0 0..3 → A 2 3 2 0..3: 11 cases over 104 values, \(1100 \ge 1040\)

Reconstruction from \(\mathit{Last}[0] = 3\): pieces \(C[0..3]\) (4 clusters: a table over 0..103) and \(C[4..8]\) (5 clusters: a table over 1000..1005). Two jump tables and a comparison between them — clang -O2 emits .LJTI0_0 and .LJTI0_1 for this switch (last box of §7).

Try it

./course drill switch-lowering --seed 5 --difficulty medium --solution runs Algorithm 11.3.7 on a random switch; --difficulty hard also asks for the bit-test masks.

4. Invariants and correctness

Jump tables

Correct by the table invariant and Lemma 11.3.2: the range check sends every value outside \([v_{\min}, v_{\max}]\) to the default, and every value inside to its table entry. The precondition that breaks is the range: sparse would need a 4998-entry table for 5 cases (the lab's JumpTable returns nullptr above 4096 entries).

Binary search trees

Theorem 11.3.5. The invariant needs signed comparisons for signed case values: an unsigned < would order −1 after 5 and send it down the wrong subtree (the lab's random switches include negative values for this reason).

Bit tests

By the mask invariant, bit & mask_d ≠ 0 iff \(x - v_{\min}\) is the offset of a case value going to the destination \(d\); the range check guarantees the shift amount is below \(w\) (a larger shift is poison in LLVM IR, undefined in C). The precondition \(\max - \min < w\) is essential.

LLVM's switch and back-end lowering

Theorem 11.3.8 gives the fewest pieces; each piece is then dispatched correctly by the arguments above, and the tree over clusters is Algorithm 11.3.4 with clusters as leaves (up to three per leaf, tested in order). At -O0 LLVM runs only the whole-switch table check: the dynamic program and the bit tests are skipped (TM->getOptLevel() == CodeGenOptLevel::None), which is why pebblec -O0 and -O2 can dispatch the same switch differently.

5. Complexity

Let \(n\) be the number of cases of a switch, \(M\) the number of its range clusters, \(R\) its range, \(D\) its number of destinations and \(w\) the word size.

Technique Dispatch time Code / data size Build time Justification
Jump table \(O(1)\): 1 compare + 1 indirect jump \(R\) table entries \(O(n + R)\) Algorithm 11.3.3 fills the table once
Binary search \(\lceil \log_2 n \rceil + 1\) compares \(O(n)\) \(O(n \log n)\) (sort) Theorem 11.3.5
Bit tests 1 range compare + \(\le D\) tests \(D\) masks \(O(n)\) Algorithm 11.3.6; \(R \le w\)
LLVM clustering \(O(\log M)\) tree levels + per-cluster cost \(O(n)\) code + tables \(O(M^2)\) for FindJumpTables, \(O(M \cdot w)\) for bit tests the DP tries every \(j > i\); the bit-test search stops after \(w\) clusters

Pathological family. \(n\) cases at \(0, 10, 20, \dots, 10(n - 1)\): the range is \(10n - 9\) and the density just above 10 %, so the whole switch is one table of \(10n - 9\) entries of which 90 % point to the default — the price of LLVM's density threshold. Cases at \(0, 11, 22, \dots\) fall below it as a whole, but not piecewise: a run of \(j\) consecutive cases spans \(11j - 10\) values, which is dense enough (\(100j \ge 10(11j - 10)\)) for every \(j \le 10\), so the dynamic program still carves out tables of 4 to 10 clusters (12 cases at spacing 11 give one table over 0…99 plus two lone cases; llvm_switch_clusters and clang -O2 agree). Only from spacing 14 on (\(4 \cdot 100 < 10 \cdot 43\)) does no table form at all, and dispatch is a tree of \(\lceil \log_2 n \rceil + 1\) levels. A second family, cases at the powers of two \(8, 16, 32, \dots, 2^{k}\) with distinct destinations, defeats the back end's tables (four consecutive cases span at least 57 values) and its bit tests (more than three destinations) — yet clang -O2 still emits a jump table for it, because SimplifyCFG's simplifySwitchOfPowersOfTwo runs first at the IR level and rewrites the switch into one on cttz(x) (guarded by ctpop(x) == 1), whose cases \(3, 4, \dots, k\) are dense. The back end's clustering is only the last word when the IR-level passes leave the switch alone.

Measured (the lab, x86-64, ns per call; half the inputs hit a case): dense16 — LLVM switch 8.27, hand jump table 7.73, search 9.59, bit tests 4.07; bits3 — switch 2.34, table 5.27, search 11.16, bits 2.93; sparse — switch 3.84, search 3.25 (labs/ch11-lowering/SPEC.md).

6. Variants and refinements

Jump tables

  • Relative entries (.long .LBB0_2-.LJTI0_0 in §7): 4-byte offsets from the table instead of 8-byte addresses — half the size and position-independent, one extra add.
  • Lookup tables instead of jumps: when every destination only produces a constant, SimplifyCFG's simplifySwitchLookup replaces the switch by a load from a constant array, with no indirect branch at all [LLVM-SimplifyCFG].

Binary search trees

  • Probability-balanced trees: LLVM splits a work item where the case probabilities (from profiles or the default's share) balance, not at the median (computeSplitWorkItemInfo) [LLVM-SwitchLowering].
  • Leaves of up to three clusters tested linearly, because a comparison chain of three is cheaper than another tree level ([LLVM-SDB], visitSwitch); and peeling a dominant case to test it first (peelDominantCaseCluster).

Bit tests

  • Skipping the subtraction when \(0 < v_{\min}\) and \(v_{\max} < w\) (LowBound = 0, the chars example): the mask is shifted by \(v_{\min}\) instead.
  • Contiguous ranges: when the clusters cover the whole range, the default test after the last mask is unnecessary (ContiguousRange).

LLVM's switch and back-end lowering

  • Kannan–Proebsting partitioning [KP94] replaced Bernstein's greedy splitting [Ber85]; LLVM adds the score to prefer partitions with more tables among equally small ones.
  • Target and size knobs: -jump-table-density (10 %), -optsize-jump-table-density (40 %), -min-jump-table-entries (4), and targets that disallow tables (areJTsAllowed).
  • GCC's switch conversion (pass_convert_switch, pass_lower_switch) forms jump-table and bit-test clusters with its own dynamic program [GCC-SwitchConv]; Go's walk phase uses binary search plus jump tables when -d=jumptables allows [GO-Switch].

7. In real compilers

Jump tables

LLVM: SwitchCG::SwitchLowering::findJumpTables and buildJumpTable in llvm/lib/CodeGen/SwitchLoweringUtils.cpp [LLVM-SwitchLowering]. GCC: jump_table_cluster::can_be_handled in gcc/tree-switch-conversion.cc [GCC-SwitchConv]. Pebble: a PIR switch goes to LLVM as switch (E5), and the back end builds the table.

A PIR switch through pebblec: LLVM switch, then a jump table

Reproduce (pebblec from this repository built with -DPEBBLE_USE_SOLUTION=all, LLVM 23.1.2; llvm-objdump 23.1.2):

cat > op.pir <<'EOF'
pir 1.0
source "op.pbl"

fn @cost(_0: i64 "op") -> i64 {
bb0:
  switch _0, [0: bb1, 1: bb1, 2: bb2, 3: bb2, 4: bb3, 6: bb4] otherwise bb5
bb1:
  return 1
bb2:
  return 3
bb3:
  return 20
bb4:
  return 40
bb5:
  return 0
}

fn @main() -> i64 {
  let _0: i64
bb0:
  _0 = call @cost(4)
  return _0
}
EOF
pebblec --emit=llvm op.pir -o - | grep -A7 'switch i64'
pebblec -O2 --passes='function(mem2reg)' --emit=obj op.pir -o op.o
llvm-objdump -d --no-show-raw-insn op.o | sed -n '/<cost>:/,/^$/p' | head -7

Output:

  switch i64 %0, label %bb5 [
    i64 0, label %bb1
    i64 1, label %bb1
    i64 2, label %bb2
    i64 3, label %bb2
    i64 4, label %bb3
    i64 6, label %bb4
  ]
0000000000000000 <cost>:
       0:       cmpq    $0x6, %rdi
       4:       ja  0x22 <cost+0x22>
       6:       leaq    (%rip), %rax            # 0xd <cost+0xd>
       d:       movslq  (%rax,%rdi,4), %rcx
      11:       addq    %rax, %rcx
      14:       jmpq    *%rcx

What to notice: the code generator only translated PIR's terminator into one switch; the back end applied Algorithm 11.3.7 and chose a table: 4 clusters, 6 cases over 7 values, 4 destinations (no bit tests). cmpq $6; ja is Lemma 11.3.2's unsigned range check (min = 0, so no subtraction), and the table holds 4-byte relative offsets (movslq, addq, §6). --passes='function(mem2reg)' keeps -O2 code generation but no inlining, so cost survives.

Binary search trees

LLVM: SelectionDAGBuilder::visitSwitch and splitWorkItem in llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp [LLVM-SDB]. Go: binarySearch in src/cmd/compile/internal/walk/switch.go [GO-Switch]. GCC: switch_decision_tree in gcc/tree-switch-conversion.cc [GCC-SwitchConv].

Clang and GCC on a sparse switch: a tree of comparisons

Reproduce (clang 23.1.2, gcc 14.2.0):

cat > sparse.c <<'EOF'
int add(void), sub(void), mul(void), dvd(void), neg(void), bad(void);
int sparse(int k) {
  switch (k) {
  case 3: return add();
  case 70: return sub();
  case 911: return mul();
  case 1200: return dvd();
  case 5000: return neg();
  default: return bad();
  }
}
EOF
clang-23 -O2 -S sparse.c -o - | sed -n '/^sparse:/,/^\.Lfunc_end0/p' | grep -v '^\s*\.\(cfi\|p2align\)'
gcc-14 -O2 -fdump-tree-switchlower1-details -c sparse.c && grep clusters sparse.c.*switchlower1

Output:

sparse:                                 # @sparse
# %bb.0:
    cmpl    $910, %edi                      # imm = 0x38E
    jle .LBB0_1
# %bb.3:
    cmpl    $911, %edi                      # imm = 0x38F
    je  mul@PLT                         # TAILCALL
# %bb.4:
    cmpl    $1200, %edi                     # imm = 0x4B0
    je  dvd@PLT                         # TAILCALL
# %bb.5:
    cmpl    $5000, %edi                     # imm = 0x1388
    jne bad@PLT                         # TAILCALL
# %bb.6:
    jmp neg@PLT                         # TAILCALL
.LBB0_1:
    cmpl    $3, %edi
    je  add@PLT                         # TAILCALL
# %bb.2:
    cmpl    $70, %edi
    je  sub@PLT                         # TAILCALL
# %bb.7:
    jmp bad@PLT                         # TAILCALL
.Lfunc_end0:
;; GIMPLE switch case clusters: 3 70 911 1200 5000 

What to notice: no table and no bit tests (§3): LLVM splits the five clusters 2 | 3 at 911 (cmpl $910; jle is \(x < 911\)) and tests each leaf's clusters linearly — at most four comparisons. GCC's cluster list is five single values as well.

Bit tests

LLVM: SwitchCG::SwitchLowering::findBitTestClusters and buildBitTests in SwitchLoweringUtils.cpp [LLVM-SwitchLowering], with TargetLowering::isSuitableForBitTests in llvm/include/llvm/CodeGen/TargetLowering.h. GCC: bit_test_cluster::find_bit_tests [GCC-SwitchConv].

Clang and GCC classify characters with three bit tests

Reproduce (clang 23.1.2, gcc 14.2.0):

cat > cls.c <<'EOF'
int digit(void), space(void), sign(void), other(void);
int classify(int c) {
  switch (c) {
  case '\t': case '\n': case '\r': case ' ':
    return space();
  case '0': case '1': case '2': case '3': case '4':
  case '5': case '6': case '7': case '8': case '9':
    return digit();
  case '+': case '-':
    return sign();
  default:
    return other();
  }
}
EOF
clang-23 -O2 -S cls.c -o - | sed -n '/^classify:/,/Lfunc_end/p' | grep -v '^\s*\.\(cfi\|p2align\)'
gcc-14 -O2 -fdump-tree-switchlower1-details -c cls.c && grep clusters cls.c.*switchlower1

Output:

classify:                               # @classify
# %bb.0:
    cmpl    $57, %edi
    ja  other@PLT                       # TAILCALL
# %bb.1:
    movl    %edi, %eax
    movabsq $287948901175001088, %rcx       # imm = 0x3FF000000000000
    btq %rax, %rcx
    jb  digit@PLT                       # TAILCALL
# %bb.2:
    movabsq $4294977024, %rcx               # imm = 0x100002600
    btq %rax, %rcx
    jb  space@PLT                       # TAILCALL
# %bb.3:
    movabsq $43980465111040, %rcx           # imm = 0x280000000000
    btq %rax, %rcx
    jae other@PLT                       # TAILCALL
# %bb.4:
    jmp sign@PLT                        # TAILCALL
.Lfunc_end0:
;; GIMPLE switch case clusters: BT(values:16 comparisons:8 range:49 density: 16.33%):9-57 

What to notice: the three masks of §3, tested most-bits-first (digits: 10 bits), one btq each; the range check is cmpl $57 with no subtraction because \(0 < 9\) and \(57 < 64\). The last test inverts to jae other and falls through to sign (contiguity is not needed for that). GCC counts the same 8 comparisons as isSuitableForBitTests (§3).

LLVM's switch and back-end lowering

LLVM: SelectionDAGBuilder::visitSwitch calls sortAndRangeify, findJumpTables, findBitTestClusters and then lowers the work list; the thresholds are TargetLoweringBase::isSuitableForJumpTable and the -jump-table-density/-min-jump-table-entries options in llvm/lib/CodeGen/TargetLoweringBase.cpp [LLVM-TLB]. The IR-level simplifySwitchLookup in SimplifyCFG runs earlier, at -O2 [LLVM-SimplifyCFG].

Two jump tables from one switch (the dynamic program of §3)

Reproduce (clang 23.1.2):

cat > mix.c <<'EOF'
int a(void), b(void), c(void), d(void), e(void), f(void), g(void), h(void), z(void);
int mix(int x) {
  switch (x) {
  case 0: case 1: case 2: case 3: return a();
  case 4: case 5: case 6: case 7: return b();
  case 100: case 101: return c();
  case 103: return a();
  case 1000: return d();
  case 1001: return e();
  case 1002: return f();
  case 1003: return g();
  case 1005: return h();
  default: return z();
  }
}
EOF
clang-23 -O2 -S mix.c -o - | grep -E '^\.LJTI|cmpl|ja|jmpq\s+\*'

Output:

    cmpl    $103, %edi
    ja  .LBB0_3
    jmpq    *%rax
    cmpl    $5, %edi
    ja  z@PLT                           # TAILCALL
    jmpq    *%rcx
.LJTI0_0:
.LJTI0_1:

What to notice: the partition of §3 — a table over 0…103 (range check cmpl $103) and one over 1000…1005 (after subtracting 1000, cmpl $5) — found by findJumpTables; neither piece suits bit tests: the first spans 104 values, more than a 64-bit word, and the second has 5 destinations.

Find where LLVM does it. In llvm/lib/CodeGen/SwitchLoweringUtils.cpp, which function refuses to build a jump table for a range that bit tests could handle, and which TargetLowering predicate does it ask? (Quiz llvm-where-jt-vs-bits.)

8. Comparison

Technique Power / precision Speed (asymptotic · practical) Output / error quality Implementation effort Typical use
Jump table Any dense switch; size grows with the range \(O(1)\) · one indirect branch (hard to predict when targets vary) Compact code, data table in .rodata Low (plus a range check) Dense switches, interpreters' opcode dispatch, dense16
Binary search tree Any switch \(O(\log n)\) · predictable when inputs are skewed Code grows linearly with cases Low Sparse switches (sparse), and the glue between clusters
Bit tests \(v_{\max} - v_{\min} < 64\), few destinations \(O(D)\) · a few ALU ops, no memory Tiny: \(D\) 64-bit constants Low Character classes (chars), flag sets (bits3)
LLVM switch + back end All of the above, chosen per cluster (Theorem 11.3.8) \(O(\log M)\) + cluster cost · matches the best hand lowering in the lab except on unpredictable dense switches One IR instruction; the choice is visible in the assembly None for the front end; the DP lives in LLVM Every LLVM front end, including Pebble's code generator

Choose a jump table when the cases are dense and many; a binary tree when they are sparse; bit tests when they fit in a word with few destinations; and emit switch and let LLVM choose when you target LLVM. In the lab its choice was the fastest, or within 0.6 ns of the fastest hand lowering, on bits3, chars, sparse and clustered; on dense16 with random inputs its jump table (8.27 ns) lost to the lab's eight bit tests (4.07 ns), because the indirect jump mispredicts and LLVM's static cost model does not know the input distribution — a profile (!prof weights) or a different density threshold is the remedy.

9. Assessment

  • Quiz (./course quiz 11): jt-density, jt-range-check (tag jump-table); bst-depth, bst-signed (tag binary-search); bits-mask, bits-suitable (tag bit-tests); llvm-switch-clusters, llvm-where-jt-vs-bits (tag llvm-switch).
  • Drill: ./course drill switch-lowering (clusters, density, the final partition; hard: masks).
  • Flashcards: tags jump-table, binary-search, bit-tests, llvm-switch.
  • Exercises: E5 (PIR switch → LLVM switch); lab part B.

References

See the chapter references.