This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Functions that are CSEable but not pure
- From: Alexandre Oliva <aoliva at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>
- Cc: GCC <gcc at gcc dot gnu dot org>
- Date: Thu, 11 Oct 2012 12:14:07 -0300
- Subject: Re: Functions that are CSEable but not pure
- References: <506C8B3F.9080303@redhat.com>
On Oct 3, 2012, Jason Merrill <jason@redhat.com> wrote:
> int& lazy_i()
> {
> static int i = init;
> return i;
> }
> If the initialization is expensive or order-sensitive, this is a
> useful alternative to initialization on load
> An interesting property of such functions is that they only have
> side-effects the first time they are called, so subsequent calls can
> be optimized away to just use the return value of the first call.
> Currently there is no way to express this in GCC so that the
> optimizers know that multiple calls are redundant.
On Oct 4, 2012, Jakub Jelinek <jakub@redhat.com> wrote:
> The singleton function really is
> void singleton (void)
> {
> static __thread bool initialized;
> if (!initialized) {
> initialized = true;
> call_some_function_that_may_modify_memory ();
> }
> }
> and has side effects just first time in a thread.
How about marking the singleton containing the call to the initializer
as always_inline, but not the initializer itself?
The compiler can then infer that initialized is set on the first inlined
call and optimize away subsequent tests and initializer calls
(call_some_function_that_may_modify_memory).
--
Alexandre Oliva, freedom fighter http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/ FSF Latin America board member
Free Software Evangelist Red Hat Brazil Compiler Engineer