LLVM Chapter 8 Validation
This directory contains a small LLVM IR harness for checking how opt + llc
lower representative Chapter 8 multiplication patterns to x86_64.
Run:
./run-ch8-validation.shOutputs:
ch8-multiplication.opt.llch8-multiplication.sch8-multiplication.clean.s
Suggested workflow:
- Start from a pattern in Hacker’s Delight.
- Encode the exact operation in LLVM IR.
- Run
opt -O2andllc -O2. - Inspect
*.clean.s. - Decide whether LLVM recognized the intended machine idiom, or whether the transform is still a source-level/manual optimization.
Current findings from ch8-multiplication.ll:
- Small constant multiplies are strength-reduced on x86_64.
Examples:
x * 3→leax * 5→leax * 7→leaplussubx * 9→leax * 10→addplusleax * 45→ twolea
- Larger constants are not necessarily strength-reduced.
Example:
x * 1000stays asimul
- The 3-multiply complex product form is preserved if you write that form in IR.
- LLVM does not rewrite the naive 4-multiply complex product into the 3-multiply variant in this test.
- Portable widened IR for high-half multiply
(
zext/sext→mul i64→ shift-right 32) lowered toimulqplusshrq, not to the x86 one-instruction high-half idiom using%edx:%eax.
Interpretation:
- LLVM covers a good subset of the machine-level strength reductions from the book, especially “multiply by constant” lowering.
- LLVM does not generally rediscover higher-level algebraic rewrites from the book once they are not already present in the IR.
- For validation, you should separate:
- backend/codegen idioms that LLVM should recognize from straightforward IR
- source-level algebraic rewrites that need to be written explicitly or taught to InstCombine/other middle-end passes