@@ -1346,20 +1346,21 @@ unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
1346
1346
return 1 ;
1347
1347
}
1348
1348
1349
- static bool ValueHasAtMostOneBitSet (SDValue Val, const SelectionDAG &DAG) {
1349
+ static bool ValueHasExactlyOneBitSet (SDValue Val, const SelectionDAG &DAG) {
1350
1350
// Logical shift right or left won't ever introduce new set bits.
1351
1351
// We check for this case because we don't care which bits are
1352
1352
// set, but ComputeMaskedBits won't know anything unless it can
1353
1353
// determine which specific bits may be set.
1354
1354
if (Val.getOpcode () == ISD::SHL || Val.getOpcode () == ISD::SRL)
1355
- return ValueHasAtMostOneBitSet (Val.getOperand (0 ), DAG);
1355
+ return ValueHasExactlyOneBitSet (Val.getOperand (0 ), DAG);
1356
1356
1357
1357
MVT OpVT = Val.getValueType ();
1358
1358
unsigned BitWidth = OpVT.getSizeInBits ();
1359
1359
APInt Mask = APInt::getAllOnesValue (BitWidth);
1360
1360
APInt KnownZero, KnownOne;
1361
1361
DAG.ComputeMaskedBits (Val, Mask, KnownZero, KnownOne);
1362
- return KnownZero.countPopulation () == BitWidth - 1 ;
1362
+ return (KnownZero.countPopulation () == BitWidth - 1 ) &&
1363
+ (KnownOne.countPopulation () == 1 );
1363
1364
}
1364
1365
1365
1366
// / SimplifySetCC - Try to simplify a setcc built with the specified operands
@@ -1832,17 +1833,20 @@ TargetLowering::SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
1832
1833
}
1833
1834
1834
1835
// Simplify x&y == y to x&y != 0 if y has exactly one bit set.
1836
+ // Note that where y is variable and is known to have at most
1837
+ // one bit set (for example, if it is z&1) we cannot do this;
1838
+ // the expressions are not equivalent when y==0.
1835
1839
if (N0.getOpcode () == ISD::AND)
1836
1840
if (N0.getOperand (0 ) == N1 || N0.getOperand (1 ) == N1) {
1837
- if (ValueHasAtMostOneBitSet (N1, DAG)) {
1841
+ if (ValueHasExactlyOneBitSet (N1, DAG)) {
1838
1842
Cond = ISD::getSetCCInverse (Cond, /* isInteger=*/ true );
1839
1843
SDValue Zero = DAG.getConstant (0 , N1.getValueType ());
1840
1844
return DAG.getSetCC (dl, VT, N0, Zero, Cond);
1841
1845
}
1842
1846
}
1843
1847
if (N1.getOpcode () == ISD::AND)
1844
1848
if (N1.getOperand (0 ) == N0 || N1.getOperand (1 ) == N0) {
1845
- if (ValueHasAtMostOneBitSet (N0, DAG)) {
1849
+ if (ValueHasExactlyOneBitSet (N0, DAG)) {
1846
1850
Cond = ISD::getSetCCInverse (Cond, /* isInteger=*/ true );
1847
1851
SDValue Zero = DAG.getConstant (0 , N0.getValueType ());
1848
1852
return DAG.getSetCC (dl, VT, N1, Zero, Cond);
0 commit comments