Skip to content

Commit da2d545

Browse files
committed
[InstCombine] Improve select equiv fold for true condition value
Refactor with a lambda function to address both false and true value.
1 parent a4e61fa commit da2d545

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -511,26 +511,36 @@ static bool simplifySeqSelectWithSameCond(SelectInst &SI,
511511
const SimplifyQuery &SQ,
512512
InstCombinerImpl &IC) {
513513
Value *CondVal = SI.getCondition();
514-
Type *CondType = CondVal->getType();
515-
SelectInst *SINext = &SI;
516-
Type *SelType = SINext->getType();
517-
Value *FalseVal = SINext->getFalseValue();
518-
Value *CondNext;
519-
Value *FalseNext;
520-
while (match(FalseVal,
521-
m_Select(m_Value(CondNext), m_Value(), m_Value(FalseNext)))) {
514+
auto trySimplifySeqSelect = [=, &SI, &IC](unsigned OpIndex) {
515+
assert((OpIndex == 1 || OpIndex == 2) && "Unexpected operand index");
516+
SelectInst *SINext = &SI;
517+
Type *SelType = SINext->getType();
518+
Value *ValOp = SINext->getOperand(OpIndex);
519+
Value *CondNext;
522520
// Only support the type of select is an integer type as float type need
523521
// address FMF flag.
524-
if (CondNext == CondVal && SelType->isIntOrIntVectorTy() &&
525-
SINext->hasOneUse()) {
526-
IC.replaceOperand(*SINext, 2, FalseNext);
527-
return true;
522+
while (match(ValOp, m_Select(m_Value(CondNext), m_Value(), m_Value()))) {
523+
if (CondNext == CondVal && SelType->isIntOrIntVectorTy() &&
524+
SINext->hasOneUse()) {
525+
IC.replaceOperand(*SINext, OpIndex,
526+
cast<SelectInst>(ValOp)->getOperand(OpIndex));
527+
return true;
528+
}
529+
530+
SINext = cast<SelectInst>(ValOp);
531+
SelType = SINext->getType();
532+
ValOp = SINext->getOperand(OpIndex);
528533
}
534+
return false;
535+
};
529536

530-
SINext = cast<SelectInst>(FalseVal);
531-
SelType = SINext->getType();
532-
FalseVal = SINext->getFalseValue();
533-
}
537+
// Try to simplify the true value of select.
538+
if (trySimplifySeqSelect(/*OpIndex=*/1))
539+
return true;
540+
541+
// Try to simplify the false value of select.
542+
if (trySimplifySeqSelect(/*OpIndex=*/2))
543+
return true;
534544

535545
return false;
536546
}

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,9 +4539,8 @@ define i32 @sequence_select_with_same_cond_false(i1 %c1, i1 %c2){
45394539

45404540
define i32 @sequence_select_with_same_cond_true(i1 %c1, i1 %c2){
45414541
; CHECK-LABEL: @sequence_select_with_same_cond_true(
4542-
; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i32 45, i32 23
4543-
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 [[S1]], i32 666
4544-
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1]], i32 [[S2]], i32 789
4542+
; CHECK-NEXT: [[S2:%.*]] = select i1 [[C2:%.*]], i32 45, i32 666
4543+
; CHECK-NEXT: [[S3:%.*]] = select i1 [[C1:%.*]], i32 [[S2]], i32 789
45454544
; CHECK-NEXT: ret i32 [[S3]]
45464545
;
45474546
%s1 = select i1 %c1, i32 45, i32 23
@@ -4639,10 +4638,12 @@ define i8 @test_replace_freeze_oneuse(i1 %x, i8 %y) {
46394638
ret i8 %sel
46404639
}
46414640

4641+
; first, %sel2 change into select i1 %cond1, i8 %sel0, i8 3, the the %sel1 is OneUse
4642+
; second, %sel1 change into select i1 %cond1, i8 %a, i8 2
46424643
define i8 @sequence_select_with_same_cond_multi_arms(i1 %cond0, i1 %cond1, i8 %a, i8 %b) {
46434644
; CHECK-LABEL: @sequence_select_with_same_cond_multi_arms(
46444645
; CHECK-NEXT: [[SEL0:%.*]] = select i1 [[COND0:%.*]], i8 [[A:%.*]], i8 [[B:%.*]]
4645-
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[COND1:%.*]], i8 [[SEL0]], i8 2
4646+
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[COND1:%.*]], i8 [[A]], i8 2
46464647
; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[COND1]], i8 [[SEL0]], i8 3
46474648
; CHECK-NEXT: [[SEL3:%.*]] = select i1 [[COND0]], i8 [[SEL1]], i8 [[SEL2]]
46484649
; CHECK-NEXT: ret i8 [[SEL3]]

0 commit comments

Comments
 (0)