@@ -454,6 +454,15 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
454
454
cast<FixedVectorType>(EI0->getVectorOperandType())->getNumElements();
455
455
Value *Vec1 = nullptr;
456
456
Value *Vec2 = nullptr;
457
+ bool HasNonUndefVec = any_of(VL, [](Value *V) {
458
+ auto *EE = dyn_cast<ExtractElementInst>(V);
459
+ if (!EE)
460
+ return false;
461
+ Value *Vec = EE->getVectorOperand();
462
+ if (isa<UndefValue>(Vec))
463
+ return false;
464
+ return isGuaranteedNotToBePoison(Vec);
465
+ });
457
466
enum ShuffleMode { Unknown, Select, Permute };
458
467
ShuffleMode CommonShuffleMode = Unknown;
459
468
Mask.assign(VL.size(), PoisonMaskElem);
@@ -466,21 +475,27 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
466
475
return std::nullopt;
467
476
auto *Vec = EI->getVectorOperand();
468
477
// We can extractelement from undef or poison vector.
469
- if (isUndefVector(Vec).all())
478
+ if (isUndefVector</*isPoisonOnly=*/true> (Vec).all())
470
479
continue;
471
480
// All vector operands must have the same number of vector elements.
472
- if (cast<FixedVectorType>(Vec->getType())->getNumElements() != Size)
473
- return std::nullopt;
474
- if (isa<UndefValue>(EI->getIndexOperand()))
475
- continue;
476
- auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
477
- if (!Idx)
478
- return std::nullopt;
479
- // Undefined behavior if Idx is negative or >= Size.
480
- if (Idx->getValue().uge(Size))
481
+ if (isa<UndefValue>(Vec)) {
482
+ Mask[I] = I;
483
+ } else {
484
+ if (cast<FixedVectorType>(Vec->getType())->getNumElements() != Size)
485
+ return std::nullopt;
486
+ if (isa<UndefValue>(EI->getIndexOperand()))
487
+ continue;
488
+ auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
489
+ if (!Idx)
490
+ return std::nullopt;
491
+ // Undefined behavior if Idx is negative or >= Size.
492
+ if (Idx->getValue().uge(Size))
493
+ continue;
494
+ unsigned IntIdx = Idx->getValue().getZExtValue();
495
+ Mask[I] = IntIdx;
496
+ }
497
+ if (isUndefVector(Vec).all() && HasNonUndefVec)
481
498
continue;
482
- unsigned IntIdx = Idx->getValue().getZExtValue();
483
- Mask[I] = IntIdx;
484
499
// For correct shuffling we have to have at most 2 different vector operands
485
500
// in all extractelement instructions.
486
501
if (!Vec1 || Vec1 == Vec) {
@@ -495,7 +510,7 @@ isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
495
510
continue;
496
511
// If the extract index is not the same as the operation number, it is a
497
512
// permutation.
498
- if (IntIdx != I) {
513
+ if (Mask[I] % Size != I) {
499
514
CommonShuffleMode = Permute;
500
515
continue;
501
516
}
0 commit comments