Skip to content

Commit 09efd4e

Browse files
committed
Limit the search depth of MaskedValueIsZero to 6 instructions, to avoid
bad cases. This fixes Markus's second testcase in PR639, and should seal it for good. llvm-svn: 24123
1 parent 0433df1 commit 09efd4e

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ static ConstantInt *SubOne(ConstantInt *C) {
389389
/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
390390
/// this predicate to simplify operations downstream. V and Mask are known to
391391
/// be the same type.
392-
static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
392+
static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask,
393+
unsigned Depth = 0) {
393394
// Note, we cannot consider 'undef' to be "IsZero" here. The problem is that
394395
// we cannot optimize based on the assumption that it is zero without changing
395396
// 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) {
400401
return true;
401402
if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
402403
return ConstantExpr::getAnd(CI, Mask)->isNullValue();
404+
405+
if (Depth == 6) return false; // Limit search depth.
403406

404407
if (Instruction *I = dyn_cast<Instruction>(V)) {
405408
switch (I->getOpcode()) {
@@ -408,21 +411,21 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
408411
if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1))) {
409412
ConstantIntegral *C1C2 =
410413
cast<ConstantIntegral>(ConstantExpr::getAnd(CI, Mask));
411-
if (MaskedValueIsZero(I->getOperand(0), C1C2))
414+
if (MaskedValueIsZero(I->getOperand(0), C1C2, Depth+1))
412415
return true;
413416
}
414417
// 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);
417420
case Instruction::Or:
418421
case Instruction::Xor:
419422
// 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);
422425
case Instruction::Select:
423426
// 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);
426429
case Instruction::Cast: {
427430
const Type *SrcTy = I->getOperand(0)->getType();
428431
if (SrcTy == Type::BoolTy)
@@ -440,7 +443,7 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
440443
Constant *NewMask =
441444
ConstantExpr::getCast(Mask, I->getOperand(0)->getType());
442445
return MaskedValueIsZero(I->getOperand(0),
443-
cast<ConstantIntegral>(NewMask));
446+
cast<ConstantIntegral>(NewMask), Depth+1);
444447
}
445448
}
446449
break;
@@ -449,7 +452,8 @@ static bool MaskedValueIsZero(Value *V, ConstantIntegral *Mask) {
449452
// (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
450453
if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1)))
451454
return MaskedValueIsZero(I->getOperand(0),
452-
cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA)));
455+
cast<ConstantIntegral>(ConstantExpr::getUShr(Mask, SA)),
456+
Depth+1);
453457
break;
454458
case Instruction::Shr:
455459
// (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0

0 commit comments

Comments
 (0)