Skip to content

Commit 37d10c3

Browse files
committed
early out if cannot be affected
1 parent 37c6e63 commit 37d10c3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,6 +3519,33 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
35193519
return false;
35203520
}
35213521

3522+
/// Check whether the KnownBits of a select arm may be affected by the
3523+
/// select condition.
3524+
static bool hasAffectedValue(Value *V, SmallPtrSetImpl<Value *> &Affected,
3525+
unsigned Depth) {
3526+
if (Depth == MaxAnalysisRecursionDepth)
3527+
return false;
3528+
3529+
// Ignore the case where the select arm itself is affected. These cases
3530+
// are handled more efficiently by other optimizations.
3531+
if (Affected.contains(V) && Depth != 0)
3532+
return true;
3533+
3534+
if (auto *I = dyn_cast<Instruction>(V)) {
3535+
if (isa<PHINode>(I)) {
3536+
if (Depth == MaxAnalysisRecursionDepth - 1)
3537+
return false;
3538+
Depth = MaxAnalysisRecursionDepth - 2;
3539+
}
3540+
return any_of(I->operands(), [&](Value *Op) {
3541+
return Op->getType()->isIntOrIntVectorTy() &&
3542+
hasAffectedValue(Op, Affected, Depth + 1);
3543+
});
3544+
}
3545+
3546+
return false;
3547+
}
3548+
35223549
Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
35233550
Value *CondVal = SI.getCondition();
35243551
Value *TrueVal = SI.getTrueValue();
@@ -4024,15 +4051,17 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
40244051
});
40254052
SimplifyQuery Q = SQ.getWithInstruction(&SI).getWithCondContext(CC);
40264053
if (!CC.AffectedValues.empty()) {
4027-
if (!isa<Constant>(TrueVal)) {
4054+
if (!isa<Constant>(TrueVal) &&
4055+
hasAffectedValue(TrueVal, CC.AffectedValues, /*Depth=*/0)) {
40284056
KnownBits Known = llvm::computeKnownBits(TrueVal, /*Depth=*/0, Q);
40294057
if (Known.isConstant())
40304058
return replaceOperand(SI, 1,
40314059
ConstantInt::get(SelType, Known.getConstant()));
40324060
}
40334061

40344062
CC.Invert = true;
4035-
if (!isa<Constant>(FalseVal)) {
4063+
if (!isa<Constant>(FalseVal) &&
4064+
hasAffectedValue(FalseVal, CC.AffectedValues, /*Depth=*/0)) {
40364065
KnownBits Known = llvm::computeKnownBits(FalseVal, /*Depth=*/0, Q);
40374066
if (Known.isConstant())
40384067
return replaceOperand(SI, 2,

0 commit comments

Comments
 (0)