| 1 |
// REQUIRES: x86-registered-target |
| 2 |
// RUN: %clang_cl -c --target=x86_64-windows-msvc -EHs-c- -O2 -GS- \ |
| 3 |
// RUN: -Xclang=-import-call-optimization \ |
| 4 |
// RUN: -clang:-S -clang:-o- -- %s 2>&1 \ |
| 5 |
// RUN: | FileCheck %s |
| 6 |
|
| 7 |
#ifdef __clang__ |
| 8 |
#define NO_TAIL __attribute((disable_tail_calls)) |
| 9 |
#else |
| 10 |
#define NO_TAIL |
| 11 |
#endif |
| 12 |
|
| 13 |
void might_throw(); |
| 14 |
void other_func(int x); |
| 15 |
|
| 16 |
void does_not_throw() noexcept(true); |
| 17 |
|
| 18 |
extern "C" void __declspec(dllimport) some_dll_import(); |
| 19 |
|
| 20 |
class HasDtor { |
| 21 |
int x; |
| 22 |
char foo[40]; |
| 23 |
|
| 24 |
public: |
| 25 |
explicit HasDtor(int x); |
| 26 |
~HasDtor(); |
| 27 |
}; |
| 28 |
|
| 29 |
void normal_has_regions() { |
| 30 |
{ |
| 31 |
HasDtor hd{42}; |
| 32 |
|
| 33 |
// because state changes, we expect the HasDtor::HasDtor() call to have a NOP |
| 34 |
might_throw(); |
| 35 |
} |
| 36 |
|
| 37 |
other_func(10); |
| 38 |
} |
| 39 |
// CHECK-LABEL: .def "?normal_has_regions@@YAXXZ" |
| 40 |
// CHECK: .seh_endprologue |
| 41 |
// CHECK: call "??0HasDtor@@QEAA@H@Z" |
| 42 |
// CHECK-NEXT: call "?might_throw@@YAXXZ" |
| 43 |
// CHECK-NEXT: mov |
| 44 |
// CHECK: call "??1HasDtor@@QEAA@XZ" |
| 45 |
// CHECK-NEXT: mov ecx, 10 |
| 46 |
// CHECK-NEXT: call "?other_func@@YAXH@Z" |
| 47 |
// CHECK-NEXT: nop |
| 48 |
// CHECK-NEXT: .seh_startepilogue |
| 49 |
// CHECK-NOT: "$ip2state$?normal_has_regions@@YAXXZ" |
| 50 |
|
| 51 |
// This tests a tail call to a destructor. |
| 52 |
void case_dtor_arg_empty_body(HasDtor x) |
| 53 |
{ |
| 54 |
} |
| 55 |
// CHECK-LABEL: .def "?case_dtor_arg_empty_body@@YAXVHasDtor@@@Z" |
| 56 |
// CHECK: jmp "??1HasDtor@@QEAA@XZ" |
| 57 |
|
| 58 |
int case_dtor_arg_empty_with_ret(HasDtor x) |
| 59 |
{ |
| 60 |
// The call to HasDtor::~HasDtor() should NOT have a NOP because the |
| 61 |
// following "mov eax, 100" instruction is in the same EH state. |
| 62 |
return 100; |
| 63 |
} |
| 64 |
// CHECK-LABEL: .def "?case_dtor_arg_empty_with_ret@@YAHVHasDtor@@@Z" |
| 65 |
// CHECK: .seh_endprologue |
| 66 |
// CHECK: call "??1HasDtor@@QEAA@XZ" |
| 67 |
// CHECK-NOT: nop |
| 68 |
// CHECK: mov eax, 100 |
| 69 |
// CHECK: .seh_startepilogue |
| 70 |
// CHECK: .seh_endepilogue |
| 71 |
// CHECK: .seh_endproc |
| 72 |
|
| 73 |
void case_except_simple_call() NO_TAIL |
| 74 |
{ |
| 75 |
does_not_throw(); |
| 76 |
} |
| 77 |
|
| 78 |
// This tests that the destructor is called right before SEH_BeginEpilogue, |
| 79 |
// but in a function that has a return value. |
| 80 |
int case_dtor_arg_calls_no_throw(HasDtor x) |
| 81 |
{ |
| 82 |
does_not_throw(); // no NOP expected |
| 83 |
return 100; |
| 84 |
} |
| 85 |
|
| 86 |
// Check the behavior of CALLs that are at the end of MBBs. If a CALL is within |
| 87 |
// a non-null EH state (state -1) and is at the end of an MBB, then we expect |
| 88 |
// to find an EH_LABEL after the CALL. This causes us to insert a NOP, which |
| 89 |
// is the desired result. |
| 90 |
void case_dtor_runs_after_join(int x) { |
| 91 |
|
| 92 |
// ctor call does not need a NOP, because it has real instructions after it |
| 93 |
HasDtor hd{42}; |
| 94 |
|
| 95 |
if (x) { |
| 96 |
might_throw(); |
| 97 |
} else { |
| 98 |
other_func(10); |
| 99 |
} |
| 100 |
does_not_throw(); |
| 101 |
// ~HasDtor() runs |
| 102 |
} |
| 103 |
|
| 104 |
// CHECK-LABEL: .def "?case_dtor_runs_after_join@@YAXH@Z" |
| 105 |
// CHECK: .seh_endprologue |
| 106 |
// CHECK: call "??0HasDtor@@QEAA@H@Z" |
| 107 |
// CHECK-NEXT: test |
| 108 |
// CHECK: call "?might_throw@@YAXXZ" |
| 109 |
// CHECK-NEXT: jmp |
| 110 |
// CHECK: call "?other_func@@YAXH@Z" |
| 111 |
// CHECK-NEXT: .LBB |
| 112 |
// CHECK: call "?does_not_throw@@YAXXZ" |
| 113 |
// CHECK-NEXT: lea |
| 114 |
// CHECK-NEXT: call "??1HasDtor@@QEAA@XZ" |
| 115 |
// CHECK-NEXT: nop |
| 116 |
// CHECK-NEXT: .seh_startepilogue |
| 117 |
// CHECK-NOT: "$ip2state$?case_dtor_runs_after_join@@YAXH@Z": |
| 118 |
|
| 119 |
|
| 120 |
// Check the behavior of NOP padding around tail calls. |
| 121 |
// We do not expect to insert NOPs around tail calls. |
| 122 |
// However, the first call (to other_func()) does get a NOP |
| 123 |
// because it comes before .seh_startepilogue. |
| 124 |
void case_tail_call_no_eh() { |
| 125 |
// ordinary call |
| 126 |
other_func(10); |
| 127 |
|
| 128 |
// tail call; no NOP padding after JMP |
| 129 |
does_not_throw(); |
| 130 |
} |
| 131 |
|
| 132 |
// CHECK-LABEL: .def "?case_tail_call_no_eh@@YAXXZ" |
| 133 |
// CHECK: .seh_endprologue |
| 134 |
// CHECK: call "?other_func@@YAXH@Z" |
| 135 |
// CHECK-NEXT: nop |
| 136 |
// CHECK-NEXT: .seh_startepilogue |
| 137 |
// CHECK: .seh_endepilogue |
| 138 |
// CHECK: jmp "?does_not_throw@@YAXXZ" |
| 139 |
// CHECK-NOT: nop |
| 140 |
// CHECK: .seh_endproc |