Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -emit-llvm -O1 -o - %s | FileCheck %s |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -emit-llvm -O1 -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; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 16 | // CHECK: call void @_ZN1XC1Ev |
| 17 | // CHECK-NEXT: ret void |
| 18 | |
| 19 | // CHECK-EH: call void @_ZN1XC1Ev |
| 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) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 27 | // CHECK: tail call void @_ZN1XC1Ev |
| 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; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 33 | // CHECK-EH: tail call void @_ZN1XC1Ev |
| 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 | |
| 48 | // CHECK: call void @_ZN1XC1Ev |
| 49 | // CHECK-NEXT: call void @_ZN1XC1Ev |
| 50 | // CHECK: call void @_ZN1XC1ERKS_ |
| 51 | // CHECK: call void @_ZN1XC1ERKS_ |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 52 | // CHECK: call void @_ZN1XD1Ev |
| 53 | // CHECK: call void @_ZN1XD1Ev |
| 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 | |
| 58 | // CHECK-EH: call void @_ZN1XC1Ev |
| 59 | // CHECK-EH-NEXT: invoke void @_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' |
| 67 | // CHECK-EH: invoke void @_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' |
| 71 | // CHECK-EH: call i8* @llvm.eh.exception() |
| 72 | // CHECK-EH: call i32 (i8*, i8*, ...)* @llvm.eh.selector |
| 73 | // CHECK-EH-NEXT: br label |
| 74 | // -> %eh.cleanup |
| 75 | |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 76 | // %lpad1: landing pad for return copy ctors, EH cleanup for 'y' |
Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 77 | // CHECK-EH: invoke void @_ZN1XD1Ev |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 78 | // -> %eh.cleanup, %terminate.lpad |
| 79 | |
| 80 | // %if.end: returning 'y' |
| 81 | // CHECK-EH: invoke void @_ZN1XC1ERKS_ |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 82 | // -> %cleanup, %lpad1 |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 83 | |
| 84 | // %cleanup: normal cleanup for 'y' |
Douglas Gregor | b5b30b9 | 2010-05-15 16:39:56 +0000 | [diff] [blame] | 85 | // CHECK-EH: invoke void @_ZN1XD1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 86 | // -> %invoke.cont11, %lpad |
| 87 | |
| 88 | // %invoke.cont11: normal cleanup for 'x' |
| 89 | // CHECK-EH: call void @_ZN1XD1Ev |
| 90 | // CHECK-EH-NEXT: ret void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 91 | |
| 92 | // %eh.cleanup: EH cleanup for 'x' |
| 93 | // CHECK-EH: invoke void @_ZN1XD1Ev |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 94 | // -> %invoke.cont17, %terminate.lpad |
| 95 | |
| 96 | // %invoke.cont17: rethrow block for %eh.cleanup. |
| 97 | // This really should be elsewhere in the function. |
| 98 | // CHECK-EH: call void @_Unwind_Resume_or_Rethrow |
| 99 | // CHECK-EH-NEXT: unreachable |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 100 | |
| 101 | // %terminate.lpad: terminate landing pad. |
| 102 | // CHECK-EH: call i8* @llvm.eh.exception() |
| 103 | // CHECK-EH-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector |
| 104 | // CHECK-EH-NEXT: call void @_ZSt9terminatev() |
| 105 | // CHECK-EH-NEXT: unreachable |
| 106 | |
Douglas Gregor | d86c477 | 2010-05-15 06:46:45 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | X test3(bool B) { |
| 110 | // FIXME: We don't manage to apply NRVO here, although we could. |
| 111 | { |
| 112 | X y; |
| 113 | return y; |
| 114 | } |
| 115 | X x; |
| 116 | return x; |
| 117 | } |
Douglas Gregor | 3d91bbc | 2010-05-17 15:52:46 +0000 | [diff] [blame] | 118 | |
| 119 | extern "C" void exit(int) throw(); |
| 120 | |
| 121 | // CHECK: define void @_Z5test4b |
| 122 | X test4(bool B) { |
| 123 | { |
| 124 | // CHECK: tail call void @_ZN1XC1Ev |
| 125 | X x; |
| 126 | // CHECK: br i1 |
| 127 | if (B) |
| 128 | return x; |
| 129 | } |
| 130 | // CHECK: tail call void @_ZN1XD1Ev |
| 131 | // CHECK: tail call void @exit(i32 1) |
| 132 | exit(1); |
| 133 | } |