[LLVMdev] Promoting malloc to alloca

Nick Lewycky nlewycky at google.com
Tue Jul 13 10:00:49 PDT 2010


On 13 July 2010 00:25, Duncan Sands <baldrick at free.fr> wrote:

> Hi Nick,
>
> >>> The attribute you're looking for, "delete if result is unused" doesn't
> >>> exist in LLVM. I've considered it in the past, but the truth is that
> >>> most of the time I want to eliminate dead malloc's, they *do* have a
> >>> use: the matching free. At some point I expect I'm going to teach LLVM
> >>> to remove dead malloc+free / new+delete / new[]+delete[] pairings, but
> I
> >>> haven't gotten around to it yet. Teaching it to kill an allocation with
> >>> no uses at all will be dead simple too.
> >>
> >> LLVM already removes malloc+(perhaps compare with null)+free.
> >
> > What?
> >
> > declare noalias i8* @malloc() nounwind
> > declare void @free(i8* nocapture) nounwind
> > define i8 @test() {
> > %A = call i8* @malloc()
>
> ^ malloc with wrong signature
>

Duh, thanks!

Then at some point, I should generalize this to handle C++ allocators too.
:)

Nick


> > call void @free(i8* %A)
> > ret i8 undef
> > }
> >
> > doesn't optimize at all. While we're at it, we also don't get:
> >
> > declare noalias i8* @malloc() nounwind
> > define i8 @test() {
> > %A = call i8* @malloc()
> > %B = load i8* %A
> > ret i8 %B
> > }
> >
> > either, though that's fixable by inserting llvm.lifetime.start right
> > after the malloc.
>
> Take a look at:
>
> Instruction *InstCombiner::visitMalloc(Instruction &MI) {
>   // If we have a malloc call which is only used in any amount of
> comparisons
>   // to null and free calls, delete the calls and replace the comparisons
> with
>   // true or false as appropriate.
>   if (IsOnlyNullComparedAndFreed(MI)) {
> ...
>
> Ciao,
>
> Duncan.
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100713/d3230551/attachment.html>


More information about the llvm-dev mailing list