[VPlan] Support PHIs as LastInst when inserting scalars in ::get().

At the moment, we create insertelement instructions directly after
LastInst when inserting scalar values in a vector in
VPTransformState::get.

This results in invalid IR when LastInst is a phi, followed by another
phi. In that case, the new instructions should be inserted just after
the last PHI node in the block.

At the moment, I don't think the problematic case can be triggered, but
it can happen once predicate regions are merged and multiple
VPredInstPHI recipes are in the same block (D100260).

Reviewed By: Ayal

Differential Revision: https://reviews.llvm.org/D104188
This commit is contained in:
Florian Hahn 2021-06-17 08:53:36 +01:00
parent 873308fd8c
commit 80a403348b
No known key found for this signature in database
GPG Key ID: 61D7554B5CECDC0D
1 changed files with 7 additions and 5 deletions

View File

@ -9730,12 +9730,14 @@ Value *VPTransformState::get(VPValue *Def, unsigned Part) {
}
auto *LastInst = cast<Instruction>(get(Def, {Part, LastLane}));
// Set the insert point after the last scalarized instruction. This
// ensures the insertelement sequence will directly follow the scalar
// definitions.
// Set the insert point after the last scalarized instruction or after the
// last PHI, if LastInst is a PHI. This ensures the insertelement sequence
// will directly follow the scalar definitions.
auto OldIP = Builder.saveIP();
auto NewIP = std::next(BasicBlock::iterator(LastInst));
auto NewIP =
isa<PHINode>(LastInst)
? BasicBlock::iterator(LastInst->getParent()->getFirstNonPHI())
: std::next(BasicBlock::iterator(LastInst));
Builder.SetInsertPoint(&*NewIP);
// However, if we are vectorizing, we need to construct the vector values.