blob: b0e9b5e2be9f943c314c511e79d1bfe8dea6b07a [file] [log] [blame]
Jan Voung7b34b592014-07-18 13:12:58 -07001//===- subzero/crosstest/test_stacksave.c - Implementation for tests ------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This aims to test that C99's VLAs (which use stacksave/stackrestore
11// intrinsics) work fine.
12//
13//===----------------------------------------------------------------------===//
14
15#include <stdint.h>
16
17#include "test_stacksave.h"
18DECLARE_TESTS()
19
20/* NOTE: This has 0 stacksaves, because the vla isn't in a loop,
21 * so the vla can just be freed by the epilogue.
22 */
23uint32_t test_basic_vla(uint32_t size, uint32_t start, uint32_t inc) {
24 uint32_t vla[size];
25 uint32_t mid = start + ((size - start) / 2);
26 for (uint32_t i = start; i < size; ++i) {
27 vla[i] = i + inc;
28 }
29 return (vla[start] << 2) + (vla[mid] << 1) + vla[size - 1];
30}
31
32static uint32_t __attribute__((noinline)) foo(uint32_t x) {
33 return x * x;
34}
35
36/* NOTE: This has 1 stacksave, because the vla is in a loop and should
37 * be freed before the next iteration.
38 */
39uint32_t test_vla_in_loop(uint32_t size, uint32_t start, uint32_t inc) {
40 uint32_t sum = 0;
41 for (uint32_t i = start; i < size; ++i) {
42 uint32_t size1 = size - i;
43 uint32_t vla[size1];
44 for (uint32_t j = 0; j < size1; ++j) {
45 /* Adjust stack again with a function call. */
46 vla[j] = foo(start * j + inc);
47 }
48 for (uint32_t j = 0; j < size1; ++j) {
49 sum += vla[j];
50 }
51 }
52 return sum;
53}
54
55uint32_t test_two_vlas_in_loops(uint32_t size, uint32_t start, uint32_t inc) {
56 uint32_t sum = 0;
57 for (uint32_t i = start; i < size; ++i) {
58 uint32_t size1 = size - i;
59 uint32_t vla1[size1];
60 for (uint32_t j = 0; j < size1; ++j) {
61 uint32_t size2 = size - j;
62 uint32_t start2 = 0;
63 uint32_t mid2 = size2 / 2;
64 uint32_t vla2[size2];
65 for (uint32_t k = start2; k < size2; ++k) {
66 /* Adjust stack again with a function call. */
67 vla2[k] = foo(start * k + inc);
68 }
69 vla1[j] = (vla2[start2] << 2) + (vla2[mid2] << 1) + vla2[size2 - 1];
70 }
71 for (uint32_t j = 0; j < size1; ++j) {
72 sum += vla1[j];
73 }
74 }
75 return sum;
76}