[llvm] [InstCombine] Implement folds of icmp of UCMP/SCMP call and a constant (PR #96118)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 23:26:32 PDT 2024


================
@@ -3926,6 +3926,60 @@ foldICmpUSubSatOrUAddSatWithConstant(ICmpInst::Predicate Pred,
       ConstantInt::get(Op1->getType(), EquivInt));
 }
 
+static Instruction *
+foldICmpOfCmpIntrinsicWithConstant(ICmpInst::Predicate Pred, IntrinsicInst *I,
+                                   const APInt &C,
+                                   InstCombiner::BuilderTy &Builder) {
+  bool IsScmp = I->getIntrinsicID() == Intrinsic::scmp;
+  Value *LHS = I->getOperand(0);
+  Value *RHS = I->getOperand(1);
+
+  switch (Pred) {
+  case ICmpInst::ICMP_EQ:
+  case ICmpInst::ICMP_NE:
+    if (C.isZero())
+      return new ICmpInst(Pred, LHS, RHS);
+    if (C.isOne()) {
+      if (Pred == ICmpInst::ICMP_EQ)
+        return new ICmpInst(IsScmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT,
+                            LHS, RHS);
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE, LHS,
+                          RHS);
+    }
+    if (C.isAllOnes()) {
+      if (Pred == ICmpInst::ICMP_EQ)
+        return new ICmpInst(IsScmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
+                            LHS, RHS);
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, LHS,
+                          RHS);
+    }
+    break;
+
+  case ICmpInst::ICMP_SGT:
+    if (C.isAllOnes())
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, LHS,
+                          RHS);
+    if (C.isZero())
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, LHS,
+                          RHS);
+    break;
+
+  case ICmpInst::ICMP_SLT:
+    if (C.isZero())
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, LHS,
+                          RHS);
+    if (C.isOne())
+      return new ICmpInst(IsScmp ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE, LHS,
+                          RHS);
+    break;
+
----------------
goldsteinn wrote:

To further reduce noise, you just set a `std::optional<ICmpInst::Predicate>`, if its set return `new ICmpInst(*NewPred, LHS, RHS)`, otherwise null.

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


More information about the llvm-commits mailing list