Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1142,7 +1142,9 @@ unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1; BasicBlock *ToRemoveSucc = CondBr->getSuccessor(ToRemove); ToRemoveSucc->removePredecessor(BB, true); - BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); + BranchInst *UncondBr = + BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); + UncondBr->setDebugLoc(CondBr->getDebugLoc()); CondBr->eraseFromParent(); if (CondCmp->use_empty()) CondCmp->eraseFromParent(); @@ -1245,7 +1247,8 @@ BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1); BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0); RemoveSucc->removePredecessor(BB); - BranchInst::Create(KeepSucc, BI); + BranchInst *UncondBI = BranchInst::Create(KeepSucc, BI); + UncondBI->setDebugLoc(BI->getDebugLoc()); BI->eraseFromParent(); DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}}); return true; Index: llvm/test/Transforms/JumpThreading/branch-debug-info.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/JumpThreading/branch-debug-info.ll @@ -0,0 +1,34 @@ +; RUN: opt %s -debugify -jump-threading -S | FileCheck %s +; Tests Bug 37966 + +define dso_local void @bar(i32 %cc) local_unnamed_addr { +; CHECK-LABEL: @bar( +; CHECK: while.body: +; CHECK-NEXT: br label %while.body, !dbg ![[DBG:[0-9]+]] +; CHECK: ![[DBG]] = !DILocation( + +entry: + %dd = alloca [3 x i32], align 4 + %tobool = icmp eq i32 %cc, 0 + br i1 %tobool, label %while.end, label %while.body.lr.ph + +while.body.lr.ph: ; preds = %entry + %idxprom = sext i32 %cc to i64 + %arrayidx = getelementptr inbounds [3 x i32], [3 x i32]* %dd, i64 0, i64 %idxprom + br label %while.body + +while.cond: ; preds = %while.body + store i32 2, i32* %arrayidx, align 4 + br label %while.end + +; The jump threading pass will replace this branch with an unconditional branch: +; br label %while.body +; The debug information for the existing branch instruction should be reused +; for the new instruction. +while.body: ; preds = %while.body.lr.ph, %while.body + br i1 %tobool, label %while.cond, label %while.body + +while.end: ; preds = %entry, %while.cond + ret void +} +