public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
       [not found] <5a0dbb28.4b279d0a.4c617.abaeSMTPIN_ADDED_MISSING@mx.google.com>
@ 2017-11-17 13:58 ` Jason Merrill
  2017-11-18  4:23   ` Nathan Froyd
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2017-11-17 13:58 UTC (permalink / raw)
  To: Nathan Froyd; +Cc: gcc-patches List

On Thu, Nov 16, 2017 at 11:21 AM, Nathan Froyd <froydnj@mozilla.com> wrote:
> Default-initialization of scalar arrays in C++ member initialization
> lists produced rather slow code, laboriously setting each element of the
> array to zero.  It would be much faster to block-initialize the array,
> and that's what this patch does.
>
> The patch works for me, but I'm not sure if it's the best way to
> accomplish this.  At least two other possibilities come to mind:
>
> 1) Detect this case in build_vec_init_expr and act as though the user
>    wrote 'member{0}', which the front-end already produces efficient
>    code for.
>
> 2) Detect this case in build_vec_init, but again, act as though the user
>    wrote 'member{0}' and let everything proceed as normal.
>    (Alternatively, handle this case prior to calling build_vec_init and
>    pass different arguments to build_vec_init.)
>
> Opinions as to the best way forward here?  I'm unsure of whether the
> code below is front-end friendly; I see in the gimple dumps that the
> solution below adds an extra CLOBBER on 'this' for 'member()', whereas
> 'member{0}' does not.  It's possible that I'm missing something.
>
> Bootstrapped on x86_64-unknown-linux-gnu, no regressions.
>
> OK for trunk?
>
> -Nathan
>
> gcc/cp/
>         PR c++/82888
>         * init.c (build_vec_init): Handle default-initialization of array
>         types.
>
> gcc/testsuite/
>         PR c++/82888
>         * g++.dg/init/pr82888.C: New.
>
> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
> index c76460d..53d6133 100644
> --- a/gcc/cp/init.c
> +++ b/gcc/cp/init.c
> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>         }
>      }
>
> +  /* Default-initialize scalar arrays directly.  */
> +  if (TREE_CODE (atype) == ARRAY_TYPE
> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
> +      && !init)

This should check explicit_value_init._p rather than !init.  And also
check zero_init_p.

> +    {
> +      gcc_assert (!from_array);
> +      return build2 (MODIFY_EXPR, atype, base, build_constructor (atype, NULL));

And this should use INIT_EXPR.

Jason

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
  2017-11-17 13:58 ` [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays Jason Merrill
@ 2017-11-18  4:23   ` Nathan Froyd
  2017-11-29 21:59     ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Nathan Froyd @ 2017-11-18  4:23 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches List

On Fri, Nov 17, 2017 at 8:50 AM, Jason Merrill <jason@redhat.com> wrote:
> On Thu, Nov 16, 2017 at 11:21 AM, Nathan Froyd <froydnj@mozilla.com> wrote:
>> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
>> index c76460d..53d6133 100644
>> --- a/gcc/cp/init.c
>> +++ b/gcc/cp/init.c
>> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>>         }
>>      }
>>
>> +  /* Default-initialize scalar arrays directly.  */
>> +  if (TREE_CODE (atype) == ARRAY_TYPE
>> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
>> +      && !init)
>
> This should check explicit_value_init._p rather than !init.  And also
> check zero_init_p.

Do you mean explicit_value_init_p && zero_init_p (atype)?  zero_init_p
doesn't sound like the correct thing to use here, because it doesn't
take into account whether a class array type has a constructor.  I am
probably misunderstanding the purpose of the zero_init_p check,
though.

-Nathan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
  2017-11-18  4:23   ` Nathan Froyd
@ 2017-11-29 21:59     ` Jason Merrill
  2017-12-21 18:52       ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2017-11-29 21:59 UTC (permalink / raw)
  To: Nathan Froyd; +Cc: gcc-patches List

On Fri, Nov 17, 2017 at 11:04 PM, Nathan Froyd <nfroyd@mozilla.com> wrote:
> On Fri, Nov 17, 2017 at 8:50 AM, Jason Merrill <jason@redhat.com> wrote:
>> On Thu, Nov 16, 2017 at 11:21 AM, Nathan Froyd <froydnj@mozilla.com> wrote:
>>> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
>>> index c76460d..53d6133 100644
>>> --- a/gcc/cp/init.c
>>> +++ b/gcc/cp/init.c
>>> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>>>         }
>>>      }
>>>
>>> +  /* Default-initialize scalar arrays directly.  */
>>> +  if (TREE_CODE (atype) == ARRAY_TYPE
>>> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
>>> +      && !init)
>>
>> This should check explicit_value_init._p rather than !init.  And also
>> check zero_init_p.
>
> Do you mean explicit_value_init_p && zero_init_p (atype)?

Yes.

> zero_init_p
> doesn't sound like the correct thing to use here, because it doesn't
> take into account whether a class array type has a constructor.  I am
> probably misunderstanding the purpose of the zero_init_p check,
> though.

Since you're already checking for scalar type, we don't need to worry
about classes.  The zero_init_p check is to handle pointers to data
members properly.

Jason

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
  2017-11-29 21:59     ` Jason Merrill
@ 2017-12-21 18:52       ` Jason Merrill
  2018-02-05 20:30         ` Jason Merrill
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Merrill @ 2017-12-21 18:52 UTC (permalink / raw)
  To: Nathan Froyd; +Cc: gcc-patches List

On Wed, Nov 29, 2017 at 4:30 PM, Jason Merrill <jason@redhat.com> wrote:
> On Fri, Nov 17, 2017 at 11:04 PM, Nathan Froyd <nfroyd@mozilla.com> wrote:
>> On Fri, Nov 17, 2017 at 8:50 AM, Jason Merrill <jason@redhat.com> wrote:
>>> On Thu, Nov 16, 2017 at 11:21 AM, Nathan Froyd <froydnj@mozilla.com> wrote:
>>>> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
>>>> index c76460d..53d6133 100644
>>>> --- a/gcc/cp/init.c
>>>> +++ b/gcc/cp/init.c
>>>> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>>>>         }
>>>>      }
>>>>
>>>> +  /* Default-initialize scalar arrays directly.  */
>>>> +  if (TREE_CODE (atype) == ARRAY_TYPE
>>>> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
>>>> +      && !init)
>>>
>>> This should check explicit_value_init._p rather than !init.  And also
>>> check zero_init_p.
>>
>> Do you mean explicit_value_init_p && zero_init_p (atype)?
>
> Yes.
>
>> zero_init_p
>> doesn't sound like the correct thing to use here, because it doesn't
>> take into account whether a class array type has a constructor.  I am
>> probably misunderstanding the purpose of the zero_init_p check,
>> though.
>
> Since you're already checking for scalar type, we don't need to worry
> about classes.  The zero_init_p check is to handle pointers to data
> members properly.

Any update?

Jason

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
  2017-12-21 18:52       ` Jason Merrill
@ 2018-02-05 20:30         ` Jason Merrill
  0 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2018-02-05 20:30 UTC (permalink / raw)
  To: Nathan Froyd; +Cc: gcc-patches List

On Thu, Dec 21, 2017 at 1:51 PM, Jason Merrill <jason@redhat.com> wrote:
> On Wed, Nov 29, 2017 at 4:30 PM, Jason Merrill <jason@redhat.com> wrote:
>> On Fri, Nov 17, 2017 at 11:04 PM, Nathan Froyd <nfroyd@mozilla.com> wrote:
>>> On Fri, Nov 17, 2017 at 8:50 AM, Jason Merrill <jason@redhat.com> wrote:
>>>> On Thu, Nov 16, 2017 at 11:21 AM, Nathan Froyd <froydnj@mozilla.com> wrote:
>>>>> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
>>>>> index c76460d..53d6133 100644
>>>>> --- a/gcc/cp/init.c
>>>>> +++ b/gcc/cp/init.c
>>>>> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>>>>>         }
>>>>>      }
>>>>>
>>>>> +  /* Default-initialize scalar arrays directly.  */
>>>>> +  if (TREE_CODE (atype) == ARRAY_TYPE
>>>>> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
>>>>> +      && !init)
>>>>
>>>> This should check explicit_value_init._p rather than !init.  And also
>>>> check zero_init_p.
>>>
>>> Do you mean explicit_value_init_p && zero_init_p (atype)?
>>
>> Yes.
>>
>>> zero_init_p
>>> doesn't sound like the correct thing to use here, because it doesn't
>>> take into account whether a class array type has a constructor.  I am
>>> probably misunderstanding the purpose of the zero_init_p check,
>>> though.
>>
>> Since you're already checking for scalar type, we don't need to worry
>> about classes.  The zero_init_p check is to handle pointers to data
>> members properly.
>
> Any update?

?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays
       [not found] <5a0dbb27.056e630a.40468.8808SMTPIN_ADDED_MISSING@mx.google.com>
@ 2017-11-17 12:24 ` Richard Biener
  0 siblings, 0 replies; 7+ messages in thread
From: Richard Biener @ 2017-11-17 12:24 UTC (permalink / raw)
  To: Nathan Froyd; +Cc: GCC Patches

On Thu, Nov 16, 2017 at 5:21 PM, Nathan Froyd <froydnj@mozilla.com> wrote:
> Default-initialization of scalar arrays in C++ member initialization
> lists produced rather slow code, laboriously setting each element of the
> array to zero.  It would be much faster to block-initialize the array,
> and that's what this patch does.
>
> The patch works for me, but I'm not sure if it's the best way to
> accomplish this.  At least two other possibilities come to mind:
>
> 1) Detect this case in build_vec_init_expr and act as though the user
>    wrote 'member{0}', which the front-end already produces efficient
>    code for.
>
> 2) Detect this case in build_vec_init, but again, act as though the user
>    wrote 'member{0}' and let everything proceed as normal.
>    (Alternatively, handle this case prior to calling build_vec_init and
>    pass different arguments to build_vec_init.)
>
> Opinions as to the best way forward here?  I'm unsure of whether the
> code below is front-end friendly; I see in the gimple dumps that the
> solution below adds an extra CLOBBER on 'this' for 'member()', whereas
> 'member{0}' does not.  It's possible that I'm missing something.
>
> Bootstrapped on x86_64-unknown-linux-gnu, no regressions.
>
> OK for trunk?

Ok. This is certainly the form the middle-end likes most.

Richard.

> -Nathan
>
> gcc/cp/
>         PR c++/82888
>         * init.c (build_vec_init): Handle default-initialization of array
>         types.
>
> gcc/testsuite/
>         PR c++/82888
>         * g++.dg/init/pr82888.C: New.
>
> diff --git a/gcc/cp/init.c b/gcc/cp/init.c
> index c76460d..53d6133 100644
> --- a/gcc/cp/init.c
> +++ b/gcc/cp/init.c
> @@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
>         }
>      }
>
> +  /* Default-initialize scalar arrays directly.  */
> +  if (TREE_CODE (atype) == ARRAY_TYPE
> +      && SCALAR_TYPE_P (TREE_TYPE (atype))
> +      && !init)
> +    {
> +      gcc_assert (!from_array);
> +      return build2 (MODIFY_EXPR, atype, base, build_constructor (atype, NULL));
> +    }
> +
>    /* If we have a braced-init-list or string constant, make sure that the array
>       is big enough for all the initializers.  */
>    bool length_check = (init
> diff --git a/gcc/testsuite/g++.dg/init/pr82888.C b/gcc/testsuite/g++.dg/init/pr82888.C
> new file mode 100644
> index 0000000..9225e23
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/init/pr82888.C
> @@ -0,0 +1,18 @@
> +// { dg-do compile }
> +// { dg-options "-fdump-tree-gimple" }
> +
> +class A
> +{
> +public:
> +  A();
> +
> +private:
> +  unsigned char mStorage[4096];
> +};
> +
> +A::A()
> +  : mStorage()
> +{}
> +
> +// { dg-final { scan-tree-dump "this->mStorage = {}" "gimple" } }
> +// { dg-final { scan-tree-dump-not "&this->mStorage" "gimple" } }

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH][PR c++/82888] smarter code for default initialization   of scalar arrays
@ 2017-11-16 16:32 Nathan Froyd
  0 siblings, 0 replies; 7+ messages in thread
From: Nathan Froyd @ 2017-11-16 16:32 UTC (permalink / raw)
  To: gcc-patches

Default-initialization of scalar arrays in C++ member initialization
lists produced rather slow code, laboriously setting each element of the
array to zero.  It would be much faster to block-initialize the array,
and that's what this patch does.

The patch works for me, but I'm not sure if it's the best way to
accomplish this.  At least two other possibilities come to mind:

1) Detect this case in build_vec_init_expr and act as though the user
   wrote 'member{0}', which the front-end already produces efficient
   code for.

2) Detect this case in build_vec_init, but again, act as though the user
   wrote 'member{0}' and let everything proceed as normal.
   (Alternatively, handle this case prior to calling build_vec_init and
   pass different arguments to build_vec_init.)

Opinions as to the best way forward here?  I'm unsure of whether the
code below is front-end friendly; I see in the gimple dumps that the
solution below adds an extra CLOBBER on 'this' for 'member()', whereas
'member{0}' does not.  It's possible that I'm missing something.

Bootstrapped on x86_64-unknown-linux-gnu, no regressions.

OK for trunk?

-Nathan

gcc/cp/
	PR c++/82888
	* init.c (build_vec_init): Handle default-initialization of array
	types.

gcc/testsuite/
	PR c++/82888
	* g++.dg/init/pr82888.C: New.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index c76460d..53d6133 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -4038,6 +4038,15 @@ build_vec_init (tree base, tree maxindex, tree init,
 	}
     }
 
+  /* Default-initialize scalar arrays directly.  */
+  if (TREE_CODE (atype) == ARRAY_TYPE
+      && SCALAR_TYPE_P (TREE_TYPE (atype))
+      && !init)
+    {
+      gcc_assert (!from_array);
+      return build2 (MODIFY_EXPR, atype, base, build_constructor (atype, NULL));
+    }
+
   /* If we have a braced-init-list or string constant, make sure that the array
      is big enough for all the initializers.  */
   bool length_check = (init
diff --git a/gcc/testsuite/g++.dg/init/pr82888.C b/gcc/testsuite/g++.dg/init/pr82888.C
new file mode 100644
index 0000000..9225e23
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/pr82888.C
@@ -0,0 +1,18 @@
+// { dg-do compile }
+// { dg-options "-fdump-tree-gimple" }
+
+class A
+{
+public:
+  A();
+
+private:
+  unsigned char mStorage[4096];
+};
+
+A::A()
+  : mStorage()
+{}
+
+// { dg-final { scan-tree-dump "this->mStorage = {}" "gimple" } }
+// { dg-final { scan-tree-dump-not "&this->mStorage" "gimple" } }

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-02-05 20:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <5a0dbb28.4b279d0a.4c617.abaeSMTPIN_ADDED_MISSING@mx.google.com>
2017-11-17 13:58 ` [PATCH][PR c++/82888] smarter code for default initialization of scalar arrays Jason Merrill
2017-11-18  4:23   ` Nathan Froyd
2017-11-29 21:59     ` Jason Merrill
2017-12-21 18:52       ` Jason Merrill
2018-02-05 20:30         ` Jason Merrill
     [not found] <5a0dbb27.056e630a.40468.8808SMTPIN_ADDED_MISSING@mx.google.com>
2017-11-17 12:24 ` Richard Biener
2017-11-16 16:32 Nathan Froyd

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).