This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Preserve debug values for eliminable casts
ClosedPublic

Authored by vsk on Jan 26 2018, 1:29 AM.

Details

Summary

A cast from A to B is eliminable if its result is casted to C, and if
the pair of casts could just be expressed as a single cast. E.g here,
%c1 is eliminable:

%c1 = zext i16 %A to i32
%c2 = sext i32 %c1 to i64

InstCombine optimizes away eliminable casts. This patch teaches it to
insert a dbg.value intrinsic pointing to the final result, so that local
variables pointing to the eliminable result are preserved.

Diff Detail

Repository
rL LLVM

Event Timeline

vsk created this revision.Jan 26 2018, 1:29 AM
JDevlieghere added inline comments.
lib/Transforms/InstCombine/InstCombineCasts.cpp
275 ↗(On Diff #131551)

Out of curiosity: why check size() and not empty()? Although irrelevant, the STL only guarantees that empty() is O(1), but it's why it caught my eye here. :-)

aprantl requested changes to this revision.Jan 26 2018, 9:12 AM

Could you achieve a reasonably similar effect with less code by invoking salvageDebugInfo() when the cast is actually eliminated?

lib/Transforms/InstCombine/InstCombineCasts.cpp
280 ↗(On Diff #131551)

The insertion point should be directly before or after DII. The position of the dbg.value intrinsic is significant as it determines when the variable will be visible in the debugger.

This revision now requires changes to proceed.Jan 26 2018, 9:12 AM
vsk added a comment.Jan 26 2018, 1:49 PM

Could you achieve a reasonably similar effect with less code by invoking salvageDebugInfo() when the cast is actually eliminated?

I don't think so, because salvageDebugInfo(DyingInst) isn't aware of the instruction that replaces the dying instruction. We could introduce a new utility, e.g forwardDebugInfo(DyingInst, ReplacementInst), but I'm not sure whether it would be sufficiently general.

lib/Transforms/InstCombine/InstCombineCasts.cpp
275 ↗(On Diff #131551)

No particular reason, it's O(1) so it should be OK.

280 ↗(On Diff #131551)

I don't think the insertion point can be directly before or after DII.

Take a look at the change to the "alloca-cast-debuginfo.ll" test below: inserting a dbg.value directly before/after DII would introduce a use of [[simplified]] before it's def.

The current code puts the fresh dbg.value in the exact spot where the dying cast is. Doesn't that reflect when the variable is visible precisely?

aprantl accepted this revision.Jan 26 2018, 1:59 PM
aprantl added inline comments.
lib/Transforms/InstCombine/InstCombineCasts.cpp
280 ↗(On Diff #131551)

I see. If CI guaranteed to be immediately after the old cast, this is indeed correct. Thanks for clarifying.

This revision is now accepted and ready to land.Jan 26 2018, 1:59 PM
vsk added a comment.Jan 26 2018, 2:03 PM

Thanks for the review :), here's to more -debugify finds!

This revision was automatically updated to reflect the committed changes.