ValueMapTest.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //===- llvm/unittest/ADT/ValueMapTest.cpp - ValueMap unit tests -*- C++ -*-===//
  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. //===----------------------------------------------------------------------===//
  8. #include "llvm/IR/ValueMap.h"
  9. #include "llvm/Config/llvm-config.h"
  10. #include "llvm/IR/Constants.h"
  11. #include "llvm/IR/Instructions.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "gtest/gtest.h"
  14. using namespace llvm;
  15. namespace {
  16. // Test fixture
  17. template<typename T>
  18. class ValueMapTest : public testing::Test {
  19. protected:
  20. LLVMContext Context;
  21. Constant *ConstantV;
  22. std::unique_ptr<BitCastInst> BitcastV;
  23. std::unique_ptr<BinaryOperator> AddV;
  24. ValueMapTest()
  25. : ConstantV(ConstantInt::get(Type::getInt32Ty(Context), 0)),
  26. BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(Context))),
  27. AddV(BinaryOperator::CreateAdd(ConstantV, ConstantV)) {}
  28. };
  29. // Run everything on Value*, a subtype to make sure that casting works as
  30. // expected, and a const subtype to make sure we cast const correctly.
  31. typedef ::testing::Types<Value, Instruction, const Instruction> KeyTypes;
  32. TYPED_TEST_CASE(ValueMapTest, KeyTypes);
  33. TYPED_TEST(ValueMapTest, Null) {
  34. ValueMap<TypeParam*, int> VM1;
  35. VM1[nullptr] = 7;
  36. EXPECT_EQ(7, VM1.lookup(nullptr));
  37. }
  38. TYPED_TEST(ValueMapTest, FollowsValue) {
  39. ValueMap<TypeParam*, int> VM;
  40. VM[this->BitcastV.get()] = 7;
  41. EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
  42. EXPECT_EQ(0u, VM.count(this->AddV.get()));
  43. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  44. EXPECT_EQ(7, VM.lookup(this->AddV.get()));
  45. EXPECT_EQ(0u, VM.count(this->BitcastV.get()));
  46. this->AddV.reset();
  47. EXPECT_EQ(0u, VM.count(this->AddV.get()));
  48. EXPECT_EQ(0u, VM.count(this->BitcastV.get()));
  49. EXPECT_EQ(0U, VM.size());
  50. }
  51. TYPED_TEST(ValueMapTest, OperationsWork) {
  52. ValueMap<TypeParam*, int> VM;
  53. ValueMap<TypeParam*, int> VM2(16); (void)VM2;
  54. typename ValueMapConfig<TypeParam*>::ExtraData Data;
  55. ValueMap<TypeParam*, int> VM3(Data, 16); (void)VM3;
  56. EXPECT_TRUE(VM.empty());
  57. VM[this->BitcastV.get()] = 7;
  58. // Find:
  59. typename ValueMap<TypeParam*, int>::iterator I =
  60. VM.find(this->BitcastV.get());
  61. ASSERT_TRUE(I != VM.end());
  62. EXPECT_EQ(this->BitcastV.get(), I->first);
  63. EXPECT_EQ(7, I->second);
  64. EXPECT_TRUE(VM.find(this->AddV.get()) == VM.end());
  65. // Const find:
  66. const ValueMap<TypeParam*, int> &CVM = VM;
  67. typename ValueMap<TypeParam*, int>::const_iterator CI =
  68. CVM.find(this->BitcastV.get());
  69. ASSERT_TRUE(CI != CVM.end());
  70. EXPECT_EQ(this->BitcastV.get(), CI->first);
  71. EXPECT_EQ(7, CI->second);
  72. EXPECT_TRUE(CVM.find(this->AddV.get()) == CVM.end());
  73. // Insert:
  74. std::pair<typename ValueMap<TypeParam*, int>::iterator, bool> InsertResult1 =
  75. VM.insert(std::make_pair(this->AddV.get(), 3));
  76. EXPECT_EQ(this->AddV.get(), InsertResult1.first->first);
  77. EXPECT_EQ(3, InsertResult1.first->second);
  78. EXPECT_TRUE(InsertResult1.second);
  79. EXPECT_EQ(1u, VM.count(this->AddV.get()));
  80. std::pair<typename ValueMap<TypeParam*, int>::iterator, bool> InsertResult2 =
  81. VM.insert(std::make_pair(this->AddV.get(), 5));
  82. EXPECT_EQ(this->AddV.get(), InsertResult2.first->first);
  83. EXPECT_EQ(3, InsertResult2.first->second);
  84. EXPECT_FALSE(InsertResult2.second);
  85. // Erase:
  86. VM.erase(InsertResult2.first);
  87. EXPECT_EQ(0U, VM.count(this->AddV.get()));
  88. EXPECT_EQ(1U, VM.count(this->BitcastV.get()));
  89. VM.erase(this->BitcastV.get());
  90. EXPECT_EQ(0U, VM.count(this->BitcastV.get()));
  91. EXPECT_EQ(0U, VM.size());
  92. // Range insert:
  93. SmallVector<std::pair<Instruction*, int>, 2> Elems;
  94. Elems.push_back(std::make_pair(this->AddV.get(), 1));
  95. Elems.push_back(std::make_pair(this->BitcastV.get(), 2));
  96. VM.insert(Elems.begin(), Elems.end());
  97. EXPECT_EQ(1, VM.lookup(this->AddV.get()));
  98. EXPECT_EQ(2, VM.lookup(this->BitcastV.get()));
  99. }
  100. template<typename ExpectedType, typename VarType>
  101. void CompileAssertHasType(VarType) {
  102. static_assert(std::is_same<ExpectedType, VarType>::value,
  103. "Not the same type");
  104. }
  105. TYPED_TEST(ValueMapTest, Iteration) {
  106. ValueMap<TypeParam*, int> VM;
  107. VM[this->BitcastV.get()] = 2;
  108. VM[this->AddV.get()] = 3;
  109. size_t size = 0;
  110. for (typename ValueMap<TypeParam*, int>::iterator I = VM.begin(), E = VM.end();
  111. I != E; ++I) {
  112. ++size;
  113. std::pair<TypeParam*, int> value = *I; (void)value;
  114. CompileAssertHasType<TypeParam*>(I->first);
  115. if (I->second == 2) {
  116. EXPECT_EQ(this->BitcastV.get(), I->first);
  117. I->second = 5;
  118. } else if (I->second == 3) {
  119. EXPECT_EQ(this->AddV.get(), I->first);
  120. I->second = 6;
  121. } else {
  122. ADD_FAILURE() << "Iterated through an extra value.";
  123. }
  124. }
  125. EXPECT_EQ(2U, size);
  126. EXPECT_EQ(5, VM[this->BitcastV.get()]);
  127. EXPECT_EQ(6, VM[this->AddV.get()]);
  128. size = 0;
  129. // Cast to const ValueMap to avoid a bug in DenseMap's iterators.
  130. const ValueMap<TypeParam*, int>& CVM = VM;
  131. for (typename ValueMap<TypeParam*, int>::const_iterator I = CVM.begin(),
  132. E = CVM.end(); I != E; ++I) {
  133. ++size;
  134. std::pair<TypeParam*, int> value = *I; (void)value;
  135. CompileAssertHasType<TypeParam*>(I->first);
  136. if (I->second == 5) {
  137. EXPECT_EQ(this->BitcastV.get(), I->first);
  138. } else if (I->second == 6) {
  139. EXPECT_EQ(this->AddV.get(), I->first);
  140. } else {
  141. ADD_FAILURE() << "Iterated through an extra value.";
  142. }
  143. }
  144. EXPECT_EQ(2U, size);
  145. }
  146. TYPED_TEST(ValueMapTest, DefaultCollisionBehavior) {
  147. // By default, we overwrite the old value with the replaced value.
  148. ValueMap<TypeParam*, int> VM;
  149. VM[this->BitcastV.get()] = 7;
  150. VM[this->AddV.get()] = 9;
  151. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  152. EXPECT_EQ(0u, VM.count(this->BitcastV.get()));
  153. EXPECT_EQ(9, VM.lookup(this->AddV.get()));
  154. }
  155. TYPED_TEST(ValueMapTest, ConfiguredCollisionBehavior) {
  156. // TODO: Implement this when someone needs it.
  157. }
  158. template<typename KeyT, typename MutexT>
  159. struct LockMutex : ValueMapConfig<KeyT, MutexT> {
  160. struct ExtraData {
  161. MutexT *M;
  162. bool *CalledRAUW;
  163. bool *CalledDeleted;
  164. };
  165. static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) {
  166. *Data.CalledRAUW = true;
  167. EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked.";
  168. }
  169. static void onDelete(const ExtraData &Data, KeyT Old) {
  170. *Data.CalledDeleted = true;
  171. EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked.";
  172. }
  173. static MutexT *getMutex(const ExtraData &Data) { return Data.M; }
  174. };
  175. // FIXME: These tests started failing on Windows.
  176. #if LLVM_ENABLE_THREADS && !defined(_WIN32)
  177. TYPED_TEST(ValueMapTest, LocksMutex) {
  178. std::mutex M;
  179. bool CalledRAUW = false, CalledDeleted = false;
  180. typedef LockMutex<TypeParam*, std::mutex> ConfigType;
  181. typename ConfigType::ExtraData Data = {&M, &CalledRAUW, &CalledDeleted};
  182. ValueMap<TypeParam*, int, ConfigType> VM(Data);
  183. VM[this->BitcastV.get()] = 7;
  184. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  185. this->AddV.reset();
  186. EXPECT_TRUE(CalledRAUW);
  187. EXPECT_TRUE(CalledDeleted);
  188. }
  189. #endif
  190. template<typename KeyT>
  191. struct NoFollow : ValueMapConfig<KeyT> {
  192. enum { FollowRAUW = false };
  193. };
  194. TYPED_TEST(ValueMapTest, NoFollowRAUW) {
  195. ValueMap<TypeParam*, int, NoFollow<TypeParam*> > VM;
  196. VM[this->BitcastV.get()] = 7;
  197. EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
  198. EXPECT_EQ(0u, VM.count(this->AddV.get()));
  199. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  200. EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
  201. EXPECT_EQ(0, VM.lookup(this->AddV.get()));
  202. this->AddV.reset();
  203. EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
  204. EXPECT_EQ(0, VM.lookup(this->AddV.get()));
  205. this->BitcastV.reset();
  206. EXPECT_EQ(0, VM.lookup(this->BitcastV.get()));
  207. EXPECT_EQ(0, VM.lookup(this->AddV.get()));
  208. EXPECT_EQ(0U, VM.size());
  209. }
  210. template<typename KeyT>
  211. struct CountOps : ValueMapConfig<KeyT> {
  212. struct ExtraData {
  213. int *Deletions;
  214. int *RAUWs;
  215. };
  216. static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) {
  217. ++*Data.RAUWs;
  218. }
  219. static void onDelete(const ExtraData &Data, KeyT Old) {
  220. ++*Data.Deletions;
  221. }
  222. };
  223. TYPED_TEST(ValueMapTest, CallsConfig) {
  224. int Deletions = 0, RAUWs = 0;
  225. typename CountOps<TypeParam*>::ExtraData Data = {&Deletions, &RAUWs};
  226. ValueMap<TypeParam*, int, CountOps<TypeParam*> > VM(Data);
  227. VM[this->BitcastV.get()] = 7;
  228. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  229. EXPECT_EQ(0, Deletions);
  230. EXPECT_EQ(1, RAUWs);
  231. this->AddV.reset();
  232. EXPECT_EQ(1, Deletions);
  233. EXPECT_EQ(1, RAUWs);
  234. this->BitcastV.reset();
  235. EXPECT_EQ(1, Deletions);
  236. EXPECT_EQ(1, RAUWs);
  237. }
  238. template<typename KeyT>
  239. struct ModifyingConfig : ValueMapConfig<KeyT> {
  240. // We'll put a pointer here back to the ValueMap this key is in, so
  241. // that we can modify it (and clobber *this) before the ValueMap
  242. // tries to do the same modification. In previous versions of
  243. // ValueMap, that exploded.
  244. typedef ValueMap<KeyT, int, ModifyingConfig<KeyT> > **ExtraData;
  245. static void onRAUW(ExtraData Map, KeyT Old, KeyT New) {
  246. (*Map)->erase(Old);
  247. }
  248. static void onDelete(ExtraData Map, KeyT Old) {
  249. (*Map)->erase(Old);
  250. }
  251. };
  252. TYPED_TEST(ValueMapTest, SurvivesModificationByConfig) {
  253. ValueMap<TypeParam*, int, ModifyingConfig<TypeParam*> > *MapAddress;
  254. ValueMap<TypeParam*, int, ModifyingConfig<TypeParam*> > VM(&MapAddress);
  255. MapAddress = &VM;
  256. // Now the ModifyingConfig can modify the Map inside a callback.
  257. VM[this->BitcastV.get()] = 7;
  258. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  259. EXPECT_EQ(0u, VM.count(this->BitcastV.get()));
  260. EXPECT_EQ(0u, VM.count(this->AddV.get()));
  261. VM[this->AddV.get()] = 7;
  262. this->AddV.reset();
  263. EXPECT_EQ(0u, VM.count(this->AddV.get()));
  264. }
  265. } // end namespace