This patch attempts to fix the bug discussed here:
http://lists.llvm.org/pipermail/cfe-dev/2016-July/050066.html
This patch fixes the bug by moving the lifetime.start of a variable to the beginning of its lexical scope.
int move_lifetime_start(int a) { int *p = 0; // This patch moves lifetime.start for "i" to the beginning of the function. label1: if (p) { foo2(*p); // The storage of "i" has to be kept alive when goto jumps to label1. return 0; } int i = 999; // lifetime.start for "i" used to be inserted here. if (a != 2) { p = &i; goto label1; } return -1; }
BB->begin() is not necessarily a legal place to insert a call. This could happen if e.g. a scope was unconditionally entered after a statement including a ternary expression.
Also, I think lexical scopes don't necessarily have an active basic block upon entry, because their entry can be unreachable. This happens most importantly with e.g. switch statements, but can also happen with goto or simply with unreachable code (which it's still important to not crash on).