public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts
@ 2005-03-04 17:05 gary at intrepid dot com
  2005-03-04 18:29 ` [Bug c/20319] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gary at intrepid dot com @ 2005-03-04 17:05 UTC (permalink / raw)
  To: gcc-bugs

Related discussion:
http://gcc.gnu.org/ml/gcc/2005-03/msg00181.html

Given the following,

static char const rcsid[] =
"$Id: f.c,v 5.4 1993/11/09 17:40:15 eggert Exp $";
int main() {}

When compiled with GCC 3.4.3, at -O2, the ident string above will _not_
appear in the executable.  This is apparently expected behavior.

However,
  gcc -fkeep-static-consts -O2 t.c
did not retain the ident string, rcsid, defined above.  Shouldn't
-fkepp-static-consts have ensured that this static constant would appear
in the executable?

The logic in wrapup_global_declarations (toplev.c) doesn't look quite right:

              else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
                       && (optimize || !flag_keep_static_consts 
                           || DECL_ARTIFICIAL (decl))) 
                needed = 0; 

If 'optimize' is asserted above then flag_keep_static_consts will
not be tested.  Perhaps it should read as follows?

                       && ((optimize && !flag_keep_static_consts)

Alternatively, I wonder if flag_keep_static_consts should be tested earlier at a
higher level, for example:

   if (flag_keep_static_consts)
      /* needed */;

but I'm not sure about which of the earlier tests which assert needed = 0;
are mandatory and which are optional.

-- 
           Summary: -fkeep-static-consts with -O asserted doesn't keep
                    consts
           Product: gcc
           Version: 3.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gary at intrepid dot com
                CC: gcc-bugs at gcc dot gnu dot org


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
@ 2005-03-04 18:29 ` pinskia at gcc dot gnu dot org
  2005-03-04 20:24 ` gary at intrepid dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-04 18:29 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-03-04 18:29 -------
Nope, -fkeep-static-consts only control when optimization is not on:
>From the docs:
Emit variables declared @code{static const} when optimization ____isn't____ turned
on, even if the variables aren't referenced.

Enphise mine.

Use the "used" attribute if you want to keep unused constants.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
  2005-03-04 18:29 ` [Bug c/20319] " pinskia at gcc dot gnu dot org
@ 2005-03-04 20:24 ` gary at intrepid dot com
  2005-03-04 20:33 ` pinskia at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gary at intrepid dot com @ 2005-03-04 20:24 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gary at intrepid dot com  2005-03-04 20:24 -------
"Emit variables declared @code{static const} when optimization isn't turned 
on, even if the variables aren't referenced."

How odd.  I should've checked the docs, but this brief explanation in the
help line made a lot of sense to me.

  -fkeep-static-consts        Emit static const variables even if they are not
                              used

Also, since the constant *was kept* when I compiled _without_ optimization,
the idea of passing a switch telling the compiler to do something that
it seemed to already be doing by default didn't make sense to me.

The comments inside toplev.c say the following:

/* Nonzero means that we should emit static const variables
   regardless of whether or not optimization is turned on.  */ 
                            
int flag_keep_static_consts = 1;


which makes sense to me.  Note that this is the default setting.

So ... coming back to the if statement in toplev.c:

              else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
                       && (optimize || !flag_keep_static_consts
                           || DECL_ARTIFICIAL (decl)))
                needed = 0;

At present,

If optimize is set, the unused static constant will always be eliminated
and -fkeep-static-consts will have no effect, and there is no way to override
this behavior (except for __attribute__ ((used))). 

This agrees with the documentation, but disagrees with the
comment in the code, and with the spirit of the help message.  It also
doesn't make sense to me that -fkeep-static-consts would be ignored. In
fact, it seems to me that -fkeep-static-consts should override the
optimization setting.  Otherwise, only -fno-keep-static-consts has
any effect and that is only when optimization is not enabled.

I think the behavior of -fkeep-static-consts should be modified so that
setting this switch overrides the optimization level.  Thus, the
documentation would need to be changed as well.  Otherwise, the internal
comments and the help line are wrong.

Regarding the "used" attibute, I thought that I'd tried that, and it
didn't work.  But upon retesting, it does seem to have the desired
effect of keeping the static const around (maybe I specified the "unused"
attribute by mistake in my test).

Note that the "used" attribute is described only under "function attributes"
and not under "variable attributes" in the documentation:

used
This attribute, attached to a function, means that code must be emitted for 
the function even if it appears that the function is not referenced. This is 
useful, for example, when the function is referenced only in inline assembly. 

(see: http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/Function-
Attributes.html#Function-Attributes)

It would be helpful if the documentation were updated to describe the
behavior of the `used' attribute when apllied to variables.


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
  2005-03-04 18:29 ` [Bug c/20319] " pinskia at gcc dot gnu dot org
  2005-03-04 20:24 ` gary at intrepid dot com
@ 2005-03-04 20:33 ` pinskia at gcc dot gnu dot org
  2005-03-04 20:42 ` joseph at codesourcery dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-04 20:33 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-03-04 20:33 -------
Don't belive the comments in the source.  Also the --help is way out of date. you want to use the used 
attribute like so:
static char const rcsid[]__attribute__((used)) =
"$Id: f.c,v 5.4 1993/11/09 17:40:15 eggert Exp $";

And this works for me.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
                   ` (2 preceding siblings ...)
  2005-03-04 20:33 ` pinskia at gcc dot gnu dot org
@ 2005-03-04 20:42 ` joseph at codesourcery dot com
  2005-03-04 21:54 ` gary at intrepid dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: joseph at codesourcery dot com @ 2005-03-04 20:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From joseph at codesourcery dot com  2005-03-04 20:42 -------
Subject: Re:  -fkeep-static-consts with -O asserted doesn't keep
 consts

On Fri, 4 Mar 2005, pinskia at gcc dot gnu dot org wrote:

> Don't belive the comments in the source.  Also the --help is way out of 
> date.

Both comments being wrong and --help being wrong are bugs.  This PR should 
be kept open until whatever semantics are agreed are documented in the 
manual, in --help output, in comments and are implemented.



-- 


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
                   ` (3 preceding siblings ...)
  2005-03-04 20:42 ` joseph at codesourcery dot com
@ 2005-03-04 21:54 ` gary at intrepid dot com
  2005-03-18 16:16 ` gary at intrepid dot com
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gary at intrepid dot com @ 2005-03-04 21:54 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gary at intrepid dot com  2005-03-04 21:54 -------


Here is some detail on my proposed change in behavior:

1) Change the default setting to -fno-keep-static-consts:

/* Nonzero means that we should emit static const variables
   regardless of whether or not optimization is turned on.  */ 
                            
int flag_keep_static_consts = 0;

2) have flag_keep_static_consts affect only the behavior of the
compiler when 'optimize' is asserted:

Change the logic in wrapup_global_declarations (toplev.c) to read:

              else if (TREE_READONLY (decl) && !TREE_PUBLIC (decl)
                       && (optimize && !flag_keep_static_consts)
                           || DECL_ARTIFICIAL (decl))) 
                needed = 0;

This change would imply that:

1) -fkeep-static-consts and -fno-keep-static-consts will have no effect
at -O0.  Presently, -fno-keep-static-consts would eliminate the unreferenced
static const variable if it isn't referenced.  This would no longer be
the case. At -O0, the compiler would always leave unrefereced static
consts alone.

2) If optimization is asserted (ie, -O1 and above), then always eliminate
static const's that aren't referenced, unless -fkeep-static-consts is
asserted.




-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
                   ` (4 preceding siblings ...)
  2005-03-04 21:54 ` gary at intrepid dot com
@ 2005-03-18 16:16 ` gary at intrepid dot com
  2005-06-20  3:04 ` pinskia at gcc dot gnu dot org
  2005-06-20 23:40 ` gary at intrepid dot com
  7 siblings, 0 replies; 9+ messages in thread
From: gary at intrepid dot com @ 2005-03-18 16:16 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gary at intrepid dot com  2005-03-18 16:16 -------
from http://gcc.gnu.org/ml/gcc/2005-03/msg00491.html

I think that the switch name
-fkeep-static-consts might be more consistenly named if it
was given the opposite sense and named something like
-fdelete-unused-static-consts.  The idea here is that by
asserting the switch a particular optimization is _enabled_.
Thus the optimizations performed at each level can be consistently
enumerated by asserting a particular set of switches which enable
specific optimizations.  This would change the present user interface,
however, I doubt that anyone is making extensive use of the current
interface because at present only -fno-keep-static-consts, asserted
at -O0 (no optimization), actually changes the default behavior of
the compiler.

-- 


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
                   ` (5 preceding siblings ...)
  2005-03-18 16:16 ` gary at intrepid dot com
@ 2005-06-20  3:04 ` pinskia at gcc dot gnu dot org
  2005-06-20 23:40 ` gary at intrepid dot com
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-06-20  3:04 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |documentation


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


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

* [Bug c/20319] -fkeep-static-consts with -O asserted doesn't keep consts
  2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
                   ` (6 preceding siblings ...)
  2005-06-20  3:04 ` pinskia at gcc dot gnu dot org
@ 2005-06-20 23:40 ` gary at intrepid dot com
  7 siblings, 0 replies; 9+ messages in thread
From: gary at intrepid dot com @ 2005-06-20 23:40 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gary at intrepid dot com  2005-06-20 23:40 -------
I guess that tagging the bug with a "documentation" keyword doesn't 
necessarily say that the bug is being classified only as a defect in the 
documentation.  However, if that is the meaning of this keyword, then I'd like 
to clarify the bug report.

- yes, the documentation, source code commments and -help are either 
incomplete or do not accurately describe present behavior.

- -fkepp-static-consts behaves in an unexpected manner, in that when asserted, 
it enables a behavior that is already enabled by default.  And interestingly, 
it has no effect when optimization is enabled.

- Only -fno-keep-static-consts has an effect, and then only when optimization 
is not enabled.

- In general, I think it would be best if switches which selectively control 
optimizations, enable that optimization when asserted, and disable the 
optimization when negatively asserted (ie, when prefixed with no- ...).  Thus 
I recommend that -fkeep-static-consts be deprecated, and replaced by a new 
switch, named something like -fdelete-unused-static-consts.  It would also be 
a good idea to look at the other optimization enabling switches to ensure that 
they follow this convention.  Ideally, each optimization level such as O1, O2, 
O3, Os, and Ot would be definitively and completely expressed by the selective 
optimization options they enable.

-- 


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


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

end of thread, other threads:[~2005-06-20 23:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-04 17:05 [Bug c/20319] New: -fkeep-static-consts with -O asserted doesn't keep consts gary at intrepid dot com
2005-03-04 18:29 ` [Bug c/20319] " pinskia at gcc dot gnu dot org
2005-03-04 20:24 ` gary at intrepid dot com
2005-03-04 20:33 ` pinskia at gcc dot gnu dot org
2005-03-04 20:42 ` joseph at codesourcery dot com
2005-03-04 21:54 ` gary at intrepid dot com
2005-03-18 16:16 ` gary at intrepid dot com
2005-06-20  3:04 ` pinskia at gcc dot gnu dot org
2005-06-20 23:40 ` gary at intrepid dot com

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