| 1 |
// PR middle-end/69780 - [4.9/5/6 Regression] ICE on |
| 2 |
// __builtin_alloca_with_align with small alignment |
| 3 |
// { dg-do compile } |
| 4 |
|
| 5 |
#define CHAR_BIT __CHAR_BIT__ |
| 6 |
#define SIZE_MAX __SIZE_MAX__ |
| 7 |
#define UINT_MAX (__INT_MAX__ + 1U) |
| 8 |
|
| 9 |
/* The largest valid alignment is undocumented and subject to change |
| 10 |
but for the purposes of white box testing we rely on knowing that |
| 11 |
it happens to be defined to (UINT_MAX >> 1) + 1. */ |
| 12 |
#define ALIGN_MAX ((UINT_MAX >> 1) + 1) |
| 13 |
|
| 14 |
#if UINT_MAX < SIZE_MAX |
| 15 |
/* Define a constant to exercise an alignment that is valid a power |
| 16 |
of 2 in excess of the maximum. */ |
| 17 |
# define MAX_X_2 (ALIGN_MAX << 1) |
| 18 |
#else |
| 19 |
/* For targets where UINT_MAX is the same as SIZE_MAX, use an invalid |
| 20 |
alignment that's less than the maximum to elicit the same errors. */ |
| 21 |
# define MAX_X_2 (ALIGN_MAX + 1) |
| 22 |
#endif |
| 23 |
|
| 24 |
static void* p; |
| 25 |
|
| 26 |
// Verify that valid __builtin_alloca_with_align expressions are accepted. |
| 27 |
void test_valid (int n) |
| 28 |
{ |
| 29 |
enum { |
| 30 |
A1 = CHAR_BIT * 1, |
| 31 |
A2 = CHAR_BIT * 2, |
| 32 |
A4 = CHAR_BIT * 4, |
| 33 |
A8 = CHAR_BIT * 8, |
| 34 |
A16 = CHAR_BIT * 16, |
| 35 |
A32 = CHAR_BIT * 32 |
| 36 |
}; |
| 37 |
|
| 38 |
const int a1 = A1; |
| 39 |
const int a2 = A2; |
| 40 |
const int a4 = A4; |
| 41 |
const int a8 = A8; |
| 42 |
const int a16 = A16; |
| 43 |
const int a32 = A32; |
| 44 |
|
| 45 |
// Valid alignments are power of 2 positive multiples of CHAR_BIT. |
| 46 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 1); |
| 47 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 2); |
| 48 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 4); |
| 49 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 8); |
| 50 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 16); |
| 51 |
p = __builtin_alloca_with_align (n, CHAR_BIT * 32); |
| 52 |
|
| 53 |
p = __builtin_alloca_with_align (n, A1); |
| 54 |
p = __builtin_alloca_with_align (n, A2); |
| 55 |
p = __builtin_alloca_with_align (n, A4); |
| 56 |
p = __builtin_alloca_with_align (n, A8); |
| 57 |
p = __builtin_alloca_with_align (n, A16); |
| 58 |
p = __builtin_alloca_with_align (n, A32); |
| 59 |
|
| 60 |
p = __builtin_alloca_with_align (n, a1); |
| 61 |
p = __builtin_alloca_with_align (n, a2); |
| 62 |
p = __builtin_alloca_with_align (n, a4); |
| 63 |
p = __builtin_alloca_with_align (n, a8); |
| 64 |
p = __builtin_alloca_with_align (n, a16); |
| 65 |
p = __builtin_alloca_with_align (n, a32); |
| 66 |
} |
| 67 |
|
| 68 |
template <int A> struct X { enum { Align = A }; }; |
| 69 |
|
| 70 |
template <int A> |
| 71 |
void test_valid_template (int n) |
| 72 |
{ |
| 73 |
// Valid alignments are power of 2 positive multiples of CHAR_BIT. |
| 74 |
p = __builtin_alloca_with_align (n, A); |
| 75 |
} |
| 76 |
|
| 77 |
template void test_valid_template<CHAR_BIT>(int); |
| 78 |
template void test_valid_template<CHAR_BIT * 2>(int); |
| 79 |
template void test_valid_template<CHAR_BIT * 4>(int); |
| 80 |
template void test_valid_template<CHAR_BIT * 8>(int); |
| 81 |
template void test_valid_template<CHAR_BIT * 16>(int); |
| 82 |
template void test_valid_template<CHAR_BIT * 32>(int); |
| 83 |
|
| 84 |
// Exercise the alignment in a dependent context. |
| 85 |
template <int A> |
| 86 |
void test_valid_template_dep (int n) |
| 87 |
{ |
| 88 |
// Valid alignments are power of 2 positive multiples of CHAR_BIT. |
| 89 |
p = __builtin_alloca_with_align (n, X<A>::Align); |
| 90 |
} |
| 91 |
|
| 92 |
template void test_valid_template_dep<CHAR_BIT>(int); |
| 93 |
template void test_valid_template_dep<CHAR_BIT * 2>(int); |
| 94 |
template void test_valid_template_dep<CHAR_BIT * 4>(int); |
| 95 |
template void test_valid_template_dep<CHAR_BIT * 8>(int); |
| 96 |
template void test_valid_template_dep<CHAR_BIT * 16>(int); |
| 97 |
template void test_valid_template_dep<CHAR_BIT * 32>(int); |
| 98 |
|
| 99 |
// Invalid size must be rejected (and not cause an ICE). |
| 100 |
void test_arg1_non_int (int n) |
| 101 |
{ |
| 102 |
extern void f (); |
| 103 |
|
| 104 |
p = __builtin_alloca_with_align ((void*)0, 32); // { dg-error "invalid conversion" } |
| 105 |
|
| 106 |
p = __builtin_alloca_with_align ("", 32); // { dg-error "invalid conversion" } |
| 107 |
p = __builtin_alloca_with_align (L"", 32); // { dg-error "invalid conversion" } |
| 108 |
p = __builtin_alloca_with_align (f, 32); // { dg-error "invalid conversion" } |
| 109 |
} |
| 110 |
|
| 111 |
// Non-integer alignment must be rejected. |
| 112 |
void test_arg2_non_int (int n) |
| 113 |
{ |
| 114 |
// Verify the full text of the diagnostic just once. |
| 115 |
p = __builtin_alloca_with_align (n, 0.0); // { dg-error "second argument to function .__builtin_alloca_with_align. must be a constant integer power of 2 between .8. and " } |
| 116 |
|
| 117 |
p = __builtin_alloca_with_align (n, (void*)0); // { dg-error "invalid conversion|must be a constant integer" } |
| 118 |
p = __builtin_alloca_with_align (n, ""); // { dg-error "invalid conversion|must be a constant integer" } |
| 119 |
p = __builtin_alloca_with_align (n, L""); // { dg-error "invalid conversion|must be a constant integer" } |
| 120 |
} |
| 121 |
|
| 122 |
// Integer alignment that's not a constant expression must be rejected. |
| 123 |
void test_arg2_non_const (int n, int a1) |
| 124 |
{ |
| 125 |
extern const int a2; |
| 126 |
static volatile const int a3 = CHAR_BIT; |
| 127 |
|
| 128 |
p = __builtin_alloca_with_align (n, a1); // { dg-error "must be a constant integer" } |
| 129 |
p = __builtin_alloca_with_align (n, a2); // { dg-error "must be a constant integer" } |
| 130 |
p = __builtin_alloca_with_align (n, a3); // { dg-error "must be a constant integer" } |
| 131 |
} |
| 132 |
|
| 133 |
// Constant integer alignment that's not a power of 2 positive multiple |
| 134 |
// of CHAR_BIT must be rejected. |
| 135 |
void test_arg2_non_pow2 (int n) |
| 136 |
{ |
| 137 |
p = __builtin_alloca_with_align (n, 0); // { dg-error "must be a constant integer" } |
| 138 |
p = __builtin_alloca_with_align (n, 1); // { dg-error "must be a constant integer" } |
| 139 |
p = __builtin_alloca_with_align (n, 2); // { dg-error "must be a constant integer" } |
| 140 |
p = __builtin_alloca_with_align (n, 3); // { dg-error "must be a constant integer" } |
| 141 |
p = __builtin_alloca_with_align (n, 4); // { dg-error "must be a constant integer" } |
| 142 |
p = __builtin_alloca_with_align (n, 5); // { dg-error "must be a constant integer" } |
| 143 |
p = __builtin_alloca_with_align (n, 6); // { dg-error "must be a constant integer" } |
| 144 |
p = __builtin_alloca_with_align (n, 7); // { dg-error "must be a constant integer" } |
| 145 |
p = __builtin_alloca_with_align (n, 9); // { dg-error "must be a constant integer" } |
| 146 |
p = __builtin_alloca_with_align (n, 10); // { dg-error "must be a constant integer" } |
| 147 |
p = __builtin_alloca_with_align (n, 11); // { dg-error "must be a constant integer" } |
| 148 |
p = __builtin_alloca_with_align (n, 12); // { dg-error "must be a constant integer" } |
| 149 |
p = __builtin_alloca_with_align (n, 13); // { dg-error "must be a constant integer" } |
| 150 |
p = __builtin_alloca_with_align (n, 14); // { dg-error "must be a constant integer" } |
| 151 |
p = __builtin_alloca_with_align (n, 15); // { dg-error "must be a constant integer" } |
| 152 |
p = __builtin_alloca_with_align (n, 17); // { dg-error "must be a constant integer" } |
| 153 |
p = __builtin_alloca_with_align (n, 31); // { dg-error "must be a constant integer" } |
| 154 |
p = __builtin_alloca_with_align (n, 33); // { dg-error "must be a constant integer" } |
| 155 |
p = __builtin_alloca_with_align (n, 63); // { dg-error "must be a constant integer" } |
| 156 |
p = __builtin_alloca_with_align (n, 65); // { dg-error "must be a constant integer" } |
| 157 |
p = __builtin_alloca_with_align (n, SIZE_MAX); /* { dg-error "must be a constant integer" } */ |
| 158 |
p = __builtin_alloca_with_align (n, MAX_X_2); /* { dg-error "must be a constant integer" } */ |
| 159 |
} |
| 160 |
|
| 161 |
// Exercise invalid alignment specified by a template argument. |
| 162 |
template <int A> |
| 163 |
void test_invalid_template_1 (int n) |
| 164 |
{ |
| 165 |
// Valid alignments are power of 2 positive multiples of CHAR_BIT. |
| 166 |
p = __builtin_alloca_with_align (n, A); // { dg-error "must be a constant integer" } |
| 167 |
} |
| 168 |
|
| 169 |
template void test_invalid_template_1<1>(int); |
| 170 |
|
| 171 |
template <int A> |
| 172 |
void test_invalid_template_7 (int n) |
| 173 |
{ |
| 174 |
p = __builtin_alloca_with_align (n, A); // { dg-error "must be a constant integer" } |
| 175 |
} |
| 176 |
|
| 177 |
template void test_invalid_template_7<7>(int); |
| 178 |
|
| 179 |
template <int A> |
| 180 |
void test_invalid_template_9 (int n) |
| 181 |
{ |
| 182 |
p = __builtin_alloca_with_align (n, A); // { dg-error "must be a constant integer" } |
| 183 |
} |
| 184 |
|
| 185 |
template void test_invalid_template_9<9>(int); |
| 186 |
|
| 187 |
// Exercise invalid alignment specified by a template dependent argument. |
| 188 |
template <int A> |
| 189 |
void test_invalid_template_dep_1 (int n) |
| 190 |
{ |
| 191 |
p = __builtin_alloca_with_align (n, X<A>::Align); // { dg-error "must be a constant integer" } |
| 192 |
} |
| 193 |
|
| 194 |
template void test_invalid_template_dep_1<1>(int); |