This is the 2nd version of the patch.

update the changelog per Jacub's comments. 

I will commit this version soon.

thanks.

Qing

========================

This is an improvement to the design of internal function .ACCESS_WITH_SIZE.

Currently, the .ACCESS_WITH_SIZE is designed as:

   ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE,
                     TYPE_OF_SIZE, ACCESS_MODE, TYPE_SIZE_UNIT for element)
   which returns the REF_TO_OBJ same as the 1st argument;

   1st argument REF_TO_OBJ: The reference to the object;
   2nd argument REF_TO_SIZE: The reference to the size of the object,
   3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents
     0: the number of bytes.
     1: the number of the elements of the object type;
   4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the
     TYPE of the object referenced by REF_TO_SIZE
   5th argument ACCESS_MODE:
     -1: Unknown access semantics
      0: none
      1: read_only
      2: write_only
      3: read_write
   6th argument: The TYPE_SIZE_UNIT of the element TYPE of the FAM when 3rd
      argument is 1. NULL when 3rd argument is 0.

Among the 6 arguments:
 A. The 3rd argument CLASS_OF_SIZE is not needed. If the REF_TO_SIZE represents
    the number of bytes, simply pass 1 to the TYPE_SIZE_UNIT argument.
 B. The 4th and the 5th arguments can be combined into 1 argument, whose TYPE
    represents the TYPE_OF_SIZE, and the constant value represents the
    ACCESS_MODE.

As a result, the new design of the .ACCESS_WITH_SIZE is:

   ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE,
                     TYPE_OF_SIZE + ACCESS_MODE, TYPE_SIZE_UNIT for element)
   which returns the REF_TO_OBJ same as the 1st argument;

   1st argument REF_TO_OBJ: The reference to the object;
   2nd argument REF_TO_SIZE: The reference to the size of the object,
   3rd argument TYPE_OF_SIZE + ACCESS_MODE: An integer constant with a pointer
     TYPE.
     The pointee TYPE of the pointer TYPE is the TYPE of the object referenced
        by REF_TO_SIZE.
     The integer constant value represents the ACCESS_MODE:
        0: none
        1: read_only
        2: write_only
        3: read_write
   4th argument: The TYPE_SIZE_UNIT of the element TYPE of the array.

gcc/c-family/ChangeLog:

        * c-ubsan.cc (get_bound_from_access_with_size): Adjust the position
        of the arguments per the new design.

gcc/c/ChangeLog:

        * c-typeck.cc (build_access_with_size_for_counted_by): Update comments.
        Adjust the arguments per the new design.

gcc/ChangeLog:

        * internal-fn.cc (expand_ACCESS_WITH_SIZE): Update comments.
        * internal-fn.def (ACCESS_WITH_SIZE): Update comments.
        * tree-object-size.cc (access_with_size_object_size): Update comments.
        Adjust the arguments per the new design.
---
 gcc/c-family/c-ubsan.cc | 10 ++--------
 gcc/c/c-typeck.cc       | 18 +++++++++---------
 gcc/internal-fn.cc      | 28 +++++++++++++---------------
 gcc/internal-fn.def     |  2 +-
 gcc/tree-object-size.cc | 34 +++++++++++++---------------------
 5 files changed, 38 insertions(+), 54 deletions(-)

diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc
index 78b78685469..a4dc31066af 100644
--- a/gcc/c-family/c-ubsan.cc
+++ b/gcc/c-family/c-ubsan.cc
@@ -397,8 +397,7 @@ get_bound_from_access_with_size (tree call)
     return NULL_TREE;
 
   tree ref_to_size = CALL_EXPR_ARG (call, 1);
-  unsigned int class_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2));
-  tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3));
+  tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (call, 2)));
   tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size),
                           build_int_cst (ptr_type_node, 0));
   /* If size is negative value, treat it as zero.  */
@@ -410,12 +409,7 @@ get_bound_from_access_with_size (tree call)
                        build_zero_cst (type), size);
   }
 
-  /* Only when class_of_size is 1, i.e, the number of the elements of
-     the object type, return the size.  */
-  if (class_of_size != 1)
-    return NULL_TREE;
-  else
-    size = fold_convert (sizetype, size);
+  size = fold_convert (sizetype, size);
 
   return size;
 }
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index de3d6c78db8..9a5eb0da3a1 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -2982,7 +2982,7 @@ build_counted_by_ref (tree datum, tree subdatum, tree 
*counted_by_type)
 
    to:
 
-   (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1,
+   (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
                        TYPE_SIZE_UNIT for element)
 
    NOTE: The return type of this function is the POINTER type pointing
@@ -2992,11 +2992,11 @@ build_counted_by_ref (tree datum, tree subdatum, tree 
*counted_by_type)
    The type of the first argument of this function is a POINTER type
    to the original flexible array type.
 
-   The 4th argument of the call is a constant 0 with the TYPE of the
-   object pointed by COUNTED_BY_REF.
+   The 3rd argument of the call is a constant 0 with the pointer TYPE whose
+   pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
 
-   The 6th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
-   of the FAM.
+   The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
+   of the array.
 
   */
 static tree
@@ -3013,16 +3013,16 @@ build_access_with_size_for_counted_by (location_t loc, 
tree ref,
     = c_fully_fold (array_to_pointer_conversion (loc, ref), false, NULL);
   tree second_param
     = c_fully_fold (counted_by_ref, false, NULL);
+  tree third_param = build_int_cst (build_pointer_type (counted_by_type), 0);
 
   tree call
     = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
-                                   result_type, 6,
+                                   result_type, 4,
                                    first_param,
                                    second_param,
-                                   build_int_cst (integer_type_node, 1),
-                                   build_int_cst (counted_by_type, 0),
-                                   build_int_cst (integer_type_node, -1),
+                                   third_param,
                                    element_size);
+
   /* Wrap the call with an INDIRECT_REF with the flexible array type.  */
   call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
   SET_EXPR_LOCATION (call, loc);
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index c6e705cb6f5..044bdc22481 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -3442,25 +3442,23 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt)
 }
 
 /* Expand the IFN_ACCESS_WITH_SIZE function:
-   ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE,
-                    TYPE_OF_SIZE, ACCESS_MODE, TYPE_SIZE_UNIT for element)
+   ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE,
+                    TYPE_OF_SIZE + ACCESS_MODE, TYPE_SIZE_UNIT for element)
    which returns the REF_TO_OBJ same as the 1st argument;
 
    1st argument REF_TO_OBJ: The reference to the object;
    2nd argument REF_TO_SIZE: The reference to the size of the object,
-   3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE 
represents
-     0: the number of bytes.
-     1: the number of the elements of the object type;
-   4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the
-    TYPE of the object referenced by REF_TO_SIZE
-   5th argument ACCESS_MODE:
-    -1: Unknown access semantics
-     0: none
-     1: read_only
-     2: write_only
-     3: read_write
-   6th argument: The TYPE_SIZE_UNIT of the element TYPE of the FAM when the
-    3rd argument is 1; NULL when the 3rd argument is 0.
+   3rd argument TYPE_OF_SIZE + ACCESS_MODE: An integer constant with a pointer
+     TYPE.
+     The pointee TYPE of the pointer TYPE is the TYPE of the object referenced
+       by REF_TO_SIZE.
+     The integer constant value represents the ACCESS_MODE:
+       0: none
+       1: read_only
+       2: write_only
+       3: read_write
+
+   4th argument: The TYPE_SIZE_UNIT of the element TYPE of the array.
 
    Both the return type and the type of the first argument of this
    function have been converted from the incomplete array type to
diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def
index 95f462096a1..d2480a1bf79 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -525,7 +525,7 @@ DEF_INTERNAL_FN (DEFERRED_INIT, ECF_CONST | ECF_LEAF | 
ECF_NOTHROW, NULL)
 
 /* A function to associate the access size and access mode information
    with the corresponding reference to an object.  It only reads from the
-   2nd and the 6th arguments.  */
+   2nd and the 4th arguments.  */
 DEF_INTERNAL_FN (ACCESS_WITH_SIZE, ECF_PURE | ECF_LEAF | ECF_NOTHROW, NULL)
 
 /* DIM_SIZE and DIM_POS return the size of a particular compute
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index 77a3258ea6e..8545eff61a3 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -852,17 +852,16 @@ addr_object_size (struct object_size_info *osi, 
const_tree ptr,
 /* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE,
    OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
 
-   The 2nd, 3rd, 4th and 6th parameters of the call determine the size of
+   The 2nd, 3rd, and 4th parameters of the call determine the size of
    the CALL:
 
    2nd argument REF_TO_SIZE: The reference to the size of the object,
-   3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE 
represents
-     0: the number of bytes;
-     1: the number of the elements of the object type;
-   4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the
-    TYPE of the object referenced by REF_TO_SIZE
-   6th argument:  The TYPE_SIZE_UNIT of the element TYPE of the FAM when the
-    3rd argument is 1; NULL when the 3rd argument is 0.  */
+   3rd argument TYPE_OF_SIZE + ACCESS_MODE: An integer constant with a pointer
+     TYPE.
+     The pointee TYPE of this pointer TYPE is the TYPE of the object referenced
+       by REF_TO_SIZE.
+
+   4th argument:  The TYPE_SIZE_UNIT of the element TYPE of the array.  */
 
 static tree
 access_with_size_object_size (const gcall *call, int object_size_type)
@@ -873,15 +872,11 @@ access_with_size_object_size (const gcall *call, int 
object_size_type)
   gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE));
 
   tree ref_to_size = gimple_call_arg (call, 1);
-  unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2));
-  tree type = TREE_TYPE (gimple_call_arg (call, 3));
+  tree type = TREE_TYPE (TREE_TYPE (gimple_call_arg (call, 2)));
 
-  /* The 6th argument is the TYPE_SIZE_UNIT for the element of the original
+  /* The 4th argument is the TYPE_SIZE_UNIT for the element of the original
      flexible array.  */
-  tree element_size = gimple_call_arg (call, 5);
-
-  gcc_assert ((class_of_size == 1 && element_size)
-             || (class_of_size == 0 && !element_size));
+  tree element_size = gimple_call_arg (call, 3);
 
   tree size = fold_build2 (MEM_REF, type, ref_to_size,
                           build_int_cst (ptr_type_node, 0));
@@ -895,12 +890,9 @@ access_with_size_object_size (const gcall *call, int 
object_size_type)
                        build_zero_cst (type), size);
   }
 
-  if (class_of_size == 1)
-    size = size_binop (MULT_EXPR,
-                      fold_convert (sizetype, size),
-                      fold_convert (sizetype, element_size));
-  else
-    size = fold_convert (sizetype, size);
+  size = size_binop (MULT_EXPR,
+                    fold_convert (sizetype, size),
+                    fold_convert (sizetype, element_size));
 
   if (!todo)
     todo = TODO_update_ssa_only_virtuals;
-- 
2.31.1

Reply via email to