Canonical forms, smart stuff, a lot of hardcoding.<p>Canonical forms are the really important part. Simple example: there are multiple ways a program might say “X * 2”. You could shift left by 1. You could multiple by 2. You could add X to itself. The idea of canonical forms is that in one pass, the compiler will pattern match all the ways you can do this and reduce all of them to the same canonical form - say, left shift by 1. Then, subsequent passes that want to catch more complex uses of that construct only have to look for one version of it (left shift 1) and not all three.<p>Here’s a more complex case. Ternary expressions in C and if-else statements have the same representation in llvm IR generated by clang: basic blocks and branches. There are multiple ways of representing the data flow (could use allocas and stores/loads or SSA data flow) but both the sroa and mem2reg passes will canonicalize to SSA. And, last I checked llvm says that the preferred canonical form of a if-then-else is a select (I.e. conditional move) whenever the two are equivalent. So, no matter what you use to write the equivalent of std::min - macros, templates, whatever, coding style don’t matter - you will end up eventually with a select instruction whose predicate is a comparison. Then - if your CPU supports doing min in a single instruction, it’s trivial for the instruction selector to just look for that kind of select. This happens not because every way of writing min is hardcoded, but because multiple rounds of canonicalization (clang using basic blocks and branches for both if/else and ternaries, sroa and mem2reg preferring SSA, and if conversion preferring select) gets you there.<p>A lot of this is hardcoding, but it’s not the boring “hardcode everything” kind of approach, but rather, it’s about using multiple phases that each produce increasingly canonical code that makes subsequent pattern matching simpler.
The C code is a low level implementation of population count. AggressiveInstCombine.cpp first matches and <i>raises</i> the IR for this into llvm.ctpop.i64.<p><a href="https://godbolt.org/z/8ronKz3Eb" rel="nofollow">https://godbolt.org/z/8ronKz3Eb</a><p>Later an x86 backend can re-<i>lower</i> this into a popcnt instruction or to CPOP on a RISC-V backend or to CNT on ARMv8 or ….<p><a href="https://godbolt.org/z/4zvWs6rzr" rel="nofollow">https://godbolt.org/z/4zvWs6rzr</a><p>It can also be re-lowered to roughly those instructions on machines lacking population count instructions.<p><a href="https://godbolt.org/z/aYsc9dz7f" rel="nofollow">https://godbolt.org/z/aYsc9dz7f</a>
Imagine if x86 had POPCNT since the beginning, implemented in microcode at first, and optimised it over time to be faster and use more available hardware. There would be no need for this sort of "decompiler" in a compiler nor would software need recompilation for each CPU model.