@@ -500,6 +500,41 @@ static bool isSelect01(const APInt &C1I, const APInt &C2I) {
500
500
return C1I.isOne () || C1I.isAllOnes () || C2I.isOne () || C2I.isAllOnes ();
501
501
}
502
502
503
+ // / Try to simplify a select instruction when the user of its select user
504
+ // / indicates the condition.
505
+ static bool simplifySeqSelectWithSameCond (SelectInst &SI,
506
+ const SimplifyQuery &SQ,
507
+ InstCombinerImpl &IC) {
508
+ Value *CondVal = SI.getCondition ();
509
+ if (isa<Constant>(CondVal))
510
+ return false ;
511
+
512
+ Type *CondType = CondVal->getType ();
513
+ SelectInst *SINext = &SI;
514
+ Type *SelType = SINext->getType ();
515
+ Value *FalseVal = SINext->getFalseValue ();
516
+ Value *CondNext;
517
+ while (match (FalseVal, m_Select (m_Value (CondNext), m_Value (), m_Value ()))) {
518
+ // If the type of select is not an integer type or if the condition and
519
+ // the selection type are not both scalar nor both vector types, there is no
520
+ // point in attempting to match these patterns.
521
+ if (CondNext == CondVal && SelType->isIntOrIntVectorTy () &&
522
+ CondType->isVectorTy () == SelType->isVectorTy ())
523
+ if (Value *S = simplifyWithOpReplaced (FalseVal, CondVal,
524
+ ConstantInt::getFalse (CondType), SQ,
525
+ /* AllowRefinement */ true )) {
526
+ IC.replaceOperand (*SINext, 2 , S);
527
+ return true ;
528
+ }
529
+
530
+ SINext = cast<SelectInst>(FalseVal);
531
+ SelType = SINext->getType ();
532
+ FalseVal = SINext->getFalseValue ();
533
+ }
534
+
535
+ return false ;
536
+ }
537
+
503
538
// / Try to fold the select into one of the operands to allow further
504
539
// / optimization.
505
540
Instruction *InstCombinerImpl::foldSelectIntoOp (SelectInst &SI, Value *TrueVal,
@@ -557,6 +592,9 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
557
592
if (Instruction *R = TryFoldSelectIntoOp (SI, FalseVal, TrueVal, true ))
558
593
return R;
559
594
595
+ if (simplifySeqSelectWithSameCond (SI, SQ, *this ))
596
+ return &SI;
597
+
560
598
return nullptr ;
561
599
}
562
600
0 commit comments