diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -2084,6 +2084,26 @@ // block, evaluate them to account for entry from PredBB. DenseMap ValueMapping; + // Retargets llvm.dbg.value to any renamed variables + auto retargetDbgValueIfPossible = [&](Instruction *NewInst) -> bool { + auto dbgInstruction = dyn_cast(NewInst); + if (!dbgInstruction) + return false; + + auto dbgValue = dyn_cast(dbgInstruction->getValue()); + if (!dbgValue) + return false; + + auto I = ValueMapping.find(dbgValue); + if (I != ValueMapping.end()) { + NewInst->setOperand( + 0, MetadataAsValue::get(dbgInstruction->getContext(), + ValueAsMetadata::get(I->second))); + return true; + } + return false; + }; + // Clone the phi nodes of the source basic block into NewBB. The resulting // phi nodes are trivial since NewBB only has one predecessor, but SSAUpdater // might need to rewrite the operand of the cloned phi. @@ -2112,6 +2132,9 @@ ValueMapping[&*BI] = New; adaptNoAliasScopes(New, ClonedScopes, Context); + if (retargetDbgValueIfPossible(New)) + continue; + // Remap operands to patch up intra-block references. for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) if (Instruction *Inst = dyn_cast(New->getOperand(i))) { diff --git a/llvm/test/Transforms/JumpThreading/thread-debug-info.ll b/llvm/test/Transforms/JumpThreading/thread-debug-info.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/JumpThreading/thread-debug-info.ll @@ -0,0 +1,55 @@ +; RUN: opt -S -passes=debugify,jump-threading < %s | FileCheck %s + +@a = global i32 0, align 4 + +; Test that the llvm.dbg.value calls in a threaded block are correctly targeting +; the locals in their block, and not the unthreaded one. +define void @test2(i32 %cond1, i32 %cond2) { +; CHECK: [[globalptr:@.*]] = global i32 0, align 4 +; CHECK: bb.cond2: +; CHECK: call void @llvm.dbg.value(metadata ptr null, metadata ![[DBG1ptr:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG2ptr:[0-9]+]] +; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i32 %cond2, 0, !dbg ![[DBGLOCtobool1:[0-9]+]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i1 [[TOBOOL1]], metadata ![[DBG1tobool1:[0-9]+]], metadata !DIExpression()), !dbg ![[DBGLOCtobool1]] +; CHECK: bb.cond2.thread: +; CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[globalptr]], metadata ![[DBG1ptr]], metadata !DIExpression()), !dbg ![[DBG2ptr]] +; CHECK-NEXT: [[TOBOOL12:%.*]] = icmp eq i32 %cond2, 0, !dbg ![[DBGLOCtobool1]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i1 [[TOBOOL12]], metadata ![[DBG1tobool1]], metadata !DIExpression()), !dbg ![[DBGLOCtobool1]] +entry: + %tobool = icmp eq i32 %cond1, 0 + br i1 %tobool, label %bb.cond2, label %bb.f1 + +bb.f1: + call void @f1() + br label %bb.cond2 + +bb.cond2: + %ptr = phi ptr [ null, %bb.f1 ], [ @a, %entry ] + %tobool1 = icmp eq i32 %cond2, 0 + br i1 %tobool1, label %bb.file, label %bb.f2 + +bb.f2: + call void @f2() + br label %exit + +bb.file: + %cmp = icmp eq ptr %ptr, null + br i1 %cmp, label %bb.f4, label %bb.f3 + +bb.f3: + br label %exit + +bb.f4: + call void @f4() + br label %exit + +exit: + ret void +} + +declare void @f1() + +declare void @f2() + +declare void @f3() + +declare void @f4() \ No newline at end of file