[llvm] [InstCombine] Simplify nested selects with implied condition (PR #83739)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 3 12:09:52 PST 2024
================
@@ -3867,5 +3867,22 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
}
}
+ // Fold nested selects if the inner condition can be implied by the outer
+ // condition.
+ Value *InnerCondVal;
+ const DataLayout &DL = getDataLayout();
+ if (match(TrueVal,
+ m_Select(m_Value(InnerCondVal), m_Value(LHS), m_Value(RHS))) &&
+ CondVal->getType() == InnerCondVal->getType())
+ if (auto Implied =
+ isImpliedCondition(CondVal, InnerCondVal, DL, /*LHSIsTrue=*/true))
+ return replaceOperand(SI, 1, *Implied ? LHS : RHS);
+ if (match(FalseVal,
+ m_Select(m_Value(InnerCondVal), m_Value(LHS), m_Value(RHS))) &&
+ CondVal->getType() == InnerCondVal->getType())
+ if (auto Implied =
+ isImpliedCondition(CondVal, InnerCondVal, DL, /*LHSIsTrue=*/false))
+ return replaceOperand(SI, 2, *Implied ? LHS : RHS);
+
----------------
goldsteinn wrote:
Maybe put this in a lambda:
```
auto CanSimplfySelectArmAs = [...](bool Arm) {
if(match(Arm ? TrueVal : FalseVal, m_Select(...)) && CondVal->getType() == ...)
if(Implied = isImplied(..., Arm)
return *Implied ? LHS : RHS;
}
if(auto * V = CanSimplfySelectArmAs(true)) replaceOperand(SI, 1, V);
if(auto * V = CanSimplfySelectArmAs(false)) replaceOperand(SI, 2, V);
```
?
There is a lot of duplication between the two arms.
https://github.com/llvm/llvm-project/pull/83739
More information about the llvm-commits
mailing list