LowerBitSets.cpp 4.1 KB

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