@@ -509,28 +509,38 @@ static bool simplifySeqSelectWithSameCond(SelectInst &SI,
509
509
if (isa<Constant>(CondVal))
510
510
return false ;
511
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
- }
512
+ auto TrysimplifySeqSelect = [=, &SI, &IC]( unsigned OpIndex) {
513
+ assert ((OpIndex == 1 || OpIndex == 2 ) && " Unexpected operand index " ) ;
514
+ Type *CondType = CondVal ->getType ();
515
+ Value *RepOp = OpIndex == 1 ? ConstantInt::getTrue (CondType)
516
+ : ConstantInt::getFalse (CondType) ;
517
+ SelectInst *SINext = &SI;
518
+ Type *SelType = SINext-> getType ();
519
+ Value *ValOp = SINext-> getOperand (OpIndex);
520
+ Value *CondNext;
521
+ while ( match (ValOp, m_Select ( m_Value ( CondNext), m_Value (), m_Value ()))) {
522
+ if (CondNext == CondVal && SelType->isIntOrIntVectorTy () &&
523
+ CondType-> isVectorTy () == SelType-> isVectorTy ())
524
+ if (Value *S = simplifyWithOpReplaced (ValOp, CondVal, RepOp , SQ,
525
+ /* AllowRefinement */ true )) {
526
+ IC.replaceOperand (*SINext, OpIndex , S);
527
+ return true ;
528
+ }
529
529
530
- SINext = cast<SelectInst>(FalseVal);
531
- SelType = SINext->getType ();
532
- FalseVal = SINext->getFalseValue ();
533
- }
530
+ SINext = cast<SelectInst>(ValOp);
531
+ SelType = SINext->getType ();
532
+ ValOp = SINext->getOperand (OpIndex);
533
+ }
534
+ return false ;
535
+ };
536
+
537
+ // Try to simplify the true value of select.
538
+ if (TrysimplifySeqSelect (1 ))
539
+ return true ;
540
+
541
+ // Try to simplify the false value of select.
542
+ if (TrysimplifySeqSelect (2 ))
543
+ return true ;
534
544
535
545
return false ;
536
546
}
0 commit comments