@@ -3519,6 +3519,33 @@ static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
3519
3519
return false ;
3520
3520
}
3521
3521
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
+
3522
3549
Instruction *InstCombinerImpl::visitSelectInst (SelectInst &SI) {
3523
3550
Value *CondVal = SI.getCondition ();
3524
3551
Value *TrueVal = SI.getTrueValue ();
@@ -4024,15 +4051,17 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
4024
4051
});
4025
4052
SimplifyQuery Q = SQ.getWithInstruction (&SI).getWithCondContext (CC);
4026
4053
if (!CC.AffectedValues .empty ()) {
4027
- if (!isa<Constant>(TrueVal)) {
4054
+ if (!isa<Constant>(TrueVal) &&
4055
+ hasAffectedValue (TrueVal, CC.AffectedValues , /* Depth=*/ 0 )) {
4028
4056
KnownBits Known = llvm::computeKnownBits (TrueVal, /* Depth=*/ 0 , Q);
4029
4057
if (Known.isConstant ())
4030
4058
return replaceOperand (SI, 1 ,
4031
4059
ConstantInt::get (SelType, Known.getConstant ()));
4032
4060
}
4033
4061
4034
4062
CC.Invert = true ;
4035
- if (!isa<Constant>(FalseVal)) {
4063
+ if (!isa<Constant>(FalseVal) &&
4064
+ hasAffectedValue (FalseVal, CC.AffectedValues , /* Depth=*/ 0 )) {
4036
4065
KnownBits Known = llvm::computeKnownBits (FalseVal, /* Depth=*/ 0 , Q);
4037
4066
if (Known.isConstant ())
4038
4067
return replaceOperand (SI, 2 ,
0 commit comments