Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / lambda-expressions.cpp
blob3ad982a195cc9610d34ab14279f96fc2ce251b08
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fblocks -emit-llvm -o - %s -fexceptions -std=c++11 | FileCheck %s
3 // CHECK-NOT: @unused
4 auto unused = [](int i) { return i+1; };
6 // CHECK: @used = internal global
7 auto used = [](int i) { return i+1; };
8 void *use = &used;
10 // CHECK: @cvar ={{.*}} global
11 extern "C" auto cvar = []{};
13 // CHECK-LABEL: define{{.*}} i32 @_Z9ARBSizeOfi(i32
14 int ARBSizeOf(int n) {
15 typedef double(T)[8][n];
16 using TT = double[8][n];
17 return [&]() -> int {
18 typedef double(T1)[8][n];
19 using TT1 = double[8][n];
20 return [&n]() -> int {
21 typedef double(T2)[8][n];
22 using TT2 = double[8][n];
23 return sizeof(T) + sizeof(T1) + sizeof(T2) + sizeof(TT) + sizeof(TT1) + sizeof(TT2);
24 }();
25 }();
28 // CHECK-LABEL: define internal noundef i32 @"_ZZ9ARBSizeOfiENK3$_0clEv"
30 int a() { return []{ return 1; }(); }
31 // CHECK-LABEL: define{{.*}} i32 @_Z1av
32 // CHECK: call noundef i32 @"_ZZ1avENK3$_0clEv"
33 // CHECK-LABEL: define internal noundef i32 @"_ZZ1avENK3$_0clEv"
34 // CHECK: ret i32 1
36 int b(int x) { return [x]{return x;}(); }
37 // CHECK-LABEL: define{{.*}} i32 @_Z1bi
38 // CHECK: store i32
39 // CHECK: load i32, ptr
40 // CHECK: store i32
41 // CHECK: call noundef i32 @"_ZZ1biENK3$_0clEv"
42 // CHECK-LABEL: define internal noundef i32 @"_ZZ1biENK3$_0clEv"
43 // CHECK: load i32, ptr
44 // CHECK: ret i32
46 int c(int x) { return [&x]{return x;}(); }
47 // CHECK-LABEL: define{{.*}} i32 @_Z1ci
48 // CHECK: store i32
49 // CHECK: store ptr
50 // CHECK: call noundef i32 @"_ZZ1ciENK3$_0clEv"
51 // CHECK-LABEL: define internal noundef i32 @"_ZZ1ciENK3$_0clEv"
52 // CHECK: load ptr, ptr
53 // CHECK: load i32, ptr
54 // CHECK: ret i32
56 struct D { D(); D(const D&); int x; };
57 int d(int x) { D y[10]; return [x,y] { return y[x].x; }(); }
59 // CHECK-LABEL: define{{.*}} i32 @_Z1di
60 // CHECK: call void @_ZN1DC1Ev
61 // CHECK: br label
62 // CHECK: call void @_ZN1DC1ERKS_
63 // CHECK: icmp eq i64 %{{.*}}, 10
64 // CHECK: br i1
65 // CHECK: call noundef i32 @"_ZZ1diENK3$_0clEv"
66 // CHECK-LABEL: define internal noundef i32 @"_ZZ1diENK3$_0clEv"
67 // CHECK: load i32, ptr
68 // CHECK: load i32, ptr
69 // CHECK: ret i32
71 struct E { E(); E(const E&); ~E(); int x; };
72 int e(E a, E b, bool cond) { return [a,b,cond](){ return (cond ? a : b).x; }(); }
73 // CHECK-LABEL: define{{.*}} i32 @_Z1e1ES_b
74 // CHECK: call void @_ZN1EC1ERKS_
75 // CHECK: invoke void @_ZN1EC1ERKS_
76 // CHECK: invoke noundef i32 @"_ZZ1e1ES_bENK3$_0clEv"
77 // CHECK: call void @"_ZZ1e1ES_bEN3$_0D1Ev"
78 // CHECK: call void @"_ZZ1e1ES_bEN3$_0D1Ev"
80 // CHECK-LABEL: define internal noundef i32 @"_ZZ1e1ES_bENK3$_0clEv"
81 // CHECK: trunc i8
82 // CHECK: load i32, ptr
83 // CHECK: ret i32
85 void f() {
86 // CHECK-LABEL: define{{.*}} void @_Z1fv()
87 // CHECK: @"_ZZ1fvENK3$_0cvPFiiiEEv"
88 // CHECK-NEXT: store ptr
89 // CHECK-NEXT: ret void
90 int (*fp)(int, int) = [](int x, int y){ return x + y; };
93 static int k;
94 int g() {
95 int &r = k;
96 // CHECK-LABEL: define internal noundef i32 @"_ZZ1gvENK3$_0clEv"(
97 // CHECK-NOT: }
98 // CHECK: load i32, ptr @_ZL1k,
99 return [] { return r; } ();
102 // PR14773
103 // CHECK: [[ARRVAL:%[0-9a-zA-Z]*]] = load i32, ptr @_ZZ14staticarrayrefvE5array, align 4
104 // CHECK-NEXT: store i32 [[ARRVAL]]
105 void staticarrayref(){
106 static int array[] = {};
107 (void)[](){
108 int (&xxx)[0] = array;
109 int y = xxx[0];
110 }();
113 // CHECK-LABEL: define internal noundef ptr @"_ZZ11PR22071_funvENK3$_0clEv"
114 // CHECK: ret ptr @PR22071_var
115 int PR22071_var;
116 int *PR22071_fun() {
117 constexpr int &y = PR22071_var;
118 return [&] { return &y; }();
121 namespace pr28595 {
122 struct Temp {
123 Temp();
124 ~Temp() noexcept(false);
126 struct A {
127 A();
128 A(const A &a, const Temp &temp = Temp());
129 ~A();
132 void after_init() noexcept;
134 // CHECK-LABEL: define{{.*}} void @_ZN7pr285954testEv()
135 void test() {
136 // CHECK: %[[SRC:.*]] = alloca [3 x [5 x %[[A:.*]]]], align 1
137 A array[3][5];
139 // Skip over the initialization loop.
140 // CHECK: call {{.*}}after_init
141 after_init();
143 // CHECK: %[[DST_0:.*]] = getelementptr {{.*}} ptr %[[DST:.*]], i64 0, i64 0
144 // CHECK: br label
145 // CHECK: %[[I:.*]] = phi i64 [ 0, %{{.*}} ], [ %[[I_NEXT:.*]], {{.*}} ]
146 // CHECK: %[[DST_I:.*]] = getelementptr {{.*}} ptr %[[DST_0]], i64 %[[I]]
147 // CHECK: %[[SRC_I:.*]] = getelementptr {{.*}} ptr %[[SRC]], i64 0, i64 %[[I]]
149 // CHECK: %[[DST_I_0:.*]] = getelementptr {{.*}} ptr %[[DST_I]], i64 0, i64 0
150 // CHECK: br label
151 // CHECK: %[[J:.*]] = phi i64 [ 0, %{{.*}} ], [ %[[J_NEXT:.*]], {{.*}} ]
152 // CHECK: %[[DST_I_J:.*]] = getelementptr {{.*}} ptr %[[DST_I_0]], i64 %[[J]]
153 // CHECK: %[[SRC_I_J:.*]] = getelementptr {{.*}} ptr %[[SRC_I]], i64 0, i64 %[[J]]
155 // CHECK: invoke void @_ZN7pr285954TempC1Ev
156 // CHECK: invoke void @_ZN7pr285951AC1ERKS0_RKNS_4TempE
157 // CHECK: invoke void @_ZN7pr285954TempD1Ev
159 // CHECK: add nuw i64 %[[J]], 1
160 // CHECK: icmp eq
161 // CHECK: br i1
163 // CHECK: add nuw i64 %[[I]], 1
164 // CHECK: icmp eq
165 // CHECK: br i1
167 // CHECK: ret void
169 // CHECK: landingpad
170 // CHECK: landingpad
171 // CHECK: br label %[[CLEANUP:.*]]{{$}}
172 // CHECK: landingpad
173 // CHECK: invoke void @_ZN7pr285954TempD1Ev
174 // CHECK: br label %[[CLEANUP]]
176 // CHECK: [[CLEANUP]]:
177 // CHECK: icmp eq ptr %[[DST_0]], %[[DST_I_J]]
178 // CHECK: %[[T0:.*]] = phi ptr
179 // CHECK: %[[T1:.*]] = getelementptr inbounds %[[A]], ptr %[[T0]], i64 -1
180 // CHECK: call void @_ZN7pr285951AD1Ev(ptr {{[^,]*}} %[[T1]])
181 // CHECK: icmp eq ptr %[[T1]], %[[DST_0]]
182 (void) [array]{};
186 // CHECK-LABEL: define internal void @"_ZZ1e1ES_bEN3$_0D2Ev"
188 // CHECK-LABEL: define internal noundef i32 @"_ZZ1fvEN3$_08__invokeEii"
189 // CHECK: store i32
190 // CHECK-NEXT: store i32
191 // CHECK-NEXT: load i32, ptr
192 // CHECK-NEXT: load i32, ptr
193 // CHECK-NEXT: call noundef i32 @"_ZZ1fvENK3$_0clEii"
194 // CHECK-NEXT: ret i32
196 // CHECK-LABEL: define internal void @"_ZZ1hvEN3$_08__invokeEv"(ptr noalias sret(%struct.A) align 1 %agg.result) {{.*}} {
197 // CHECK: call void @"_ZZ1hvENK3$_0clEv"(ptr sret(%struct.A) align 1 %agg.result,
198 // CHECK-NEXT: ret void
199 struct A { ~A(); };
200 void h() {
201 A (*h)() = [] { return A(); };
204 struct XXX {};
205 void nestedCapture () {
206 XXX localKey;
207 ^() {
208 [&]() {
209 ^{ XXX k = localKey; };
214 // Ensure we don't assert here.
215 struct CaptureArrayAndThis {
216 CaptureArrayAndThis() {
217 char array[] = "floop";
218 [array, this] {};
220 } capture_array_and_this;