1 //===- LowerTypeTests.cpp - Unit tests for type test lowering -------------===//
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/Transforms/IPO/LowerTypeTests.h"
10 #include "gtest/gtest.h"
13 using namespace lowertypetests
;
15 TEST(LowerTypeTests
, BitSetBuilder
) {
17 std::vector
<uint64_t> Offsets
;
18 std::set
<uint64_t> Bits
;
25 {{}, std::set
<uint64_t>{}, 0, 1, 0, false, false},
26 {{0}, {0}, 0, 1, 0, true, true},
27 {{4}, {0}, 4, 1, 0, true, true},
28 {{37}, {0}, 37, 1, 0, true, true},
29 {{0, 1}, {0, 1}, 0, 2, 0, false, true},
30 {{0, 4}, {0, 1}, 0, 2, 2, false, true},
31 {{0, uint64_t(1) << 33}, {0, 1}, 0, 2, 33, false, true},
32 {{3, 7}, {0, 1}, 3, 2, 2, false, true},
33 {{0, 1, 7}, {0, 1, 7}, 0, 8, 0, false, false},
34 {{0, 2, 14}, {0, 1, 7}, 0, 8, 1, false, false},
35 {{0, 1, 8}, {0, 1, 8}, 0, 9, 0, false, false},
36 {{0, 2, 16}, {0, 1, 8}, 0, 9, 1, false, false},
37 {{0, 1, 2, 3, 4, 5, 6, 7},
38 {0, 1, 2, 3, 4, 5, 6, 7},
44 {{0, 1, 2, 3, 4, 5, 6, 7, 8},
45 {0, 1, 2, 3, 4, 5, 6, 7, 8},
53 for (auto &&T
: BSBTests
) {
55 for (auto Offset
: T
.Offsets
)
56 BSB
.addOffset(Offset
);
58 BitSetInfo BSI
= BSB
.build();
60 EXPECT_EQ(T
.Bits
, BSI
.Bits
);
61 EXPECT_EQ(T
.ByteOffset
, BSI
.ByteOffset
);
62 EXPECT_EQ(T
.BitSize
, BSI
.BitSize
);
63 EXPECT_EQ(T
.AlignLog2
, BSI
.AlignLog2
);
64 EXPECT_EQ(T
.IsSingleOffset
, BSI
.isSingleOffset());
65 EXPECT_EQ(T
.IsAllOnes
, BSI
.isAllOnes());
67 for (auto Offset
: T
.Offsets
)
68 EXPECT_TRUE(BSI
.containsGlobalOffset(Offset
));
70 auto I
= T
.Offsets
.begin();
71 for (uint64_t NonOffset
= 0; NonOffset
!= 256; ++NonOffset
) {
72 if (I
!= T
.Offsets
.end() && *I
== NonOffset
) {
77 EXPECT_FALSE(BSI
.containsGlobalOffset(NonOffset
));
82 TEST(LowerTypeTests
, GlobalLayoutBuilder
) {
85 std::vector
<std::set
<uint64_t>> Fragments
;
86 std::vector
<uint64_t> WantLayout
;
89 {4, {{0, 1}, {2, 3}}, {0, 1, 2, 3}},
90 {3, {{0, 1}, {1, 2}}, {0, 1, 2}},
91 {4, {{0, 1}, {1, 2}, {2, 3}}, {0, 1, 2, 3}},
92 {4, {{0, 1}, {2, 3}, {1, 2}}, {0, 1, 2, 3}},
93 {6, {{2, 5}, {0, 1, 2, 3, 4, 5}}, {0, 1, 2, 5, 3, 4}},
96 for (auto &&T
: GLBTests
) {
97 GlobalLayoutBuilder
GLB(T
.NumObjects
);
98 for (auto &&F
: T
.Fragments
)
101 std::vector
<uint64_t> ComputedLayout
;
102 for (auto &&F
: GLB
.Fragments
)
103 ComputedLayout
.insert(ComputedLayout
.end(), F
.begin(), F
.end());
105 EXPECT_EQ(T
.WantLayout
, ComputedLayout
);
109 TEST(LowerTypeTests
, ByteArrayBuilder
) {
111 std::set
<uint64_t> Bits
;
113 uint64_t WantByteOffset
;
118 std::vector
<BABAlloc
> Allocs
;
119 std::vector
<uint8_t> WantBytes
;
121 {{{{0}, 1, 0, 1}, {{0}, 1, 0, 2}}, {3}},
137 {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0, 0x80, 0x40, 0x20, 0x10, 8, 4,
141 for (auto &&T
: BABTests
) {
142 ByteArrayBuilder BABuilder
;
144 for (auto &&A
: T
.Allocs
) {
145 uint64_t GotByteOffset
;
148 BABuilder
.allocate(A
.Bits
, A
.BitSize
, GotByteOffset
, GotMask
);
149 EXPECT_EQ(A
.WantByteOffset
, GotByteOffset
);
150 EXPECT_EQ(A
.WantMask
, GotMask
);
153 EXPECT_EQ(T
.WantBytes
, BABuilder
.Bytes
);