[LoopUnroll] Use IRBuilder to create branch instructions.

Use IRBuilder::Create(Cond)?Br instead of constructing instructions
manually with BranchInst::Create(). It's consistent with other
uses of IRBuilder in this pass, and has an additional important
benefit:

Using IRBuilder will ensure that new branch instruction will get
the same debug location as original terminator instruction it will
eventually replace.

For now I'm not adding a testcase, as currently original terminator
instruction also lack debug location due to missing debug location
propagation in BasicBlock::splitBasicBlock. That is, the testcase
will accompany the fix for the latter I'm going to mail soon.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239550 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov 2015-06-11 18:25:44 +00:00
parent 29edabcc44
commit 7356846f65

View File

@ -113,6 +113,7 @@ static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
// Create a branch around the orignal loop, which is taken if there are no // Create a branch around the orignal loop, which is taken if there are no
// iterations remaining to be executed after running the prologue. // iterations remaining to be executed after running the prologue.
Instruction *InsertPt = PrologEnd->getTerminator(); Instruction *InsertPt = PrologEnd->getTerminator();
IRBuilder<> B(InsertPt);
assert(Count != 0 && "nonsensical Count!"); assert(Count != 0 && "nonsensical Count!");
@ -120,9 +121,8 @@ static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
// (since Count is a power of 2). This means %xtraiter is (BECount + 1) and // (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 // and all of the iterations of this loop were executed by the prologue. Note
// that if BECount <u (Count - 1) then (BECount + 1) cannot unsigned-overflow. // that if BECount <u (Count - 1) then (BECount + 1) cannot unsigned-overflow.
Instruction *BrLoopExit = Value *BrLoopExit =
new ICmpInst(InsertPt, ICmpInst::ICMP_ULT, BECount, B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1));
ConstantInt::get(BECount->getType(), Count - 1));
BasicBlock *Exit = L->getUniqueExitBlock(); BasicBlock *Exit = L->getUniqueExitBlock();
assert(Exit && "Loop must have a single exit block only"); assert(Exit && "Loop must have a single exit block only");
// Split the exit to maintain loop canonicalization guarantees // Split the exit to maintain loop canonicalization guarantees
@ -130,7 +130,7 @@ static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
SplitBlockPredecessors(Exit, Preds, ".unr-lcssa", AA, DT, LI, SplitBlockPredecessors(Exit, Preds, ".unr-lcssa", AA, DT, LI,
P->mustPreserveAnalysisID(LCSSAID)); P->mustPreserveAnalysisID(LCSSAID));
// Add the branch to the exit block (around the unrolled loop) // Add the branch to the exit block (around the unrolled loop)
BranchInst::Create(Exit, NewPH, BrLoopExit, InsertPt); B.CreateCondBr(BrLoopExit, Exit, NewPH);
InsertPt->eraseFromParent(); InsertPt->eraseFromParent();
} }
@ -184,23 +184,22 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
VMap.erase((*BB)->getTerminator()); VMap.erase((*BB)->getTerminator());
BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]); BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator()); BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
IRBuilder<> Builder(LatchBR);
if (UnrollProlog) { if (UnrollProlog) {
LatchBR->eraseFromParent(); Builder.CreateBr(InsertBot);
BranchInst::Create(InsertBot, NewBB);
} else { } else {
PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2, "prol.iter", PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2, "prol.iter",
FirstLoopBB->getFirstNonPHI()); FirstLoopBB->getFirstNonPHI());
IRBuilder<> Builder(LatchBR);
Value *IdxSub = Value *IdxSub =
Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1), Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
NewIdx->getName() + ".sub"); NewIdx->getName() + ".sub");
Value *IdxCmp = Value *IdxCmp =
Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp"); Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp");
BranchInst::Create(FirstLoopBB, InsertBot, IdxCmp, NewBB); Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
NewIdx->addIncoming(NewIter, InsertTop); NewIdx->addIncoming(NewIter, InsertTop);
NewIdx->addIncoming(IdxSub, NewBB); NewIdx->addIncoming(IdxSub, NewBB);
LatchBR->eraseFromParent();
} }
LatchBR->eraseFromParent();
} }
} }
@ -370,7 +369,7 @@ bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count,
// Branch to either the extra iterations or the cloned/unrolled loop // Branch to either the extra iterations or the cloned/unrolled loop
// We will fix up the true branch label when adding loop body copies // 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() && assert(PreHeaderBR->isUnconditional() &&
PreHeaderBR->getSuccessor(0) == PEnd && PreHeaderBR->getSuccessor(0) == PEnd &&
"CFG edges in Preheader are not correct"); "CFG edges in Preheader are not correct");