@@ -319,20 +319,20 @@ Instruction *InstCombinerImpl::foldBitcastExtElt(ExtractElementInst &Ext) {
319
319
return nullptr ;
320
320
}
321
321
322
- // / Find elements of V demanded by UserInstr.
323
- static APInt findDemandedEltsBySingleUser (Value *V, Instruction *UserInstr) {
322
+ // / Find elements of V demanded by UserInstr. If returns false, we were not able
323
+ // / to determine all elements.
324
+ static bool findDemandedEltsBySingleUser (Value *V, Instruction *UserInstr,
325
+ APInt &UnionUsedElts) {
324
326
unsigned VWidth = cast<FixedVectorType>(V->getType ())->getNumElements ();
325
327
326
- // Conservatively assume that all elements are needed.
327
- APInt UsedElts (APInt::getAllOnes (VWidth));
328
-
329
328
switch (UserInstr->getOpcode ()) {
330
329
case Instruction::ExtractElement: {
331
330
ExtractElementInst *EEI = cast<ExtractElementInst>(UserInstr);
332
331
assert (EEI->getVectorOperand () == V);
333
332
ConstantInt *EEIIndexC = dyn_cast<ConstantInt>(EEI->getIndexOperand ());
334
333
if (EEIIndexC && EEIIndexC->getValue ().ult (VWidth)) {
335
- UsedElts = APInt::getOneBitSet (VWidth, EEIIndexC->getZExtValue ());
334
+ UnionUsedElts.setBit (EEIIndexC->getZExtValue ());
335
+ return true ;
336
336
}
337
337
break ;
338
338
}
@@ -341,23 +341,23 @@ static APInt findDemandedEltsBySingleUser(Value *V, Instruction *UserInstr) {
341
341
unsigned MaskNumElts =
342
342
cast<FixedVectorType>(UserInstr->getType ())->getNumElements ();
343
343
344
- UsedElts = APInt (VWidth, 0 );
345
- for (unsigned i = 0 ; i < MaskNumElts; i++) {
346
- unsigned MaskVal = Shuffle->getMaskValue (i);
344
+ for (auto I : llvm::seq (MaskNumElts)) {
345
+ unsigned MaskVal = Shuffle->getMaskValue (I);
347
346
if (MaskVal == -1u || MaskVal >= 2 * VWidth)
348
347
continue ;
349
348
if (Shuffle->getOperand (0 ) == V && (MaskVal < VWidth))
350
- UsedElts .setBit (MaskVal);
349
+ UnionUsedElts .setBit (MaskVal);
351
350
if (Shuffle->getOperand (1 ) == V &&
352
351
((MaskVal >= VWidth) && (MaskVal < 2 * VWidth)))
353
- UsedElts .setBit (MaskVal - VWidth);
352
+ UnionUsedElts .setBit (MaskVal - VWidth);
354
353
}
355
- break ;
354
+ return true ;
356
355
}
357
356
default :
358
357
break ;
359
358
}
360
- return UsedElts;
359
+
360
+ return false ;
361
361
}
362
362
363
363
// / Find union of elements of V demanded by all its users.
@@ -370,7 +370,8 @@ static APInt findDemandedEltsByAllUsers(Value *V) {
370
370
APInt UnionUsedElts (VWidth, 0 );
371
371
for (const Use &U : V->uses ()) {
372
372
if (Instruction *I = dyn_cast<Instruction>(U.getUser ())) {
373
- UnionUsedElts |= findDemandedEltsBySingleUser (V, I);
373
+ if (!findDemandedEltsBySingleUser (V, I, UnionUsedElts))
374
+ return APInt::getAllOnes (VWidth);
374
375
} else {
375
376
UnionUsedElts = APInt::getAllOnes (VWidth);
376
377
break ;
0 commit comments