This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][RFC] Step 1 to a middle-end type-system


These two patches (splitted to ease review) restructure 
type_conversion_needed_p to not require the types_compatible_p langhook
(apart from the remaining AGGREGATE_TYPE_P case).  It's not ready
for prime-time yet because the convert_parm_for_inlining langhook
interacts with this change -- I'll have to see to get rid of that
or at least remove the warning/error code from it.

Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages.
There are some libstdc++ fails that need investigation, but only after
I solved the convert_parm_for_inlining issue.

Thanks,
Richard.

2007-06-20  Richard Guenther  <rguenther@suse.de>

	* tree-ssa.c (type_conversion_needed_p): Document
	future intent.  Re-structure to call langhook last, mark
	questionable parts.

Index: tree-ssa.c
===================================================================
*** tree-ssa.c.orig	2007-06-20 17:00:19.000000000 +0200
--- tree-ssa.c	2007-06-20 18:55:42.000000000 +0200
*************** delete_tree_ssa (void)
*** 888,894 ****
  
  
  /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is
!    explicitly required, otherwise return false.  */
  
  bool
  type_conversion_needed_p (tree outer_type, tree inner_type)
--- 888,913 ----
  
  
  /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is
!    explicitly, otherwise return false.
!    This function implicitly defines the middle-end type system.  The
!    following invariants shall be fulfilled:
! 
!      1) type_conversion_needed_p is transitive.
! 	If a < b and b < c then a < c.
! 
!      2) type_conversion_needed_p is not symmetric.
! 	From a < b does not follow a > b.
! 
!      3) Types define the available set of operations applicable to values.
! 	A type conversion is useless if the operations for the target type
! 	is a subset of the operations for the source type.  For example
! 	casts to void* are useless, casts from void* are not (void* can't
! 	be dereferenced or offsetted, but copied, hence its set of operations
! 	is a strict subset of that of all other data pointer types).  Casts
! 	to const T* are useless (can't be written to), casts from const T*
! 	to T* are not.
! 
!    ???  The above do not hold currently.  */
  
  bool
  type_conversion_needed_p (tree outer_type, tree inner_type)
*************** type_conversion_needed_p (tree outer_typ
*** 900,974 ****
    if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
      return true;
  
!   /* If the inner and outer types are effectively the same, then
!      strip the type conversion and enter the equivalence into
!      the table.  */
!   if (lang_hooks.types_compatible_p (inner_type, outer_type))
!     return false;
  
!   /* If both types are pointers and the outer type is a (void *), then
!      the conversion is not necessary.  The opposite is not true since
!      that conversion would result in a loss of information if the
!      equivalence was used.  Consider an indirect function call where
!      we need to know the exact type of the function to correctly
!      implement the ABI.  */
!   else if (POINTER_TYPE_P (inner_type)
!            && POINTER_TYPE_P (outer_type)
! 	   && TYPE_REF_CAN_ALIAS_ALL (inner_type)
! 	      == TYPE_REF_CAN_ALIAS_ALL (outer_type)
! 	   && TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
!     return false;
  
!   /* Don't lose casts between pointers to volatile and non-volatile
!      qualified types.  Doing so would result in changing the semantics
!      of later accesses.  */
!   else if (POINTER_TYPE_P (inner_type)
!            && POINTER_TYPE_P (outer_type)
! 	   && TYPE_VOLATILE (TREE_TYPE (outer_type))
! 	      != TYPE_VOLATILE (TREE_TYPE (inner_type)))
!     return true;
  
!   /* Pointers/references are equivalent if their pointed to types
!      are effectively the same.  This allows to strip conversions between
!      pointer types with different type qualifiers.  */
    else if (POINTER_TYPE_P (inner_type)
!            && POINTER_TYPE_P (outer_type)
! 	   && TYPE_REF_CAN_ALIAS_ALL (inner_type)
! 	      == TYPE_REF_CAN_ALIAS_ALL (outer_type)
!            && lang_hooks.types_compatible_p (TREE_TYPE (inner_type),
! 					     TREE_TYPE (outer_type)))
!     return false;
! 
!   /* If both the inner and outer types are integral types, then the
!      conversion is not necessary if they have the same mode and
!      signedness and precision, and both or neither are boolean.  Some
!      code assumes an invariant that boolean types stay boolean and do
!      not become 1-bit bit-field types.  Note that types with precision
!      not using all bits of the mode (such as bit-field types in C)
!      mean that testing of precision is necessary.  */
!   else if (INTEGRAL_TYPE_P (inner_type)
!            && INTEGRAL_TYPE_P (outer_type)
! 	   && TYPE_UNSIGNED (inner_type) == TYPE_UNSIGNED (outer_type)
! 	   && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
      {
!       tree min_inner = fold_convert (outer_type, TYPE_MIN_VALUE (inner_type));
!       tree max_inner = fold_convert (outer_type, TYPE_MAX_VALUE (inner_type));
!       bool first_boolean = (TREE_CODE (inner_type) == BOOLEAN_TYPE);
!       bool second_boolean = (TREE_CODE (outer_type) == BOOLEAN_TYPE);
!       if (simple_cst_equal (max_inner, TYPE_MAX_VALUE (outer_type))
! 	  && simple_cst_equal (min_inner, TYPE_MIN_VALUE (outer_type))
! 	  && first_boolean == second_boolean)
  	return false;
      }
  
    /* Recurse for complex types.  */
    else if (TREE_CODE (inner_type) == COMPLEX_TYPE
! 	   && TREE_CODE (outer_type) == COMPLEX_TYPE
! 	   && !type_conversion_needed_p (TREE_TYPE (outer_type),
! 						  TREE_TYPE (inner_type)))
!     return false;
! 
!   return true;
  }
  
  /* Return true if EXPR is a useless type conversion, otherwise return
--- 919,1001 ----
    if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type))
      return true;
  
!   /* If both the inner and outer types are integral types, then the
!      conversion is not necessary if they have the same mode and
!      signedness and precision, and both or neither are boolean.  */
!   if (INTEGRAL_TYPE_P (inner_type)
!       && INTEGRAL_TYPE_P (outer_type))
!     {
!       /* Preserve changes in signedness or precision.  */
!       if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
! 	  || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
! 	return true;
! 
!       /* Preserve booleaness.  Some code assumes an invariant that boolean
! 	 types stay boolean and do not become 1-bit bit-field types.  */
!       if ((TREE_CODE (inner_type) == BOOLEAN_TYPE)
! 	  != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
! 	return true;
! 
!       /* Preserve changes in the types minimum or maximum value.
! 	 ???  Due to the way we handle sizetype as signed we need
! 	 to jump through hoops here to make sizetype and size_type_node
! 	 compatible.  */
!       if (!tree_int_cst_equal (fold_convert (outer_type,
! 					     TYPE_MIN_VALUE (inner_type)),
! 			       TYPE_MIN_VALUE (outer_type))
! 	  || !tree_int_cst_equal (fold_convert (outer_type,
! 						TYPE_MAX_VALUE (inner_type)),
! 				  TYPE_MAX_VALUE (outer_type)))
! 	return true;
  
!       /* ???  We might want to preserve base type changes because of
! 	 TBAA.  Or we need to be extra careful below.  */
  
!       return false;
!     }
  
!   /* We need to take special care recursing to pointed-to types.  */
    else if (POINTER_TYPE_P (inner_type)
! 	   && POINTER_TYPE_P (outer_type))
      {
!       /* Don't lose casts between pointers to volatile and non-volatile
! 	 qualified types.  Doing so would result in changing the semantics
! 	 of later accesses.  */
!       if (TYPE_VOLATILE (TREE_TYPE (outer_type))
! 	  != TYPE_VOLATILE (TREE_TYPE (inner_type)))
! 	return true;
! 
!       /* Do not lose casts between pointers with different
! 	 TYPE_REF_CAN_ALIAS_ALL setting.  */
!       if (TYPE_REF_CAN_ALIAS_ALL (inner_type)
! 	  != TYPE_REF_CAN_ALIAS_ALL (outer_type))
! 	return true;
! 
!       /* If the outer type is (void *), then the conversion is not
! 	 necessary.
! 	 ???  Together with calling the langhook below this makes
! 	 type_conversion_needed_p not transitive.  */
!       if (TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
  	return false;
+ 
+       /* Otherwise pointers/references are equivalent if their pointed
+ 	 to types are effectively the same.  This allows to strip conversions
+ 	 between pointer types with different type qualifiers.
+ 	 ???  We should recurse here with
+ 	 type_conversion_needed_p.  */
+       return !lang_hooks.types_compatible_p (TREE_TYPE (inner_type),
+ 					    TREE_TYPE (outer_type));
      }
  
    /* Recurse for complex types.  */
    else if (TREE_CODE (inner_type) == COMPLEX_TYPE
! 	   && TREE_CODE (outer_type) == COMPLEX_TYPE)
!     return type_conversion_needed_p (TREE_TYPE (outer_type),
! 				      TREE_TYPE (inner_type));
! 
!   /* Fall back to what the frontend thinks of type compatibility.
!      ???  This should eventually just return false.  */
!   return !lang_hooks.types_compatible_p (inner_type, outer_type);
  }
  
  /* Return true if EXPR is a useless type conversion, otherwise return



2007-06-22  Richard Guenther  <rguenther@suse.de>

	* tree-ssa.c (type_conversion_needed_p): Add handling for
	scalar float and vector types.  Only call the types_compatible_p
	langhook for aggregate types as last resort.  Follow the
	rules more.

	* testsuite/gcc.dg/pr29254.c: The warning is bogus.

Index: gcc/tree-ssa.c
===================================================================
*** gcc.orig/tree-ssa.c	2007-06-21 18:40:06.000000000 +0200
--- gcc/tree-ssa.c	2007-06-21 18:41:01.000000000 +0200
*************** delete_tree_ssa (void)
*** 912,917 ****
--- 912,921 ----
  bool
  type_conversion_needed_p (tree outer_type, tree inner_type)
  {
+   /* Qualifiers on value types do not matter.  */
+   inner_type = TYPE_MAIN_VARIANT (inner_type);
+   outer_type = TYPE_MAIN_VARIANT (outer_type);
+ 
    if (inner_type == outer_type)
      return false;
  
*************** type_conversion_needed_p (tree outer_typ
*** 948,990 ****
  				  TYPE_MAX_VALUE (outer_type)))
  	return true;
  
-       /* ???  We might want to preserve base type changes because of
- 	 TBAA.  Or we need to be extra careful below.  */
- 
        return false;
      }
  
    /* We need to take special care recursing to pointed-to types.  */
    else if (POINTER_TYPE_P (inner_type)
  	   && POINTER_TYPE_P (outer_type))
      {
        /* Don't lose casts between pointers to volatile and non-volatile
  	 qualified types.  Doing so would result in changing the semantics
  	 of later accesses.  */
!       if (TYPE_VOLATILE (TREE_TYPE (outer_type))
! 	  != TYPE_VOLATILE (TREE_TYPE (inner_type)))
  	return true;
  
        /* Do not lose casts between pointers with different
! 	 TYPE_REF_CAN_ALIAS_ALL setting.  */
!       if (TYPE_REF_CAN_ALIAS_ALL (inner_type)
! 	  != TYPE_REF_CAN_ALIAS_ALL (outer_type))
  	return true;
  
!       /* If the outer type is (void *), then the conversion is not
! 	 necessary.
! 	 ???  Together with calling the langhook below this makes
! 	 type_conversion_needed_p not transitive.  */
!       if (TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
! 	return false;
  
        /* Otherwise pointers/references are equivalent if their pointed
! 	 to types are effectively the same.  This allows to strip conversions
! 	 between pointer types with different type qualifiers.
! 	 ???  We should recurse here with
! 	 type_conversion_needed_p.  */
!       return !lang_hooks.types_compatible_p (TREE_TYPE (inner_type),
! 					    TREE_TYPE (outer_type));
      }
  
    /* Recurse for complex types.  */
--- 952,1010 ----
  				  TYPE_MAX_VALUE (outer_type)))
  	return true;
  
        return false;
      }
  
+   /* Scalar floating point types with the same mode are compatible.  */
+   else if (SCALAR_FLOAT_TYPE_P (inner_type)
+ 	   && SCALAR_FLOAT_TYPE_P (outer_type))
+     return false;
+ 
    /* We need to take special care recursing to pointed-to types.  */
    else if (POINTER_TYPE_P (inner_type)
  	   && POINTER_TYPE_P (outer_type))
      {
+       /* If the outer type is (void *), then the conversion is not
+ 	 necessary.  */
+       if (TREE_CODE (TREE_TYPE (outer_type)) == VOID_TYPE)
+ 	return false;
+ 
        /* Don't lose casts between pointers to volatile and non-volatile
  	 qualified types.  Doing so would result in changing the semantics
  	 of later accesses.  */
!       if ((TYPE_VOLATILE (TREE_TYPE (outer_type))
! 	   != TYPE_VOLATILE (TREE_TYPE (inner_type)))
! 	  && TYPE_VOLATILE (TREE_TYPE (outer_type)))
  	return true;
  
        /* Do not lose casts between pointers with different
! 	 TYPE_REF_CAN_ALIAS_ALL setting or alias sets.  */
!       if ((TYPE_REF_CAN_ALIAS_ALL (inner_type)
! 	   != TYPE_REF_CAN_ALIAS_ALL (outer_type))
! 	  || (get_alias_set (TREE_TYPE (inner_type))
! 	      != get_alias_set (TREE_TYPE (outer_type))))
  	return true;
  
!       /* Do not lose casts from const qualified to non-const
! 	 qualified.  */
!       if ((TYPE_READONLY (TREE_TYPE (outer_type))
! 	   != TYPE_READONLY (TREE_TYPE (inner_type)))
! 	  /* ???  Unless we fix the convert_param_for_inlining
! 	     langhook to not warn/error, no way to do the following.  */
! 	  /*&& TYPE_READONLY (TREE_TYPE (inner_type))*/)
! 	return true;
! 
!       /* Do not lose casts to restrict qualified pointers.  */
!       if ((TYPE_RESTRICT (outer_type)
! 	   != TYPE_RESTRICT (inner_type))
! 	  && TYPE_RESTRICT (outer_type))
! 	return true;
  
        /* Otherwise pointers/references are equivalent if their pointed
! 	 to types are effectively the same.  We can strip qualifiers
! 	 on pointed-to types for further comparsion.  */
!       return type_conversion_needed_p (TYPE_MAIN_VARIANT (TREE_TYPE (outer_type)),
! 				       TYPE_MAIN_VARIANT (TREE_TYPE (inner_type)));
      }
  
    /* Recurse for complex types.  */
*************** type_conversion_needed_p (tree outer_typ
*** 993,1001 ****
      return type_conversion_needed_p (TREE_TYPE (outer_type),
  				      TREE_TYPE (inner_type));
  
!   /* Fall back to what the frontend thinks of type compatibility.
!      ???  This should eventually just return false.  */
!   return !lang_hooks.types_compatible_p (inner_type, outer_type);
  }
  
  /* Return true if EXPR is a useless type conversion, otherwise return
--- 1013,1047 ----
      return type_conversion_needed_p (TREE_TYPE (outer_type),
  				      TREE_TYPE (inner_type));
  
!   /* Recurse for vector types with the same number of subparts.  */
!   else if (TREE_CODE (inner_type) == VECTOR_TYPE
! 	   && TREE_CODE (outer_type) == VECTOR_TYPE
! 	   && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
!     return type_conversion_needed_p (TREE_TYPE (outer_type),
! 				     TREE_TYPE (inner_type));
! 
!   /* For aggregates we may need to fall back to structural equality
!      checks.  */
!   else if (AGGREGATE_TYPE_P (inner_type)
! 	   && AGGREGATE_TYPE_P (outer_type))
!     {
!       /* Different types of aggregates are incompatible.  */
!       if (TREE_CODE (inner_type) != TREE_CODE (outer_type))
! 	return true;
! 
!       /* If we know the canonical types, compare them.  */
!       if (TYPE_CANONICAL (inner_type)
! 	  && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
! 	return false;
! 
!       /* ???  Add structural equivalence check.  */
! 
!       /* Fall back to what the frontend thinks of type compatibility.
! 	 ???  This should eventually just return false.  */
!       return !lang_hooks.types_compatible_p (inner_type, outer_type);
!     }
! 
!   return true;
  }
  
  /* Return true if EXPR is a useless type conversion, otherwise return
Index: gcc/testsuite/gcc.dg/pr29254.c
===================================================================
*** gcc.orig/testsuite/gcc.dg/pr29254.c	2007-06-21 18:33:10.000000000 +0200
--- gcc/testsuite/gcc.dg/pr29254.c	2007-06-21 18:40:07.000000000 +0200
***************
*** 1,6 ****
  /* { dg-do compile } */
  /* { dg-options "-O3 -Werror" } */
- /* { dg-message "warnings being treated as errors" "" {target "*-*-*"} 0 } */
  
  list_compare (int * list1)
  {
--- 1,5 ----
*************** value_compare (int * a)
*** 18,22 ****
  
  func2 (const int * fb)
  {
!   func1 ((int *) fb); /* { dg-error "discards qualifiers" } */
  }
--- 17,21 ----
  
  func2 (const int * fb)
  {
!   func1 ((int *) fb); /* { dg-bogus "discards qualifiers" } */
  }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]