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