On Thu, May 12, 2011 at 06:11:59PM +0200, Piotr Wyderski wrote: > Hello, > > I'm not sure if it should be better handled as missed optimization, > but there is a certain lack of functionality in the GCC's __sync_* > function family. > > When implementing a reference counting smart pointer, two operations > are of crucial importance: > > void __sync_increment(T* p); > bool __sync_decrement_iszero(T* p); > > The former only increments the location pointed to by p, the latter decrements > it and returns true if and only if the result was zero. > > Both can be implemented in terms of existing __sync functions (and what > can't? -- since there is __sync_bool_compare_and_swap()), e.g.: > > void __sync_increment(T* p) { > > __sync_fetch_and_add(p, 1); > } > > bool __sync_decrement(T* p) { > > return __sync_fetch_and_add(p, -1) == 1; > }
And that's the right thing to do. If the generated code is not optimal, we should just improve __sync_fetch_and_add code generation. It isn't hard to special case addition of 1 or -1, and we already care whether the result of the builtin is used or ignored. For == 1 we could add some pattern that combiner would merge. Please file an enhancement request in gcc bugzilla. Jakub