[llvm] [InstCombine] Improve select equiv fold for plain condition (PR #83405)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 7 02:50:10 PDT 2024


================
@@ -500,6 +500,44 @@ static bool isSelect01(const APInt &C1I, const APInt &C2I) {
   return C1I.isOne() || C1I.isAllOnes() || C2I.isOne() || C2I.isAllOnes();
 }
 
+/// Try to simplify a select instruction when the user of its select user
+/// indicates the condition.
+static bool simplifySeqSelectWithSameCond(SelectInst &SI,
+                                          const SimplifyQuery &SQ,
+                                          InstCombinerImpl &IC) {
+  Value *CondVal = SI.getCondition();
+  if (match(CondVal, m_ImmConstant()))
+    return false;
+
+  auto trySimplifySeqSelect = [=, &SI, &IC](unsigned OpIndex) {
+    assert((OpIndex == 1 || OpIndex == 2) && "Unexpected operand index");
+    SelectInst *SINext = &SI;
+    Value *ValOp = SINext->getOperand(OpIndex);
+    Value *CondNext;
+    while (match(ValOp, m_Select(m_Value(CondNext), m_Value(), m_Value()))) {
+      if (CondNext == CondVal) {
+        IC.replaceOperand(*SINext, OpIndex,
----------------
vfdff wrote:

hi @dtcxzyw , the above case can be addressed correctly https://alive2.llvm.org/ce/z/HyqtF4, and it will replace the **%sel2** because we find the first 3 selects  match the combine pattern
 - note:  here it doesn't update the %sel1, but updates the %sel2
```
define i8 @tgt(i1 %cond0, i1 %cond1, i8 %a, i8 %b) {
entry:
%sel0 = select i1 %cond0, i8 %a, i8 %b
%sel1 = select i1 %cond1, i8 %sel0, i8 %b
%sel2 = select i1 %cond1, i8 %sel0, i8 %b  ;   %sel1 --> %sel0
%sel3 = select i1 %cond0, i8 %sel1, i8 %sel2
ret i8 %sel3
}
```



https://github.com/llvm/llvm-project/pull/83405


More information about the llvm-commits mailing list