2 ms·
Godbolt shows that Clang is a lot more aggressive with use of "cmov" -- conditional move instructions. When the Spectre and Meltdown family of security debacle
by ncmncm 7y ago
Godbolt shows that Clang is a lot more aggressive with use of "cmov" -- conditional move instructions.
When the Spectre and Meltdown family of security debacles surfaced, cmov was promoted as a way to choke off speculative execution. But that suggests, also, that cmov would be bad for performance in places where cross-process security isn't a concern.
Did something important change, or did I misunderstand? Maybe only cmov memory -> register blocks speculation?
- dbcurtis 7y agoCMOV replaces branches. So no branch, no speculation. Typically, though, only branches that the compiler identifies as not reliably predictable would be replaced. Because with a well predicted branch you do not waste resources. CMOV executes both paths, wasting the work of one path, but saving the mispredict penalty. Which for “flakey” branches can be a overall win.
- nikic 7y agoChoice of cmov vs branch is pretty tricky because profitability depends on branch probabilities, which are commonly not known. LLVM has an X86 cmov conversion pass that does this based on some cost modeling for loop-carried dependencies, but it does not always make the right choice. Common problem is binary search code where you really, really want that cmov, but might not get it.