PR 36410 describes a problem where as a result of hoisting common code from "then" and "else" branches of a condition to before the "if", we get a llvm.dbg.value intrinsic with an invalid location. For those intrinsics, the scope indicated in the !dbg location must be identical with the scope of the variable tracked by the intrinsics, or else the module verifier will abort. This constraint can be violated by the current algorithm of getMergedLocation.
To fix this problem, this patch re-implements the algorithm used in getMergedLocation as follows (for the case of merging call instructions):
- First, we determine a merged scope. This is closest common scope of the two locations. If this does not exist, use the current function as scope. Note that in the special case of debug intrinsics for the same variable, the scopes must be identical, and therefore this algorithm will always return the same scope as well.
- If both original locations were inline, we need to return an inline location as well. To determine the inlined-at location, we use the closest common location in the inlined-at chain of both locations. If none exist, use a compiler-generated location in the current function.
This should handle the following cases:
- Same location (inlined or non-inlined): return that location
- Locations in the same scope (non-inlined): return a compiler-generated location in that scope
- Locations in different but nested scopes (non-inlined): return a compiler-generated location in the outer scope
- Locations in the same scope or nested scopes, inlined directly at the same location: return the outer scope, using the original (shared) inlined-at chain
- Locations in the same scope or nested scopes, inlined indirectly at the same location: return the outer scope, using the longest common part of the two original inlined-at chains
- Locations in the same scope or nested scopes, inlined at different locations of the same function: return the outer scope, using an inlined-at pointing to a compiler-generated location in that function.