diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -4152,13 +4152,17 @@ VPWidenPHIRecipe *VPPhi = dyn_cast(&P); if (!VPPhi) continue; - PHINode *NewPhi = cast(State.get(VPPhi, 0)); - // Make sure the builder has a valid insert point. - Builder.SetInsertPoint(NewPhi); - for (unsigned i = 0; i < VPPhi->getNumOperands(); ++i) { - VPValue *Inc = VPPhi->getIncomingValue(i); - VPBasicBlock *VPBB = VPPhi->getIncomingBlock(i); - NewPhi->addIncoming(State.get(Inc, 0), State.CFG.VPBB2IRBB[VPBB]); + for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) { + PHINode *NewPhi = cast(State.get(VPPhi, Part)); + // Make sure the builder has a valid insert point. + Builder.SetInsertPoint(NewPhi); + for (unsigned i = 0; i < VPPhi->getNumOperands(); ++i) { + VPValue *Inc = VPPhi->getIncomingValue(i); + BasicBlock *BB = State.CFG.VPBB2IRBB[VPPhi->getIncomingBlock(i)]; + if (is_contained(NewPhi->blocks(), BB)) + continue; + NewPhi->addIncoming(State.get(Inc, Part), BB); + } } } } diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp --- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp @@ -812,10 +812,13 @@ StartIdx = I; } } - Value *Op0 = State.get(getOperand(StartIdx), 0); - Type *VecTy = Op0->getType(); - Value *VecPhi = State.Builder.CreatePHI(VecTy, 2, "vec.phi"); - State.set(this, VecPhi, 0); + for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) { + Value *Start = State.get(getOperand(StartIdx), Part); + Type *VecTy = Start->getType(); + PHINode *VecPhi = State.Builder.CreatePHI(VecTy, 2, "vec.phi"); + VecPhi->addIncoming(Start, State.CFG.VPBB2IRBB[getIncomingBlock(StartIdx)]); + State.set(this, VecPhi, 0); + } } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)