xref: /llvm-project/llvm/test/Transforms/ObjCARC/weak-copies.ll (revision 01e4f41b43b57dee751146fde9992c660bd7c714)
1; RUN: opt -S -passes=objc-arc < %s | FileCheck %s
2
3target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
4target triple = "x86_64-apple-darwin11.0.0"
5
6%0 = type { i64, i64, ptr, ptr, ptr, ptr }
7%1 = type <{ ptr, i32, i32, ptr, ptr, ptr }>
8%struct.__block_descriptor = type { i64, i64 }
9
10@_NSConcreteStackBlock = external global ptr
11@.str = private unnamed_addr constant [6 x i8] c"v8@?0\00"
12@"\01L_OBJC_CLASS_NAME_" = internal global [3 x i8] c"\01@\00", section "__TEXT,__objc_classname,cstring_literals", align 1
13@__block_descriptor_tmp = internal constant %0 { i64 0, i64 40, ptr @__copy_helper_block_, ptr @__destroy_helper_block_, ptr @.str, ptr @"\01L_OBJC_CLASS_NAME_" }
14@"\01L_OBJC_IMAGE_INFO" = internal constant [2 x i32] [i32 0, i32 16], section "__DATA, __objc_imageinfo, regular, no_dead_strip"
15@llvm.used = appending global [2 x ptr] [ptr @"\01L_OBJC_CLASS_NAME_", ptr @"\01L_OBJC_IMAGE_INFO"], section "llvm.metadata"
16
17; Eliminate unnecessary weak pointer copies.
18
19; CHECK:      define void @foo() {
20; CHECK-NEXT: entry:
21; CHECK-NEXT:   %call = call ptr @bar()
22; CHECK-NEXT:   call void @use(ptr %call) [[NUW:#[0-9]+]]
23; CHECK-NEXT:   ret void
24; CHECK-NEXT: }
25define void @foo() {
26entry:
27  %w = alloca ptr, align 8
28  %x = alloca ptr, align 8
29  %call = call ptr @bar()
30  %0 = call ptr @llvm.objc.initWeak(ptr %w, ptr %call) nounwind
31  %1 = call ptr @llvm.objc.loadWeak(ptr %w) nounwind
32  %2 = call ptr @llvm.objc.initWeak(ptr %x, ptr %1) nounwind
33  %3 = call ptr @llvm.objc.loadWeak(ptr %x) nounwind
34  call void @use(ptr %3) nounwind
35  call void @llvm.objc.destroyWeak(ptr %x) nounwind
36  call void @llvm.objc.destroyWeak(ptr %w) nounwind
37  ret void
38}
39
40; Eliminate unnecessary weak pointer copies in a block initialization.
41
42; CHECK:      define void @qux(ptr %me) #0 {
43; CHECK-NEXT: entry:
44; CHECK-NEXT:   %block = alloca %1, align 8
45; CHECK-NOT:    alloca
46; CHECK:      }
47define void @qux(ptr %me) nounwind {
48entry:
49  %w = alloca ptr, align 8
50  %block = alloca %1, align 8
51  %0 = call ptr @llvm.objc.retain(ptr %me) nounwind
52  %1 = call ptr @llvm.objc.initWeak(ptr %w, ptr %0) nounwind
53  store ptr @_NSConcreteStackBlock, ptr %block, align 8
54  %block.flags = getelementptr inbounds %1, ptr %block, i64 0, i32 1
55  store i32 1107296256, ptr %block.flags, align 8
56  %block.reserved = getelementptr inbounds %1, ptr %block, i64 0, i32 2
57  store i32 0, ptr %block.reserved, align 4
58  %block.invoke = getelementptr inbounds %1, ptr %block, i64 0, i32 3
59  store ptr @__qux_block_invoke_0, ptr %block.invoke, align 8
60  %block.descriptor = getelementptr inbounds %1, ptr %block, i64 0, i32 4
61  store ptr @__block_descriptor_tmp, ptr %block.descriptor, align 8
62  %block.captured = getelementptr inbounds %1, ptr %block, i64 0, i32 5
63  %2 = call ptr @llvm.objc.loadWeak(ptr %w) nounwind
64  %3 = call ptr @llvm.objc.initWeak(ptr %block.captured, ptr %2) nounwind
65  call void @use_block(ptr %block) nounwind
66  call void @llvm.objc.destroyWeak(ptr %block.captured) nounwind
67  call void @llvm.objc.destroyWeak(ptr %w) nounwind
68  call void @llvm.objc.release(ptr %0) nounwind, !clang.imprecise_release !0
69  ret void
70}
71
72declare ptr @llvm.objc.retain(ptr)
73declare void @use_block(ptr) nounwind
74declare void @__qux_block_invoke_0(ptr %.block_descriptor) nounwind
75declare void @__copy_helper_block_(ptr, ptr) nounwind
76declare void @llvm.objc.copyWeak(ptr, ptr)
77declare void @__destroy_helper_block_(ptr) nounwind
78declare void @llvm.objc.release(ptr)
79declare ptr @bar()
80declare ptr @llvm.objc.initWeak(ptr, ptr)
81declare ptr @llvm.objc.loadWeak(ptr)
82declare void @use(ptr) nounwind
83declare void @llvm.objc.destroyWeak(ptr)
84
85; CHECK: attributes [[NUW]] = { nounwind }
86
87!0 = !{}
88