rv.ll 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
; RUN: opt -objc-arc -S < %s | FileCheck %s

target datalayout = "e-p:64:64:64"

declare i8* @llvm.objc.retain(i8*)
declare i8* @llvm.objc.retainAutoreleasedReturnValue(i8*)
declare void @llvm.objc.release(i8*)
declare i8* @llvm.objc.autorelease(i8*)
declare i8* @llvm.objc.autoreleaseReturnValue(i8*)
declare i8* @llvm.objc.retainAutoreleaseReturnValue(i8*)
declare void @llvm.objc.autoreleasePoolPop(i8*)
declare void @llvm.objc.autoreleasePoolPush()
declare i8* @llvm.objc.retainBlock(i8*)

declare i8* @objc_retainedObject(i8*)
declare i8* @objc_unretainedObject(i8*)
declare i8* @objc_unretainedPointer(i8*)

declare void @use_pointer(i8*)
declare void @callee()
declare void @callee_fnptr(void ()*)
declare void @invokee()
declare i8* @returner()
declare i8* @returner1(i8*)
declare i32 @__gxx_personality_v0(...)

; Test that retain+release elimination is suppressed when the
; retain is an objc_retainAutoreleasedReturnValue, since it's
; better to do the RV optimization.

; CHECK-LABEL:      define void @test0(
; CHECK-NEXT: entry:
; CHECK-NEXT:   %x = call i8* @returner
; CHECK-NEXT:   %0 = tail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %x) [[NUW:#[0-9]+]]
; CHECK: t:
; CHECK-NOT: @llvm.objc.
; CHECK: return:
; CHECK-NEXT: call void @llvm.objc.release(i8* %x)
; CHECK-NEXT: ret void
; CHECK-NEXT: }
define void @test0(i1 %p) nounwind {
entry:
  %x = call i8* @returner()
  %0 = call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %x)
  br i1 %p, label %t, label %return

t:
  call void @use_pointer(i8* %x)
  store i8 0, i8* %x
  br label %return

return:
  call void @llvm.objc.release(i8* %x) nounwind
  ret void
}

; Delete no-ops.

; CHECK-LABEL: define void @test2(
; CHECK-NOT: @llvm.objc.
; CHECK: }
define void @test2() {
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* null)
  call i8* @llvm.objc.autoreleaseReturnValue(i8* null)
  ; call i8* @llvm.objc.retainAutoreleaseReturnValue(i8* null) ; TODO
  %bitcast = bitcast i32* null to i8*
  %rb = call i8* @llvm.objc.retainBlock(i8* %bitcast)
  call void @use_pointer(i8* %rb)
  %rb2 = call i8* @llvm.objc.retainBlock(i8* undef)
  call void @use_pointer(i8* %rb2)
  ret void
}

; Delete a redundant retainRV,autoreleaseRV when forwaring a call result
; directly to a return value.

; CHECK-LABEL: define i8* @test3(
; CHECK: call i8* @returner()
; CHECK-NEXT: ret i8* %call
define i8* @test3() {
entry:
  %call = tail call i8* @returner()
  %0 = call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %call) nounwind
  %1 = call i8* @llvm.objc.autoreleaseReturnValue(i8* %0) nounwind
  ret i8* %1
}

; Delete a redundant retain,autoreleaseRV when forwaring a call result
; directly to a return value.

; CHECK-LABEL: define i8* @test4(
; CHECK: call i8* @returner()
; CHECK-NEXT: ret i8* %call
define i8* @test4() {
entry:
  %call = call i8* @returner()
  %0 = call i8* @llvm.objc.retain(i8* %call) nounwind
  %1 = call i8* @llvm.objc.autoreleaseReturnValue(i8* %0) nounwind
  ret i8* %1
}

; Delete a redundant fused retain+autoreleaseRV when forwaring a call result
; directly to a return value.

; TODO
; HECK: define i8* @test5
; HECK: call i8* @returner()
; HECK-NEXT: ret i8* %call
;define i8* @test5() {
;entry:
;  %call = call i8* @returner()
;  %0 = call i8* @llvm.objc.retainAutoreleaseReturnValue(i8* %call) nounwind
;  ret i8* %0
;}

; Don't eliminate objc_retainAutoreleasedReturnValue by merging it into
; an objc_autorelease.
; TODO? Merge objc_retainAutoreleasedReturnValue and objc_autorelease into
; objc_retainAutoreleasedReturnValueAutorelease and merge
; objc_retainAutoreleasedReturnValue and objc_autoreleaseReturnValue
; into objc_retainAutoreleasedReturnValueAutoreleaseReturnValue?
; Those entrypoints don't exist yet though.

; CHECK-LABEL: define i8* @test7(
; CHECK: call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
; CHECK: %t = tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
define i8* @test7() {
  %p = call i8* @returner()
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  %t = call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  call void @use_pointer(i8* %p)
  ret i8* %t
}

; CHECK-LABEL: define i8* @test7b(
; CHECK: call i8* @llvm.objc.retain(i8* %p)
; CHECK: %t = tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
define i8* @test7b() {
  %p = call i8* @returner()
  call void @use_pointer(i8* %p)
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  %t = call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  ret i8* %p
}

; Don't apply the RV optimization to autorelease if there's no retain.

; CHECK: define i8* @test9(i8* %p)
; CHECK: call i8* @llvm.objc.autorelease(i8* %p)
define i8* @test9(i8* %p) {
  call i8* @llvm.objc.autorelease(i8* %p)
  ret i8* %p
}

; Do not apply the RV optimization.

; CHECK: define i8* @test10(i8* %p)
; CHECK: tail call i8* @llvm.objc.retain(i8* %p) [[NUW]]
; CHECK: call i8* @llvm.objc.autorelease(i8* %p) [[NUW]]
; CHECK-NEXT: ret i8* %p
define i8* @test10(i8* %p) {
  %1 = call i8* @llvm.objc.retain(i8* %p)
  %2 = call i8* @llvm.objc.autorelease(i8* %p)
  ret i8* %p
}

; Don't do the autoreleaseRV optimization because @use_pointer
; could undo the retain.

; CHECK: define i8* @test11(i8* %p)
; CHECK: tail call i8* @llvm.objc.retain(i8* %p)
; CHECK-NEXT: call void @use_pointer(i8* %p)
; CHECK: call i8* @llvm.objc.autorelease(i8* %p)
; CHECK-NEXT: ret i8* %p
define i8* @test11(i8* %p) {
  %1 = call i8* @llvm.objc.retain(i8* %p)
  call void @use_pointer(i8* %p)
  %2 = call i8* @llvm.objc.autorelease(i8* %p)
  ret i8* %p
}

; Don't spoil the RV optimization.

; CHECK: define i8* @test12(i8* %p)
; CHECK: tail call i8* @llvm.objc.retain(i8* %p)
; CHECK: call void @use_pointer(i8* %p)
; CHECK: tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
; CHECK: ret i8* %p
define i8* @test12(i8* %p) {
  %1 = call i8* @llvm.objc.retain(i8* %p)
  call void @use_pointer(i8* %p)
  %2 = call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  ret i8* %p
}

; Don't zap the objc_retainAutoreleasedReturnValue.

; CHECK-LABEL: define i8* @test13(
; CHECK: tail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
; CHECK: call i8* @llvm.objc.autorelease(i8* %p)
; CHECK: ret i8* %p
define i8* @test13() {
  %p = call i8* @returner()
  %1 = call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  call void @callee()
  %2 = call i8* @llvm.objc.autorelease(i8* %p)
  ret i8* %p
}

; Convert objc_retainAutoreleasedReturnValue to objc_retain if its
; argument is not a return value.

; CHECK-LABEL: define void @test14(
; CHECK-NEXT: tail call i8* @llvm.objc.retain(i8* %p) [[NUW]]
; CHECK-NEXT: ret void
define void @test14(i8* %p) {
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  ret void
}

; Don't convert objc_retainAutoreleasedReturnValue to objc_retain if its
; argument is a return value.

; CHECK-LABEL: define void @test15(
; CHECK-NEXT: %y = call i8* @returner()
; CHECK-NEXT: tail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %y) [[NUW]]
; CHECK-NEXT: ret void
define void @test15() {
  %y = call i8* @returner()
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %y)
  ret void
}

; Delete autoreleaseRV+retainRV pairs.

; CHECK: define i8* @test19(i8* %p) {
; CHECK-NEXT: ret i8* %p
define i8* @test19(i8* %p) {
  call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  ret i8* %p
}

; Delete autoreleaseRV+retainRV pairs when they have equivalent PHIs as inputs

; CHECK: define i8* @test19phi(i8* %p) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label %test19bb
; CHECK: test19bb:
; CHECK-NEXT: ret i8* %p
define i8* @test19phi(i8* %p) {
entry:
  br label %test19bb
test19bb:
  %phi1 = phi i8* [ %p, %entry ]
  %phi2 = phi i8* [ %p, %entry ]
  call i8* @llvm.objc.autoreleaseReturnValue(i8* %phi1)
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %phi2)
  ret i8* %p
}

; Like test19 but with plain autorelease.

; CHECK: define i8* @test20(i8* %p) {
; CHECK-NEXT: call i8* @llvm.objc.autorelease(i8* %p)
; CHECK-NEXT: call i8* @llvm.objc.retain(i8* %p)
; CHECK-NEXT: ret i8* %p
define i8* @test20(i8* %p) {
  call i8* @llvm.objc.autorelease(i8* %p)
  call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %p)
  ret i8* %p
}

; Like test19 but with plain retain.

; CHECK: define i8* @test21(i8* %p) {
; CHECK-NEXT: call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
; CHECK-NEXT: call i8* @llvm.objc.retain(i8* %p)
; CHECK-NEXT: ret i8* %p
define i8* @test21(i8* %p) {
  call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  call i8* @llvm.objc.retain(i8* %p)
  ret i8* %p
}

; Like test19 but with plain retain and autorelease.

; CHECK: define i8* @test22(i8* %p) {
; CHECK-NEXT: call i8* @llvm.objc.autorelease(i8* %p)
; CHECK-NEXT: call i8* @llvm.objc.retain(i8* %p)
; CHECK-NEXT: ret i8* %p
define i8* @test22(i8* %p) {
  call i8* @llvm.objc.autorelease(i8* %p)
  call i8* @llvm.objc.retain(i8* %p)
  ret i8* %p
}

; Convert autoreleaseRV to autorelease.

; CHECK-LABEL: define void @test23(
; CHECK: call i8* @llvm.objc.autorelease(i8* %p) [[NUW]]
define void @test23(i8* %p) {
  store i8 0, i8* %p
  call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  ret void
}

; Don't convert autoreleaseRV to autorelease if the result is returned,
; even through a bitcast.

; CHECK-LABEL: define {}* @test24(
; CHECK: tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
define {}* @test24(i8* %p) {
  %t = call i8* @llvm.objc.autoreleaseReturnValue(i8* %p)
  %s = bitcast i8* %p to {}*
  ret {}* %s
}

declare i8* @first_test25();
declare i8* @second_test25(i8*);
declare void @somecall_test25();

; ARC optimizer used to move the last release between the call to second_test25
; and the call to objc_retainAutoreleasedReturnValue, causing %second to be
; released prematurely when %first and %second were pointing to the same object.

; CHECK-LABEL: define void @test25(
; CHECK: %[[CALL1:.*]] = call i8* @second_test25(
; CHECK-NEXT: tail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[CALL1]])

define void @test25() {
  %first = call i8* @first_test25()
  %v0 = call i8* @llvm.objc.retain(i8* %first)
  call void @somecall_test25()
  %second = call i8* @second_test25(i8* %first)
  %call2 = call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %second)
  call void @llvm.objc.release(i8* %second), !clang.imprecise_release !0
  call void @llvm.objc.release(i8* %first), !clang.imprecise_release !0
  ret void
}

; Check that ObjCARCOpt::OptimizeReturns removes the redundant calls even when
; they are not in the same basic block. This code used to cause an assertion
; failure.

; CHECK-LABEL: define i8* @test26()
; CHECK: call i8* @returner()
; CHECK-NOT:  call
define i8* @test26() {
bb0:
  %v0 = call i8* @returner()
  %v1 = tail call i8* @llvm.objc.retain(i8* %v0)
  br label %bb1
bb1:
  %v2 = tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %v1)
  br label %bb2
bb2:
  ret i8* %v2
}

declare i32* @func27(i32);

; Check that ObjCARCOpt::OptimizeAutoreleaseRVCall doesn't turn a call to
; @llvm.objc.autoreleaseReturnValue into a call to @llvm.objc.autorelease when a return
; instruction uses a value equivalent to @llvm.objc.autoreleaseReturnValue's operand.
; In the code below, %phival and %retval are considered equivalent.

; CHECK-LABEL: define i32* @test27(
; CHECK: %[[PHIVAL:.*]] = phi i8* [ %{{.*}}, %bb1 ], [ %{{.*}}, %bb2 ]
; CHECK: %[[RETVAL:.*]] = phi i32* [ %{{.*}}, %bb1 ], [ %{{.*}}, %bb2 ]
; CHECK: tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %[[PHIVAL]])
; CHECK: ret i32* %[[RETVAL]]

define i32* @test27(i1 %cond) {
entry:
  br i1 %cond, label %bb1, label %bb2
bb1:
  %v0 = call i32* @func27(i32 1)
  %v1 = bitcast i32* %v0 to i8*
  br label %bb3
bb2:
  %v2 = call i32* @func27(i32 2)
  %v3 = bitcast i32* %v2 to i8*
  br label %bb3
bb3:
  %phival = phi i8* [ %v1, %bb1 ], [ %v3, %bb2 ]
  %retval = phi i32* [ %v0, %bb1 ], [ %v2, %bb2 ]
  %v4 = tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %phival)
  ret i32* %retval
}

; Don't eliminate the retainRV/autoreleaseRV pair if the call isn't a tail call.

; CHECK-LABEL: define i8* @test28(
; CHECK: call i8* @returner()
; CHECK: call i8* @llvm.objc.retainAutoreleasedReturnValue(
; CHECK: call i8* @llvm.objc.autoreleaseReturnValue(
define i8* @test28() {
entry:
  %call = call i8* @returner()
  %0 = call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %call) nounwind
  %1 = call i8* @llvm.objc.autoreleaseReturnValue(i8* %0) nounwind
  ret i8* %1
}

; CHECK-LABEL: define i8* @test29(
; CHECK: call i8* @llvm.objc.retainAutoreleasedReturnValue(
; CHECK: call i8* @llvm.objc.autoreleaseReturnValue(

define i8* @test29(i8* %k) local_unnamed_addr personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
entry:
  %0 = tail call i8* @llvm.objc.retain(i8* %k)
  %call = invoke i8* @returner1(i8* %k)
          to label %invoke.cont unwind label %lpad

invoke.cont:
  %1 = bitcast i8* %call to i8*
  %2 = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %1)
  tail call void @llvm.objc.release(i8* %k), !clang.imprecise_release !0
  %3 = tail call i8* @llvm.objc.autoreleaseReturnValue(i8* %1)
  ret i8* %call

lpad:
  %4 = landingpad { i8*, i32 }
          cleanup
  tail call void @llvm.objc.release(i8* %k) #1, !clang.imprecise_release !0
  resume { i8*, i32 } %4
}

!0 = !{}

; CHECK: attributes [[NUW]] = { nounwind }