@@ -389,7 +389,8 @@ static ConstantInt *SubOne(ConstantInt *C) {
389
389
// / MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
390
390
// / this predicate to simplify operations downstream. V and Mask are known to
391
391
// / be the same type.
392
- static bool MaskedValueIsZero (Value *V, ConstantIntegral *Mask) {
392
+ static bool MaskedValueIsZero (Value *V, ConstantIntegral *Mask,
393
+ unsigned Depth = 0 ) {
393
394
// Note, we cannot consider 'undef' to be "IsZero" here. The problem is that
394
395
// we cannot optimize based on the assumption that it is zero without changing
395
396
// to to an explicit zero. If we don't change it to zero, other code could
@@ -400,6 +401,8 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
400
401
return true ;
401
402
if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
402
403
return ConstantExpr::getAnd (CI, Mask)->isNullValue ();
404
+
405
+ if (Depth == 6 ) return false ; // Limit search depth.
403
406
404
407
if (Instruction *I = dyn_cast<Instruction>(V)) {
405
408
switch (I->getOpcode ()) {
@@ -408,21 +411,21 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
408
411
if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand (1 ))) {
409
412
ConstantIntegral *C1C2 =
410
413
cast<ConstantIntegral>(ConstantExpr::getAnd (CI, Mask));
411
- if (MaskedValueIsZero (I->getOperand (0 ), C1C2))
414
+ if (MaskedValueIsZero (I->getOperand (0 ), C1C2, Depth+ 1 ))
412
415
return true ;
413
416
}
414
417
// If either the LHS or the RHS are MaskedValueIsZero, the result is zero.
415
- return MaskedValueIsZero (I->getOperand (1 ), Mask) ||
416
- MaskedValueIsZero (I->getOperand (0 ), Mask);
418
+ return MaskedValueIsZero (I->getOperand (1 ), Mask, Depth+ 1 ) ||
419
+ MaskedValueIsZero (I->getOperand (0 ), Mask, Depth+ 1 );
417
420
case Instruction::Or:
418
421
case Instruction::Xor:
419
422
// If the LHS and the RHS are MaskedValueIsZero, the result is also zero.
420
- return MaskedValueIsZero (I->getOperand (1 ), Mask) &&
421
- MaskedValueIsZero (I->getOperand (0 ), Mask);
423
+ return MaskedValueIsZero (I->getOperand (1 ), Mask, Depth+ 1 ) &&
424
+ MaskedValueIsZero (I->getOperand (0 ), Mask, Depth+ 1 );
422
425
case Instruction::Select:
423
426
// If the T and F values are MaskedValueIsZero, the result is also zero.
424
- return MaskedValueIsZero (I->getOperand (2 ), Mask) &&
425
- MaskedValueIsZero (I->getOperand (1 ), Mask);
427
+ return MaskedValueIsZero (I->getOperand (2 ), Mask, Depth+ 1 ) &&
428
+ MaskedValueIsZero (I->getOperand (1 ), Mask, Depth+ 1 );
426
429
case Instruction::Cast: {
427
430
const Type *SrcTy = I->getOperand (0 )->getType ();
428
431
if (SrcTy == Type::BoolTy)
@@ -440,7 +443,7 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
440
443
Constant *NewMask =
441
444
ConstantExpr::getCast (Mask, I->getOperand (0 )->getType ());
442
445
return MaskedValueIsZero (I->getOperand (0 ),
443
- cast<ConstantIntegral>(NewMask));
446
+ cast<ConstantIntegral>(NewMask), Depth+ 1 );
444
447
}
445
448
}
446
449
break ;
@@ -449,7 +452,8 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
449
452
// (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
450
453
if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand (1 )))
451
454
return MaskedValueIsZero (I->getOperand (0 ),
452
- cast<ConstantIntegral>(ConstantExpr::getUShr (Mask, SA)));
455
+ cast<ConstantIntegral>(ConstantExpr::getUShr (Mask, SA)),
456
+ Depth+1 );
453
457
break ;
454
458
case Instruction::Shr:
455
459
// (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
0 commit comments