[gcc r15-11409] ipa-cp: Fix ipa-vr intersection in wrong type (PR124128)

Martin Jambor via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:7b1b3c5de2a78ba43e61b24d6ffac47fde2c8fbf

commit r15-11409-g7b1b3c5de2a78ba43e61b24d6ffac47fde2c8fbf
Author: Martin Jambor <[email protected]>
Date:   Thu Jun 18 14:55:33 2026 +0200

    ipa-cp: Fix ipa-vr intersection in wrong type (PR124128)
    
    Function ipa_vr_intersect_with_arith_jfunc in ipa-cp.cc contains a
    check
    
      if (src_type == dst_type)
    
    which should have been
    
      if (operation_type == dst_type)
    
    which lead to an ICE in the ranger machinery as it tried to intersect
    a signed and an unsigned integer when compiling the testcases from
    PR124128.
    
    The condition itself is basically an early exit to avoid calling yet
    another ipa_vr_operation_and_type_effects to type-convert the value
    range when we already can simply compare the types and see we already
    have the righ thing.  A better place for it is however just before
    that conversion, where it can also avoid the call when dealing with
    the most simple of pass-through jump functions (and where it generally
    "makes more sense") so this is what the patch does.
    
    gcc/ChangeLog:
    
    2026-06-04  Martin Jambor  <[email protected]>
    
            PR ipa/124128
            * ipa-cp.cc (ipa_vr_intersect_with_arith_jfunc): Move the check if
            the final type conversion needs to happen before the conversion.
    
    gcc/testsuite/ChangeLog:
    
    2026-06-04  Martin Jambor  <[email protected]>
    
            PR ipa/124128
            * gcc.dg/ipa/pr124128.c: New test.
    
    (cherry picked from commit 281b8d054f6b98386f3ecd8a1e82a3374dd9d674)

Diff:
---
 gcc/ipa-cp.cc                       | 19 ++++++++++---------
 gcc/testsuite/gcc.dg/ipa/pr124128.c | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index 4b8d23775afd..e9120473f13c 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -1749,21 +1749,22 @@ ipa_vr_intersect_with_arith_jfunc (vrange &vr,
 	  if (!ipa_vr_operation_and_type_effects (op_res, src_vr, operation,
 						  operation_type, src_type))
 	    return;
-	  if (src_type == dst_type)
-	    {
-	      vr.intersect (op_res);
-	      return;
-	    }
 	  inter_vr = &op_res;
 	  src_type = operation_type;
 	}
       else
 	inter_vr = &src_vr;
 
-      value_range tmp_res (dst_type);
-      if (ipa_vr_operation_and_type_effects (tmp_res, *inter_vr, NOP_EXPR,
-					     dst_type, src_type))
-	vr.intersect (tmp_res);
+      if (src_type != dst_type)
+	{
+	  value_range tmp_res (dst_type);
+	  if (!ipa_vr_operation_and_type_effects (tmp_res, *inter_vr, NOP_EXPR,
+						  dst_type, src_type))
+	    return;
+	  vr.intersect (tmp_res);
+	}
+      else
+	vr.intersect (*inter_vr);
       return;
     }
 
diff --git a/gcc/testsuite/gcc.dg/ipa/pr124128.c b/gcc/testsuite/gcc.dg/ipa/pr124128.c
new file mode 100644
index 000000000000..e41f0d56bfd3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/pr124128.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-early-inlining" } */
+
+typedef int blasint;
+void blas_level1_thread(long);
+void cblas_sscal(blasint, float, float, blasint incx) {
+  blas_level1_thread(incx);
+}
+blasint c_api_check_saxpby_n;
+float c_api_check_saxpby_beta;
+long labs(long);
+static void c_api_check_saxpby(blasint incy) {
+  blasint incy_abs = labs(incy);
+  cblas_sscal(c_api_check_saxpby_n, c_api_check_saxpby_beta, 0, incy_abs);
+}
+void __ctest_saxpby_c_api_inc_x_2_inc_y_1_N_100_run() {
+  c_api_check_saxpby(1);
+  c_api_check_saxpby(2);
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.