Skip to content

Commit 248a354

Browse files
committed
[InstCombine] Fold its select user into select for true value
Refactor with a lambda function to address both false and true value.
1 parent c67620b commit 248a354

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -509,28 +509,38 @@ static bool simplifySeqSelectWithSameCond(SelectInst &SI,
509509
if (isa<Constant>(CondVal))
510510
return false;
511511

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+
}
529529

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;
534544

535545
return false;
536546
}

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,9 +3724,8 @@ define i32 @sequence_select_with_same_cond_flase (i1 %c1, i1 %c2){
37243724
; https://alive2.llvm.org/ce/z/HHF-VD
37253725
define i32 @sequence_select_with_same_cond_true (i1 %c1, i1 %c2){
37263726
; CHECK-LABEL: @sequence_select_with_same_cond_true(
3727-
; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 45, i32 23
3728-
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 [[S1]], i32 666
3729-
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 [[S2]], i32 789
3727+
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 45, i32 666
3728+
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1:%.*]], i32 [[S2]], i32 789
37303729
; CHECK-NEXT: ret i32 [[S3]]
37313730
;
37323731
%s1 = select i1 %c1, i32 45, i32 23

0 commit comments

Comments
 (0)