[flang-commits] [flang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

Nikita Popov via flang-commits flang-commits at lists.llvm.org
Sun Sep 24 06:41:45 PDT 2023


================
@@ -6380,7 +6380,69 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
       Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
     return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  Instruction *ExtI;
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+                         m_CombineAnd(m_Instruction(ExtI),
+                                      m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),
+                                                          m_APInt(C))))))) {
+    bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
+    bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
+    auto CreateRangeCheck = [&] {
+      Value *V1 = Constant::getNullValue(X->getType());
+      Value *V2 = ConstantInt::get(X->getType(), IsSExt ? -1 : 1);
+      return BinaryOperator::Create(
+          Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
+          Builder.CreateICmp(Pred1, X, V1), Builder.CreateICmp(Pred1, X, V2));
----------------
nikic wrote:

You need to move one of the inner calls out, otherwise you will get compiler-dependent IR.

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


More information about the flang-commits mailing list