Clang Project

clang_source_code/test/CodeGenObjC/objc-non-trivial-struct-nrvo.m
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-arc -fobjc-runtime-has-weak -o - %s | FileCheck %s
2
3// CHECK: %[[STRUCT_TRIVIAL:.*]] = type { i32 }
4// CHECK: %[[STRUCT_TRIVIALBIG:.*]] = type { [64 x i32] }
5// CHECK: %[[STRUCT_STRONG:.*]] = type { i8* }
6// CHECK: %[[STRUCT_WEAK:.*]] = type { i8* }
7
8typedef struct {
9  int x;
10} Trivial;
11
12typedef struct {
13  int x[64];
14} TrivialBig;
15
16typedef struct {
17  id x;
18} Strong;
19
20typedef struct {
21  __weak id x;
22} Weak;
23
24// CHECK: define i32 @testTrivial()
25// CHECK: %[[RETVAL:.*]] = alloca %[[STRUCT_TRIVIAL]], align 4
26// CHECK-NEXT: call void @func0(%[[STRUCT_TRIVIAL]]* %[[RETVAL]])
27// CHECK-NOT: memcpy
28// CHECK: ret i32 %
29
30void func0(Trivial *);
31
32Trivial testTrivial(void) {
33  Trivial a;
34  func0(&a);
35  return a;
36}
37
38void func1(TrivialBig *);
39
40// CHECK: define void @testTrivialBig(%[[STRUCT_TRIVIALBIG]]* noalias sret %[[AGG_RESULT:.*]])
41// CHECK-NOT: alloca
42// CHECK: call void @func1(%[[STRUCT_TRIVIALBIG]]* %[[AGG_RESULT]])
43// CHECK-NEXT: ret void
44
45TrivialBig testTrivialBig(void) {
46  TrivialBig a;
47  func1(&a);
48  return a;
49}
50
51// CHECK: define i8* @testStrong()
52// CHECK: %[[RETVAL:.*]] = alloca %[[STRUCT_STRONG]], align 8
53// CHECK: %[[NRVO:.*]] = alloca i1, align 1
54// CHECK: %[[V0:.*]] = bitcast %[[STRUCT_STRONG]]* %[[RETVAL]] to i8**
55// CHECK: call void @__default_constructor_8_s0(i8** %[[V0]])
56// CHECK: store i1 true, i1* %[[NRVO]], align 1
57// CHECK: %[[NRVO_VAL:.*]] = load i1, i1* %[[NRVO]], align 1
58// CHECK: br i1 %[[NRVO_VAL]],
59
60// CHECK: %[[V1:.*]] = bitcast %[[STRUCT_STRONG]]* %[[RETVAL]] to i8**
61// CHECK: call void @__destructor_8_s0(i8** %[[V1]])
62// CHECK: br
63
64// CHECK: %[[COERCE_DIVE:.*]] = getelementptr inbounds %[[STRUCT_STRONG]], %[[STRUCT_STRONG]]* %[[RETVAL]], i32 0, i32 0
65// CHECK: %[[V2:.*]] = load i8*, i8** %[[COERCE_DIVE]], align 8
66// CHECK: ret i8* %[[V2]]
67
68Strong testStrong(void) {
69  Strong a;
70  return a;
71}
72
73// CHECK: define void @testWeak(%[[STRUCT_WEAK]]* noalias sret %[[AGG_RESULT:.*]])
74// CHECK: %[[NRVO:.*]] = alloca i1, align 1
75// CHECK: %[[V0:.*]] = bitcast %[[STRUCT_WEAK]]* %[[AGG_RESULT]] to i8**
76// CHECK: call void @__default_constructor_8_w0(i8** %[[V0]])
77// CHECK: store i1 true, i1* %[[NRVO]], align 1
78// CHECK: %[[NRVO_VAL:.*]] = load i1, i1* %[[NRVO]], align 1
79// CHECK: br i1 %[[NRVO_VAL]],
80
81// CHECK: %[[V1:.*]] = bitcast %[[STRUCT_WEAK]]* %[[AGG_RESULT]] to i8**
82// CHECK: call void @__destructor_8_w0(i8** %[[V1]])
83// CHECK: br
84
85// CHECK-NOT: call
86// CHECK: ret void
87
88Weak testWeak(void) {
89  Weak a;
90  return a;
91}
92
93// CHECK: define void @testWeak2(
94// CHECK: call void @__default_constructor_8_w0(
95// CHECK: call void @__default_constructor_8_w0(
96// CHECK: call void @__copy_constructor_8_8_w0(
97// CHECK: call void @__copy_constructor_8_8_w0(
98// CHECK: call void @__destructor_8_w0(
99// CHECK: call void @__destructor_8_w0(
100
101Weak testWeak2(int c) {
102  Weak a, b;
103  if (c)
104    return a;
105  else
106    return b;
107}
108
109// CHECK: define internal void @"\01-[C1 foo1]"(%[[STRUCT_WEAK]]* noalias sret %[[AGG_RESULT:.*]], %{{.*}}* %{{.*}}, i8* %{{.*}})
110// CHECK: %[[NRVO:.*]] = alloca i1, align 1
111// CHECK: %[[V0:.*]] = bitcast %[[STRUCT_WEAK]]* %[[AGG_RESULT]] to i8**
112// CHECK: call void @__default_constructor_8_w0(i8** %[[V0]])
113// CHECK: store i1 true, i1* %[[NRVO]], align 1
114// CHECK: %[[NRVO_VAL:.*]] = load i1, i1* %[[NRVO]], align 1
115// CHECK: br i1 %[[NRVO_VAL]],
116
117// CHECK: %[[V1:.*]] = bitcast %[[STRUCT_WEAK]]* %[[AGG_RESULT]] to i8**
118// CHECK: call void @__destructor_8_w0(i8** %[[V1]])
119// CHECK: br
120
121// CHECK-NOT: call
122// CHECK: ret void
123
124__attribute__((objc_root_class))
125@interface C1
126- (Weak)foo1;
127@end
128
129@implementation C1
130- (Weak)foo1 {
131  Weak a;
132  return a;
133}
134@end
135