1 ; RUN: opt < %s -instcombine -S | FileCheck %s 2 ; PR1201 3 define i32 @main(i32 %argc, i8** %argv) { 4 ; CHECK-LABEL: @main( 5 %c_19 = alloca i8* 6 %malloc_206 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i8* getelementptr (i8, i8* null, i32 1) to i32), i32 10)) 7 store i8* %malloc_206, i8** %c_19 8 %tmp_207 = load i8*, 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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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-LABEL: @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 124 declare i8* @_ZnwmRKSt9nothrow_t(i64, i8*) nobuiltin 125 declare void @_ZdlPvRKSt9nothrow_t(i8*, i8*) nobuiltin 126 declare i32 @__gxx_personality_v0(...) 127 declare void @_ZN1AC2Ev(i8* %this) 128 129 ; CHECK-LABEL: @test7( 130 define void @test7() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 131 entry: 132 %nt = alloca i8 133 ; CHECK-NOT: call {{.*}}@_ZnwmRKSt9nothrow_t( 134 %call.i = tail call i8* @_ZnwmRKSt9nothrow_t(i64 1, i8* %nt) builtin nounwind 135 invoke void @_ZN1AC2Ev(i8* undef) 136 to label %.noexc.i unwind label %lpad.i 137 138 .noexc.i: ; preds = %entry 139 unreachable 140 141 lpad.i: ; preds = %entry 142 %0 = landingpad { i8*, i32 } cleanup 143 ; CHECK-NOT: call {{.*}}@_ZdlPvRKSt9nothrow_t( 144 call void @_ZdlPvRKSt9nothrow_t(i8* %call.i, i8* %nt) builtin nounwind 145 resume { i8*, i32 } %0 146 } 147 148 declare i8* @_Znwm(i64) nobuiltin 149 declare i8* @_Znwj(i32) nobuiltin 150 declare i8* @_Znam(i64) nobuiltin 151 declare i8* @_Znaj(i32) nobuiltin 152 declare void @_ZdlPv(i8*) nobuiltin 153 declare void @_ZdaPv(i8*) nobuiltin 154 155 define linkonce void @_ZdlPvm(i8* %p, i64) nobuiltin { 156 call void @_ZdlPv(i8* %p) 157 ret void 158 } 159 define linkonce void @_ZdlPvj(i8* %p, i32) nobuiltin { 160 call void @_ZdlPv(i8* %p) 161 ret void 162 } 163 define linkonce void @_ZdaPvm(i8* %p, i64) nobuiltin { 164 call void @_ZdaPv(i8* %p) 165 ret void 166 } 167 define linkonce void @_ZdaPvj(i8* %p, i32) nobuiltin { 168 call void @_ZdaPv(i8* %p) 169 ret void 170 } 171 172 ; CHECK-LABEL: @test8( 173 define void @test8() { 174 ; CHECK-NOT: call 175 %nw = call i8* @_Znwm(i64 32) builtin 176 call void @_ZdlPv(i8* %nw) builtin 177 %na = call i8* @_Znam(i64 32) builtin 178 call void @_ZdaPv(i8* %na) builtin 179 %nwm = call i8* @_Znwm(i64 32) builtin 180 call void @_ZdlPvm(i8* %nwm, i64 32) builtin 181 %nwj = call i8* @_Znwj(i32 32) builtin 182 call void @_ZdlPvj(i8* %nwj, i32 32) builtin 183 %nam = call i8* @_Znam(i64 32) builtin 184 call void @_ZdaPvm(i8* %nam, i64 32) builtin 185 %naj = call i8* @_Znaj(i32 32) builtin 186 call void @_ZdaPvj(i8* %naj, i32 32) builtin 187 ret void 188 } 189 190 declare noalias i8* @"\01??2@YAPEAX_K@Z"(i64) nobuiltin 191 declare void @"\01??3@YAXPEAX@Z"(i8*) nobuiltin 192 193 ; CHECK-LABEL: @test9( 194 define void @test9() { 195 ; CHECK-NOT: call 196 %new_long_long = call noalias i8* @"\01??2@YAPEAX_K@Z"(i64 32) builtin 197 call void @"\01??3@YAXPEAX@Z"(i8* %new_long_long) builtin 198 ret void 199 } 200