aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/Transforms/IPO/LowerBitSets.cpp
blob: 49a42cd20d7a940696375ded372e63c6f8ea8934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===- LowerBitSets.cpp - Unit tests for bitset lowering ------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/IPO/LowerBitSets.h"
#include "gtest/gtest.h"

using namespace llvm;

TEST(LowerBitSets, BitSetBuilder) {
  struct {
    std::vector<uint64_t> Offsets;
    std::set<uint64_t> Bits;
    uint64_t ByteOffset;
    uint64_t BitSize;
    unsigned AlignLog2;
    bool IsSingleOffset;
    bool IsAllOnes;
  } BSBTests[] = {
      {{}, std::set<uint64_t>{}, 0, 1, 0, false, false},
      {{0}, {0}, 0, 1, 0, true, true},
      {{4}, {0}, 4, 1, 0, true, true},
      {{37}, {0}, 37, 1, 0, true, true},
      {{0, 1}, {0, 1}, 0, 2, 0, false, true},
      {{0, 4}, {0, 1}, 0, 2, 2, false, true},
      {{0, uint64_t(1) << 33}, {0, 1}, 0, 2, 33, false, true},
      {{3, 7}, {0, 1}, 3, 2, 2, false, true},
      {{0, 1, 7}, {0, 1, 7}, 0, 8, 0, false, false},
      {{0, 2, 14}, {0, 1, 7}, 0, 8, 1, false, false},
      {{0, 1, 8}, {0, 1, 8}, 0, 9, 0, false, false},
      {{0, 2, 16}, {0, 1, 8}, 0, 9, 1, false, false},
      {{0, 1, 2, 3, 4, 5, 6, 7},
       {0, 1, 2, 3, 4, 5, 6, 7},
       0,
       8,
       0,
       false,
       true},
      {{0, 1, 2, 3, 4, 5, 6, 7, 8},
       {0, 1, 2, 3, 4, 5, 6, 7, 8},
       0,
       9,
       0,
       false,
       true},
  };

  for (auto &&T : BSBTests) {
    BitSetBuilder BSB;
    for (auto Offset : T.Offsets)
      BSB.addOffset(Offset);

    BitSetInfo BSI = BSB.build();

    EXPECT_EQ(T.Bits, BSI.Bits);
    EXPECT_EQ(T.ByteOffset, BSI.ByteOffset);
    EXPECT_EQ(T.BitSize, BSI.BitSize);
    EXPECT_EQ(T.AlignLog2, BSI.AlignLog2);
    EXPECT_EQ(T.IsSingleOffset, BSI.isSingleOffset());
    EXPECT_EQ(T.IsAllOnes, BSI.isAllOnes());

    for (auto Offset : T.Offsets)
      EXPECT_TRUE(BSI.containsGlobalOffset(Offset));

    auto I = T.Offsets.begin();
    for (uint64_t NonOffset = 0; NonOffset != 256; ++NonOffset) {
      if (I != T.Offsets.end() && *I == NonOffset) {
        ++I;
        continue;
      }

      EXPECT_FALSE(BSI.containsGlobalOffset(NonOffset));
    }
  }
}

TEST(LowerBitSets, GlobalLayoutBuilder) {
  struct {
    uint64_t NumObjects;
    std::vector<std::set<uint64_t>> Fragments;
    std::vector<uint64_t> WantLayout;
  } GLBTests[] = {
    {0, {}, {}},
    {4, {{0, 1}, {2, 3}}, {0, 1, 2, 3}},
    {3, {{0, 1}, {1, 2}}, {0, 1, 2}},
    {4, {{0, 1}, {1, 2}, {2, 3}}, {0, 1, 2, 3}},
    {4, {{0, 1}, {2, 3}, {1, 2}}, {0, 1, 2, 3}},
    {6, {{2, 5}, {0, 1, 2, 3, 4, 5}}, {0, 1, 2, 5, 3, 4}},
  };

  for (auto &&T : GLBTests) {
    GlobalLayoutBuilder GLB(T.NumObjects);
    for (auto &&F : T.Fragments)
      GLB.addFragment(F);

    std::vector<uint64_t> ComputedLayout;
    for (auto &&F : GLB.Fragments)
      ComputedLayout.insert(ComputedLayout.end(), F.begin(), F.end());

    EXPECT_EQ(T.WantLayout, ComputedLayout);
  }
}

TEST(LowerBitSets, ByteArrayBuilder) {
  struct BABAlloc {
    std::set<uint64_t> Bits;
    uint64_t BitSize;
    uint64_t WantByteOffset;
    uint8_t WantMask;
  };

  struct {
    std::vector<BABAlloc> Allocs;
    std::vector<uint8_t> WantBytes;
  } BABTests[] = {
      {{{{0}, 1, 0, 1}, {{0}, 1, 0, 2}}, {3}},
      {{{{0}, 16, 0, 1},
        {{1}, 15, 0, 2},
        {{2}, 14, 0, 4},
        {{3}, 13, 0, 8},
        {{4}, 12, 0, 0x10},
        {{5}, 11, 0, 0x20},
        {{6}, 10, 0, 0x40},
        {{7}, 9, 0, 0x80},
        {{0}, 7, 9, 0x80},
        {{0}, 6, 10, 0x40},
        {{0}, 5, 11, 0x20},
        {{0}, 4, 12, 0x10},
        {{0}, 3, 13, 8},
        {{0}, 2, 14, 4},
        {{0}, 1, 15, 2}},
       {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0, 0x80, 0x40, 0x20, 0x10, 8, 4,
        2}},
  };

  for (auto &&T : BABTests) {
    ByteArrayBuilder BABuilder;

    for (auto &&A : T.Allocs) {
      uint64_t GotByteOffset;
      uint8_t GotMask;

      BABuilder.allocate(A.Bits, A.BitSize, GotByteOffset, GotMask);
      EXPECT_EQ(A.WantByteOffset, GotByteOffset);
      EXPECT_EQ(A.WantMask, GotMask);
    }

    EXPECT_EQ(T.WantBytes, BABuilder.Bytes);
  }
}