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

Matti Niemenmaa matti.niemenmaa+llvm at iki.fi
Sat May 22 02:52:19 PDT 2010


On 2010-05-22 01:55, Chris Lattner wrote:
> A couple more requests :)
>
> +++ test/Transforms/InstCombine/malloc-free-delete2.ll	(revision 0)
> @@ -0,0 +1,11 @@
> +; RUN: opt<  %s -instcombine -S | FileCheck %s
> +define i1 @foo() {
> +; CHECK: @foo
> +; CHECK-NEXT: ret i1 false
> +  %m = call i8* @malloc(i64 1)
> +  %z = icmp eq i8* %m, null
> +  call void @free(i8* %m)
> +  ret i1 %z
> +}
> +declare i8* @malloc(i64)
> +declare void @free(i8*)
>
> Please merge this into test/Transforms/InstCombine/malloc-free-delete.ll since it already uses filecheck, thanks!

Done. I removed -globaldce from that as well, since it wasn't depending 
on it.

> +    if (const ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
> +      ICmpInst::Predicate Pred = ICI->getPredicate();
> +      if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE&&
>
> Please use ICI->isEquality()
>
> +          (isa<ConstantPointerNull>(ICI->getOperand(0)) ||
> +           isa<ConstantPointerNull>(ICI->getOperand(1))))
>
> comparisons against null will put the null on the RHS of the compare, just check operand #1.

Both done in the attached, thanks for the simplifications.

> If you feel like it, you could also generalize this to handle calloc/valloc as well.

There're more functions like malloc that return an allocated value which 
should be passed to free(): malloc, calloc, realloc, valloc, memalign, 
and posix_memalign, at least. They should probably all be handled.

I think that's something that should be done over the whole codebase, 
around every use of isMalloc or extractMallocCall(FromBitCast), and is 
something for another patch. :-)

(Hopefully we'll have "nobuiltin" soon, so Analysis/MemoryBuiltins.cpp 
doesn't have to check the type separately for all of them!)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: instcombine-mallocs-only-null-compared-against.patch
Type: text/x-patch
Size: 7336 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20100522/45a3f71f/attachment.bin>


More information about the llvm-commits mailing list