https://gcc.gnu.org/g:61d56c487bfad088de0265796d2f7ae3da619240

commit r17-5592-g61d56c487bfad088de0265796d2f7ae3da619240
Author: Andrea Pinski <[email protected]>
Date:   Sun Oct 25 10:58:25 2026 -0700

    gimple: Fix factor_operation_ok's type matching for functions [PR128143]
    
    The problem here is after r17-3822-g44c89620f73, the code to make
    sure the types match when factoring out operations, was moved after
    the check for some functions which could return true early.
    Moving the types matching code fixes the problem by not returning early.
    Also change the function args check not to return early so that
    the problem like this does not happen in the future if more code is added
    afterwards.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/128143
    
    gcc/ChangeLog:
    
            * gimple-match-exports.cc (factor_operation_ok): Move the
            type matching code before the function check. Also don't
            return early for some builtin functions.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/sub-overflow-1.c: New test.
    
    Signed-off-by: Andrea Pinski <[email protected]>

Diff:
---
 gcc/gimple-match-exports.cc                   |  9 +++++----
 gcc/testsuite/gcc.dg/torture/sub-overflow-1.c | 13 +++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc
index 8077bbad165a..8f2de738b2f4 100644
--- a/gcc/gimple-match-exports.cc
+++ b/gcc/gimple-match-exports.cc
@@ -1645,10 +1645,6 @@ factor_operation_ok (code_helper code, int opnum,
       && code == VEC_PERM_EXPR && opnum == 2)
     return false;
 
-  /*  Some builtins should not be factored out with constants being involved.  
*/
-  if (!all_ssa_names && code.is_fn_code ())
-    return !fn_arg_must_be_const_p (combined_fn (code), opnum);
-
   // The types are need to be compatible.
   if (opnum >= 0)
     {
@@ -1657,5 +1653,10 @@ factor_operation_ok (code_helper code, int opnum,
          return false;
     }
 
+  // Some builtins should not be factored out with constants being involved.
+  if (!all_ssa_names && code.is_fn_code ()
+      && fn_arg_must_be_const_p (combined_fn (code), opnum))
+    return false;
+
   return true;
 }
diff --git a/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c 
b/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c
new file mode 100644
index 000000000000..841cc7f8e927
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/sub-overflow-1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* PR tree-optimization/128143 */
+
+int a(int c, signed char x, long long y)
+{
+  int r;
+  if (c)
+    r = __builtin_sub_overflow_p(y, x, (short) 0);
+  else
+    r = __builtin_sub_overflow_p(y, -40000, (short) 0);
+  return r;
+}
+

Reply via email to