@@ -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+
459479Value *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