blob: 26a425283782a21d113004b64cedda854e0aa9d2 [file] [log] [blame]
Peter Collingbournee6909c82015-02-20 20:30:47 +00001//===- 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
13using namespace llvm;
14
15TEST(LowerBitSets, BitSetBuilder) {
16 struct {
17 std::vector<uint64_t> Offsets;
18 std::vector<uint8_t> Bits;
19 uint64_t ByteOffset;
20 uint64_t BitSize;
21 unsigned AlignLog2;
22 bool IsSingleOffset;
Peter Collingbourneeba7f732015-02-25 20:42:41 +000023 bool IsAllOnes;
Peter Collingbournee6909c82015-02-20 20:30:47 +000024 } BSBTests[] = {
Peter Collingbourneeba7f732015-02-25 20:42:41 +000025 {{}, {0}, 0, 1, 0, false, false},
26 {{0}, {1}, 0, 1, 0, true, true},
27 {{4}, {1}, 4, 1, 0, true, true},
28 {{37}, {1}, 37, 1, 0, true, true},
29 {{0, 1}, {3}, 0, 2, 0, false, true},
30 {{0, 4}, {3}, 0, 2, 2, false, true},
31 {{0, uint64_t(1) << 33}, {3}, 0, 2, 33, false, true},
32 {{3, 7}, {3}, 3, 2, 2, false, true},
33 {{0, 1, 7}, {131}, 0, 8, 0, false, false},
34 {{0, 2, 14}, {131}, 0, 8, 1, false, false},
35 {{0, 1, 8}, {3, 1}, 0, 9, 0, false, false},
36 {{0, 2, 16}, {3, 1}, 0, 9, 1, false, false},
37 {{0, 1, 2, 3, 4, 5, 6, 7}, {255}, 0, 8, 0, false, true},
38 {{0, 1, 2, 3, 4, 5, 6, 7, 8}, {255, 1}, 0, 9, 0, false, true},
Peter Collingbournee6909c82015-02-20 20:30:47 +000039 };
40
41 for (auto &&T : BSBTests) {
42 BitSetBuilder BSB;
43 for (auto Offset : T.Offsets)
44 BSB.addOffset(Offset);
45
46 BitSetInfo BSI = BSB.build();
47
48 EXPECT_EQ(T.Bits, BSI.Bits);
49 EXPECT_EQ(T.ByteOffset, BSI.ByteOffset);
50 EXPECT_EQ(T.BitSize, BSI.BitSize);
51 EXPECT_EQ(T.AlignLog2, BSI.AlignLog2);
52 EXPECT_EQ(T.IsSingleOffset, BSI.isSingleOffset());
Peter Collingbourneeba7f732015-02-25 20:42:41 +000053 EXPECT_EQ(T.IsAllOnes, BSI.isAllOnes());
Peter Collingbournee6909c82015-02-20 20:30:47 +000054
55 for (auto Offset : T.Offsets)
56 EXPECT_TRUE(BSI.containsGlobalOffset(Offset));
57
58 auto I = T.Offsets.begin();
59 for (uint64_t NonOffset = 0; NonOffset != 256; ++NonOffset) {
60 if (I != T.Offsets.end() && *I == NonOffset) {
61 ++I;
62 continue;
63 }
64
65 EXPECT_FALSE(BSI.containsGlobalOffset(NonOffset));
66 }
67 }
68}
Peter Collingbourne1baeaa32015-02-24 23:17:02 +000069
70TEST(LowerBitSets, GlobalLayoutBuilder) {
71 struct {
72 uint64_t NumObjects;
73 std::vector<std::set<uint64_t>> Fragments;
74 std::vector<uint64_t> WantLayout;
75 } GLBTests[] = {
76 {0, {}, {}},
77 {4, {{0, 1}, {2, 3}}, {0, 1, 2, 3}},
78 {3, {{0, 1}, {1, 2}}, {0, 1, 2}},
79 {4, {{0, 1}, {1, 2}, {2, 3}}, {0, 1, 2, 3}},
80 {4, {{0, 1}, {2, 3}, {1, 2}}, {0, 1, 2, 3}},
81 {6, {{2, 5}, {0, 1, 2, 3, 4, 5}}, {0, 1, 2, 5, 3, 4}},
82 };
83
84 for (auto &&T : GLBTests) {
85 GlobalLayoutBuilder GLB(T.NumObjects);
86 for (auto &&F : T.Fragments)
87 GLB.addFragment(F);
88
89 std::vector<uint64_t> ComputedLayout;
90 for (auto &&F : GLB.Fragments)
91 ComputedLayout.insert(ComputedLayout.end(), F.begin(), F.end());
92
93 EXPECT_EQ(T.WantLayout, ComputedLayout);
94 }
95}