[llvm] [InstCombine] Fold icmp on frexp exponent to fcmp on fabs(x) (PR #210711)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 21 03:20:25 PDT 2026


================
@@ -3606,6 +3606,51 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstant(ICmpInst &Cmp) {
       if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, II, *C))
         return I;
 
+    {
+      // icmp slt/sgt (extractvalue (frexp X), 1), C -->
+      //                         fcmp olt/oge (fabs X), 2^ExpVal
+      // slt -> olt, ExpVal = C-1; sgt -> oge, ExpVal = C.
+      Value *X;
+      if (match(Cmp.getOperand(0),
+                m_OneUse(m_ExtractValue<1>(
+                    m_OneUse(m_Intrinsic<Intrinsic::frexp>(m_Value(X)))))) &&
+          isKnownNeverInfOrNaN(X, SQ.getWithInstruction(&Cmp))) {
+        ICmpInst::Predicate Pred = Cmp.getPredicate();
+        APInt Exp;
+        FCmpInst::Predicate NewPred;
+        bool ValidPred = true;
+
+        switch (Pred) {
+        case ICmpInst::ICMP_SLT:
+          NewPred = FCmpInst::FCMP_OLT;
+          Exp = *C - 1;
+          break;
+        case ICmpInst::ICMP_SGT:
+          NewPred = FCmpInst::FCMP_OGE;
+          Exp = *C;
+          break;
+        default:
+          ValidPred = false;
+          break;
+        }
+
+        if (ValidPred) {
+          const fltSemantics &Sem =
+              X->getType()->getScalarType()->getFltSemantics();
+          int MaxExp = APFloat::semanticsMaxExponent(Sem);
+
+          if (!Exp.isNegative() && Exp.sle(MaxExp + 1)) {
+            int ExpVal = static_cast<int>(Exp.getSExtValue());
----------------
arsenm wrote:

Exp may be > 64-bits and this will assert. please add a test using i128 as the exponent type 

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


More information about the llvm-commits mailing list