@@ -502,6 +502,15 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
502
502
cast<FixedVectorType>(EI0->getVectorOperandType())->getNumElements();
503
503
Value *Vec1 = nullptr;
504
504
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
+ });
505
514
enum ShuffleMode { Unknown, Select, Permute };
506
515
ShuffleMode CommonShuffleMode = Unknown;
507
516
Mask.assign(VL.size(), PoisonMaskElem);
@@ -514,21 +523,27 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
514
523
return std::nullopt;
515
524
auto *Vec = EI->getVectorOperand();
516
525
// We can extractelement from undef or poison vector.
517
- if (isUndefVector(Vec).all())
526
+ if (isUndefVector</*isPoisonOnly=*/true> (Vec).all())
518
527
continue;
519
528
// 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)
529
546
continue;
530
- unsigned IntIdx = Idx->getValue().getZExtValue();
531
- Mask[I] = IntIdx;
532
547
// For correct shuffling we have to have at most 2 different vector operands
533
548
// in all extractelement instructions.
534
549
if (!Vec1 || Vec1 == Vec) {
@@ -543,7 +558,7 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
543
558
continue;
544
559
// If the extract index is not the same as the operation number, it is a
545
560
// permutation.
546
- if (IntIdx != I) {
561
+ if (Mask[I] % Size != I) {
547
562
CommonShuffleMode = Permute;
548
563
continue;
549
564
}
0 commit comments