[llvm-commits] [PATCH] InstCombine: remove malloc+free if malloc's only uses are comparisons to null
Chris Lattner
clattner at apple.com
Fri May 21 15:55:55 PDT 2010
On May 20, 2010, at 9:04 PM, Matti Niemenmaa wrote:
>
> Good catch, that's possible. It depends on how exactly Value::use_iterator works, which I don't know in enough detail. I did, however, find some code in Transforms/Scalar/SCCP.cpp which does an early increment to avoid such issues, so there probably is such a danger here.
>
> Attached an amended patch with the cleanups and incrementing UI before the erases instead of after.
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!
+ 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.
If you feel like it, you could also generalize this to handle calloc/valloc as well.
Thanks for working on this!
-Chris
More information about the llvm-commits
mailing list