ValueMapTest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/ADT/ValueMap.h"
  10. #include "llvm/ADT/OwningPtr.h"
  11. #include "llvm/Config/llvm-config.h"
  12. #include "llvm/Constants.h"
  13. #include "llvm/Instructions.h"
  14. #include "llvm/LLVMContext.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. // Test fixture
  19. template<typename T>
  20. class ValueMapTest : public testing::Test {
  21. protected:
  22. Constant *ConstantV;
  23. OwningPtr<BitCastInst> BitcastV;
  24. OwningPtr<BinaryOperator> AddV;
  25. ValueMapTest() :
  26. ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
  27. BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))),
  28. AddV(BinaryOperator::CreateAdd(ConstantV, ConstantV)) {
  29. }
  30. };
  31. // Run everything on Value*, a subtype to make sure that casting works as
  32. // expected, and a const subtype to make sure we cast const correctly.
  33. typedef ::testing::Types<Value, Instruction, const Instruction> KeyTypes;
  34. TYPED_TEST_CASE(ValueMapTest, KeyTypes);
  35. TYPED_TEST(ValueMapTest, Null) {
  36. ValueMap<TypeParam*, int> VM1;
  37. VM1[NULL] = 7;
  38. EXPECT_EQ(7, VM1.lookup(NULL));
  39. }
  40. TYPED_TEST(ValueMapTest, FollowsValue) {
  41. ValueMap<TypeParam*, int> VM;
  42. VM[this->BitcastV.get()] = 7;
  43. EXPECT_EQ(7, VM.lookup(this->BitcastV.get()));
  44. EXPECT_EQ(0, VM.count(this->AddV.get()));
  45. this->BitcastV->replaceAllUsesWith(this->AddV.get());
  46. EXPECT_EQ(7, VM.lookup(this->AddV.get()));
  47. EXPECT_EQ(0, VM.count(this->BitcastV.get()));
  48. this->AddV.reset();
  49. EXPECT_EQ(0, VM.count(this->AddV.get()));
  50. EXPECT_EQ(0, VM.count(this->BitcastV.get()));
  51. EXPECT_EQ(0U, VM.size());
  52. }
  53. TYPED_TEST(ValueMapTest, OperationsWork) {
  54. ValueMap<TypeParam*, int> VM;
  55. ValueMap<TypeParam*, int> VM2(16); (void)VM2;
  56. typename ValueMapConfig<TypeParam*>::ExtraData Data;
  57. ValueMap<TypeParam*, int> VM3(Data, 16); (void)VM3;
  58. EXPECT_TRUE(VM.empty());
  59. VM[this->BitcastV.get()] = 7;
  60. // Find:
  61. typename ValueMap<TypeParam*, int>::iterator I =
  62. VM.find(this->BitcastV.get());
  63. ASSERT_TRUE(I != VM.end());
  64. EXPECT_EQ(this->BitcastV.get(), I->first);
  65. EXPECT_EQ(7, I->second);
  66. EXPECT_TRUE(VM.find(this->AddV.get()) == VM.end());
  67. // Const find:
  68. const ValueMap<TypeParam*, int> &CVM = VM;
  69. typename ValueMap<TypeParam*, int>::const_iterator CI =
  70. CVM.find(this->BitcastV.get());
  71. ASSERT_TRUE(CI != CVM.end());
  72. EXPECT_EQ(this->BitcastV.get(), CI->first);
  73. EXPECT_EQ(7, CI->second);
  74. EXPECT_TRUE(CVM.find(this->AddV.get()) == CVM.end());
  75. // Insert:
  76. std::pair<typename ValueMap<TypeParam*, int>::iterator, bool> InsertResult1 =
  77. VM.insert(std::make_pair(this->AddV.get(), 3));
  78. EXPECT_EQ(this->AddV.get(), InsertResult1.first->first);
  79. EXPECT_EQ(3, InsertResult1.first->second);
  80. EXPECT_TRUE(InsertResult1.second);
  81. EXPECT_EQ(true, VM.count(this->AddV.get()));
  82. std::pair<typename ValueMap<TypeParam*, int>::iterator, bool> InsertResult2 =
  83. VM.insert(std::make_pair(this->AddV.get(), 5));
  84. EXPECT_EQ(this->AddV.get(), InsertResult2.first->first);
  85. EXPECT_EQ(3, InsertResult2.first->second);
  86. EXPECT_FALSE(InsertResult2.second);
  87. // Erase:
  88. VM.erase(InsertResult2.first);
  89. EXPECT_EQ(0U, VM.count(this->AddV.get()));
  90. EXPECT_EQ(1U, VM.count(this->BitcastV.get()));
  91. VM.erase(this->BitcastV.get());
  92. EXPECT_EQ(0U, VM.count(this->BitcastV.get()));
  93. EXPECT_EQ(0U, VM.size());
  94. // Range insert:
  95. SmallVector<std::pair<Instruction*, int>, 2> Elems;
  96. Elems.push_back(std::make_pair(this->AddV.get(), 1));
  97. Elems.push_back(std::make_pair(this->BitcastV.get(), 2));
  98. VM.insert(Elems.begin(), Elems.end());
  99. EXPECT_EQ(1, VM.lookup(this->AddV.get()));
  100. EXPECT_EQ(2, VM.lookup(this->BitcastV.get()));
  101. }
  102. template<typename ExpectedType, typename VarType>
  103. void CompileAssertHasType(VarType) {
  104. typedef char assert[is_same<ExpectedType, VarType>::value ? 1 : -1];
  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(0, 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>
  160. struct LockMutex : ValueMapConfig<KeyT> {
  161. struct ExtraData {
  162. sys::Mutex *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->tryacquire()) << "Mutex should already be locked.";
  169. }
  170. static void onDelete(const ExtraData &Data, KeyT Old) {
  171. *Data.CalledDeleted = true;
  172. EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked.";
  173. }
  174. static sys::Mutex *getMutex(const ExtraData &Data) { return Data.M; }
  175. };
  176. #if LLVM_ENABLE_THREADS
  177. TYPED_TEST(ValueMapTest, LocksMutex) {
  178. sys::Mutex M(false); // Not recursive.
  179. bool CalledRAUW = false, CalledDeleted = false;
  180. typename LockMutex<TypeParam*>::ExtraData Data =
  181. {&M, &CalledRAUW, &CalledDeleted};
  182. ValueMap<TypeParam*, int, LockMutex<TypeParam*> > 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(0, 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_FALSE(VM.count(this->BitcastV.get()));
  260. EXPECT_FALSE(VM.count(this->AddV.get()));
  261. VM[this->AddV.get()] = 7;
  262. this->AddV.reset();
  263. EXPECT_FALSE(VM.count(this->AddV.get()));
  264. }
  265. }