1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DataLayout.h"
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/Type.h"
14 #include "llvm/TargetParser/Triple.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "gtest/gtest.h"
22 class DataLayoutTest
: public ::testing::Test
{};
24 TEST(DataLayout
, LayoutStringFormat
) {
25 for (StringRef Str
: {"", "e", "m:e", "m:e-e"})
26 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
28 for (StringRef Str
: {"-", "e-", "-m:e", "m:e--e"})
30 DataLayout::parse(Str
),
31 FailedWithMessage("empty specification is not allowed"));
34 TEST(DataLayoutTest
, InvalidSpecifier
) {
35 EXPECT_THAT_EXPECTED(DataLayout::parse("^"),
36 FailedWithMessage("unknown specifier '^'"));
37 EXPECT_THAT_EXPECTED(DataLayout::parse("I8:8"),
38 FailedWithMessage("unknown specifier 'I'"));
39 EXPECT_THAT_EXPECTED(DataLayout::parse("e-X"),
40 FailedWithMessage("unknown specifier 'X'"));
41 EXPECT_THAT_EXPECTED(DataLayout::parse("p0:32:32-64"),
42 FailedWithMessage("unknown specifier '6'"));
45 TEST(DataLayoutTest
, ParseEndianness
) {
46 EXPECT_THAT_EXPECTED(DataLayout::parse("e"), Succeeded());
47 EXPECT_THAT_EXPECTED(DataLayout::parse("E"), Succeeded());
49 for (StringRef Str
: {"ee", "e0", "e:0", "E0:E", "El", "E:B"})
51 DataLayout::parse(Str
),
52 FailedWithMessage("malformed specification, must be just 'e' or 'E'"));
55 TEST(DataLayoutTest
, ParseMangling
) {
56 for (StringRef Str
: {"m:a", "m:e", "m:l", "m:m", "m:o", "m:w", "m:x"})
57 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
59 for (StringRef Str
: {"m", "ms:m", "m:"})
61 DataLayout::parse(Str
),
63 "malformed specification, must be of the form \"m:<mangling>\""));
65 for (StringRef Str
: {"m:ms", "m:E", "m:0"})
66 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
),
67 FailedWithMessage("unknown mangling mode"));
70 TEST(DataLayoutTest
, ParseStackNaturalAlign
) {
71 for (StringRef Str
: {"S8", "S32768"})
72 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
75 DataLayout::parse("S"),
77 "malformed specification, must be of the form \"S<size>\""));
79 for (StringRef Str
: {"SX", "S0x20", "S65536"})
81 DataLayout::parse(Str
),
82 FailedWithMessage("stack natural alignment must be a 16-bit integer"));
85 DataLayout::parse("S0"),
86 FailedWithMessage("stack natural alignment must be non-zero"));
88 for (StringRef Str
: {"S1", "S7", "S24", "S65535"})
90 DataLayout::parse(Str
),
91 FailedWithMessage("stack natural alignment must be a power of two "
92 "times the byte width"));
95 TEST(DataLayoutTest
, ParseAddrSpace
) {
96 for (StringRef Str
: {"P0", "A0", "G0", "P1", "A1", "G1", "P16777215",
97 "A16777215", "G16777215"})
98 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
100 for (StringRef Str
: {"P", "A", "G"})
101 EXPECT_THAT_EXPECTED(
102 DataLayout::parse(Str
),
103 FailedWithMessage(("malformed specification, must be of the form \"" +
104 Twine(Str
.front()) + "<address space>\"")
107 for (StringRef Str
: {"Px", "A0x1", "G16777216"})
108 EXPECT_THAT_EXPECTED(
109 DataLayout::parse(Str
),
110 FailedWithMessage("address space must be a 24-bit integer"));
113 TEST(DataLayoutTest
, ParseFuncPtrSpec
) {
114 for (StringRef Str
: {"Fi8", "Fn16", "Fi32768", "Fn32768"})
115 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
117 EXPECT_THAT_EXPECTED(
118 DataLayout::parse("F"),
120 "malformed specification, must be of the form \"F<type><abi>\""));
122 EXPECT_THAT_EXPECTED(
123 DataLayout::parse("FN"),
124 FailedWithMessage("unknown function pointer alignment type 'N'"));
125 EXPECT_THAT_EXPECTED(
126 DataLayout::parse("F32"),
127 FailedWithMessage("unknown function pointer alignment type '3'"));
129 for (StringRef Str
: {"Fi", "Fn"})
130 EXPECT_THAT_EXPECTED(
131 DataLayout::parse(Str
),
132 FailedWithMessage("ABI alignment component cannot be empty"));
134 for (StringRef Str
: {"Fii", "Fn32x", "Fi65536", "Fn65536"})
135 EXPECT_THAT_EXPECTED(
136 DataLayout::parse(Str
),
137 FailedWithMessage("ABI alignment must be a 16-bit integer"));
139 for (StringRef Str
: {"Fi0", "Fn0"})
140 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
),
141 FailedWithMessage("ABI alignment must be non-zero"));
143 for (StringRef Str
: {"Fi12", "Fn24"})
144 EXPECT_THAT_EXPECTED(
145 DataLayout::parse(Str
),
147 "ABI alignment must be a power of two times the byte width"));
150 class DataLayoutPrimitiveSpecificationTest
151 : public DataLayoutTest
,
152 public ::testing::WithParamInterface
<char> {
156 DataLayoutPrimitiveSpecificationTest() : Specifier(GetParam()) {}
158 std::string
format(StringRef Str
) const {
159 std::string Res
= Str
.str();
160 std::replace(Res
.begin(), Res
.end(), '!', Specifier
);
165 INSTANTIATE_TEST_SUITE_P(PrmitiveSpecifiers
,
166 DataLayoutPrimitiveSpecificationTest
,
167 ::testing::Values('i', 'f', 'v'));
169 TEST_P(DataLayoutPrimitiveSpecificationTest
, ParsePrimitiveSpec
) {
171 {"!1:16", "!8:8:8", "!16:32:64", "!16777215:32768:32768"})
172 EXPECT_THAT_EXPECTED(DataLayout::parse(format(Str
)), Succeeded());
174 for (StringRef Str
: {"!", "!1", "!32:32:32:32", "!16:32:64:128"})
175 EXPECT_THAT_EXPECTED(
176 DataLayout::parse(format(Str
)),
177 FailedWithMessage(format("malformed specification, must be of the form "
178 "\"!<size>:<abi>[:<pref>]\"")));
181 for (StringRef Str
: {"!:8", "!:16:16", "!:32:64"})
182 EXPECT_THAT_EXPECTED(DataLayout::parse(format(Str
)),
183 FailedWithMessage("size component cannot be empty"));
186 {"!0:8", "!0x8:8", "!x:8:8", "!0:16:32", "!16777216:64:64"})
187 EXPECT_THAT_EXPECTED(
188 DataLayout::parse(format(Str
)),
189 FailedWithMessage("size must be a non-zero 24-bit integer"));
192 for (StringRef Str
: {"!8:", "!16::16", "!32::64"})
193 EXPECT_THAT_EXPECTED(
194 DataLayout::parse(format(Str
)),
195 FailedWithMessage("ABI alignment component cannot be empty"));
197 for (StringRef Str
: {"!1:x", "!8:8x:8", "!16:65536:65536"})
198 EXPECT_THAT_EXPECTED(
199 DataLayout::parse(format(Str
)),
200 FailedWithMessage("ABI alignment must be a 16-bit integer"));
202 for (StringRef Str
: {"!8:0", "!16:0:16", "!32:0:64"})
203 EXPECT_THAT_EXPECTED(DataLayout::parse(format(Str
)),
204 FailedWithMessage("ABI alignment must be non-zero"));
206 for (StringRef Str
: {"!1:1", "!8:4", "!16:6:16", "!32:24:64"})
207 EXPECT_THAT_EXPECTED(
208 DataLayout::parse(format(Str
)),
210 "ABI alignment must be a power of two times the byte width"));
212 // preferred alignment
213 for (StringRef Str
: {"!1:8:", "!16:16:", "!64:32:"})
214 EXPECT_THAT_EXPECTED(
215 DataLayout::parse(format(Str
)),
216 FailedWithMessage("preferred alignment component cannot be empty"));
218 for (StringRef Str
: {"!1:8:x", "!8:8:0x8", "!16:32:65536"})
219 EXPECT_THAT_EXPECTED(
220 DataLayout::parse(format(Str
)),
221 FailedWithMessage("preferred alignment must be a 16-bit integer"));
223 for (StringRef Str
: {"!8:8:0", "!32:16:0"})
224 EXPECT_THAT_EXPECTED(
225 DataLayout::parse(format(Str
)),
226 FailedWithMessage("preferred alignment must be non-zero"));
228 for (StringRef Str
: {"!1:8:12", "!8:8:17", "!16:32:40"})
229 EXPECT_THAT_EXPECTED(
230 DataLayout::parse(format(Str
)),
232 "preferred alignment must be a power of two times the byte width"));
234 for (StringRef Str
: {"!1:16:8", "!64:32:16"})
235 EXPECT_THAT_EXPECTED(
236 DataLayout::parse(format(Str
)),
238 "preferred alignment cannot be less than the ABI alignment"));
240 // Additional check for byte-sized integer.
241 if (GetParam() == 'i') {
242 for (StringRef Str
: {"!8:16", "!8:16:8", "!8:16:32"})
243 EXPECT_THAT_EXPECTED(DataLayout::parse(format(Str
)),
244 FailedWithMessage("i8 must be 8-bit aligned"));
248 TEST(DataLayoutTest
, ParseAggregateSpec
) {
249 for (StringRef Str
: {"a:8", "a:0:16", "a0:32:64", "a:32768:32768"})
250 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
252 for (StringRef Str
: {"a", "a0", "a:32:32:32", "a0:32:64:128"})
253 EXPECT_THAT_EXPECTED(
254 DataLayout::parse(Str
),
255 FailedWithMessage("malformed specification, must be of the form "
256 "\"a:<abi>[:<pref>]\""));
259 for (StringRef Str
: {"a1:8", "a0x0:8", "ax:16:32"})
260 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
),
261 FailedWithMessage("size must be zero"));
264 for (StringRef Str
: {"a:", "a0:", "a::32"})
265 EXPECT_THAT_EXPECTED(
266 DataLayout::parse(Str
),
267 FailedWithMessage("ABI alignment component cannot be empty"));
269 for (StringRef Str
: {"a:x", "a0:0x0", "a:65536", "a0:65536:65536"})
270 EXPECT_THAT_EXPECTED(
271 DataLayout::parse(Str
),
272 FailedWithMessage("ABI alignment must be a 16-bit integer"));
274 for (StringRef Str
: {"a:1", "a:4", "a:9:16", "a0:24:32"})
275 EXPECT_THAT_EXPECTED(
276 DataLayout::parse(Str
),
278 "ABI alignment must be a power of two times the byte width"));
280 // preferred alignment
281 for (StringRef Str
: {"a:8:", "a0:16:", "a0:0:"})
282 EXPECT_THAT_EXPECTED(
283 DataLayout::parse(Str
),
284 FailedWithMessage("preferred alignment component cannot be empty"));
286 for (StringRef Str
: {"a:16:x", "a0:8:0x8", "a:16:65536"})
287 EXPECT_THAT_EXPECTED(
288 DataLayout::parse(Str
),
289 FailedWithMessage("preferred alignment must be a 16-bit integer"));
291 for (StringRef Str
: {"a:0:0", "a0:16:0"})
292 EXPECT_THAT_EXPECTED(
293 DataLayout::parse(Str
),
294 FailedWithMessage("preferred alignment must be non-zero"));
296 for (StringRef Str
: {"a:8:12", "a:16:17", "a0:32:40"})
297 EXPECT_THAT_EXPECTED(
298 DataLayout::parse(Str
),
300 "preferred alignment must be a power of two times the byte width"));
302 for (StringRef Str
: {"a:16:8", "a0:32:16"})
303 EXPECT_THAT_EXPECTED(
304 DataLayout::parse(Str
),
306 "preferred alignment cannot be less than the ABI alignment"));
309 TEST(DataLayout
, ParsePointerSpec
) {
311 {"p:16:8", "p:16:16:64", "p:32:64:64:32", "p0:32:64", "p42:64:32:32",
312 "p16777215:32:32:64:8", "p16777215:16777215:32768:32768:16777215"})
313 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
316 {"p", "p0", "p:32", "p0:32", "p:32:32:32:32:32", "p0:32:32:32:32:32"})
317 EXPECT_THAT_EXPECTED(
318 DataLayout::parse(Str
),
319 FailedWithMessage("malformed specification, must be of the form "
320 "\"p[<n>]:<size>:<abi>[:<pref>[:<idx>]]\""));
323 for (StringRef Str
: {"p0x0:32:32", "px:32:32:32", "p16777216:32:32:32:32"})
324 EXPECT_THAT_EXPECTED(
325 DataLayout::parse(Str
),
326 FailedWithMessage("address space must be a 24-bit integer"));
329 for (StringRef Str
: {"p::32", "p0::32"})
330 EXPECT_THAT_EXPECTED(
331 DataLayout::parse(Str
),
332 FailedWithMessage("pointer size component cannot be empty"));
334 for (StringRef Str
: {"p:0:32", "p0:0x1:32:32", "p42:16777216:32:32:32"})
335 EXPECT_THAT_EXPECTED(
336 DataLayout::parse(Str
),
337 FailedWithMessage("pointer size must be a non-zero 24-bit integer"));
340 for (StringRef Str
: {"p:32:", "p0:32::32", "p42:32::32:32"})
341 EXPECT_THAT_EXPECTED(
342 DataLayout::parse(Str
),
343 FailedWithMessage("ABI alignment component cannot be empty"));
345 for (StringRef Str
: {"p:32:x", "p0:32:0x20:32", "p42:32:65536:32:32"})
346 EXPECT_THAT_EXPECTED(
347 DataLayout::parse(Str
),
348 FailedWithMessage("ABI alignment must be a 16-bit integer"));
350 for (StringRef Str
: {"p:32:0", "p0:32:0:32", "p42:32:0:32:32"})
351 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
),
352 FailedWithMessage("ABI alignment must be non-zero"));
354 for (StringRef Str
: {"p:32:4", "p42:32:24:32", "p0:32:65535:32:32"})
355 EXPECT_THAT_EXPECTED(
356 DataLayout::parse(Str
),
358 "ABI alignment must be a power of two times the byte width"));
360 // preferred alignment
361 for (StringRef Str
: {"p:32:32:", "p0:32:32:", "p42:32:32::32"})
362 EXPECT_THAT_EXPECTED(
363 DataLayout::parse(Str
),
364 FailedWithMessage("preferred alignment component cannot be empty"));
366 for (StringRef Str
: {"p:32:32:x", "p0:32:32:0x20", "p42:32:32:65536:32"})
367 EXPECT_THAT_EXPECTED(
368 DataLayout::parse(Str
),
369 FailedWithMessage("preferred alignment must be a 16-bit integer"));
371 for (StringRef Str
: {"p:32:32:0", "p0:32:32:0", "p42:32:32:0:32"})
372 EXPECT_THAT_EXPECTED(
373 DataLayout::parse(Str
),
374 FailedWithMessage("preferred alignment must be non-zero"));
376 for (StringRef Str
: {"p:32:32:4", "p0:32:32:24", "p42:32:32:65535:32"})
377 EXPECT_THAT_EXPECTED(
378 DataLayout::parse(Str
),
380 "preferred alignment must be a power of two times the byte width"));
382 for (StringRef Str
: {"p:64:64:32", "p0:16:32:16:16"})
383 EXPECT_THAT_EXPECTED(
384 DataLayout::parse(Str
),
386 "preferred alignment cannot be less than the ABI alignment"));
389 for (StringRef Str
: {"p:32:32:32:", "p0:32:32:32:"})
390 EXPECT_THAT_EXPECTED(
391 DataLayout::parse(Str
),
392 FailedWithMessage("index size component cannot be empty"));
395 {"p:32:32:32:0", "p0:32:32:32:0x20", "p42:32:32:32:16777216"})
396 EXPECT_THAT_EXPECTED(
397 DataLayout::parse(Str
),
398 FailedWithMessage("index size must be a non-zero 24-bit integer"));
400 for (StringRef Str
: {"p:16:16:16:17", "p0:32:64:64:64", "p42:16:64:64:32"})
401 EXPECT_THAT_EXPECTED(
402 DataLayout::parse(Str
),
403 FailedWithMessage("index size cannot be larger than the pointer size"));
406 TEST(DataLayoutTest
, ParseNativeIntegersSpec
) {
407 for (StringRef Str
: {"n1", "n1:8", "n24:12:16777215"})
408 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
410 for (StringRef Str
: {"n", "n1:", "n:8", "n16::32"})
411 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
),
412 FailedWithMessage("size component cannot be empty"));
414 for (StringRef Str
: {"n0", "n0x8:16", "n8:0", "n16:0:32", "n16777216",
415 "n16:16777216", "n32:64:16777216"})
416 EXPECT_THAT_EXPECTED(
417 DataLayout::parse(Str
),
418 FailedWithMessage("size must be a non-zero 24-bit integer"));
421 TEST(DataLayout
, ParseNonIntegralAddrSpace
) {
422 for (StringRef Str
: {"ni:1", "ni:16777215", "ni:1:16777215"})
423 EXPECT_THAT_EXPECTED(DataLayout::parse(Str
), Succeeded());
425 for (StringRef Str
: {"ni", "ni42", "nix"})
426 EXPECT_THAT_EXPECTED(
427 DataLayout::parse(Str
),
428 FailedWithMessage("malformed specification, must be of the form "
429 "\"ni:<address space>[:<address space>]...\""));
431 for (StringRef Str
: {"ni:", "ni::42", "ni:42:"})
432 EXPECT_THAT_EXPECTED(
433 DataLayout::parse(Str
),
434 FailedWithMessage("address space component cannot be empty"));
436 for (StringRef Str
: {"ni:x", "ni:42:0x1", "ni:16777216", "ni:42:16777216"})
437 EXPECT_THAT_EXPECTED(
438 DataLayout::parse(Str
),
439 FailedWithMessage("address space must be a 24-bit integer"));
441 for (StringRef Str
: {"ni:0", "ni:42:0"})
442 EXPECT_THAT_EXPECTED(
443 DataLayout::parse(Str
),
444 FailedWithMessage("address space 0 cannot be non-integral"));
447 TEST(DataLayout
, GetStackAlignment
) {
449 EXPECT_FALSE(Default
.getStackAlignment().has_value());
451 std::pair
<StringRef
, Align
> Cases
[] = {
454 {"S32768", Align(4096)},
456 for (auto [Layout
, Val
] : Cases
) {
457 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
458 EXPECT_EQ(DL
.getStackAlignment(), Val
) << Layout
;
462 TEST(DataLayout
, GetPointerSizeInBits
) {
463 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
465 {"p:16:32", 16, 16, 16},
466 {"p0:32:64", 32, 32, 32},
467 {"p1:16:32", 64, 16, 64},
468 {"p1:31:32-p2:15:16:16:14", 64, 31, 15},
470 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
471 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
472 EXPECT_EQ(DL
.getPointerSizeInBits(0), V0
) << Layout
;
473 EXPECT_EQ(DL
.getPointerSizeInBits(1), V1
) << Layout
;
474 EXPECT_EQ(DL
.getPointerSizeInBits(2), V2
) << Layout
;
478 TEST(DataLayout
, GetPointerSize
) {
479 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
481 {"p:16:32", 2, 2, 2},
482 {"p0:32:64", 4, 4, 4},
483 {"p1:17:32", 8, 3, 8},
484 {"p1:31:64-p2:23:8:16:9", 8, 4, 3},
486 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
487 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
488 EXPECT_EQ(DL
.getPointerSize(0), V0
) << Layout
;
489 EXPECT_EQ(DL
.getPointerSize(1), V1
) << Layout
;
490 EXPECT_EQ(DL
.getPointerSize(2), V2
) << Layout
;
494 TEST(DataLayout
, GetIndexSizeInBits
) {
495 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
497 {"p:16:32", 16, 16, 16},
498 {"p0:32:64", 32, 32, 32},
499 {"p1:16:32:32:10", 64, 10, 64},
500 {"p1:31:32:64:20-p2:17:16:16:15", 64, 20, 15},
502 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
503 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
504 EXPECT_EQ(DL
.getIndexSizeInBits(0), V0
) << Layout
;
505 EXPECT_EQ(DL
.getIndexSizeInBits(1), V1
) << Layout
;
506 EXPECT_EQ(DL
.getIndexSizeInBits(2), V2
) << Layout
;
510 TEST(DataLayout
, GetIndexSize
) {
511 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
513 {"p:16:32", 2, 2, 2},
514 {"p0:27:64", 4, 4, 4},
515 {"p1:19:32:64:5", 8, 1, 8},
516 {"p1:33:32:64:23-p2:21:8:16:13", 8, 3, 2},
518 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
519 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
520 EXPECT_EQ(DL
.getIndexSize(0), V0
) << Layout
;
521 EXPECT_EQ(DL
.getIndexSize(1), V1
) << Layout
;
522 EXPECT_EQ(DL
.getIndexSize(2), V2
) << Layout
;
526 TEST(DataLayout
, GetPointerABIAlignment
) {
527 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
529 {"p:16:32", 4, 4, 4},
530 {"p0:16:32:64", 4, 4, 4},
531 {"p1:32:16:64", 8, 2, 8},
532 {"p1:33:16:32:15-p2:23:8:16:9", 8, 2, 1},
534 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
535 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
536 EXPECT_EQ(DL
.getPointerABIAlignment(0).value(), V0
) << Layout
;
537 EXPECT_EQ(DL
.getPointerABIAlignment(1).value(), V1
) << Layout
;
538 EXPECT_EQ(DL
.getPointerABIAlignment(2).value(), V2
) << Layout
;
542 TEST(DataLayout
, GetPointerPrefAlignment
) {
543 std::tuple
<StringRef
, unsigned, unsigned, unsigned> Cases
[] = {
545 {"p:16:32", 4, 4, 4},
546 {"p0:8:16:32", 4, 4, 4},
547 {"p1:32:8:16", 8, 2, 8},
548 {"p1:33:8:16:31-p2:23:8:32:17", 8, 2, 4},
550 for (auto [Layout
, V0
, V1
, V2
] : Cases
) {
551 DataLayout DL
= cantFail(DataLayout::parse(Layout
));
552 EXPECT_EQ(DL
.getPointerPrefAlignment(0).value(), V0
) << Layout
;
553 EXPECT_EQ(DL
.getPointerPrefAlignment(1).value(), V1
) << Layout
;
554 EXPECT_EQ(DL
.getPointerPrefAlignment(2).value(), V2
) << Layout
;
558 TEST(DataLayout
, IsNonIntegralAddressSpace
) {
560 EXPECT_THAT(Default
.getNonIntegralAddressSpaces(), ::testing::SizeIs(0));
561 EXPECT_FALSE(Default
.isNonIntegralAddressSpace(0));
562 EXPECT_FALSE(Default
.isNonIntegralAddressSpace(1));
564 DataLayout Custom
= cantFail(DataLayout::parse("ni:2:16777215"));
565 EXPECT_THAT(Custom
.getNonIntegralAddressSpaces(),
566 ::testing::ElementsAreArray({2U, 16777215U}));
567 EXPECT_FALSE(Custom
.isNonIntegralAddressSpace(0));
568 EXPECT_FALSE(Custom
.isNonIntegralAddressSpace(1));
569 EXPECT_TRUE(Custom
.isNonIntegralAddressSpace(2));
570 EXPECT_TRUE(Custom
.isNonIntegralAddressSpace(16777215));
573 TEST(DataLayoutTest
, CopyAssignmentInvalidatesStructLayout
) {
574 DataLayout DL1
= cantFail(DataLayout::parse("p:32:32"));
575 DataLayout DL2
= cantFail(DataLayout::parse("p:64:64"));
578 StructType
*Ty
= StructType::get(PointerType::getUnqual(Ctx
));
580 // Initialize struct layout caches.
581 EXPECT_EQ(DL1
.getStructLayout(Ty
)->getSizeInBits(), 32U);
582 EXPECT_EQ(DL1
.getStructLayout(Ty
)->getAlignment(), Align(4));
583 EXPECT_EQ(DL2
.getStructLayout(Ty
)->getSizeInBits(), 64U);
584 EXPECT_EQ(DL2
.getStructLayout(Ty
)->getAlignment(), Align(8));
586 // The copy should invalidate DL1's cache.
588 EXPECT_EQ(DL1
.getStructLayout(Ty
)->getSizeInBits(), 64U);
589 EXPECT_EQ(DL1
.getStructLayout(Ty
)->getAlignment(), Align(8));
590 EXPECT_EQ(DL2
.getStructLayout(Ty
)->getSizeInBits(), 64U);
591 EXPECT_EQ(DL2
.getStructLayout(Ty
)->getAlignment(), Align(8));
594 TEST(DataLayoutTest
, FunctionPtrAlign
) {
595 EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
596 EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());
597 EXPECT_EQ(MaybeAlign(2), DataLayout("Fi16").getFunctionPtrAlign());
598 EXPECT_EQ(MaybeAlign(4), DataLayout("Fi32").getFunctionPtrAlign());
599 EXPECT_EQ(MaybeAlign(8), DataLayout("Fi64").getFunctionPtrAlign());
600 EXPECT_EQ(MaybeAlign(1), DataLayout("Fn8").getFunctionPtrAlign());
601 EXPECT_EQ(MaybeAlign(2), DataLayout("Fn16").getFunctionPtrAlign());
602 EXPECT_EQ(MaybeAlign(4), DataLayout("Fn32").getFunctionPtrAlign());
603 EXPECT_EQ(MaybeAlign(8), DataLayout("Fn64").getFunctionPtrAlign());
604 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent
,
605 DataLayout("").getFunctionPtrAlignType());
606 EXPECT_EQ(DataLayout::FunctionPtrAlignType::Independent
,
607 DataLayout("Fi8").getFunctionPtrAlignType());
608 EXPECT_EQ(DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign
,
609 DataLayout("Fn8").getFunctionPtrAlignType());
610 EXPECT_EQ(DataLayout("Fi8"), DataLayout("Fi8"));
611 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fi16"));
612 EXPECT_NE(DataLayout("Fi8"), DataLayout("Fn8"));
614 DataLayout
a(""), b("Fi8"), c("Fn8");
625 TEST(DataLayoutTest
, ValueOrABITypeAlignment
) {
626 const DataLayout
DL("Fi8");
628 Type
*const FourByteAlignType
= Type::getInt32Ty(Context
);
630 DL
.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType
));
632 DL
.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType
));
635 TEST(DataLayoutTest
, GlobalsAddressSpace
) {
636 // When not explicitly defined the globals address space should be zero:
637 EXPECT_EQ(DataLayout("").getDefaultGlobalsAddressSpace(), 0u);
638 EXPECT_EQ(DataLayout("P1-A2").getDefaultGlobalsAddressSpace(), 0u);
639 EXPECT_EQ(DataLayout("G2").getDefaultGlobalsAddressSpace(), 2u);
640 // Check that creating a GlobalVariable without an explicit address space
641 // in a module with a default globals address space respects that default:
643 std::unique_ptr
<Module
> M(new Module("MyModule", Context
));
644 // Default is globals in address space zero:
645 auto *Int32
= Type::getInt32Ty(Context
);
646 auto *DefaultGlobal1
= new GlobalVariable(
647 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr);
648 EXPECT_EQ(DefaultGlobal1
->getAddressSpace(), 0u);
649 auto *ExplicitGlobal1
= new GlobalVariable(
650 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr, "", nullptr,
651 GlobalValue::NotThreadLocal
, 123);
652 EXPECT_EQ(ExplicitGlobal1
->getAddressSpace(), 123u);
654 // When using a datalayout with the global address space set to 200, global
655 // variables should default to 200
656 M
->setDataLayout("G200");
657 auto *DefaultGlobal2
= new GlobalVariable(
658 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr);
659 EXPECT_EQ(DefaultGlobal2
->getAddressSpace(), 200u);
660 auto *ExplicitGlobal2
= new GlobalVariable(
661 *M
, Int32
, false, GlobalValue::ExternalLinkage
, nullptr, "", nullptr,
662 GlobalValue::NotThreadLocal
, 123);
663 EXPECT_EQ(ExplicitGlobal2
->getAddressSpace(), 123u);
666 TEST(DataLayoutTest
, VectorAlign
) {
667 Expected
<DataLayout
> DL
= DataLayout::parse("v64:64");
668 EXPECT_THAT_EXPECTED(DL
, Succeeded());
671 Type
*const FloatTy
= Type::getFloatTy(Context
);
672 Type
*const V8F32Ty
= FixedVectorType::get(FloatTy
, 8);
674 // The alignment for a vector type larger than any specified vector type uses
675 // the natural alignment as a fallback.
676 EXPECT_EQ(Align(4 * 8), DL
->getABITypeAlign(V8F32Ty
));
677 EXPECT_EQ(Align(4 * 8), DL
->getPrefTypeAlign(V8F32Ty
));
680 TEST(DataLayoutTest
, UEFI
) {
681 Triple TT
= Triple("x86_64-unknown-uefi");
683 // Test UEFI X86_64 Mangling Component.
684 EXPECT_STREQ(DataLayout::getManglingComponent(TT
), "-m:w");
687 } // anonymous namespace