diff options
author | Vedant Kumar <vsk@apple.com> | 2018-01-26 22:02:52 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2018-01-26 22:02:52 +0000 |
commit | e48597a50ebc918897a5752a553f459ff7ce4fb8 (patch) | |
tree | 5b249daf8ece8b17fd89eac5ed11507320ca46c7 /llvm/lib | |
parent | 8610c9f43a386ee40cbfd7addb507434205b2cc4 (diff) | |
download | bcm5719-llvm-e48597a50ebc918897a5752a553f459ff7ce4fb8.tar.gz bcm5719-llvm-e48597a50ebc918897a5752a553f459ff7ce4fb8.zip |
[InstCombine] Preserve debug values for eliminable casts
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.
Differential Revision: https://reviews.llvm.org/D42566
llvm-svn: 323570
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index 178c8eaf250..74d026e641e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -16,6 +16,7 @@ #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/PatternMatch.h" #include "llvm/Support/KnownBits.h" using namespace llvm; @@ -265,7 +266,20 @@ Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) { // The first cast (CSrc) is eliminable so we need to fix up or replace // the second cast (CI). CSrc will then have a good chance of being dead. - return CastInst::Create(NewOpc, CSrc->getOperand(0), CI.getType()); + auto *Res = CastInst::Create(NewOpc, CSrc->getOperand(0), CI.getType()); + + // If the eliminable cast has debug users, insert a debug value after the + // cast pointing to the new Value. + SmallVector<DbgInfoIntrinsic *, 1> CSrcDbgInsts; + findDbgUsers(CSrcDbgInsts, CSrc); + if (CSrcDbgInsts.size()) { + DIBuilder DIB(*CI.getModule()); + for (auto *DII : CSrcDbgInsts) + DIB.insertDbgValueIntrinsic( + Res, DII->getVariable(), DII->getExpression(), + DII->getDebugLoc().get(), &*std::next(CI.getIterator())); + } + return Res; } } |