[llvm] r323570 - [InstCombine] Preserve debug values for eliminable casts
Vedant Kumar via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 26 14:02:53 PST 2018
Author: vedantk
Date: Fri Jan 26 14:02:52 2018
New Revision: 323570
URL: http://llvm.org/viewvc/llvm-project?rev=323570&view=rev
Log:
[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
Added:
llvm/trunk/test/Transforms/InstCombine/debuginfo-variables.ll
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/trunk/test/Transforms/InstCombine/alloca-cast-debuginfo.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=323570&r1=323569&r2=323570&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Fri Jan 26 14:02:52 2018
@@ -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::commonCastTra
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;
}
}
Modified: llvm/trunk/test/Transforms/InstCombine/alloca-cast-debuginfo.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/alloca-cast-debuginfo.ll?rev=323570&r1=323569&r2=323570&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/alloca-cast-debuginfo.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/alloca-cast-debuginfo.ll Fri Jan 26 14:02:52 2018
@@ -40,6 +40,10 @@ entry:
; CHECK-LABEL: define void @f(%struct.Foo* %p)
; CHECK: %local = alloca i64, align 8
; CHECK: call void @llvm.dbg.declare(metadata i64* %local, metadata !22, metadata !DIExpression())
+; CHECK: [[simplified:%.*]] = bitcast i64* %local to i8*
+; CHECK: call void @llvm.dbg.value(metadata i8* [[simplified]], metadata !22, metadata !DIExpression())
+; CHECK: call void @escape(i8* [[simplified]])
+; CHECK: ret void
declare void @llvm.dbg.declare(metadata, metadata, metadata)
Added: llvm/trunk/test/Transforms/InstCombine/debuginfo-variables.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/debuginfo-variables.ll?rev=323570&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/debuginfo-variables.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/debuginfo-variables.ll Fri Jan 26 14:02:52 2018
@@ -0,0 +1,18 @@
+; RUN: opt < %s -debugify -instcombine -S | FileCheck %s
+
+define i64 @test_sext_zext(i16 %A) {
+; CHECK-LABEL: @test_sext_zext(
+; CHECK-NEXT: [[C2:%.*]] = zext i16 %A to i64
+; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 [[C2]], metadata !8, metadata !DIExpression()), !dbg !13
+; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 [[C2]], metadata !10, metadata !DIExpression()), !dbg !12
+ %c1 = zext i16 %A to i32
+ %c2 = sext i32 %c1 to i64
+ ret i64 %c2
+}
+
+; CHECK: !8 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !9)
+; CHECK: !9 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
+; CHECK: !10 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !11)
+; CHECK: !11 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
+; CHECK: !12 = !DILocation(line: 2, column: 1, scope: !5)
+; CHECK: !13 = !DILocation(line: 1, column: 1, scope: !5)
More information about the llvm-commits
mailing list