[PATCH] D49919: [InstCombine] Fold Select with AND/OR condition

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 27 11:25:30 PDT 2018


lebedev.ri added inline comments.


================
Comment at: lib/Analysis/InstructionSimplify.cpp:78-93
+static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
+                                     Value *FalseVal) {
+  Value *V1, *V2;
+  CmpInst::Predicate Pred1, Pred2;
+  if (match(Cond, m_c_Or(m_c_ICmp(Pred1, m_Deferred(TrueVal), m_Value(V1)),
+                         m_c_ICmp(Pred2, m_Deferred(TrueVal), m_Value(V2)))))
+    if (FalseVal == V1 || FalseVal == V2)
----------------
Nice.
Then i'd suggest something a bit more flatter:
```

static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
                                     Value *FalseVal) {
  BinaryOperator::BinaryOps BinOpCode;
  if (auto *BO = dyn_cast<BinaryOperator>(Cond))
    BinOpCode = BO->getOpcode();
  else
    return nullptr;

  CmpInst::Predicate ExpectedPred;
  if (BinOpCode == BinaryOperator::Or) {
    ExpectedPred = ICmpInst::ICMP_NE;
  } else if (BinOpCode == BinaryOperator::And) {
    ExpectedPred = ICmpInst::ICMP_EQ;
    std::swap(TrueVal, FalseVal);
  } else
    return nullptr;

  CmpInst::Predicate Pred1, Pred2;
  if (!match(
          Cond,
          m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal), m_Specific(FalseVal)),
                    m_c_ICmp(Pred2, m_Specific(TrueVal), m_Value()))) ||
      Pred1 != Pred2 || Pred1 != ExpectedPred)
    return nullptr;

  return TrueVal;
}

```


================
Comment at: test/Transforms/InstCombine/select-or-cmp.ll:56
+  %B = icmp eq i32 %y, %z
+  %C = and i1 %A, %B
+  %D = select i1 %C, i32 %z, i32 %x
----------------
Please split this into two different test case files, i.e. the tests for `and` variant should go into the new file.


https://reviews.llvm.org/D49919





More information about the llvm-commits mailing list