Skip to content

Commit c67620b

Browse files
committed
[InstCombine] Fold its select user into select
Fold select if the user of its select user indicates the condition Fix #83225
1 parent acf0d16 commit c67620b

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,41 @@ static bool isSelect01(const APInt &C1I, const APInt &C2I) {
500500
return C1I.isOne() || C1I.isAllOnes() || C2I.isOne() || C2I.isAllOnes();
501501
}
502502

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+
503538
/// Try to fold the select into one of the operands to allow further
504539
/// optimization.
505540
Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
@@ -557,6 +592,9 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
557592
if (Instruction *R = TryFoldSelectIntoOp(SI, FalseVal, TrueVal, true))
558593
return R;
559594

595+
if (simplifySeqSelectWithSameCond(SI, SQ, *this))
596+
return &SI;
597+
560598
return nullptr;
561599
}
562600

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,9 +3711,8 @@ define i32 @src_select_xxory_eq0_xorxy_y(i32 %x, i32 %y) {
37113711

37123712
define i32 @sequence_select_with_same_cond_flase (i1 %c1, i1 %c2){
37133713
; CHECK-LABEL: @sequence_select_with_same_cond_flase(
3714-
; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 23, i32 45
3715-
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 [[S1]]
3716-
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 789, i32 [[S2]]
3714+
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 45
3715+
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1:%.*]], i32 789, i32 [[S2]]
37173716
; CHECK-NEXT: ret i32 [[S3]]
37183717
;
37193718
%s1 = select i1 %c1, i32 23, i32 45
@@ -3742,7 +3741,7 @@ define i32 @sequence_select_with_same_cond_extra_use (i1 %c1, i1 %c2){
37423741
; CHECK-LABEL: @sequence_select_with_same_cond_extra_use(
37433742
; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 23, i32 45
37443743
; CHECK-NEXT: call void @use32(i32 [[S1]])
3745-
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 [[S1]]
3744+
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 666, i32 45
37463745
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 789, i32 [[S2]]
37473746
; CHECK-NEXT: ret i32 [[S3]]
37483747
;

0 commit comments

Comments
 (0)