public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8141] tree.cc: Use useless_type_conversion_p in tree_builtin_call_types_compatible_p while in gimple form
@ 2022-04-13 13:45 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-04-13 13:45 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:29c46490de4616b911fccb34a9479f768fb51e94

commit r12-8141-g29c46490de4616b911fccb34a9479f768fb51e94
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Apr 13 15:44:51 2022 +0200

    tree.cc: Use useless_type_conversion_p in tree_builtin_call_types_compatible_p while in gimple form [PR105253]
    
    tree_builtin_call_types_compatible_p uses TYPE_MAIN_VARIANT comparisons
    or tree_nop_conversion_p to ensure a builtin has correct GENERIC arguments.
    Unfortunately this regressed when get_call_combined_fn is called during
    GIMPLE optimizations.  E.g. when number_of_iterations_popcount is called,
    it doesn't ensure TYPE_MAIN_VARIABLE compatible argument type, it picks
    __builtin_popcount{,l,ll} based just on types' precision and doesn't
    fold_convert the arg to the right type.  We are in GIMPLE, such conversions
    are useless...
    So, either we'd need to fix number_of_iterations_popcount to add casts
    and inspect anything else that creates CALL_EXPRs late, or we can
    in tree_builtin_call_types_compatible_p just use the GIMPLE type
    comparisons (useless_type_conversion_p) when we are in GIMPLE form and
    the TYPE_MAIN_VARIANT comparison or tree_nop_conversion_p test otherwise.
    
    I think especially this late in stage4 the latter seems safer to me.
    
    2022-04-13  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/105253
            * tree.cc (tree_builtin_call_types_compatible_p): If PROP_gimple,
            use useless_type_conversion_p checks instead of TYPE_MAIN_VARIANT
            comparisons or tree_nop_conversion_p checks.
    
            * gcc.target/i386/pr105253.c: New test.

Diff:
---
 gcc/testsuite/gcc.target/i386/pr105253.c | 17 +++++++++++++++++
 gcc/tree.cc                              | 22 ++++++++++++++++------
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/gcc/testsuite/gcc.target/i386/pr105253.c b/gcc/testsuite/gcc.target/i386/pr105253.c
new file mode 100644
index 00000000000..79444e5b28b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr105253.c
@@ -0,0 +1,17 @@
+/* PR middle-end/105253 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mno-popcnt" } */
+/* { dg-final { scan-assembler-not "__popcount\[sd]i2" } } */
+
+int
+foo (unsigned long long *p)
+{
+  int i, cnt = 0;
+  unsigned long long elem;
+  for (i = 0; i < 4; i++)
+    {
+      for (elem = p[i]; elem; cnt++)
+	elem &= elem - 1;
+    }
+  return cnt;
+}
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 4f997578bac..3c39be4f501 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -8427,8 +8427,12 @@ tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
     if (tree decl = builtin_decl_explicit (DECL_FUNCTION_CODE (fndecl)))
       fndecl = decl;
 
-  if (TYPE_MAIN_VARIANT (TREE_TYPE (call))
-      != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))))
+  bool gimple_form = (cfun && (cfun->curr_properties & PROP_gimple)) != 0;
+  if (gimple_form
+      ? !useless_type_conversion_p (TREE_TYPE (call),
+				    TREE_TYPE (TREE_TYPE (fndecl)))
+      : (TYPE_MAIN_VARIANT (TREE_TYPE (call))
+	 != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))))
     return false;
 
   tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
@@ -8440,12 +8444,15 @@ tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
 	return true;
       tree arg = CALL_EXPR_ARG (call, i);
       tree type = TREE_VALUE (targs);
-      if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
+      if (gimple_form
+	  ? !useless_type_conversion_p (type, TREE_TYPE (arg))
+	  : TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (arg)))
 	{
 	  /* For pointer arguments be more forgiving, e.g. due to
 	     FILE * vs. fileptr_type_node, or say char * vs. const char *
 	     differences etc.  */
-	  if (POINTER_TYPE_P (type)
+	  if (!gimple_form
+	      && POINTER_TYPE_P (type)
 	      && POINTER_TYPE_P (TREE_TYPE (arg))
 	      && tree_nop_conversion_p (type, TREE_TYPE (arg)))
 	    continue;
@@ -8457,8 +8464,11 @@ tree_builtin_call_types_compatible_p (const_tree call, tree fndecl)
 	      && INTEGRAL_TYPE_P (TREE_TYPE (arg))
 	      && !TYPE_UNSIGNED (TREE_TYPE (arg))
 	      && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))
-	      && tree_nop_conversion_p (integer_type_node,
-					TREE_TYPE (arg)))
+	      && (gimple_form
+		  ? useless_type_conversion_p (integer_type_node,
+					       TREE_TYPE (arg))
+		  : tree_nop_conversion_p (integer_type_node,
+					   TREE_TYPE (arg))))
 	    continue;
 	  return false;
 	}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-13 13:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-13 13:45 [gcc r12-8141] tree.cc: Use useless_type_conversion_p in tree_builtin_call_types_compatible_p while in gimple form Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).