xref: /llvm-project/libcxxabi/test/vendor/ibm/cond_reg_restore.pass.cpp (revision 2d3655037ccfa276cb0949c2ce0cff56985f6637)
1bc7f0fb5SXing Xue //===----------------------------------------------------------------------===//
2bc7f0fb5SXing Xue //
3bc7f0fb5SXing Xue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bc7f0fb5SXing Xue // See https://llvm.org/LICENSE.txt for license information.
5bc7f0fb5SXing Xue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bc7f0fb5SXing Xue //
7bc7f0fb5SXing Xue //===----------------------------------------------------------------------===//
8bc7f0fb5SXing Xue 
9bc7f0fb5SXing Xue // Check that the condition register is restored properly during unwinding
10bc7f0fb5SXing Xue // on AIX. Option -O3 is required so that the compiler will re-use the value
11bc7f0fb5SXing Xue // in the condition register instead of re-evaluating the condition expression.
12bc7f0fb5SXing Xue 
13*2d365503SXing Xue // REQUIRES: target={{.+}}-aix{{.*}}
14bc7f0fb5SXing Xue // ADDITIONAL_COMPILE_FLAGS: -O3
15bc7f0fb5SXing Xue // UNSUPPORTED: no-exceptions
16bc7f0fb5SXing Xue 
17bc7f0fb5SXing Xue #include <cstdlib>
18bc7f0fb5SXing Xue #include <cassert>
19bc7f0fb5SXing Xue 
20bc7f0fb5SXing Xue int __attribute__((noinline)) test2(int i) {
21bc7f0fb5SXing Xue   // The inline assembly forces the prologue/epilogue to save/restore the
22bc7f0fb5SXing Xue   // condition register.
23bc7f0fb5SXing Xue   asm volatile("nop" : : : "cr2");
24bc7f0fb5SXing Xue   if (i > 3) {
25bc7f0fb5SXing Xue     throw i;
26bc7f0fb5SXing Xue   }
27bc7f0fb5SXing Xue   srand(i);
28bc7f0fb5SXing Xue   return rand() + i;
29bc7f0fb5SXing Xue }
30bc7f0fb5SXing Xue 
31bc7f0fb5SXing Xue void __attribute__((noinline)) test(int argc, const char **argv) {
32bc7f0fb5SXing Xue   int a = atoi(argv[1]);
33bc7f0fb5SXing Xue   int b = atoi(argv[2]);
34bc7f0fb5SXing Xue   try {
35bc7f0fb5SXing Xue     test2(a < b ? argc : b);
36bc7f0fb5SXing Xue   } catch (int num) {
37bc7f0fb5SXing Xue     assert(!(a < b));
38bc7f0fb5SXing Xue   }
39bc7f0fb5SXing Xue }
40bc7f0fb5SXing Xue int main(int, char**) {
41bc7f0fb5SXing Xue   const char *av[]={"a.out", "12", "10"};
42bc7f0fb5SXing Xue   test(4, av);
43bc7f0fb5SXing Xue   return 0;
44bc7f0fb5SXing Xue }
45