notes

Whitespace IR-level optimizations

Control flow

Data flow

Duplication

Peephole

Idioms

Popcount

LLVM transforms this loop into the popcount instruction:

for (cnt = init; x != 0; cnt++) {
  x &= (x - 1);
}

I want to also recognize this more naïve version:

for (cnt = init; x != 0; x >>= 1) {
  cnt += x & 1;
}

Division and modulo lowering

  1. Lower / to tdiv, fdiv, ediv, rdiv, or cdiv; and % to tmod, fmod, emod, rmod, or cmod, according to the execution division mode (--div).
  2. Replace div and mod of constant non-zero divisor with _unchecked variants and zero divisor with error.
  3. Replace division mode conversion idioms.
  4. Strength reduce constant divisor.
  5. Lower to target division mode.
  6. Replace with _unchecked variants and add explicit divisor checks when divisor may be zero.
  7. Annotate zero divisor branches as unlikely like Rust’s unlikely! intrinsic in checked_div.