This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Functions that are CSEable but not pure

On Oct  3, 2012, Jason Merrill <> 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 <> 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

Alexandre Oliva, freedom fighter
You must be the change you wish to see in the world. -- Gandhi
Be Free! --   FSF Latin America board member
Free Software Evangelist      Red Hat Brazil Compiler Engineer

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]