Hacker News new | ask | show | jobs
by khuey 20 days ago
In the one statement case, clang emits LLVM IR where the last operation in each branch is the store. In the two statement case, clang emits LLVM IR where the last operation in each branch is the [pointer math](https://llvm.org/docs/GetElementPtr.html).

The SimplifyCFG pass in LLVM pulls the identical-in-all-but-one-operand store IR instructions into a successor block with a [select instruction](https://llvm.org/docs/LangRef.html#select-instruction) that will ultimately become a conditional move. But the pointer math differs by two operands (because there are two different pointers and one is being decremented and one is being incremented) so SimplifyCFG stops immediately and never reaches the stores.

This sort of brittle behavior where semantically identical code produces different optimization results is common in optimizing compilers unfortunately.