ValueMapTest.cpp 9.6 KB

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