This patch addresses PR41675 [0], where a stack-pointer variable is dereferenced too many times by its location expression, presenting a value on the stack as the pointer to the stack.
The difference between a stack *pointer* DBG_VALUE and one that refers to a value on the stack, is currently the indirect flag. In the below, the former is the address of, the latter the value in %stack_loc:
DBG_VALUE %stack_loc, $noreg, !1, !DIExpression() DBG_VALUE %stack_loc, 0, !1, !DIExpression()
When the prologepilog pass runs, the stack references are replaced with complex expression opcodes:
DBG_VALUE $rsp, $noreg, !1, !DIExpression(DW_OP_plus_uconst, 4) DBG_VALUE $rsp, 0, !1, !DIExpression(DW_OP_plus_uconst, 4)
Unfortunately, the DWARF backend interprets both of these as being memory locations. For the second DBG_VALUE this is because it's explicitly indirect, but for the first it's because there's a complex expression without DW_OP_stack_value.
Get around this by detecting empty-to-non-empty DIExpression transitions in prologepilog, and adding DW_OP_stack_value to them. This ensures that pointers-to-stack-locs are interpreted as implicit locations, which is what they are.
For the edge case where the offset is zero and the location could be a register location, DIExpression::prepend will still generate opcodes, and thus DW_OP_stack_value must still be added.
The only thing I'm concerned about is the DIExpr->getNumElements() == 0. This sounds like it would also reject other cases where we'd want this to apply, such as an expression that only contains a DW_OP_LLVM_fragment. Is there a better condition? Perhaps by inverting it? What's an example of an expression where we don't want this to happen?