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

Quentin Colombet qcolombet at apple.com
Fri Dec 21 15:43:13 PST 2012


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 :)).

The proposed patch moves a call to free from basic block FB into FB's predecessor, P, when the path from P to FB is taken only if the argument of free is not equal to NULL.

It also has more restriction on P and FB to be sure that this code motion is profitable. Namely:
1. FB must have only one predecessor P
2. FB must contain only the call to free plus an unconditional branch to S
3. P successors are FB and S.

Because of 1., we will not increase the code size when moving the call to free from FB to P.
Because of 2., FB will be empty after the move.
Because of 2. and 3., P's branch instruction becomes useless, so as FB (simplifycfg will do the job).

We can extend this optimization if we are able to derive a good cost/profitability model. Currently, I do not have anything in mind, so I've kept it simple.

The patch also contains a testcase just for the code motion part, not the total simplification.

-Quentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121221/ddc463d7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: skipNullPtrCheckForFreeFct.patch
Type: application/octet-stream
Size: 4752 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121221/ddc463d7/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121221/ddc463d7/attachment-0001.html>


More information about the llvm-commits mailing list