Ulrich Weigand | 9230997 | 2012-10-24 12:22:56 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm -O1 -o - %s | FileCheck %s |
| 2 | // RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm -O1 -fcxx-exceptions -fexceptions -o - %s | FileCheck --check-prefix=CHECK-EH %s |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 3 | |
| 4 | // Test code generation for the named return value optimization. |
| 5 | class X { |
| 6 | public: |
| 7 | X(); |
| 8 | X(const X&); |
| 9 | ~X(); |
| 10 | }; |
| 11 | |
| 12 | // CHECK: define void @_Z5test0v |
Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 13 | // CHECK-EH: define void @_Z5test0v |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 14 | X test0() { |
| 15 | X x; |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 16 | // CHECK: call {{.*}} @_ZN1XC1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 17 | // CHECK-NEXT: ret void |
| 18 | |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 19 | // CHECK-EH: call {{.*}} @_ZN1XC1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 20 | // CHECK-EH-NEXT: ret void |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 21 | return x; |
| 22 | } |
| 23 | |
| 24 | // CHECK: define void @_Z5test1b( |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 25 | // CHECK-EH: define void @_Z5test1b( |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 26 | X test1(bool B) { |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 27 | // CHECK: tail call {{.*}} @_ZN1XC1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 28 | // CHECK-NEXT: ret void |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 29 | X x; |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 30 | if (B) |
| 31 | return (x); |
| 32 | return x; |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 33 | // CHECK-EH: tail call {{.*}} @_ZN1XC1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 34 | // CHECK-EH-NEXT: ret void |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // CHECK: define void @_Z5test2b |
Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 38 | // CHECK-EH: define void @_Z5test2b |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 39 | X test2(bool B) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 40 | // No NRVO. |
| 41 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 42 | X x; |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 43 | X y; |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 44 | if (B) |
| 45 | return y; |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 46 | return x; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 47 | |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 48 | // CHECK: call {{.*}} @_ZN1XC1Ev |
| 49 | // CHECK-NEXT: call {{.*}} @_ZN1XC1Ev |
| 50 | // CHECK: call {{.*}} @_ZN1XC1ERKS_ |
| 51 | // CHECK: call {{.*}} @_ZN1XC1ERKS_ |
| 52 | // CHECK: call {{.*}} @_ZN1XD1Ev |
| 53 | // CHECK: call {{.*}} @_ZN1XD1Ev |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 54 | // CHECK: ret void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 55 | |
| 56 | // The block ordering in the -fexceptions IR is unfortunate. |
| 57 | |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 58 | // CHECK-EH: call {{.*}} @_ZN1XC1Ev |
| 59 | // CHECK-EH-NEXT: invoke {{.*}} @_ZN1XC1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 60 | // -> %invoke.cont, %lpad |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 61 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 62 | // %invoke.cont: |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 63 | // CHECK-EH: br i1 |
| 64 | // -> %if.then, %if.end |
| 65 | |
| 66 | // %if.then: returning 'x' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 67 | // CHECK-EH: invoke {{.*}} @_ZN1XC1ERKS_ |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 68 | // -> %cleanup, %lpad1 |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 69 | |
| 70 | // %lpad: landing pad for ctor of 'y', dtor of 'y' |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 71 | // CHECK-EH: [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) |
| 72 | // CHECK-EH-NEXT: cleanup |
| 73 | // CHECK-EH-NEXT: extractvalue { i8*, i32 } [[CAUGHTVAL]], 0 |
| 74 | // CHECK-EH-NEXT: extractvalue { i8*, i32 } [[CAUGHTVAL]], 1 |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 75 | // CHECK-EH-NEXT: br label |
| 76 | // -> %eh.cleanup |
| 77 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 78 | // %lpad1: landing pad for return copy ctors, EH cleanup for 'y' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 79 | // CHECK-EH: invoke {{.*}} @_ZN1XD1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 80 | // -> %eh.cleanup, %terminate.lpad |
| 81 | |
| 82 | // %if.end: returning 'y' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 83 | // CHECK-EH: invoke {{.*}} @_ZN1XC1ERKS_ |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 84 | // -> %cleanup, %lpad1 |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 85 | |
| 86 | // %cleanup: normal cleanup for 'y' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 87 | // CHECK-EH: invoke {{.*}} @_ZN1XD1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 88 | // -> %invoke.cont11, %lpad |
| 89 | |
| 90 | // %invoke.cont11: normal cleanup for 'x' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 91 | // CHECK-EH: call {{.*}} @_ZN1XD1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 92 | // CHECK-EH-NEXT: ret void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 93 | |
| 94 | // %eh.cleanup: EH cleanup for 'x' |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 95 | // CHECK-EH: invoke {{.*}} @_ZN1XD1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 96 | // -> %invoke.cont17, %terminate.lpad |
| 97 | |
| 98 | // %invoke.cont17: rethrow block for %eh.cleanup. |
| 99 | // This really should be elsewhere in the function. |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 100 | // CHECK-EH: resume { i8*, i32 } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 101 | |
| 102 | // %terminate.lpad: terminate landing pad. |
John McCall | 66b2277 | 2013-02-12 03:51:46 +0000 | [diff] [blame] | 103 | // CHECK-EH: [[T0:%.*]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 104 | // CHECK-EH-NEXT: catch i8* null |
John McCall | 66b2277 | 2013-02-12 03:51:46 +0000 | [diff] [blame] | 105 | // CHECK-EH-NEXT: [[T1:%.*]] = extractvalue { i8*, i32 } [[T0]], 0 |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 106 | // CHECK-EH-NEXT: call void @__clang_call_terminate(i8* [[T1]]) [[NR_NUW:#[0-9]+]] |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 107 | // CHECK-EH-NEXT: unreachable |
| 108 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | X test3(bool B) { |
| 112 | // FIXME: We don't manage to apply NRVO here, although we could. |
| 113 | { |
| 114 | X y; |
| 115 | return y; |
| 116 | } |
| 117 | X x; |
| 118 | return x; |
| 119 | } |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 120 | |
| 121 | extern "C" void exit(int) throw(); |
| 122 | |
| 123 | // CHECK: define void @_Z5test4b |
| 124 | X test4(bool B) { |
| 125 | { |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 126 | // CHECK: tail call {{.*}} @_ZN1XC1Ev |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 127 | X x; |
| 128 | // CHECK: br i1 |
| 129 | if (B) |
| 130 | return x; |
| 131 | } |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 132 | // CHECK: tail call {{.*}} @_ZN1XD1Ev |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 133 | // CHECK: tail call void @exit(i32 1) |
| 134 | exit(1); |
| 135 | } |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | e41721e | 2011-02-19 19:23:03 +0000 | [diff] [blame] | 137 | #ifdef __EXCEPTIONS |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 138 | // CHECK-EH: define void @_Z5test5 |
| 139 | void may_throw(); |
| 140 | X test5() { |
| 141 | try { |
| 142 | may_throw(); |
| 143 | } catch (X x) { |
Eli Friedman | 12e9f8e | 2011-06-14 21:20:53 +0000 | [diff] [blame] | 144 | // CHECK-EH: invoke {{.*}} @_ZN1XC1ERKS_ |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 145 | // CHECK-EH: call void @__cxa_end_catch() |
| 146 | // CHECK-EH: ret void |
| 147 | return x; |
| 148 | } |
| 149 | } |
Anders Carlsson | e41721e | 2011-02-19 19:23:03 +0000 | [diff] [blame] | 150 | #endif |
John McCall | 1cd76e8 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 151 | |
| 152 | // rdar://problem/10430868 |
| 153 | // CHECK: define void @_Z5test6v |
| 154 | X test6() { |
| 155 | X a __attribute__((aligned(8))); |
| 156 | return a; |
| 157 | // CHECK: [[A:%.*]] = alloca [[X:%.*]], align 8 |
Eli Friedman | 194428c | 2011-11-11 23:36:04 +0000 | [diff] [blame] | 158 | // CHECK-NEXT: call {{.*}} @_ZN1XC1Ev([[X]]* [[A]]) |
| 159 | // CHECK-NEXT: call {{.*}} @_ZN1XC1ERKS_([[X]]* {{%.*}}, [[X]]* [[A]]) |
| 160 | // CHECK-NEXT: call {{.*}} @_ZN1XD1Ev([[X]]* [[A]]) |
John McCall | 1cd76e8 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 161 | // CHECK-NEXT: ret void |
| 162 | } |
Bill Wendling | 4e1125f | 2013-02-22 09:10:20 +0000 | [diff] [blame] | 163 | |
| 164 | // CHECK-EH: attributes [[NR_NUW]] = { noreturn nounwind } |