Skip to content

Commit 9cae5f7

Browse files
committed
!fixup address latest comments, thanks
1 parent ba15842 commit 9cae5f7

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ bool VPBlockUtils::isHeader(const VPBlockBase *VPB,
226226

227227
bool VPBlockUtils::isLatch(const VPBlockBase *VPB,
228228
const VPDominatorTree &VPDT) {
229+
// A latch has a header as its second successor, with its other successor
230+
// leaving the loop. A preheader OTOH has a header as its first (and only)
231+
// successor.
229232
return VPB->getNumSuccessors() == 2 &&
230233
VPBlockUtils::isHeader(VPB->getSuccessors()[1], VPDT);
231234
}
@@ -455,7 +458,7 @@ void VPBasicBlock::connectToPredecessors(VPTransformState &State) {
455458
auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors();
456459
assert(CFG.VPBB2IRBB.contains(PredVPBB) &&
457460
"Predecessor basic-block not found building successor.");
458-
BasicBlock *PredBB = CFG.VPBB2IRBB.lookup(PredVPBB);
461+
BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
459462
auto *PredBBTerminator = PredBB->getTerminator();
460463
LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
461464

@@ -1406,18 +1409,17 @@ void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
14061409

14071410
#endif
14081411

1409-
bool VPValue::isDefinedOutsideLoop() const {
1410-
auto *DefR = getDefiningRecipe();
1411-
if (!DefR)
1412-
return true;
1413-
1414-
// For non-live-ins, check if is in a region only if the top-level loop region
1415-
// still exits.
1416-
const VPBasicBlock *DefVPBB = DefR->getParent();
1417-
auto *Plan = DefVPBB->getPlan();
1418-
return Plan->getVectorLoopRegion() && !DefVPBB->getEnclosingLoopRegion();
1412+
/// Returns true if there is a vector loop region and \p VPV is defined in a
1413+
/// loop region.
1414+
static bool isDefinedInsideLoopRegions(const VPValue *VPV) {
1415+
const VPRecipeBase *DefR = VPV->getDefiningRecipe();
1416+
return DefR && (!DefR->getParent()->getPlan()->getVectorLoopRegion() ||
1417+
DefR->getParent()->getEnclosingLoopRegion());
14191418
}
14201419

1420+
bool VPValue::isDefinedOutsideLoop() const {
1421+
return !isDefinedInsideLoopRegions(this);
1422+
}
14211423
void VPValue::replaceAllUsesWith(VPValue *New) {
14221424
replaceUsesWithIf(New, [](VPUser &, unsigned) { return true; });
14231425
}

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,26 @@ Value *VPInstruction::generatePerLane(VPTransformState &State,
456456
State.get(getOperand(1), Lane), Name);
457457
}
458458

459+
/// Create a conditional branch using \p Cond branching to the successors of \p
460+
/// VPBB. Note that the first successor is always forward (i.e. not created yet)
461+
/// while the second successor may already have been created (if it is a header
462+
/// block and VPBB is a header).
463+
static BranchInst *createCondBranch(Value *Cond, VPBasicBlock *VPBB,
464+
VPTransformState &State) {
465+
// Replace the temporary unreachable terminator with a new conditional
466+
// branch, hooking it up to backward destination (header) for latch blocks
467+
// now, and to forward destination(s) later when they are created.
468+
// Second successor may be backwards - iff it is already in VPBB2IRBB.
469+
VPBasicBlock *SecondVPSucc = cast<VPBasicBlock>(VPBB->getSuccessors()[1]);
470+
BasicBlock *SecondIRSucc = State.CFG.VPBB2IRBB.lookup(SecondVPSucc);
471+
BasicBlock *IRBB = State.CFG.VPBB2IRBB[VPBB];
472+
BranchInst *CondBr = State.Builder.CreateCondBr(Cond, IRBB, SecondIRSucc);
473+
// First successor is always forward, reset it to nullptr
474+
CondBr->setSuccessor(0, nullptr);
475+
IRBB->getTerminator()->eraseFromParent();
476+
return CondBr;
477+
}
478+
459479
Value *VPInstruction::generate(VPTransformState &State) {
460480
IRBuilderBase &Builder = State.Builder;
461481

@@ -575,40 +595,14 @@ Value *VPInstruction::generate(VPTransformState &State) {
575595
}
576596
case VPInstruction::BranchOnCond: {
577597
Value *Cond = State.get(getOperand(0), VPLane(0));
578-
// Replace the temporary unreachable terminator with a new conditional
579-
// branch, hooking it up to backward destination (header) for latch blocks
580-
// now, and to forward destination(s) later when they are created.
581-
BranchInst *CondBr =
582-
Builder.CreateCondBr(Cond, Builder.GetInsertBlock(), nullptr);
583-
CondBr->setSuccessor(0, nullptr);
584-
Builder.GetInsertBlock()->getTerminator()->eraseFromParent();
585-
586-
VPBasicBlock *Header = cast<VPBasicBlock>(getParent()->getSuccessors()[1]);
587-
if (!State.CFG.VPBB2IRBB.contains(Header))
588-
return CondBr;
589-
590-
CondBr->setSuccessor(1, State.CFG.VPBB2IRBB[Header]);
591-
return CondBr;
598+
return createCondBranch(Cond, getParent(), State);
592599
}
593600
case VPInstruction::BranchOnCount: {
594601
// First create the compare.
595602
Value *IV = State.get(getOperand(0), /*IsScalar*/ true);
596603
Value *TC = State.get(getOperand(1), /*IsScalar*/ true);
597604
Value *Cond = Builder.CreateICmpEQ(IV, TC);
598-
599-
// Now create the branch.
600-
VPBasicBlock *Header = cast<VPBasicBlock>(getParent()->getSuccessors()[1]);
601-
602-
// Replace the temporary unreachable terminator with a new conditional
603-
// branch, hooking it up to backward destination (the header) for latch
604-
// blocks now, and to forward destination (the exit/middle block) later when
605-
// it is created. Note that CreateCondBr expects a valid BB as first
606-
// argument, so we need to set it to nullptr later.
607-
BranchInst *CondBr = Builder.CreateCondBr(Cond, Builder.GetInsertBlock(),
608-
State.CFG.VPBB2IRBB[Header]);
609-
CondBr->setSuccessor(0, nullptr);
610-
Builder.GetInsertBlock()->getTerminator()->eraseFromParent();
611-
return CondBr;
605+
return createCondBranch(Cond, getParent(), State);
612606
}
613607
case VPInstruction::Broadcast: {
614608
return Builder.CreateVectorSplat(

0 commit comments

Comments
 (0)