1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 ; PR1201 3 define i32 @main(i32 %argc, i8** %argv) { 4 ; CHECK: @main 5 %c_19 = alloca i8* 6 %malloc_206 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i8* getelementptr (i8* null, i32 1) to i32), i32 10)) 7 store i8* %malloc_206, i8** %c_19 8 %tmp_207 = load i8** %c_19 9 tail call void @free(i8* %tmp_207) 10 ret i32 0 11 ; CHECK-NEXT: ret i32 0 12 } 13 14 declare noalias i8* @calloc(i32, i32) nounwind 15 declare noalias i8* @malloc(i32) 16 declare void @free(i8*) 17 18 define i1 @foo() { 19 ; CHECK: @foo 20 ; CHECK-NEXT: ret i1 false 21 %m = call i8* @malloc(i32 1) 22 %z = icmp eq i8* %m, null 23 call void @free(i8* %m) 24 ret i1 %z 25 } 26 27 declare void @llvm.lifetime.start(i64, i8*) 28 declare void @llvm.lifetime.end(i64, i8*) 29 declare i64 @llvm.objectsize.i64(i8*, i1) 30 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind 31 declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind 32 declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i32, i1) nounwind 33 34 define void @test3(i8* %src) { 35 ; CHECK: @test3 36 ; CHECK-NEXT: ret void 37 %a = call noalias i8* @malloc(i32 10) 38 call void @llvm.lifetime.start(i64 10, i8* %a) 39 call void @llvm.lifetime.end(i64 10, i8* %a) 40 %size = call i64 @llvm.objectsize.i64(i8* %a, i1 true) 41 store i8 42, i8* %a 42 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i32 1, i1 false) 43 call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i32 1, i1 false) 44 call void @llvm.memset.p0i8.i32(i8* %a, i8 5, i32 32, i32 1, i1 false) 45 %alloc2 = call noalias i8* @calloc(i32 5, i32 7) nounwind 46 %z = icmp ne i8* %alloc2, null 47 ret void 48 } 49 50 ;; This used to crash. 51 define void @test4() { 52 ; CHECK: @test4 53 ; CHECK-NEXT: ret void 54 %A = call i8* @malloc(i32 16000) 55 %B = bitcast i8* %A to double* 56 %C = bitcast double* %B to i8* 57 call void @free(i8* %C) 58 ret void 59 } 60 61 ; CHECK: @test5 62 define void @test5(i8* %ptr, i8** %esc) { 63 ; CHECK-NEXT: call i8* @malloc 64 ; CHECK-NEXT: call i8* @malloc 65 ; CHECK-NEXT: call i8* @malloc 66 ; CHECK-NEXT: call i8* @malloc 67 ; CHECK-NEXT: call i8* @malloc 68 ; CHECK-NEXT: call i8* @malloc 69 ; CHECK-NEXT: call i8* @malloc 70 ; CHECK-NEXT: call void @llvm.memcpy 71 ; CHECK-NEXT: call void @llvm.memmove 72 ; CHECK-NEXT: store 73 ; CHECK-NEXT: call void @llvm.memcpy 74 ; CHECK-NEXT: call void @llvm.memmove 75 ; CHECK-NEXT: call void @llvm.memset 76 ; CHECK-NEXT: store volatile 77 ; CHECK-NEXT: ret 78 %a = call i8* @malloc(i32 700) 79 %b = call i8* @malloc(i32 700) 80 %c = call i8* @malloc(i32 700) 81 %d = call i8* @malloc(i32 700) 82 %e = call i8* @malloc(i32 700) 83 %f = call i8* @malloc(i32 700) 84 %g = call i8* @malloc(i32 700) 85 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %a, i32 32, i32 1, i1 false) 86 call void @llvm.memmove.p0i8.p0i8.i32(i8* %ptr, i8* %b, i32 32, i32 1, i1 false) 87 store i8* %c, i8** %esc 88 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %d, i8* %ptr, i32 32, i32 1, i1 true) 89 call void @llvm.memmove.p0i8.p0i8.i32(i8* %e, i8* %ptr, i32 32, i32 1, i1 true) 90 call void @llvm.memset.p0i8.i32(i8* %f, i8 5, i32 32, i32 1, i1 true) 91 store volatile i8 4, i8* %g 92 ret void 93 } 94 95 ;; When a basic block contains only a call to free and this block is accessed 96 ;; through a test of the argument of free against null, move the call in the 97 ;; predecessor block. 98 ;; Using simplifycfg will remove the empty basic block and the branch operation 99 ;; Then, performing a dead elimination will remove the comparison. 100 ;; This is what happens with -O1 and upper. 101 ; CHECK: @test6 102 define void @test6(i8* %foo) minsize { 103 ; CHECK: %tobool = icmp eq i8* %foo, null 104 ;; Call to free moved 105 ; CHECK-NEXT: tail call void @free(i8* %foo) 106 ; CHECK-NEXT: br i1 %tobool, label %if.end, label %if.then 107 ; CHECK: if.then: 108 ;; Block is now empty and may be simplified by simplifycfg 109 ; CHECK-NEXT: br label %if.end 110 ; CHECK: if.end: 111 ; CHECK-NEXT: ret void 112 entry: 113 %tobool = icmp eq i8* %foo, null 114 br i1 %tobool, label %if.end, label %if.then 115 116 if.then: ; preds = %entry 117 tail call void @free(i8* %foo) 118 br label %if.end 119 120 if.end: ; preds = %entry, %if.then 121 ret void 122 } 123