1 //===- LowerBitSets.cpp - Unit tests for bitset lowering ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Transforms/IPO/LowerBitSets.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
TEST(LowerBitSets,BitSetBuilder)15 TEST(LowerBitSets, BitSetBuilder) {
16 struct {
17 std::vector<uint64_t> Offsets;
18 std::set<uint64_t> Bits;
19 uint64_t ByteOffset;
20 uint64_t BitSize;
21 unsigned AlignLog2;
22 bool IsSingleOffset;
23 bool IsAllOnes;
24 } BSBTests[] = {
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},
39 0,
40 8,
41 0,
42 false,
43 true},
44 {{0, 1, 2, 3, 4, 5, 6, 7, 8},
45 {0, 1, 2, 3, 4, 5, 6, 7, 8},
46 0,
47 9,
48 0,
49 false,
50 true},
51 };
52
53 for (auto &&T : BSBTests) {
54 BitSetBuilder BSB;
55 for (auto Offset : T.Offsets)
56 BSB.addOffset(Offset);
57
58 BitSetInfo BSI = BSB.build();
59
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());
66
67 for (auto Offset : T.Offsets)
68 EXPECT_TRUE(BSI.containsGlobalOffset(Offset));
69
70 auto I = T.Offsets.begin();
71 for (uint64_t NonOffset = 0; NonOffset != 256; ++NonOffset) {
72 if (I != T.Offsets.end() && *I == NonOffset) {
73 ++I;
74 continue;
75 }
76
77 EXPECT_FALSE(BSI.containsGlobalOffset(NonOffset));
78 }
79 }
80 }
81
TEST(LowerBitSets,GlobalLayoutBuilder)82 TEST(LowerBitSets, GlobalLayoutBuilder) {
83 struct {
84 uint64_t NumObjects;
85 std::vector<std::set<uint64_t>> Fragments;
86 std::vector<uint64_t> WantLayout;
87 } GLBTests[] = {
88 {0, {}, {}},
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}},
94 };
95
96 for (auto &&T : GLBTests) {
97 GlobalLayoutBuilder GLB(T.NumObjects);
98 for (auto &&F : T.Fragments)
99 GLB.addFragment(F);
100
101 std::vector<uint64_t> ComputedLayout;
102 for (auto &&F : GLB.Fragments)
103 ComputedLayout.insert(ComputedLayout.end(), F.begin(), F.end());
104
105 EXPECT_EQ(T.WantLayout, ComputedLayout);
106 }
107 }
108
TEST(LowerBitSets,ByteArrayBuilder)109 TEST(LowerBitSets, ByteArrayBuilder) {
110 struct BABAlloc {
111 std::set<uint64_t> Bits;
112 uint64_t BitSize;
113 uint64_t WantByteOffset;
114 uint8_t WantMask;
115 };
116
117 struct {
118 std::vector<BABAlloc> Allocs;
119 std::vector<uint8_t> WantBytes;
120 } BABTests[] = {
121 {{{{0}, 1, 0, 1}, {{0}, 1, 0, 2}}, {3}},
122 {{{{0}, 16, 0, 1},
123 {{1}, 15, 0, 2},
124 {{2}, 14, 0, 4},
125 {{3}, 13, 0, 8},
126 {{4}, 12, 0, 0x10},
127 {{5}, 11, 0, 0x20},
128 {{6}, 10, 0, 0x40},
129 {{7}, 9, 0, 0x80},
130 {{0}, 7, 9, 0x80},
131 {{0}, 6, 10, 0x40},
132 {{0}, 5, 11, 0x20},
133 {{0}, 4, 12, 0x10},
134 {{0}, 3, 13, 8},
135 {{0}, 2, 14, 4},
136 {{0}, 1, 15, 2}},
137 {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0, 0x80, 0x40, 0x20, 0x10, 8, 4,
138 2}},
139 };
140
141 for (auto &&T : BABTests) {
142 ByteArrayBuilder BABuilder;
143
144 for (auto &&A : T.Allocs) {
145 uint64_t GotByteOffset;
146 uint8_t GotMask;
147
148 BABuilder.allocate(A.Bits, A.BitSize, GotByteOffset, GotMask);
149 EXPECT_EQ(A.WantByteOffset, GotByteOffset);
150 EXPECT_EQ(A.WantMask, GotMask);
151 }
152
153 EXPECT_EQ(T.WantBytes, BABuilder.Bytes);
154 }
155 }
156