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

Nick Lewycky nicholas at mxc.ca
Fri Dec 21 15:53:32 PST 2012


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. Have you considered 
transforming bare free(foo) into "if (foo) free(foo)"?

Nick

>
> 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
>
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list