[llvm-commits] [PATCH] InstCombine: remove malloc+free if malloc's only uses are comparisons to null

Matti Niemenmaa matti.niemenmaa+llvm at iki.fi
Sun May 16 09:59:49 PDT 2010


The attached patch makes InstCombine remove malloc+free pairs if the 
only uses of the malloc are comparisons of the returned pointer to null. 
Previously, they would only be removed if there are no uses at all 
(other than the free, of course).

Since real-world code often actually checks whether malloc returned 
null, this makes the optimization significantly more useful, in my opinion.

A test is provided, and an old test which checks that this optimization 
does /not/ happen gets an additional use for its malloc.

There's one slight weirdness in it whose cleanness I'm not so sure 
about: when getting rid of the malloc+free, we want to replace the 
remaining uses of the malloc with a constant (trivially folded) non-null 
pointer. The only way of getting such a thing I could think of was to 
bitcast the enclosing function's address, since that's a GlobalValue and 
hence can be handled by ConstantFoldCompareInstruction. It definitely 
works, but it seems a bit hacky.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: instcombine-mallocs-only-null-compared-against.patch
Type: text/x-patch
Size: 3474 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20100516/afd8770a/attachment.bin>


More information about the llvm-commits mailing list