summaryrefslogtreecommitdiff
path: root/test/Transforms/InstCombine/malloc-free-delete.ll
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2012-07-06 23:09:25 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2012-07-06 23:09:25 +0000
commit9969458b1673648394fce3f50f3f4a84071c7ca7 (patch)
tree253c1ee0feb6cc68ff421a433088fba1eb227f3d /test/Transforms/InstCombine/malloc-free-delete.ll
parenteb54603ddd4fb9fc255372c7904dc0dd6a162968 (diff)
downloadllvm-9969458b1673648394fce3f50f3f4a84071c7ca7.tar.gz
llvm-9969458b1673648394fce3f50f3f4a84071c7ca7.tar.bz2
llvm-9969458b1673648394fce3f50f3f4a84071c7ca7.tar.xz
teach instcombine to remove allocated buffers even if there are stores, memcpy/memmove/memset, and objectsize users.
This means we can do cheap DSE for heap memory. Nothing is done if the pointer excapes or has a load. The churn in the tests is mostly due to objectsize, since we want to make sure we don't delete the malloc call before evaluating the objectsize (otherwise it becomes -1/0) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@159876 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Transforms/InstCombine/malloc-free-delete.ll')
-rw-r--r--test/Transforms/InstCombine/malloc-free-delete.ll53
1 files changed, 49 insertions, 4 deletions
diff --git a/test/Transforms/InstCombine/malloc-free-delete.ll b/test/Transforms/InstCombine/malloc-free-delete.ll
index eae973df0a..4e3217dc2d 100644
--- a/test/Transforms/InstCombine/malloc-free-delete.ll
+++ b/test/Transforms/InstCombine/malloc-free-delete.ll
@@ -1,17 +1,17 @@
; RUN: opt < %s -instcombine -S | FileCheck %s
; PR1201
define i32 @main(i32 %argc, i8** %argv) {
+; CHECK: @main
%c_19 = alloca i8*
%malloc_206 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i8* getelementptr (i8* null, i32 1) to i32), i32 10))
store i8* %malloc_206, i8** %c_19
%tmp_207 = load i8** %c_19
tail call void @free(i8* %tmp_207)
ret i32 0
-; CHECK-NOT: malloc
-; CHECK-NOT: free
-; CHECK: ret i32 0
+; CHECK-NEXT: ret i32 0
}
+declare noalias i8* @calloc(i32, i32) nounwind
declare noalias i8* @malloc(i32)
declare void @free(i8*)
@@ -26,13 +26,24 @@ define i1 @foo() {
declare void @llvm.lifetime.start(i64, i8*)
declare void @llvm.lifetime.end(i64, i8*)
+declare i64 @llvm.objectsize.i64(i8*, i1)
+declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
+declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
+declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i32, i1) nounwind
-define void @test3() {
+define void @test3(i8* %src) {
; CHECK: @test3
; CHECK-NEXT: ret void
%a = call noalias i8* @malloc(i32 10)
call void @llvm.lifetime.start(i64 10, i8* %a)
call void @llvm.lifetime.end(i64 10, i8* %a)
+ %size = call i64 @llvm.objectsize.i64(i8* %a, i1 true)
+ store i8 42, i8* %a
+ call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i32 1, i1 false)
+ call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i32 1, i1 false)
+ call void @llvm.memset.p0i8.i32(i8* %a, i8 5, i32 32, i32 1, i1 false)
+ %alloc2 = call noalias i8* @calloc(i32 5, i32 7) nounwind
+ %z = icmp ne i8* %alloc2, null
ret void
}
@@ -46,3 +57,37 @@ define void @test4() {
call void @free(i8* %C)
ret void
}
+
+; CHECK: @test5
+define void @test5(i8* %ptr, i8** %esc) {
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call i8* @malloc
+; CHECK-NEXT: call void @llvm.memcpy
+; CHECK-NEXT: call void @llvm.memmove
+; CHECK-NEXT: store
+; CHECK-NEXT: call void @llvm.memcpy
+; CHECK-NEXT: call void @llvm.memmove
+; CHECK-NEXT: call void @llvm.memset
+; CHECK-NEXT: store volatile
+; CHECK-NEXT: ret
+ %a = call i8* @malloc(i32 700)
+ %b = call i8* @malloc(i32 700)
+ %c = call i8* @malloc(i32 700)
+ %d = call i8* @malloc(i32 700)
+ %e = call i8* @malloc(i32 700)
+ %f = call i8* @malloc(i32 700)
+ %g = call i8* @malloc(i32 700)
+ call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %a, i32 32, i32 1, i1 false)
+ call void @llvm.memmove.p0i8.p0i8.i32(i8* %ptr, i8* %b, i32 32, i32 1, i1 false)
+ store i8* %c, i8** %esc
+ call void @llvm.memcpy.p0i8.p0i8.i32(i8* %d, i8* %ptr, i32 32, i32 1, i1 true)
+ call void @llvm.memmove.p0i8.p0i8.i32(i8* %e, i8* %ptr, i32 32, i32 1, i1 true)
+ call void @llvm.memset.p0i8.i32(i8* %f, i8 5, i32 32, i32 1, i1 true)
+ store volatile i8 4, i8* %g
+ ret void
+}