This is an archive of the discontinued LLVM Phabricator instance.

Add @llvm.dbg.value entries for the phi node created by -mem2reg
ClosedPublic

Authored by keith.walker.arm on Aug 19 2016, 8:28 AM.

Details

Summary

When phi nodes are created in the -mem2reg phase, the @llvm.dbg.declare
entries are converted to @llvm.dbg.value entries at the place where the
store instructions existed. However no entry is created to describe
the resulting value of the phi node.

The effect of this is especially noticeable in for loops which have a
constant for the intial value; the loop control variable's location
would be described as the intial constant value in the loop body once
the -mem2reg optimization phase was run.

This change adds the creation of the @llvm.dbg.value entries to describe
variables whose location is the result of a phi node created in -mem2reg.

Also when the phi node is finally lowered to a machine instruction it
is important that the lowered "load" instruction is placed before the
associated DEBUG_VALUE entry describing the value loaded.

Diff Detail

Event Timeline

keith.walker.arm retitled this revision from to Add @llvm.dbg.value entries for the phi node created by -mem2reg.
keith.walker.arm updated this object.
dblaikie added inline comments.Aug 19 2016, 8:48 AM
lib/Transforms/Utils/Local.cpp
1066–1078

This might be better written as an llvm::any_of (or at least llvm::find_if), perhaps?

1067–1069

This looks like it might try to dyn_cast the end iterator, which I would imagine isn't valid in general? (is there a reason it's incrementing first then doing work, rather than a more normal for (even range-for) loop?)

test/CodeGen/AArch64/phi-dbg.ll
1–4 ↗(On Diff #68691)

Seems like this patch could be split in two - this test and the associated improvements to lib/CodeGen - and, separately, the changes mem2reg (& the other test) mentioned in the description.

Thanks for the review, I will update the patch taking into account of your comments.

lib/Transforms/Utils/Local.cpp
1066–1078

I'll try doing as you suggest as see if I can re-write it in a more concise form.

1067–1069

The main reason is because I originally copied it from the previous function and then made it work forward rather than back and also become a loop - so it's basic structure is similar to the previous function. I will re-work it.

test/CodeGen/AArch64/phi-dbg.ll
1–4 ↗(On Diff #68691)

Good idea. I will split it into 2 separate patches.

  • Moved the changes for ensuring the lowered load instruction in placed before the DEBUG_VALUE entry into a separate change: https://reviews.llvm.org/D23760
  • Updated PhiHasDebugValue() to use llvm::any_of
dblaikie edited edge metadata.Aug 22 2016, 11:03 AM

I'll leave final sign-off to Adrian, who's worked with the value/variable tracking stuff in more depth more recently than I have.

lib/Transforms/Utils/Local.cpp
1069

Might even just roll the DVI test into the single return expression:

return DVI && DVI->getValue() == APN && ... ;
test/Transforms/Util/mem2reg-dbg.ll
36–37

LLVM registers don't necessarily have names in non-Asserts builds, so it's best to phrase the test so it doesn't use register names (c.0, if.then, entry, etc).

[[PHI:%.*]] = phi i32 [ 12, {{.*}} ], [ 1, {{.*}} ]
call.void @llvm.dbg.value(metadata i32 [[PHI]], ...

(in some cases register names still appear in Asserts builds, but these are mostly just oversight/things we haven't consistently removed)

aprantl added inline comments.Aug 22 2016, 1:09 PM
lib/Transforms/Utils/Local.cpp
1147

What's the point of this return value? Isn't it always true?

aprantl added inline comments.Aug 22 2016, 1:12 PM
lib/Transforms/Utils/Local.cpp
1067

Would it be possible to do something like in llvm::FindAllocaDbgDeclare() to avoid scanning all instructions here?

lib/Transforms/Utils/Local.cpp
1147

Yes, it does always return true ..... I was just following the coding style in the preceding 2 functions which do a similar job for the load and store operations and which also always returns true. I'll make this a void function and remove the return true values.

keith.walker.arm edited edge metadata.

Updated PhiHasDebugValue() to avoid scanning the whole instructions stream by using a new function llvm::FindAllocaDbgValues(). This new function is similar to the exiting function llvm::FindAllocaDbgDeclare() but has the additional complication that the result of the find can find multiple entries when applied to a phi node.

Updated test to capture matching names.

I decided to leave the function llvm::ConvertDebugDeclareToDebugValue() always returning true so that it has a similar form to the preceding 2 functions which do the same thing.

aprantl accepted this revision.Sep 16 2016, 3:57 PM
aprantl edited edge metadata.

Couple of small style nitpicks, but otherwise LGTM.

Thanks!

lib/Transforms/Utils/Local.cpp
1147

Would be a good opportunity to also convert the other function to return void. The return value used to necessary when the function was originally added, but this is no longer the case.

1229

The "FindAllocaDbgValues - " is no longer needed with our current doxygen settings.

1231

This is up to taste, but this could also be written as taking a function/lambda callback instead of creating a temporary array.

This revision is now accepted and ready to land.Sep 16 2016, 3:57 PM

I decided to leave the function llvm::ConvertDebugDeclareToDebugValue() always returning true so that it has a similar form to the preceding 2 functions which do the same thing.

It would be awesome if you could clean this up in a separate NFC commit (no need to review this separately).

This revision was automatically updated to reflect the committed changes.