xref: /llvm-project/libcxxabi/test/vendor/ibm/vec_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 PowerPC vector registers are restored properly during
10bc7f0fb5SXing Xue // unwinding. Option -mabi=vec-extabi is required to compile the test case.
11bc7f0fb5SXing Xue 
12*2d365503SXing Xue // REQUIRES: target={{.+}}-aix{{.*}}
13bc7f0fb5SXing Xue // ADDITIONAL_COMPILE_FLAGS: -mabi=vec-extabi
14bc7f0fb5SXing Xue // UNSUPPORTED: no-exceptions
15bc7f0fb5SXing Xue 
16bc7f0fb5SXing Xue // AIX does not support the eh_frame section. Instead, the traceback table
17bc7f0fb5SXing Xue // located at the end of each function provides the information for stack
18bc7f0fb5SXing Xue // unwinding. Non-volatile GRs, FRs, and VRs clobbered by the function are
19bc7f0fb5SXing Xue // saved on the stack and the numbers of saved registers are available in the
20bc7f0fb5SXing Xue // traceback table. Registers are saved from high number to low consecutively,
211e3e3e28SXing Xue // e.g., if n VRs are saved, the order on the stack will be VR31, VR30, ...,
221e3e3e28SXing Xue // VR31-n+1. This test cases checks the unwinder gets to the location of saved
23bc7f0fb5SXing Xue // VRs which should be 16-byte aligned and restores them correctly based on
24bc7f0fb5SXing Xue // the number specified in the traceback table. To simplify, only the 2 high
25bc7f0fb5SXing Xue // numbered VRs are checked. Because PowerPC CPUs do not have instructions to
261e3e3e28SXing Xue // assign a literal value to a VR directly until Power10, and the instructions
271e3e3e28SXing Xue // to assign to a VR from a GR and vice versa are not available until Power8,
281e3e3e28SXing Xue // vector instructions available on Power7 are used to facilitate the test
291e3e3e28SXing Xue // so that it can run on all supported PowerPC architectures. In the code
301e3e3e28SXing Xue // below, VR31 is equivalent to VS63, VR30 is equivalent to VS62 (see PowerPC
311e3e3e28SXing Xue // documents for details).
32bc7f0fb5SXing Xue //
33bc7f0fb5SXing Xue 
34bc7f0fb5SXing Xue #include <cstdlib>
35bc7f0fb5SXing Xue #include <cassert>
36bc7f0fb5SXing Xue 
37bc7f0fb5SXing Xue int __attribute__((noinline)) test2(int i)
38bc7f0fb5SXing Xue {
39bc7f0fb5SXing Xue   if (i > 3)
40bc7f0fb5SXing Xue     throw i;
41bc7f0fb5SXing Xue   srand(i);
42bc7f0fb5SXing Xue   return rand();
43bc7f0fb5SXing Xue }
44bc7f0fb5SXing Xue 
45bc7f0fb5SXing Xue int __attribute__((noinline)) test(int i) {
461e3e3e28SXing Xue   // Clobber VS63 and VS62 in the function body.
471e3e3e28SXing Xue   // Set VS63 to 16 bytes each with value 9
481e3e3e28SXing Xue   asm volatile("vspltisb 31, 9" : : : "v31");
491e3e3e28SXing Xue 
501e3e3e28SXing Xue   // Set VS62 to 16 bytes each with value 12
511e3e3e28SXing Xue   asm volatile("vspltisb 30, 12" : : : "v30");
52bc7f0fb5SXing Xue   return test2(i);
53bc7f0fb5SXing Xue }
541e3e3e28SXing Xue #define cmpVS63(vec, result)                                                                                           \
551e3e3e28SXing Xue   {                                                                                                                    \
561e3e3e28SXing Xue     vector unsigned char gbg;                                                                                          \
571e3e3e28SXing Xue     asm volatile("vcmpequb. %[gbg], 31, %[veca];"                                                                      \
581e3e3e28SXing Xue                  "mfocrf %[res], 2;"                                                                                   \
591e3e3e28SXing Xue                  "rlwinm %[res], %[res], 25, 31, 31"                                                                   \
601e3e3e28SXing Xue                  : [res] "=r"(result), [gbg] "=v"(gbg)                                                                 \
611e3e3e28SXing Xue                  : [veca] "v"(vec)                                                                                     \
621e3e3e28SXing Xue                  : "cr6");                                                                                             \
631e3e3e28SXing Xue   }
64bc7f0fb5SXing Xue 
651e3e3e28SXing Xue #define cmpVS62(vec, result)                                                                                           \
661e3e3e28SXing Xue   {                                                                                                                    \
671e3e3e28SXing Xue     vector unsigned char gbg;                                                                                          \
681e3e3e28SXing Xue     asm volatile("vcmpequb. %[gbg], 30, %[veca];"                                                                      \
691e3e3e28SXing Xue                  "mfocrf %[res], 2;"                                                                                   \
701e3e3e28SXing Xue                  "rlwinm %[res], %[res], 25, 31, 31"                                                                   \
711e3e3e28SXing Xue                  : [res] "=r"(result), [gbg] "=v"(gbg)                                                                 \
721e3e3e28SXing Xue                  : [veca] "v"(vec)                                                                                     \
731e3e3e28SXing Xue                  : "cr6");                                                                                             \
741e3e3e28SXing Xue   }
75bc7f0fb5SXing Xue int main(int, char**) {
761e3e3e28SXing Xue   // Set VS63 to 16 bytes each with value 1
771e3e3e28SXing Xue   asm volatile("vspltisb 31, 1" : : : "v31");
781e3e3e28SXing Xue 
791e3e3e28SXing Xue   // Set VS62 to 16 bytes each with value 2
801e3e3e28SXing Xue   asm volatile("vspltisb 30, 2" : : : "v30");
811e3e3e28SXing Xue   vector unsigned long long expectedVS63Value = {0x101010101010101, 0x101010101010101};
821e3e3e28SXing Xue   vector unsigned long long expectedVS62Value = {0x202020202020202, 0x202020202020202};
83bc7f0fb5SXing Xue   try {
84bc7f0fb5SXing Xue     test(4);
85bc7f0fb5SXing Xue   } catch (int num) {
861e3e3e28SXing Xue     // If the unwinder restores VS63 and VS62 correctly, they should contain
871e3e3e28SXing Xue     // 0x01's and 0x02's respectively instead of 0x09's and 0x12's.
881e3e3e28SXing Xue     bool isEqualVS63, isEqualVS62;
891e3e3e28SXing Xue     cmpVS63(expectedVS63Value, isEqualVS63);
901e3e3e28SXing Xue     cmpVS62(expectedVS62Value, isEqualVS62);
911e3e3e28SXing Xue     assert(isEqualVS63 && isEqualVS62);
92bc7f0fb5SXing Xue   }
93bc7f0fb5SXing Xue   return 0;
94bc7f0fb5SXing Xue }
95