[InstCombine] Signed saturation patterns
[llvm-complete.git] / unittests / Transforms / IPO / LowerTypeTests.cpp
blobba13378099ecb01b34e98672504a120ba24665e3
1 //===- LowerTypeTests.cpp - Unit tests for type test lowering -------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/IPO/LowerTypeTests.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
13 using namespace lowertypetests;
15 TEST(LowerTypeTests, 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},
42 false,
43 true},
44 {{0, 1, 2, 3, 4, 5, 6, 7, 8},
45 {0, 1, 2, 3, 4, 5, 6, 7, 8},
49 false,
50 true},
53 for (auto &&T : BSBTests) {
54 BitSetBuilder BSB;
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) {
73 ++I;
74 continue;
77 EXPECT_FALSE(BSI.containsGlobalOffset(NonOffset));
82 TEST(LowerTypeTests, 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}},
96 for (auto &&T : GLBTests) {
97 GlobalLayoutBuilder GLB(T.NumObjects);
98 for (auto &&F : T.Fragments)
99 GLB.addFragment(F);
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) {
110 struct BABAlloc {
111 std::set<uint64_t> Bits;
112 uint64_t BitSize;
113 uint64_t WantByteOffset;
114 uint8_t WantMask;
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}},
141 for (auto &&T : BABTests) {
142 ByteArrayBuilder BABuilder;
144 for (auto &&A : T.Allocs) {
145 uint64_t GotByteOffset;
146 uint8_t GotMask;
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);