[llvm-commits] [PATCH] Skip NULL pointer check for free

Quentin Colombet qcolombet at apple.com
Fri Dec 21 16:04:12 PST 2012


Hi Nick,

On Dec 21, 2012, at 3:59 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:

> On Sat, Dec 22, 2012 at 1:53 AM, Nick Lewycky <nicholas at mxc.ca> wrote:
>> On 12/21/2012 03:43 PM, Quentin Colombet wrote:
>>> 
>>> Hi,
>>> 
>>> Here is a patch to help llvm turning a code like this:
>>> if (foo)
>>> free(foo)
>>> 
>>> into that:
>>> free(foo)
>>> 
>>> It is legal and safe, since free is already checking its argument
>>> against NULL internally (I may find the reference in the Standard if
>>> needed :)).
>> 
>> It's legal and safe, but is it desirable? Calling a function is expensive
>> even if the function doesn't do anything.

Yes, you're right, it may not be desirable but I'm tempt to think like Dmitri.

Do you think that this optimization should be controlled by a flag?
At least, for code size (Oz and maybe Os), it may always be desirable.

I did not consider changing free(foo) into if(foo) free(foo)

> 
> That's true, but there's another viewpoint: programmers usually write
> 'if(!foo) free(foo);' not because they are manually inlining the fast
> path of free(), but because they think that free(NULL) is UB.
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/




More information about the llvm-commits mailing list