public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/49673 (constexpr init should go in rodata)
@ 2011-07-08 14:27 Jason Merrill
  2011-07-08 14:53 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2011-07-08 14:27 UTC (permalink / raw)
  To: gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 271 bytes --]

Now that we have constexpr constructors, having a non-trivial 
constructor no longer precludes a variable being TREE_READONLY.  The 
front end will clear TREE_READONLY if the variable requires non-constant 
initialization.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: 49673.patch --]
[-- Type: text/x-patch, Size: 2940 bytes --]

commit bd0343de0277ae5d66f60a42d6479df0161fc075
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jul 8 09:32:45 2011 -0400

    	PR c++/49673
    gcc/c-family/
    	* c-common.c (c_apply_type_quals_to_decl): Don't check
    	TYPE_NEEDS_CONSTRUCTING.
    gcc/cp/
    	* typeck.c (cp_apply_type_quals_to_decl): Don't check
    	TYPE_NEEDS_CONSTRUCTING.

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 67291de..f61b9cc 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -4058,14 +4058,11 @@ c_apply_type_quals_to_decl (int type_quals, tree decl)
   if (type == error_mark_node)
     return;
 
-  if (((type_quals & TYPE_QUAL_CONST)
-       || (type && TREE_CODE (type) == REFERENCE_TYPE))
-      /* An object declared 'const' is only readonly after it is
-	 initialized.  We don't have any way of expressing this currently,
-	 so we need to be conservative and unset TREE_READONLY for types
-	 with constructors.  Otherwise aliasing code will ignore stores in
-	 an inline constructor.  */
-      && !(type && TYPE_NEEDS_CONSTRUCTING (type)))
+  if ((type_quals & TYPE_QUAL_CONST)
+      || (type && TREE_CODE (type) == REFERENCE_TYPE))
+    /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
+       constructor can produce constant init, so rely on the front end to
+       clear TREE_READONLY if the variable has non-constant init.  */
     TREE_READONLY (decl) = 1;
   if (type_quals & TYPE_QUAL_VOLATILE)
     {
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 2acb18e..f0d68c3 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -8127,12 +8127,12 @@ cp_apply_type_quals_to_decl (int type_quals, tree decl)
 		&& type_quals != TYPE_UNQUALIFIED));
 
   /* Avoid setting TREE_READONLY incorrectly.  */
-  if (/* If the object has a constructor, the constructor may modify
-	 the object.  */
-      TYPE_NEEDS_CONSTRUCTING (type)
-      /* If the type isn't complete, we don't know yet if it will need
+  /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
+     constructor can produce constant init, so rely on cp_finish_decl to
+     clear TREE_READONLY if the variable has non-constant init.  */
+  if (/* If the type isn't complete, we don't know yet if it will need
 	 constructing.  */
-      || !COMPLETE_TYPE_P (type)
+      !COMPLETE_TYPE_P (type)
       /* If the type has a mutable component, that component might be
 	 modified.  */
       || TYPE_HAS_MUTABLE_P (type))
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
new file mode 100644
index 0000000..e2edb2e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
@@ -0,0 +1,11 @@
+// PR c++/49673: check that test_data goes into .rodata
+// { dg-options -std=c++0x }
+// { dg-final { scan-assembler "rodata" } }
+
+struct Data
+{
+  int i;
+  constexpr Data(int i = 0) : i(i+1) {}
+};
+
+extern const Data test_data = { 1 };

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

* Re: C++ PATCH for c++/49673 (constexpr init should go in rodata)
  2011-07-08 14:27 C++ PATCH for c++/49673 (constexpr init should go in rodata) Jason Merrill
@ 2011-07-08 14:53 ` Jakub Jelinek
  2011-07-08 18:01   ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2011-07-08 14:53 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches List

On Fri, Jul 08, 2011 at 10:22:16AM -0400, Jason Merrill wrote:
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
> @@ -0,0 +1,11 @@
> +// PR c++/49673: check that test_data goes into .rodata
> +// { dg-options -std=c++0x }
> +// { dg-final { scan-assembler "rodata" } }

That will match only on ELF and perhaps a few other targets, but I'm certain
many targets put read-only data elsewhere.  I'd just guard this
with a few most common target triplets that are known to use .rodata
section.

> +
> +struct Data
> +{
> +  int i;
> +  constexpr Data(int i = 0) : i(i+1) {}
> +};
> +
> +extern const Data test_data = { 1 };

	Jakub

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

* Re: C++ PATCH for c++/49673 (constexpr init should go in rodata)
  2011-07-08 14:53 ` Jakub Jelinek
@ 2011-07-08 18:01   ` Jason Merrill
  2012-07-19 14:55     ` H.J. Lu
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2011-07-08 18:01 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 394 bytes --]

On 07/08/2011 10:35 AM, Jakub Jelinek wrote:
> That will match only on ELF and perhaps a few other targets, but I'm certain
> many targets put read-only data elsewhere.  I'd just guard this
> with a few most common target triplets that are known to use .rodata
> section.

Done, thanks.  I've also removed the unneeded check for COMPLETE_TYPE_P.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: 49673-3.patch --]
[-- Type: text/x-patch, Size: 754 bytes --]

commit e24d93b0a1e1df42c4f1197515e7e2fbe211a0cb
Author: jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Fri Jul 8 16:46:28 2011 +0000

    constexpr-rom.C tweak
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@176049 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
index e2edb2e..144be2d 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-rom.C
@@ -1,6 +1,6 @@
 // PR c++/49673: check that test_data goes into .rodata
 // { dg-options -std=c++0x }
-// { dg-final { scan-assembler "rodata" } }
+// { dg-final { scan-assembler "rodata" { target { *-*-linux-gnu || *-*-elf } } } }
 
 struct Data
 {

[-- Attachment #3: 49673-2.patch --]
[-- Type: text/x-patch, Size: 1095 bytes --]

commit e2f7f86c6d5e734fe393217eaca8d4da9969f343
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jul 8 10:27:55 2011 -0400

    	* typeck.c (cp_apply_type_quals_to_decl): Don't check
    	COMPLETE_TYPE_P either.

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index f0d68c3..5febff5 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -8130,12 +8130,10 @@ cp_apply_type_quals_to_decl (int type_quals, tree decl)
   /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
      constructor can produce constant init, so rely on cp_finish_decl to
      clear TREE_READONLY if the variable has non-constant init.  */
-  if (/* If the type isn't complete, we don't know yet if it will need
-	 constructing.  */
-      !COMPLETE_TYPE_P (type)
-      /* If the type has a mutable component, that component might be
-	 modified.  */
-      || TYPE_HAS_MUTABLE_P (type))
+
+  /* If the type has a mutable component, that component might be
+     modified.  */
+  if (TYPE_HAS_MUTABLE_P (type))
     type_quals &= ~TYPE_QUAL_CONST;
 
   c_apply_type_quals_to_decl (type_quals, decl);

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

* Re: C++ PATCH for c++/49673 (constexpr init should go in rodata)
  2011-07-08 18:01   ` Jason Merrill
@ 2012-07-19 14:55     ` H.J. Lu
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2012-07-19 14:55 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Jakub Jelinek, gcc-patches List

On Fri, Jul 8, 2011 at 10:37 AM, Jason Merrill <jason@redhat.com> wrote:
> On 07/08/2011 10:35 AM, Jakub Jelinek wrote:
>>
>> That will match only on ELF and perhaps a few other targets, but I'm
>> certain
>> many targets put read-only data elsewhere.  I'd just guard this
>> with a few most common target triplets that are known to use .rodata
>> section.
>
>
> Done, thanks.  I've also removed the unneeded check for COMPLETE_TYPE_P.
>
>
> Tested x86_64-pc-linux-gnu, applying to trunk.

This caused:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54026

-- 
H.J.

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

end of thread, other threads:[~2012-07-19 14:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-08 14:27 C++ PATCH for c++/49673 (constexpr init should go in rodata) Jason Merrill
2011-07-08 14:53 ` Jakub Jelinek
2011-07-08 18:01   ` Jason Merrill
2012-07-19 14:55     ` H.J. Lu

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