While playing with LLVM, I’ve found a weird behavior in mem2reg pass.
When optimizing single stores, undefined value is placed before any load preceding the store (based on basicblock’s ordering and simple dominator analysis, if I remember correctly).
This is the line that is responsible for the behavior: (LLVM9 does the same)
A problem arises, and I am not sure if it is really a problem or just weird C-compliant behavior.
int a; // or, equally, int a=0;
if (b) // (*)
printf(“This will be called”);
The first load of variable b, before the single store (the first branching) is replaced by undef, so the second branch will be replaced by a phi node, if the (*) branch is taken, the value is 0, else undef.
I’m concerned that this is an LLVM bug.
clang -S -emit-llvm test.c
opt -mem2reg test.ll
I’m not at the computer right now, so I cannot show the exact generated code.