// Step 1: Insert the ctpop instruction at the end of the precondition block
IRBuilderTy Builder(PreCondBr);
- Value *PopCnt, *PopCntZext, *NewCount;
+ Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
{
PopCnt = createPopcntIntrinsic(Builder, Var, DL);
NewCount = PopCntZext =
if (NewCount != PopCnt)
(cast<Instruction>(NewCount))->setDebugLoc(DL);
+ // TripCnt is exactly the number of iterations the loop has
+ TripCnt = NewCount;
+
// If the popoulation counter's initial value is not zero, insert Add Inst.
Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
if (!InitConst || !InitConst->isZero()) {
- NewCount = Builder.CreateAdd(PopCnt, InitConst);
+ NewCount = Builder.CreateAdd(NewCount, CntInitVal);
(cast<Instruction>(NewCount))->setDebugLoc(DL);
}
}
{
BranchInst *LbBr = LIRUtil::getBranch(Body);
ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
- Type *Ty = NewCount->getType();
+ Type *Ty = TripCnt->getType();
PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", Body->begin());
Instruction *TcDec =
cast<Instruction>(Builder.CreateSub(Opnd1, Opnd2, "tcdec", false, true));
- TcPhi->addIncoming(NewCount, PreHead);
+ TcPhi->addIncoming(TripCnt, PreHead);
TcPhi->addIncoming(TcDec, Body);
CmpInst::Predicate Pred = (LbBr->getSuccessor(0) == Body) ?