Index: lib/IR/BasicBlock.cpp =================================================================== --- lib/IR/BasicBlock.cpp +++ lib/IR/BasicBlock.cpp @@ -362,12 +362,15 @@ BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), InsertBefore); + // Save DebugLoc of split point before invalidating iterator. + DebugLoc Loc = I->getDebugLoc(); // Move all of the specified instructions from the original basic block into // the new basic block. New->getInstList().splice(New->end(), this->getInstList(), I, end()); // Add a branch instruction to the newly formed basic block. - BranchInst::Create(New, this); + BranchInst *BI = BranchInst::Create(New, this); + BI->setDebugLoc(Loc); // Now we must loop through all of the successors of the New block (which // _were_ the successors of the 'this' block), and update any PHI nodes in Index: lib/Transforms/Utils/LoopUnrollRuntime.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -113,6 +113,7 @@ // Create a branch around the orignal loop, which is taken if there are no // iterations remaining to be executed after running the prologue. Instruction *InsertPt = PrologEnd->getTerminator(); + IRBuilder<> B(InsertPt); assert(Count != 0 && "nonsensical Count!"); @@ -120,9 +121,8 @@ // (since Count is a power of 2). This means %xtraiter is (BECount + 1) and // and all of the iterations of this loop were executed by the prologue. Note // that if BECount getType(), Count - 1)); + Value *BrLoopExit = + B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1)); BasicBlock *Exit = L->getUniqueExitBlock(); assert(Exit && "Loop must have a single exit block only"); // Split the exit to maintain loop canonicalization guarantees @@ -130,7 +130,7 @@ SplitBlockPredecessors(Exit, Preds, ".unr-lcssa", AA, DT, LI, P->mustPreserveAnalysisID(LCSSAID)); // Add the branch to the exit block (around the unrolled loop) - BranchInst::Create(Exit, NewPH, BrLoopExit, InsertPt); + B.CreateCondBr(BrLoopExit, Exit, NewPH); InsertPt->eraseFromParent(); } @@ -184,23 +184,22 @@ VMap.erase((*BB)->getTerminator()); BasicBlock *FirstLoopBB = cast(VMap[Header]); BranchInst *LatchBR = cast(NewBB->getTerminator()); + IRBuilder<> Builder(LatchBR); if (UnrollProlog) { - LatchBR->eraseFromParent(); - BranchInst::Create(InsertBot, NewBB); + Builder.CreateBr(InsertBot); } else { PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2, "prol.iter", FirstLoopBB->getFirstNonPHI()); - IRBuilder<> Builder(LatchBR); Value *IdxSub = Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1), NewIdx->getName() + ".sub"); Value *IdxCmp = Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp"); - BranchInst::Create(FirstLoopBB, InsertBot, IdxCmp, NewBB); + Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot); NewIdx->addIncoming(NewIter, InsertTop); NewIdx->addIncoming(IdxSub, NewBB); - LatchBR->eraseFromParent(); } + LatchBR->eraseFromParent(); } } @@ -370,7 +369,7 @@ // Branch to either the extra iterations or the cloned/unrolled loop // We will fix up the true branch label when adding loop body copies - BranchInst::Create(PEnd, PEnd, BranchVal, PreHeaderBR); + B.CreateCondBr(BranchVal, PEnd, PEnd); assert(PreHeaderBR->isUnconditional() && PreHeaderBR->getSuccessor(0) == PEnd && "CFG edges in Preheader are not correct"); Index: test/Transforms/LoopUnroll/runtime-loop1.ll =================================================================== --- test/Transforms/LoopUnroll/runtime-loop1.ll +++ test/Transforms/LoopUnroll/runtime-loop1.ll @@ -2,29 +2,50 @@ ; This tests that setting the unroll count works +; CHECK: for.body.preheader: +; CHECK: br {{.*}} label %for.body.prol, label %for.body.preheader.split, !dbg [[PH_LOC:![0-9]+]] ; CHECK: for.body.prol: -; CHECK: br label %for.body.preheader.split +; CHECK: br label %for.body.preheader.split, !dbg [[BODY_LOC:![0-9]+]] +; CHECK: for.body.preheader.split: +; CHECK: br {{.*}} label %for.end.loopexit, label %for.body.preheader.split.split, !dbg [[PH_LOC]] ; CHECK: for.body: -; CHECK: br i1 %exitcond.1, label %for.end.loopexit.unr-lcssa, label %for.body +; CHECK: br i1 %exitcond.1, label %for.end.loopexit.unr-lcssa, label %for.body, !dbg [[BODY_LOC]] ; CHECK-NOT: br i1 %exitcond.4, label %for.end.loopexit{{.*}}, label %for.body +; CHECK-DAG: [[PH_LOC]] = !DILocation(line: 101, column: 1, scope: !{{.*}}) +; CHECK-DAG: [[BODY_LOC]] = !DILocation(line: 102, column: 1, scope: !{{.*}}) + define i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly { entry: - %cmp1 = icmp eq i32 %n, 0 - br i1 %cmp1, label %for.end, label %for.body + %cmp1 = icmp eq i32 %n, 0, !dbg !7 + br i1 %cmp1, label %for.end, label %for.body, !dbg !7 for.body: ; preds = %for.body, %entry %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ] %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ] - %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv - %0 = load i32, i32* %arrayidx, align 4 - %add = add nsw i32 %0, %sum.02 - %indvars.iv.next = add i64 %indvars.iv, 1 - %lftr.wideiv = trunc i64 %indvars.iv.next to i32 - %exitcond = icmp eq i32 %lftr.wideiv, %n - br i1 %exitcond, label %for.end, label %for.body + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv, !dbg !8 + %0 = load i32, i32* %arrayidx, align 4, !dbg !8 + %add = add nsw i32 %0, %sum.02, !dbg !8 + %indvars.iv.next = add i64 %indvars.iv, 1, !dbg !9 + %lftr.wideiv = trunc i64 %indvars.iv.next to i32, !dbg !9 + %exitcond = icmp eq i32 %lftr.wideiv, %n, !dbg !9 + br i1 %exitcond, label %for.end, label %for.body, !dbg !9 for.end: ; preds = %for.body, %entry %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ] - ret i32 %sum.0.lcssa + ret i32 %sum.0.lcssa, !dbg !10 } + +!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: "test.cpp", directory: "/tmp") +!6 = !DISubprogram(name: "test", scope: !5, file: !5, line: 99, type: !4, isLocal: false, isDefinition: true, scopeLine: 100, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32*, i32)* @test, variables: !3) +!7 = !DILocation(line: 100, column: 1, scope: !6) +!8 = !DILocation(line: 101, column: 1, scope: !6) +!9 = !DILocation(line: 102, column: 1, scope: !6) +!10 = !DILocation(line: 103, column: 1, scope: !6)