Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-05-29 Thread Qing Zhao
Richard and Joseph:


> On May 28, 2024, at 17:09, Qing Zhao  wrote:
> 
>>> 
>>> diff --git a/gcc/varasm.cc b/gcc/varasm.cc
>>> index fa17eff551e8..d75b23668925 100644
>>> --- a/gcc/varasm.cc
>>> +++ b/gcc/varasm.cc
>>> @@ -5082,6 +5082,11 @@ initializer_constant_valid_p_1 (tree value, tree 
>>> endtype, tree *cache)
>>>   }
>>>  return ret;
>>> 
>>> +case CALL_EXPR:
>>> +  /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
>>> +  if (tree ref = get_ref_from_access_with_size (value))
>>> +   return initializer_constant_valid_p_1 (ref, endtype, cache);
>> 
>> I think we should fold/strip .ACCESS_WITH_SIZE from initializers
>> instead.  That would be
>> the frontends job I guess, most probably not even generate those in
>> the first place?
> 
> Sounds reasonable, I will see how to do this in C FE. 
> Joseph, do you have any suggestion where in C FE I should do this folding? 
> 
> thanks.
> 
> Qing

In order to address this above comment from Richard, I studied a little bit 
more on the C FE code, and then come up with the following patch:

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index ac306749e8d7..efd111305b5a 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -8650,6 +8650,20 @@ digest_init (location_t init_loc, tree type, tree init, 
tree origtype,
 STRIP_TYPE_NOPS (inside_init);
+  /* If require_constant is TRUE,  when the initializer is a call to
+ .ACCESS_WITH_SIZE, use the first argument as the initializer.
+ For example:
+ y = (char *) .ACCESS_WITH_SIZE ((char *) _annotated.c,...)
+ will be converted to
+ y = _annotated.c.  */
+
+  if (require_constant
+  && TREE_CODE (inside_init) == NOP_EXPR
+  && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
+  && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
+inside_init
+  = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
+
   if (!c_in_omp_for)
 {
   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
diff --git a/gcc/varasm.cc b/gcc/varasm.cc
index 2e8fa5e30a80..e1a8458f8749 100644
--- a/gcc/varasm.cc
+++ b/gcc/varasm.cc
@@ -5078,10 +5078,6 @@ initializer_constant_valid_p_1 (tree value, tree 
endtype, tree *cache)
  }
   return ret;
-case CALL_EXPR:
-  /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
-  if (tree ref = get_ref_from_access_with_size (value))
- return initializer_constant_valid_p_1 (ref, endtype, cache);
   /* FALLTHROUGH.  */
 default:
   break;
@@ -5277,11 +5273,6 @@ output_constant (tree exp, unsigned HOST_WIDE_INT size, 
unsigned int align,
  exp = TREE_OPERAND (exp, 0);
 }
-  /* For a call to .ACCESS_WITH_SIZE, check the first argument.  */
-  if (TREE_CODE (exp) == CALL_EXPR)
-if (tree ref = get_ref_from_access_with_size (exp))
-  exp = ref;
-
   code = TREE_CODE (TREE_TYPE (exp));

This resolved the issue well. 

Let me know if you have any comment or suggestion. 

I have fixed all the issues Richard raised in my private workspace, testing is 
ongoing.
Will post the modified patch set soon.

Thanks a lot!

Qing

Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-05-29 Thread Qing Zhao


> On May 29, 2024, at 02:57, Richard Biener  wrote:
> 
> On Tue, May 28, 2024 at 11:09 PM Qing Zhao  wrote:
>> 
>> Thank you for the comments. See my answers below:
>> 
>> Joseph, please see the last question, I need your help on it. Thanks a lot 
>> for the help.
>> 
>> Qing
>> 
>>> On May 28, 2024, at 03:38, Richard Biener  
>>> wrote:
>>> 
>>> On Fri, Apr 12, 2024 at 3:54 PM Qing Zhao  wrote:
 
 Including the following changes:
 * The definition of the new internal function .ACCESS_WITH_SIZE
 in internal-fn.def.
 * C FE converts every reference to a FAM with a "counted_by" attribute
 to a call to the internal function .ACCESS_WITH_SIZE.
 (build_component_ref in c_typeck.cc)
 
 This includes the case when the object is statically allocated and
 initialized.
 In order to make this working, the routines initializer_constant_valid_p_1
 and output_constant in varasm.cc are updated to handle calls to
 .ACCESS_WITH_SIZE.
 (initializer_constant_valid_p_1 and output_constant in varasm.c)
 
 However, for the reference inside "offsetof", the "counted_by" attribute is
 ignored since it's not useful at all.
 (c_parser_postfix_expression in c/c-parser.cc)
 
 In addtion to "offsetof", for the reference inside operator "typeof" and
 "alignof", we ignore counted_by attribute too.
 
 When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE,
 replace the call with its first argument.
 
 * Convert every call to .ACCESS_WITH_SIZE to its first argument.
 (expand_ACCESS_WITH_SIZE in internal-fn.cc)
 * Adjust alias analysis to exclude the new internal from clobbering 
 anything.
 (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in 
 tree-ssa-alias.cc)
 * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE 
 when
 it's LHS is eliminated as dead code.
 (eliminate_unnecessary_stmts in tree-ssa-dce.cc)
 * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and
 get the reference from the call to .ACCESS_WITH_SIZE.
 (is_access_with_size_p and get_ref_from_access_with_size in tree.cc)
 
 gcc/c/ChangeLog:
 
   * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by
   attribute when build_component_ref inside offsetof operator.
   * c-tree.h (build_component_ref): Add one more parameter.
   * c-typeck.cc (build_counted_by_ref): New function.
   (build_access_with_size_for_counted_by): New function.
   (build_component_ref): Check the counted-by attribute and build
   call to .ACCESS_WITH_SIZE.
   (build_unary_op): When building ADDR_EXPR for
   .ACCESS_WITH_SIZE, use its first argument.
   (lvalue_p): Accept call to .ACCESS_WITH_SIZE.
 
 gcc/ChangeLog:
 
   * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function.
   * internal-fn.def (ACCESS_WITH_SIZE): New internal function.
   * tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case
   IFN_ACCESS_WITH_SIZE.
   (call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE.
   * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call
   to .ACCESS_WITH_SIZE when its LHS is dead.
   * tree.cc (process_call_operands): Adjust side effect for function
   .ACCESS_WITH_SIZE.
   (is_access_with_size_p): New function.
   (get_ref_from_access_with_size): New function.
   * tree.h (is_access_with_size_p): New prototype.
   (get_ref_from_access_with_size): New prototype.
   * varasm.cc (initializer_constant_valid_p_1): Handle call to
   .ACCESS_WITH_SIZE.
   (output_constant): Handle call to .ACCESS_WITH_SIZE.
 
 gcc/testsuite/ChangeLog:
 
   * gcc.dg/flex-array-counted-by-2.c: New test.
 ---
 gcc/c/c-parser.cc |  10 +-
 gcc/c/c-tree.h|   2 +-
 gcc/c/c-typeck.cc | 128 +-
 gcc/internal-fn.cc|  35 +
 gcc/internal-fn.def   |   4 +
 .../gcc.dg/flex-array-counted-by-2.c  | 112 +++
 gcc/tree-ssa-alias.cc |   2 +
 gcc/tree-ssa-dce.cc   |   5 +-
 gcc/tree.cc   |  25 +++-
 gcc/tree.h|   8 ++
 gcc/varasm.cc |  10 ++
 11 files changed, 331 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c
 
 diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
 index c31349dae2ff..a6ed5ac43bb1 100644
 --- a/gcc/c/c-parser.cc
 +++ b/gcc/c/c-parser.cc
 @@ -10850,9 +10850,12 @@ c_parser_postfix_expression 

Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-05-29 Thread Richard Biener
On Tue, May 28, 2024 at 11:09 PM Qing Zhao  wrote:
>
> Thank you for the comments. See my answers below:
>
> Joseph, please see the last question, I need your help on it. Thanks a lot 
> for the help.
>
> Qing
>
> > On May 28, 2024, at 03:38, Richard Biener  
> > wrote:
> >
> > On Fri, Apr 12, 2024 at 3:54 PM Qing Zhao  wrote:
> >>
> >> Including the following changes:
> >> * The definition of the new internal function .ACCESS_WITH_SIZE
> >>  in internal-fn.def.
> >> * C FE converts every reference to a FAM with a "counted_by" attribute
> >>  to a call to the internal function .ACCESS_WITH_SIZE.
> >>  (build_component_ref in c_typeck.cc)
> >>
> >>  This includes the case when the object is statically allocated and
> >>  initialized.
> >>  In order to make this working, the routines initializer_constant_valid_p_1
> >>  and output_constant in varasm.cc are updated to handle calls to
> >>  .ACCESS_WITH_SIZE.
> >>  (initializer_constant_valid_p_1 and output_constant in varasm.c)
> >>
> >>  However, for the reference inside "offsetof", the "counted_by" attribute 
> >> is
> >>  ignored since it's not useful at all.
> >>  (c_parser_postfix_expression in c/c-parser.cc)
> >>
> >>  In addtion to "offsetof", for the reference inside operator "typeof" and
> >>  "alignof", we ignore counted_by attribute too.
> >>
> >>  When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE,
> >>  replace the call with its first argument.
> >>
> >> * Convert every call to .ACCESS_WITH_SIZE to its first argument.
> >>  (expand_ACCESS_WITH_SIZE in internal-fn.cc)
> >> * Adjust alias analysis to exclude the new internal from clobbering 
> >> anything.
> >>  (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in 
> >> tree-ssa-alias.cc)
> >> * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE 
> >> when
> >>  it's LHS is eliminated as dead code.
> >>  (eliminate_unnecessary_stmts in tree-ssa-dce.cc)
> >> * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and
> >>  get the reference from the call to .ACCESS_WITH_SIZE.
> >>  (is_access_with_size_p and get_ref_from_access_with_size in tree.cc)
> >>
> >> gcc/c/ChangeLog:
> >>
> >>* c-parser.cc (c_parser_postfix_expression): Ignore the counted-by
> >>attribute when build_component_ref inside offsetof operator.
> >>* c-tree.h (build_component_ref): Add one more parameter.
> >>* c-typeck.cc (build_counted_by_ref): New function.
> >>(build_access_with_size_for_counted_by): New function.
> >>(build_component_ref): Check the counted-by attribute and build
> >>call to .ACCESS_WITH_SIZE.
> >>(build_unary_op): When building ADDR_EXPR for
> >>.ACCESS_WITH_SIZE, use its first argument.
> >>(lvalue_p): Accept call to .ACCESS_WITH_SIZE.
> >>
> >> gcc/ChangeLog:
> >>
> >>* internal-fn.cc (expand_ACCESS_WITH_SIZE): New function.
> >>* internal-fn.def (ACCESS_WITH_SIZE): New internal function.
> >>* tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case
> >>IFN_ACCESS_WITH_SIZE.
> >>(call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE.
> >>* tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call
> >>to .ACCESS_WITH_SIZE when its LHS is dead.
> >>* tree.cc (process_call_operands): Adjust side effect for function
> >>.ACCESS_WITH_SIZE.
> >>(is_access_with_size_p): New function.
> >>(get_ref_from_access_with_size): New function.
> >>* tree.h (is_access_with_size_p): New prototype.
> >>(get_ref_from_access_with_size): New prototype.
> >>* varasm.cc (initializer_constant_valid_p_1): Handle call to
> >>.ACCESS_WITH_SIZE.
> >>(output_constant): Handle call to .ACCESS_WITH_SIZE.
> >>
> >> gcc/testsuite/ChangeLog:
> >>
> >>* gcc.dg/flex-array-counted-by-2.c: New test.
> >> ---
> >> gcc/c/c-parser.cc |  10 +-
> >> gcc/c/c-tree.h|   2 +-
> >> gcc/c/c-typeck.cc | 128 +-
> >> gcc/internal-fn.cc|  35 +
> >> gcc/internal-fn.def   |   4 +
> >> .../gcc.dg/flex-array-counted-by-2.c  | 112 +++
> >> gcc/tree-ssa-alias.cc |   2 +
> >> gcc/tree-ssa-dce.cc   |   5 +-
> >> gcc/tree.cc   |  25 +++-
> >> gcc/tree.h|   8 ++
> >> gcc/varasm.cc |  10 ++
> >> 11 files changed, 331 insertions(+), 10 deletions(-)
> >> create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c
> >>
> >> diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
> >> index c31349dae2ff..a6ed5ac43bb1 100644
> >> --- a/gcc/c/c-parser.cc
> >> +++ b/gcc/c/c-parser.cc
> >> @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser)
> >>   

Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-05-28 Thread Qing Zhao
Thank you for the comments. See my answers below:

Joseph, please see the last question, I need your help on it. Thanks a lot for 
the help.

Qing

> On May 28, 2024, at 03:38, Richard Biener  wrote:
> 
> On Fri, Apr 12, 2024 at 3:54 PM Qing Zhao  wrote:
>> 
>> Including the following changes:
>> * The definition of the new internal function .ACCESS_WITH_SIZE
>>  in internal-fn.def.
>> * C FE converts every reference to a FAM with a "counted_by" attribute
>>  to a call to the internal function .ACCESS_WITH_SIZE.
>>  (build_component_ref in c_typeck.cc)
>> 
>>  This includes the case when the object is statically allocated and
>>  initialized.
>>  In order to make this working, the routines initializer_constant_valid_p_1
>>  and output_constant in varasm.cc are updated to handle calls to
>>  .ACCESS_WITH_SIZE.
>>  (initializer_constant_valid_p_1 and output_constant in varasm.c)
>> 
>>  However, for the reference inside "offsetof", the "counted_by" attribute is
>>  ignored since it's not useful at all.
>>  (c_parser_postfix_expression in c/c-parser.cc)
>> 
>>  In addtion to "offsetof", for the reference inside operator "typeof" and
>>  "alignof", we ignore counted_by attribute too.
>> 
>>  When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE,
>>  replace the call with its first argument.
>> 
>> * Convert every call to .ACCESS_WITH_SIZE to its first argument.
>>  (expand_ACCESS_WITH_SIZE in internal-fn.cc)
>> * Adjust alias analysis to exclude the new internal from clobbering anything.
>>  (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in 
>> tree-ssa-alias.cc)
>> * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE 
>> when
>>  it's LHS is eliminated as dead code.
>>  (eliminate_unnecessary_stmts in tree-ssa-dce.cc)
>> * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and
>>  get the reference from the call to .ACCESS_WITH_SIZE.
>>  (is_access_with_size_p and get_ref_from_access_with_size in tree.cc)
>> 
>> gcc/c/ChangeLog:
>> 
>>* c-parser.cc (c_parser_postfix_expression): Ignore the counted-by
>>attribute when build_component_ref inside offsetof operator.
>>* c-tree.h (build_component_ref): Add one more parameter.
>>* c-typeck.cc (build_counted_by_ref): New function.
>>(build_access_with_size_for_counted_by): New function.
>>(build_component_ref): Check the counted-by attribute and build
>>call to .ACCESS_WITH_SIZE.
>>(build_unary_op): When building ADDR_EXPR for
>>.ACCESS_WITH_SIZE, use its first argument.
>>(lvalue_p): Accept call to .ACCESS_WITH_SIZE.
>> 
>> gcc/ChangeLog:
>> 
>>* internal-fn.cc (expand_ACCESS_WITH_SIZE): New function.
>>* internal-fn.def (ACCESS_WITH_SIZE): New internal function.
>>* tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case
>>IFN_ACCESS_WITH_SIZE.
>>(call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE.
>>* tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call
>>to .ACCESS_WITH_SIZE when its LHS is dead.
>>* tree.cc (process_call_operands): Adjust side effect for function
>>.ACCESS_WITH_SIZE.
>>(is_access_with_size_p): New function.
>>(get_ref_from_access_with_size): New function.
>>* tree.h (is_access_with_size_p): New prototype.
>>(get_ref_from_access_with_size): New prototype.
>>* varasm.cc (initializer_constant_valid_p_1): Handle call to
>>.ACCESS_WITH_SIZE.
>>(output_constant): Handle call to .ACCESS_WITH_SIZE.
>> 
>> gcc/testsuite/ChangeLog:
>> 
>>* gcc.dg/flex-array-counted-by-2.c: New test.
>> ---
>> gcc/c/c-parser.cc |  10 +-
>> gcc/c/c-tree.h|   2 +-
>> gcc/c/c-typeck.cc | 128 +-
>> gcc/internal-fn.cc|  35 +
>> gcc/internal-fn.def   |   4 +
>> .../gcc.dg/flex-array-counted-by-2.c  | 112 +++
>> gcc/tree-ssa-alias.cc |   2 +
>> gcc/tree-ssa-dce.cc   |   5 +-
>> gcc/tree.cc   |  25 +++-
>> gcc/tree.h|   8 ++
>> gcc/varasm.cc |  10 ++
>> 11 files changed, 331 insertions(+), 10 deletions(-)
>> create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c
>> 
>> diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
>> index c31349dae2ff..a6ed5ac43bb1 100644
>> --- a/gcc/c/c-parser.cc
>> +++ b/gcc/c/c-parser.cc
>> @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser)
>>if (c_parser_next_token_is (parser, CPP_NAME))
>>  {
>>c_token *comp_tok = c_parser_peek_token (parser);
>> +   /* Ignore the counted_by attribute for reference inside
>> +  offsetof since the 

Re: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-05-28 Thread Richard Biener
On Fri, Apr 12, 2024 at 3:54 PM Qing Zhao  wrote:
>
> Including the following changes:
> * The definition of the new internal function .ACCESS_WITH_SIZE
>   in internal-fn.def.
> * C FE converts every reference to a FAM with a "counted_by" attribute
>   to a call to the internal function .ACCESS_WITH_SIZE.
>   (build_component_ref in c_typeck.cc)
>
>   This includes the case when the object is statically allocated and
>   initialized.
>   In order to make this working, the routines initializer_constant_valid_p_1
>   and output_constant in varasm.cc are updated to handle calls to
>   .ACCESS_WITH_SIZE.
>   (initializer_constant_valid_p_1 and output_constant in varasm.c)
>
>   However, for the reference inside "offsetof", the "counted_by" attribute is
>   ignored since it's not useful at all.
>   (c_parser_postfix_expression in c/c-parser.cc)
>
>   In addtion to "offsetof", for the reference inside operator "typeof" and
>   "alignof", we ignore counted_by attribute too.
>
>   When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE,
>   replace the call with its first argument.
>
> * Convert every call to .ACCESS_WITH_SIZE to its first argument.
>   (expand_ACCESS_WITH_SIZE in internal-fn.cc)
> * Adjust alias analysis to exclude the new internal from clobbering anything.
>   (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in 
> tree-ssa-alias.cc)
> * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE when
>   it's LHS is eliminated as dead code.
>   (eliminate_unnecessary_stmts in tree-ssa-dce.cc)
> * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and
>   get the reference from the call to .ACCESS_WITH_SIZE.
>   (is_access_with_size_p and get_ref_from_access_with_size in tree.cc)
>
> gcc/c/ChangeLog:
>
> * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by
> attribute when build_component_ref inside offsetof operator.
> * c-tree.h (build_component_ref): Add one more parameter.
> * c-typeck.cc (build_counted_by_ref): New function.
> (build_access_with_size_for_counted_by): New function.
> (build_component_ref): Check the counted-by attribute and build
> call to .ACCESS_WITH_SIZE.
> (build_unary_op): When building ADDR_EXPR for
> .ACCESS_WITH_SIZE, use its first argument.
> (lvalue_p): Accept call to .ACCESS_WITH_SIZE.
>
> gcc/ChangeLog:
>
> * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function.
> * internal-fn.def (ACCESS_WITH_SIZE): New internal function.
> * tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case
> IFN_ACCESS_WITH_SIZE.
> (call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE.
> * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call
> to .ACCESS_WITH_SIZE when its LHS is dead.
> * tree.cc (process_call_operands): Adjust side effect for function
> .ACCESS_WITH_SIZE.
> (is_access_with_size_p): New function.
> (get_ref_from_access_with_size): New function.
> * tree.h (is_access_with_size_p): New prototype.
> (get_ref_from_access_with_size): New prototype.
> * varasm.cc (initializer_constant_valid_p_1): Handle call to
> .ACCESS_WITH_SIZE.
> (output_constant): Handle call to .ACCESS_WITH_SIZE.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/flex-array-counted-by-2.c: New test.
> ---
>  gcc/c/c-parser.cc |  10 +-
>  gcc/c/c-tree.h|   2 +-
>  gcc/c/c-typeck.cc | 128 +-
>  gcc/internal-fn.cc|  35 +
>  gcc/internal-fn.def   |   4 +
>  .../gcc.dg/flex-array-counted-by-2.c  | 112 +++
>  gcc/tree-ssa-alias.cc |   2 +
>  gcc/tree-ssa-dce.cc   |   5 +-
>  gcc/tree.cc   |  25 +++-
>  gcc/tree.h|   8 ++
>  gcc/varasm.cc |  10 ++
>  11 files changed, 331 insertions(+), 10 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c
>
> diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
> index c31349dae2ff..a6ed5ac43bb1 100644
> --- a/gcc/c/c-parser.cc
> +++ b/gcc/c/c-parser.cc
> @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser)
> if (c_parser_next_token_is (parser, CPP_NAME))
>   {
> c_token *comp_tok = c_parser_peek_token (parser);
> +   /* Ignore the counted_by attribute for reference inside
> +  offsetof since the information is not useful at all.  */
> offsetof_ref
>   = build_component_ref (loc, offsetof_ref, comp_tok->value,
> -comp_tok->location, 
> UNKNOWN_LOCATION);
> + 

[PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE.

2024-04-12 Thread Qing Zhao
Including the following changes:
* The definition of the new internal function .ACCESS_WITH_SIZE
  in internal-fn.def.
* C FE converts every reference to a FAM with a "counted_by" attribute
  to a call to the internal function .ACCESS_WITH_SIZE.
  (build_component_ref in c_typeck.cc)

  This includes the case when the object is statically allocated and
  initialized.
  In order to make this working, the routines initializer_constant_valid_p_1
  and output_constant in varasm.cc are updated to handle calls to
  .ACCESS_WITH_SIZE.
  (initializer_constant_valid_p_1 and output_constant in varasm.c)

  However, for the reference inside "offsetof", the "counted_by" attribute is
  ignored since it's not useful at all.
  (c_parser_postfix_expression in c/c-parser.cc)

  In addtion to "offsetof", for the reference inside operator "typeof" and
  "alignof", we ignore counted_by attribute too.

  When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE,
  replace the call with its first argument.

* Convert every call to .ACCESS_WITH_SIZE to its first argument.
  (expand_ACCESS_WITH_SIZE in internal-fn.cc)
* Adjust alias analysis to exclude the new internal from clobbering anything.
  (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in tree-ssa-alias.cc)
* Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE when
  it's LHS is eliminated as dead code.
  (eliminate_unnecessary_stmts in tree-ssa-dce.cc)
* Provide the utility routines to check the call is .ACCESS_WITH_SIZE and
  get the reference from the call to .ACCESS_WITH_SIZE.
  (is_access_with_size_p and get_ref_from_access_with_size in tree.cc)

gcc/c/ChangeLog:

* c-parser.cc (c_parser_postfix_expression): Ignore the counted-by
attribute when build_component_ref inside offsetof operator.
* c-tree.h (build_component_ref): Add one more parameter.
* c-typeck.cc (build_counted_by_ref): New function.
(build_access_with_size_for_counted_by): New function.
(build_component_ref): Check the counted-by attribute and build
call to .ACCESS_WITH_SIZE.
(build_unary_op): When building ADDR_EXPR for
.ACCESS_WITH_SIZE, use its first argument.
(lvalue_p): Accept call to .ACCESS_WITH_SIZE.

gcc/ChangeLog:

* internal-fn.cc (expand_ACCESS_WITH_SIZE): New function.
* internal-fn.def (ACCESS_WITH_SIZE): New internal function.
* tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case
IFN_ACCESS_WITH_SIZE.
(call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE.
* tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call
to .ACCESS_WITH_SIZE when its LHS is dead.
* tree.cc (process_call_operands): Adjust side effect for function
.ACCESS_WITH_SIZE.
(is_access_with_size_p): New function.
(get_ref_from_access_with_size): New function.
* tree.h (is_access_with_size_p): New prototype.
(get_ref_from_access_with_size): New prototype.
* varasm.cc (initializer_constant_valid_p_1): Handle call to
.ACCESS_WITH_SIZE.
(output_constant): Handle call to .ACCESS_WITH_SIZE.

gcc/testsuite/ChangeLog:

* gcc.dg/flex-array-counted-by-2.c: New test.
---
 gcc/c/c-parser.cc |  10 +-
 gcc/c/c-tree.h|   2 +-
 gcc/c/c-typeck.cc | 128 +-
 gcc/internal-fn.cc|  35 +
 gcc/internal-fn.def   |   4 +
 .../gcc.dg/flex-array-counted-by-2.c  | 112 +++
 gcc/tree-ssa-alias.cc |   2 +
 gcc/tree-ssa-dce.cc   |   5 +-
 gcc/tree.cc   |  25 +++-
 gcc/tree.h|   8 ++
 gcc/varasm.cc |  10 ++
 11 files changed, 331 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index c31349dae2ff..a6ed5ac43bb1 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser)
if (c_parser_next_token_is (parser, CPP_NAME))
  {
c_token *comp_tok = c_parser_peek_token (parser);
+   /* Ignore the counted_by attribute for reference inside
+  offsetof since the information is not useful at all.  */
offsetof_ref
  = build_component_ref (loc, offsetof_ref, comp_tok->value,
-comp_tok->location, UNKNOWN_LOCATION);
+comp_tok->location, UNKNOWN_LOCATION,
+false);
c_parser_consume_token (parser);
while (c_parser_next_token_is (parser, CPP_DOT)
   ||