Index: lib/Transforms/Utils/BasicBlockUtils.cpp =================================================================== --- lib/Transforms/Utils/BasicBlockUtils.cpp +++ lib/Transforms/Utils/BasicBlockUtils.cpp @@ -486,11 +486,12 @@ } // Create new basic block, insert right before the original block. - BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix, - BB->getParent(), BB); + BasicBlock *NewBB = BasicBlock::Create( + BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB); // The new block unconditionally branches to the old block. BranchInst *BI = BranchInst::Create(BB, NewBB); + BI->setDebugLoc(BB->getFirstNonPHI()->getDebugLoc()); // Move the edges from Preds to point to NewBB instead of BB. for (unsigned i = 0, e = Preds.size(); i != e; ++i) { @@ -553,6 +554,7 @@ // The new block unconditionally branches to the old block. BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); + BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); // Move the edges from Preds to point to NewBB1 instead of OrigBB. for (unsigned i = 0, e = Preds.size(); i != e; ++i) { @@ -593,6 +595,7 @@ // The new block unconditionally branches to the old block. BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); + BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); // Move the remaining edges from OrigBB to point to NewBB2. for (SmallVectorImpl::iterator Index: lib/Transforms/Utils/LoopSimplify.cpp =================================================================== --- lib/Transforms/Utils/LoopSimplify.cpp +++ lib/Transforms/Utils/LoopSimplify.cpp @@ -144,8 +144,6 @@ PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader", AA, DT, LI, PreserveLCSSA); - PreheaderBB->getTerminator()->setDebugLoc( - Header->getFirstNonPHI()->getDebugLoc()); DEBUG(dbgs() << "LoopSimplify: Creating pre-header " << PreheaderBB->getName() << "\n"); Index: test/Transforms/LoopSimplify/dbg-loc.ll =================================================================== --- /dev/null +++ test/Transforms/LoopSimplify/dbg-loc.ll @@ -0,0 +1,56 @@ +; Check that LoopSimplify creates debug locations in synthesized basic blocks. +; RUN: opt -loop-simplify %s -S -o - | FileCheck %s + +; CHECK: for.body.preheader: +; CHECK-NEXT: br label %for.body, !dbg [[PREHEADER_LOC:![0-9]+]] +; CHECK: for.end.loopexit: +; CHECK-NEXT: br label %for.end, !dbg [[LOOPEXIT_LOC:![0-9]+]] +; CHECK-DAG: [[PREHEADER_LOC]] = !DILocation(line: 73, column: 27, scope: !{{[0-9]+}}) +; CHECK-DAG: [[LOOPEXIT_LOC]] = !DILocation(line: 75, column: 9, scope: !{{[0-9]+}}) + +%union.anon = type { i32 } +%"Length" = type <{ %union.anon, i8, i8, i8, i8 }> + +; Function Attrs: nounwind ssp uwtable +define linkonce_odr hidden void @foo(%"Length"* %begin, %"Length"* %end) #4 align 2 { +entry: + %cmp.4 = icmp eq %"Length"* %begin, %end, !dbg !7 + br i1 %cmp.4, label %for.end, label %for.body, !dbg !8 + +for.body: ; preds = %entry, %length.exit + %begin.sink5 = phi %"Length"* [ %incdec.ptr, %length.exit ], [ %begin, %entry ] + %m_type.i.i.i = getelementptr inbounds %"Length", %"Length"* %begin.sink5, i64 0, i32 2, !dbg !9 + %0 = load i8, i8* %m_type.i.i.i, align 1, !dbg !9 + %cmp.i.i = icmp eq i8 %0, 9, !dbg !7 + br i1 %cmp.i.i, label %if.then.i, label %length.exit, !dbg !8 + +if.then.i: ; preds = %for.body + tail call void @bar(%"Length"* %begin.sink5) #7, !dbg !10 + br label %length.exit, !dbg !10 + +length.exit: ; preds = %for.body, %if.then.i + %incdec.ptr = getelementptr inbounds %"Length", %"Length"* %begin.sink5, i64 1, !dbg !11 + %cmp = icmp eq %"Length"* %incdec.ptr, %end, !dbg !7 + br i1 %cmp, label %for.end, label %for.body, !dbg !8 + +for.end: ; preds = %length.exit, %entry + ret void, !dbg !12 +} + +declare void @bar(%"Length"*) #3 + +!llvm.module.flags = !{!0, !1, !2} +!0 = !{i32 2, !"Dwarf Version", i32 4} +!1 = !{i32 2, !"Debug Info Version", i32 3} +!2 = !{i32 1, !"PIC Level", i32 2} + +!3 = !{} +!4 = !DISubroutineType(types: !3) +!5 = !DIFile(filename: "Vector.h", directory: "/tmp") +!6 = !DISubprogram(name: "destruct", scope: !5, file: !5, line: 71, type: !4, isLocal: false, isDefinition: true, scopeLine: 72, flags: DIFlagPrototyped, isOptimized: false, function: void (%"Length"*, %"Length"*)* @foo, variables: !3) +!7 = !DILocation(line: 73, column: 38, scope: !6) +!8 = !DILocation(line: 73, column: 13, scope: !6) +!9 = !DILocation(line: 73, column: 27, scope: !6) +!10 = !DILocation(line: 74, column: 17, scope: !6) +!11 = !DILocation(line: 73, column: 46, scope: !6) +!12 = !DILocation(line: 75, column: 9, scope: !6)