[llvm] [InstCombine] Fold its select user into select (PR #83405)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 15 19:21:51 PDT 2024


================
@@ -500,6 +500,51 @@ 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 (isa<Constant>(CondVal))
+    return false;
+
+  Type *CondType = CondVal->getType();
+  SelectInst *SINext = &SI;
+  Value *FalseVal = SINext->getFalseValue();
+  Value *CondNext;
+  while (match(FalseVal, m_Select(m_Value(CondNext), m_Value(), m_Value()))) {
+    if (CondNext == CondVal)
+      if (Value *S = simplifyWithOpReplaced(FalseVal, CondVal,
+                                            ConstantInt::getFalse(CondType), SQ,
+                                            /* AllowRefinement */ true)) {
+        IC.replaceOperand(*SINext, 2, S);
+        return true;
+      }
+
+    SINext = cast<SelectInst>(FalseVal);
+    FalseVal = SINext->getFalseValue();
+  }
+
+  // Resuming from SI
+  SINext = &SI;
+  Value *TrueVal = SINext->getTrueValue();
+  while (match(TrueVal, m_Select(m_Value(CondNext), m_Value(), m_Value()))) {
+    if (CondNext == CondVal)
+      if (Value *S = simplifyWithOpReplaced(TrueVal, CondVal,
+                                            ConstantInt::getTrue(CondType), SQ,
+                                            /* AllowRefinement */ true)) {
+        IC.replaceOperand(*SINext, 1, S);
+        return true;
+      }
+
+    SINext = cast<SelectInst>(TrueVal);
+    TrueVal = SINext->getTrueValue();
+  }
----------------
vfdff wrote:

Apply your comment, thanks @goldsteinn 

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


More information about the llvm-commits mailing list