@@ -2512,26 +2512,41 @@ static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
2512
2512
return ComputeNumSignBits (U->getOperand (0 ), Depth + 1 , Q);
2513
2513
2514
2514
case Instruction::ShuffleVector: {
2515
- // If the shuffle mask contains any undefined elements, that element of the
2516
- // result is undefined. Propagating information from a source operand may
2517
- // not be correct in that case, so just bail out.
2518
- if (cast<ShuffleVectorInst>(U)->getMask ()->containsUndefElement ())
2519
- break ;
2520
-
2521
- // If everything is undef, we can't say anything. This should be simplified.
2522
- Value *Op0 = U->getOperand (0 ), *Op1 = U->getOperand (1 );
2523
- if (isa<UndefValue>(Op0) && isa<UndefValue>(Op1))
2515
+ // TODO: This is copied almost directly from the SelectionDAG version of
2516
+ // ComputeNumSignBits. It would be better if we could share common
2517
+ // code. If not, make sure that changes are translated to the DAG.
2518
+
2519
+ // Collect the minimum number of sign bits that are shared by every vector
2520
+ // element referenced by the shuffle.
2521
+ auto *Shuf = cast<ShuffleVectorInst>(U);
2522
+ int NumElts = Shuf->getOperand (0 )->getType ()->getVectorNumElements ();
2523
+ int NumMaskElts = Shuf->getMask ()->getType ()->getVectorNumElements ();
2524
+ APInt DemandedLHS (NumElts, 0 ), DemandedRHS (NumElts, 0 );
2525
+ for (int i = 0 ; i != NumMaskElts; ++i) {
2526
+ int M = Shuf->getMaskValue (i);
2527
+ assert (M < NumElts * 2 && " Invalid shuffle mask constant" );
2528
+ // For undef elements, we don't know anything about the common state of
2529
+ // the shuffle result.
2530
+ if (M == -1 )
2531
+ return 1 ;
2532
+ if (M < NumElts)
2533
+ DemandedLHS.setBit (M % NumElts);
2534
+ else
2535
+ DemandedRHS.setBit (M % NumElts);
2536
+ }
2537
+ Tmp = std::numeric_limits<unsigned >::max ();
2538
+ if (!!DemandedLHS)
2539
+ Tmp = ComputeNumSignBits (Shuf->getOperand (0 ), Depth + 1 , Q);
2540
+ if (!!DemandedRHS) {
2541
+ Tmp2 = ComputeNumSignBits (Shuf->getOperand (1 ), Depth + 1 , Q);
2542
+ Tmp = std::min (Tmp, Tmp2);
2543
+ }
2544
+ // If we don't know anything, early out and try computeKnownBits fall-back.
2545
+ if (Tmp == 1 )
2524
2546
break ;
2525
-
2526
- // Look through shuffle of 1 source vector.
2527
- if (isa<UndefValue>(Op0))
2528
- return ComputeNumSignBits (Op1, Depth + 1 , Q);
2529
- if (isa<UndefValue>(Op1))
2530
- return ComputeNumSignBits (Op0, Depth + 1 , Q);
2531
-
2532
- // TODO: We can look through shuffles of 2 sources by computing the minimum
2533
- // sign bits for each operand (similar to what we do for binops).
2534
- break ;
2547
+ assert (Tmp <= V->getType ()->getScalarSizeInBits () &&
2548
+ " Failed to determine minimum sign bits" );
2549
+ return Tmp;
2535
2550
}
2536
2551
}
2537
2552
0 commit comments