]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fab/forwprop: Move memcmp->memcmp_eq to forwprop
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sat, 20 Sep 2025 06:02:35 +0000 (23:02 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 3 Oct 2025 14:01:02 +0000 (07:01 -0700)
This moves from the memcmp->memcmp_eq to forwprop by
checking PROP_last_full_fold.

Note this means at -Og, we no longer optimize some
memcmp. That will be fixed when I exchange fab/copyprop
for a (late) forwprop.

Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

* tree-ssa-ccp.cc (optimize_memcmp_eq): Remove.
(pass_fold_builtins::execute): Remove handling of memcmp.
* tree-ssa-forwprop.cc (simplify_builtin_memcmp): Add folding
of memcmp to memcmp_eq for PROP_last_full_fold.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/tree-ssa-ccp.cc
gcc/tree-ssa-forwprop.cc

index 4165e596033f96d22622c1edf6e3b6078e304d18..1fbc3de2bfe74ab61d71f1751297bdfa9766359b 100644 (file)
@@ -4205,36 +4205,6 @@ public:
 
 }; // class pass_fold_builtins
 
-/* Optimize memcmp STMT into memcmp_eq if it is only used with
-   `== 0` or `!= 0`. */
-
-static void
-optimize_memcmp_eq (gcall *stmt)
-{
-  /* Make sure memcmp arguments are the correct type.  */
-  if (gimple_call_num_args (stmt) != 3)
-    return;
-  tree arg1 = gimple_call_arg (stmt, 0);
-  tree arg2 = gimple_call_arg (stmt, 1);
-  tree len = gimple_call_arg (stmt, 2);
-
-  if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
-    return;
-  if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
-    return;
-  if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
-    return;
-  /* The return value of the memcmp has to be used
-     equality comparison to zero. */
-  tree res = gimple_call_lhs (stmt);
-
-  if (!res || !use_in_zero_equality (res))
-    return;
-
-  gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
-  update_stmt (stmt);
-}
-
 unsigned int
 pass_fold_builtins::execute (function *fun)
 {
@@ -4291,10 +4261,6 @@ pass_fold_builtins::execute (function *fun)
                  gsi_next (&i);
                  continue;
 
-               case BUILT_IN_MEMCMP:
-                 optimize_memcmp_eq (as_a<gcall*>(stmt));
-                 break;
-
                case BUILT_IN_UNREACHABLE:
                  if (optimize_unreachable (i))
                    cfg_changed = true;
index 4c438a0f86c9ff603245447f7b946ac906720740..917f3d90f6c26381e22d478100158a38b5968c16 100644 (file)
@@ -1842,7 +1842,15 @@ simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
          return true;
        }
     }
-  return false;
+
+  /* Replace memcmp with memcmp_eq if the above fails. */
+  if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
+    return false;
+  if (!(cfun->curr_properties & (PROP_last_full_fold)))
+    return false;
+  gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
+  update_stmt (stmt);
+  return true;
 }
 
 /* Optimizes builtin memchrs for small constant sizes with a const string.