diff mbox series

[C++] Improve compile-time by ordering expensive checks last

Message ID alpine.LSU.2.20.1904161425100.27537@zhemvz.fhfr.qr
State New
Headers show
Series [C++] Improve compile-time by ordering expensive checks last | expand

Commit Message

Richard Biener April 16, 2019, 12:26 p.m. UTC
Two cases from a -fsynax-only tramp3d callgrind profile.

Bootstrapped / tested on x86_64-unknown-linux-gnu.

OK for trunk?

Richard.

2019-04-16  Richard Biener  <rguenther@suse.de>

	cp/
	* call.c (null_ptr_cst_p): Order checks according to expensiveness.
	(conversion_null_warnings): Likewise.

Comments

Richard Biener April 17, 2019, 1:44 p.m. UTC | #1
On Tue, 16 Apr 2019, Richard Biener wrote:

> 
> Two cases from a -fsynax-only tramp3d callgrind profile.

Amended by two others.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

OK?

Thanks,
Richard.

2019-04-17  Richard Biener  <rguenther@suse.de>

	cp/
	* call.c (null_ptr_cst_p): Order checks according to expensiveness.
	(conversion_null_warnings): Likewise.
	* typeck.c (same_type_ignoring_top_level_qualifiers_p): Return
	early if type1 == type2.

Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c	(revision 270407)
+++ gcc/cp/call.c	(working copy)
@@ -541,11 +541,11 @@ null_ptr_cst_p (tree t)
       STRIP_ANY_LOCATION_WRAPPER (t);
 
       /* Core issue 903 says only literal 0 is a null pointer constant.  */
-      if (TREE_CODE (type) == INTEGER_TYPE
-	  && !char_type_p (type)
-	  && TREE_CODE (t) == INTEGER_CST
+      if (TREE_CODE (t) == INTEGER_CST
+	  && !TREE_OVERFLOW (t)
+	  && TREE_CODE (type) == INTEGER_TYPE
 	  && integer_zerop (t)
-	  && !TREE_OVERFLOW (t))
+	  && !char_type_p (type))
 	return true;
     }
   else if (CP_INTEGRAL_TYPE_P (type))
@@ -6844,8 +6844,9 @@ static void
 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
 {
   /* Issue warnings about peculiar, but valid, uses of NULL.  */
-  if (null_node_p (expr) && TREE_CODE (totype) != BOOLEAN_TYPE
-      && ARITHMETIC_TYPE_P (totype))
+  if (TREE_CODE (totype) != BOOLEAN_TYPE
+      && ARITHMETIC_TYPE_P (totype)
+      && null_node_p (expr))
     {
       location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
       if (fn)
@@ -6882,8 +6883,8 @@ conversion_null_warnings (tree totype, t
     }
   /* Handle zero as null pointer warnings for cases other
      than EQ_EXPR and NE_EXPR */
-  else if (null_ptr_cst_p (expr) &&
-	   (TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)))
+  else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
+	   && null_ptr_cst_p (expr))
     {
       location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
       maybe_warn_zero_as_null_pointer_constant (expr, loc);
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 270407)
+++ gcc/cp/typeck.c	(working copy)
@@ -1508,6 +1508,8 @@ same_type_ignoring_top_level_qualifiers_
 {
   if (type1 == error_mark_node || type2 == error_mark_node)
     return false;
+  if (type1 == type2)
+    return true;
 
   type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
   type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
Jason Merrill April 19, 2019, 12:58 a.m. UTC | #2
OK.

On Wed, Apr 17, 2019 at 6:44 AM Richard Biener <rguenther@suse.de> wrote:
>
> On Tue, 16 Apr 2019, Richard Biener wrote:
>
> >
> > Two cases from a -fsynax-only tramp3d callgrind profile.
>
> Amended by two others.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu.
>
> OK?
>
> Thanks,
> Richard.
>
> 2019-04-17  Richard Biener  <rguenther@suse.de>
>
>         cp/
>         * call.c (null_ptr_cst_p): Order checks according to expensiveness.
>         (conversion_null_warnings): Likewise.
>         * typeck.c (same_type_ignoring_top_level_qualifiers_p): Return
>         early if type1 == type2.
>
> Index: gcc/cp/call.c
> ===================================================================
> --- gcc/cp/call.c       (revision 270407)
> +++ gcc/cp/call.c       (working copy)
> @@ -541,11 +541,11 @@ null_ptr_cst_p (tree t)
>        STRIP_ANY_LOCATION_WRAPPER (t);
>
>        /* Core issue 903 says only literal 0 is a null pointer constant.  */
> -      if (TREE_CODE (type) == INTEGER_TYPE
> -         && !char_type_p (type)
> -         && TREE_CODE (t) == INTEGER_CST
> +      if (TREE_CODE (t) == INTEGER_CST
> +         && !TREE_OVERFLOW (t)
> +         && TREE_CODE (type) == INTEGER_TYPE
>           && integer_zerop (t)
> -         && !TREE_OVERFLOW (t))
> +         && !char_type_p (type))
>         return true;
>      }
>    else if (CP_INTEGRAL_TYPE_P (type))
> @@ -6844,8 +6844,9 @@ static void
>  conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
>  {
>    /* Issue warnings about peculiar, but valid, uses of NULL.  */
> -  if (null_node_p (expr) && TREE_CODE (totype) != BOOLEAN_TYPE
> -      && ARITHMETIC_TYPE_P (totype))
> +  if (TREE_CODE (totype) != BOOLEAN_TYPE
> +      && ARITHMETIC_TYPE_P (totype)
> +      && null_node_p (expr))
>      {
>        location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
>        if (fn)
> @@ -6882,8 +6883,8 @@ conversion_null_warnings (tree totype, t
>      }
>    /* Handle zero as null pointer warnings for cases other
>       than EQ_EXPR and NE_EXPR */
> -  else if (null_ptr_cst_p (expr) &&
> -          (TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)))
> +  else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
> +          && null_ptr_cst_p (expr))
>      {
>        location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
>        maybe_warn_zero_as_null_pointer_constant (expr, loc);
> Index: gcc/cp/typeck.c
> ===================================================================
> --- gcc/cp/typeck.c     (revision 270407)
> +++ gcc/cp/typeck.c     (working copy)
> @@ -1508,6 +1508,8 @@ same_type_ignoring_top_level_qualifiers_
>  {
>    if (type1 == error_mark_node || type2 == error_mark_node)
>      return false;
> +  if (type1 == type2)
> +    return true;
>
>    type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
>    type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
Rainer Orth April 25, 2019, 11:09 a.m. UTC | #3
Hi Richard,

> On Tue, 16 Apr 2019, Richard Biener wrote:
>
>> 
>> Two cases from a -fsynax-only tramp3d callgrind profile.
>
> Amended by two others.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu.
>
> OK?
>
> Thanks,
> Richard.
>
> 2019-04-17  Richard Biener  <rguenther@suse.de>
>
> 	cp/
> 	* call.c (null_ptr_cst_p): Order checks according to expensiveness.
> 	(conversion_null_warnings): Likewise.
> 	* typeck.c (same_type_ignoring_top_level_qualifiers_p): Return
> 	early if type1 == type2.

this patch caused

+XPASS: g++.dg/warn/Wunused-var-35.C  -std=gnu++98 bug (test for warnings, line 14)

First seen on i386-pc-solaris2.11 and sparc-sun-solaris2.11, according
to gcc-testresults everywhere.  Confirmed by reverting the patch locally
and re-testing the affected testcase.

	Rainer
diff mbox series

Patch

Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c	(revision 270387)
+++ gcc/cp/call.c	(working copy)
@@ -541,11 +541,11 @@  null_ptr_cst_p (tree t)
       STRIP_ANY_LOCATION_WRAPPER (t);
 
       /* Core issue 903 says only literal 0 is a null pointer constant.  */
-      if (TREE_CODE (type) == INTEGER_TYPE
-	  && !char_type_p (type)
-	  && TREE_CODE (t) == INTEGER_CST
+      if (TREE_CODE (t) == INTEGER_CST
+	  && !TREE_OVERFLOW (t)
+	  && TREE_CODE (type) == INTEGER_TYPE
 	  && integer_zerop (t)
-	  && !TREE_OVERFLOW (t))
+	  && !char_type_p (type))
 	return true;
     }
   else if (CP_INTEGRAL_TYPE_P (type))
@@ -6882,8 +6882,8 @@  conversion_null_warnings (tree totype, t
     }
   /* Handle zero as null pointer warnings for cases other
      than EQ_EXPR and NE_EXPR */
-  else if (null_ptr_cst_p (expr) &&
-	   (TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)))
+  else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
+	   && null_ptr_cst_p (expr))
     {
       location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
       maybe_warn_zero_as_null_pointer_constant (expr, loc);