LLVM Chapter 3 Validation

This directory contains a small LLVM IR harness for checking how current-head opt + llc lower representative Chapter 3 power-of-two boundary idioms to x86_64.

Run:

./run-ch3-validation.sh

Current findings:

  • Alignment idioms like round_up_8, round_up_16, and round_down_8 lower well to lea plus and.
  • x & (x - 1) == 0 is recognized:
    • is_pow2_or_zero lowers to blsr plus sete
    • is_pow2_nonzero is canonicalized through ctpop
  • Crossing-page tests are improved:
    • the naive form becomes an xor plus single compare
    • the Hacker’s Delight form becomes a direct unsigned compare against the remaining bytes in the page
  • The main missed optimization is that the classic Hacker’s Delight smear networks for flp2 and clp2 are not canonicalized to ctlz/bit-scan forms. Equivalent ctlz formulations in the same harness lower to compact lzcnt + shrx / shlx sequences.