@@ -502,6 +502,15 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
502502 cast<FixedVectorType>(EI0->getVectorOperandType())->getNumElements();
503503 Value *Vec1 = nullptr;
504504 Value *Vec2 = nullptr;
505+ bool HasNonUndefVec = any_of(VL, [](Value *V) {
506+ auto *EE = dyn_cast<ExtractElementInst>(V);
507+ if (!EE)
508+ return false;
509+ Value *Vec = EE->getVectorOperand();
510+ if (isa<UndefValue>(Vec))
511+ return false;
512+ return isGuaranteedNotToBePoison(Vec);
513+ });
505514 enum ShuffleMode { Unknown, Select, Permute };
506515 ShuffleMode CommonShuffleMode = Unknown;
507516 Mask.assign(VL.size(), PoisonMaskElem);
@@ -514,21 +523,27 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
514523 return std::nullopt;
515524 auto *Vec = EI->getVectorOperand();
516525 // We can extractelement from undef or poison vector.
517- if (isUndefVector(Vec).all())
526+ if (isUndefVector</*isPoisonOnly=*/true> (Vec).all())
518527 continue;
519528 // All vector operands must have the same number of vector elements.
520- if (cast<FixedVectorType>(Vec->getType())->getNumElements() != Size)
521- return std::nullopt;
522- if (isa<UndefValue>(EI->getIndexOperand()))
523- continue;
524- auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
525- if (!Idx)
526- return std::nullopt;
527- // Undefined behavior if Idx is negative or >= Size.
528- if (Idx->getValue().uge(Size))
529+ if (isa<UndefValue>(Vec)) {
530+ Mask[I] = I;
531+ } else {
532+ if (cast<FixedVectorType>(Vec->getType())->getNumElements() != Size)
533+ return std::nullopt;
534+ if (isa<UndefValue>(EI->getIndexOperand()))
535+ continue;
536+ auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
537+ if (!Idx)
538+ return std::nullopt;
539+ // Undefined behavior if Idx is negative or >= Size.
540+ if (Idx->getValue().uge(Size))
541+ continue;
542+ unsigned IntIdx = Idx->getValue().getZExtValue();
543+ Mask[I] = IntIdx;
544+ }
545+ if (isUndefVector(Vec).all() && HasNonUndefVec)
529546 continue;
530- unsigned IntIdx = Idx->getValue().getZExtValue();
531- Mask[I] = IntIdx;
532547 // For correct shuffling we have to have at most 2 different vector operands
533548 // in all extractelement instructions.
534549 if (!Vec1 || Vec1 == Vec) {
@@ -543,7 +558,7 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
543558 continue;
544559 // If the extract index is not the same as the operation number, it is a
545560 // permutation.
546- if (IntIdx != I) {
561+ if (Mask[I] % Size != I) {
547562 CommonShuffleMode = Permute;
548563 continue;
549564 }
0 commit comments