ValueHandleTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //===- ValueHandleTest.cpp - ValueHandle tests ----------------------------===//
  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/ValueHandle.h"
  9. #include "llvm/IR/Constants.h"
  10. #include "llvm/IR/Instructions.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "gtest/gtest.h"
  13. #include <memory>
  14. using namespace llvm;
  15. namespace {
  16. class ValueHandle : public testing::Test {
  17. protected:
  18. LLVMContext Context;
  19. Constant *ConstantV;
  20. std::unique_ptr<BitCastInst> BitcastV;
  21. ValueHandle()
  22. : ConstantV(ConstantInt::get(Type::getInt32Ty(Context), 0)),
  23. BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(Context))) {}
  24. };
  25. class ConcreteCallbackVH final : public CallbackVH {
  26. public:
  27. ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
  28. };
  29. TEST_F(ValueHandle, WeakVH_BasicOperation) {
  30. WeakVH WVH(BitcastV.get());
  31. EXPECT_EQ(BitcastV.get(), WVH);
  32. WVH = ConstantV;
  33. EXPECT_EQ(ConstantV, WVH);
  34. // Make sure I can call a method on the underlying Value. It
  35. // doesn't matter which method.
  36. EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
  37. EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
  38. WVH = BitcastV.get();
  39. BitcastV->replaceAllUsesWith(ConstantV);
  40. EXPECT_EQ(WVH, BitcastV.get());
  41. BitcastV.reset();
  42. EXPECT_EQ(WVH, nullptr);
  43. }
  44. TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
  45. WeakTrackingVH WVH(BitcastV.get());
  46. EXPECT_EQ(BitcastV.get(), WVH);
  47. WVH = ConstantV;
  48. EXPECT_EQ(ConstantV, WVH);
  49. // Make sure I can call a method on the underlying Value. It
  50. // doesn't matter which method.
  51. EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
  52. EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
  53. }
  54. TEST_F(ValueHandle, WeakTrackingVH_Comparisons) {
  55. WeakTrackingVH BitcastWVH(BitcastV.get());
  56. WeakTrackingVH ConstantWVH(ConstantV);
  57. EXPECT_TRUE(BitcastWVH == BitcastWVH);
  58. EXPECT_TRUE(BitcastV.get() == BitcastWVH);
  59. EXPECT_TRUE(BitcastWVH == BitcastV.get());
  60. EXPECT_FALSE(BitcastWVH == ConstantWVH);
  61. EXPECT_TRUE(BitcastWVH != ConstantWVH);
  62. EXPECT_TRUE(BitcastV.get() != ConstantWVH);
  63. EXPECT_TRUE(BitcastWVH != ConstantV);
  64. EXPECT_FALSE(BitcastWVH != BitcastWVH);
  65. // Cast to Value* so comparisons work.
  66. Value *BV = BitcastV.get();
  67. Value *CV = ConstantV;
  68. EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
  69. EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
  70. EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
  71. EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
  72. EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
  73. EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
  74. EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
  75. EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
  76. EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
  77. EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
  78. EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
  79. EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
  80. }
  81. TEST_F(ValueHandle, WeakTrackingVH_FollowsRAUW) {
  82. WeakTrackingVH WVH(BitcastV.get());
  83. WeakTrackingVH WVH_Copy(WVH);
  84. WeakTrackingVH WVH_Recreated(BitcastV.get());
  85. BitcastV->replaceAllUsesWith(ConstantV);
  86. EXPECT_EQ(ConstantV, WVH);
  87. EXPECT_EQ(ConstantV, WVH_Copy);
  88. EXPECT_EQ(ConstantV, WVH_Recreated);
  89. }
  90. TEST_F(ValueHandle, WeakTrackingVH_NullOnDeletion) {
  91. WeakTrackingVH WVH(BitcastV.get());
  92. WeakTrackingVH WVH_Copy(WVH);
  93. WeakTrackingVH WVH_Recreated(BitcastV.get());
  94. BitcastV.reset();
  95. Value *null_value = nullptr;
  96. EXPECT_EQ(null_value, WVH);
  97. EXPECT_EQ(null_value, WVH_Copy);
  98. EXPECT_EQ(null_value, WVH_Recreated);
  99. }
  100. TEST_F(ValueHandle, AssertingVH_BasicOperation) {
  101. AssertingVH<CastInst> AVH(BitcastV.get());
  102. CastInst *implicit_to_exact_type = AVH;
  103. (void)implicit_to_exact_type; // Avoid warning.
  104. AssertingVH<Value> GenericAVH(BitcastV.get());
  105. EXPECT_EQ(BitcastV.get(), GenericAVH);
  106. GenericAVH = ConstantV;
  107. EXPECT_EQ(ConstantV, GenericAVH);
  108. // Make sure I can call a method on the underlying CastInst. It
  109. // doesn't matter which method.
  110. EXPECT_FALSE(AVH->mayWriteToMemory());
  111. EXPECT_FALSE((*AVH).mayWriteToMemory());
  112. }
  113. TEST_F(ValueHandle, AssertingVH_Const) {
  114. const CastInst *ConstBitcast = BitcastV.get();
  115. AssertingVH<const CastInst> AVH(ConstBitcast);
  116. const CastInst *implicit_to_exact_type = AVH;
  117. (void)implicit_to_exact_type; // Avoid warning.
  118. }
  119. TEST_F(ValueHandle, AssertingVH_Comparisons) {
  120. AssertingVH<Value> BitcastAVH(BitcastV.get());
  121. AssertingVH<Value> ConstantAVH(ConstantV);
  122. EXPECT_TRUE(BitcastAVH == BitcastAVH);
  123. EXPECT_TRUE(BitcastV.get() == BitcastAVH);
  124. EXPECT_TRUE(BitcastAVH == BitcastV.get());
  125. EXPECT_FALSE(BitcastAVH == ConstantAVH);
  126. EXPECT_TRUE(BitcastAVH != ConstantAVH);
  127. EXPECT_TRUE(BitcastV.get() != ConstantAVH);
  128. EXPECT_TRUE(BitcastAVH != ConstantV);
  129. EXPECT_FALSE(BitcastAVH != BitcastAVH);
  130. // Cast to Value* so comparisons work.
  131. Value *BV = BitcastV.get();
  132. Value *CV = ConstantV;
  133. EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
  134. EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
  135. EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
  136. EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
  137. EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
  138. EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
  139. EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
  140. EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
  141. EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
  142. EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
  143. EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
  144. EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
  145. }
  146. TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
  147. AssertingVH<Value> AVH(BitcastV.get());
  148. BitcastV->replaceAllUsesWith(ConstantV);
  149. EXPECT_EQ(BitcastV.get(), AVH);
  150. }
  151. #ifdef NDEBUG
  152. TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
  153. EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
  154. }
  155. #else // !NDEBUG
  156. #ifdef GTEST_HAS_DEATH_TEST
  157. TEST_F(ValueHandle, AssertingVH_Asserts) {
  158. AssertingVH<Value> AVH(BitcastV.get());
  159. EXPECT_DEATH({BitcastV.reset();},
  160. "An asserting value handle still pointed to this value!");
  161. AssertingVH<Value> Copy(AVH);
  162. AVH = nullptr;
  163. EXPECT_DEATH({BitcastV.reset();},
  164. "An asserting value handle still pointed to this value!");
  165. Copy = nullptr;
  166. BitcastV.reset();
  167. }
  168. #endif // GTEST_HAS_DEATH_TEST
  169. #endif // NDEBUG
  170. TEST_F(ValueHandle, CallbackVH_BasicOperation) {
  171. ConcreteCallbackVH CVH(BitcastV.get());
  172. EXPECT_EQ(BitcastV.get(), CVH);
  173. CVH = ConstantV;
  174. EXPECT_EQ(ConstantV, CVH);
  175. // Make sure I can call a method on the underlying Value. It
  176. // doesn't matter which method.
  177. EXPECT_EQ(Type::getInt32Ty(Context), CVH->getType());
  178. EXPECT_EQ(Type::getInt32Ty(Context), (*CVH).getType());
  179. }
  180. TEST_F(ValueHandle, CallbackVH_Comparisons) {
  181. ConcreteCallbackVH BitcastCVH(BitcastV.get());
  182. ConcreteCallbackVH ConstantCVH(ConstantV);
  183. EXPECT_TRUE(BitcastCVH == BitcastCVH);
  184. EXPECT_TRUE(BitcastV.get() == BitcastCVH);
  185. EXPECT_TRUE(BitcastCVH == BitcastV.get());
  186. EXPECT_FALSE(BitcastCVH == ConstantCVH);
  187. EXPECT_TRUE(BitcastCVH != ConstantCVH);
  188. EXPECT_TRUE(BitcastV.get() != ConstantCVH);
  189. EXPECT_TRUE(BitcastCVH != ConstantV);
  190. EXPECT_FALSE(BitcastCVH != BitcastCVH);
  191. // Cast to Value* so comparisons work.
  192. Value *BV = BitcastV.get();
  193. Value *CV = ConstantV;
  194. EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
  195. EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
  196. EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
  197. EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
  198. EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
  199. EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
  200. EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
  201. EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
  202. EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
  203. EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
  204. EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
  205. EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
  206. }
  207. TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
  208. class RecordingVH final : public CallbackVH {
  209. public:
  210. int DeletedCalls;
  211. int AURWCalls;
  212. RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
  213. RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
  214. private:
  215. void deleted() override {
  216. DeletedCalls++;
  217. CallbackVH::deleted();
  218. }
  219. void allUsesReplacedWith(Value *) override { AURWCalls++; }
  220. };
  221. RecordingVH RVH;
  222. RVH = BitcastV.get();
  223. EXPECT_EQ(0, RVH.DeletedCalls);
  224. EXPECT_EQ(0, RVH.AURWCalls);
  225. BitcastV.reset();
  226. EXPECT_EQ(1, RVH.DeletedCalls);
  227. EXPECT_EQ(0, RVH.AURWCalls);
  228. }
  229. TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
  230. class RecordingVH final : public CallbackVH {
  231. public:
  232. int DeletedCalls;
  233. Value *AURWArgument;
  234. RecordingVH() : DeletedCalls(0), AURWArgument(nullptr) {}
  235. RecordingVH(Value *V)
  236. : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr) {}
  237. private:
  238. void deleted() override {
  239. DeletedCalls++;
  240. CallbackVH::deleted();
  241. }
  242. void allUsesReplacedWith(Value *new_value) override {
  243. EXPECT_EQ(nullptr, AURWArgument);
  244. AURWArgument = new_value;
  245. }
  246. };
  247. RecordingVH RVH;
  248. RVH = BitcastV.get();
  249. EXPECT_EQ(0, RVH.DeletedCalls);
  250. EXPECT_EQ(nullptr, RVH.AURWArgument);
  251. BitcastV->replaceAllUsesWith(ConstantV);
  252. EXPECT_EQ(0, RVH.DeletedCalls);
  253. EXPECT_EQ(ConstantV, RVH.AURWArgument);
  254. }
  255. TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
  256. class RecoveringVH final : public CallbackVH {
  257. public:
  258. int DeletedCalls;
  259. Value *AURWArgument;
  260. LLVMContext *Context;
  261. RecoveringVH(LLVMContext &TheContext)
  262. : DeletedCalls(0), AURWArgument(nullptr), Context(&TheContext) {}
  263. RecoveringVH(LLVMContext &TheContext, Value *V)
  264. : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr),
  265. Context(&TheContext) {}
  266. private:
  267. void deleted() override {
  268. getValPtr()->replaceAllUsesWith(
  269. Constant::getNullValue(Type::getInt32Ty(*Context)));
  270. setValPtr(nullptr);
  271. }
  272. void allUsesReplacedWith(Value *new_value) override {
  273. ASSERT_TRUE(nullptr != getValPtr());
  274. EXPECT_EQ(1U, getValPtr()->getNumUses());
  275. EXPECT_EQ(nullptr, AURWArgument);
  276. AURWArgument = new_value;
  277. }
  278. };
  279. // Normally, if a value has uses, deleting it will crash. However, we can use
  280. // a CallbackVH to remove the uses before the check for no uses.
  281. RecoveringVH RVH(Context);
  282. RVH = RecoveringVH(Context, BitcastV.get());
  283. std::unique_ptr<BinaryOperator> BitcastUser(BinaryOperator::CreateAdd(
  284. RVH, Constant::getNullValue(Type::getInt32Ty(Context))));
  285. EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
  286. BitcastV.reset(); // Would crash without the ValueHandler.
  287. EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
  288. RVH.AURWArgument);
  289. EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
  290. BitcastUser->getOperand(0));
  291. }
  292. TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
  293. // When a CallbackVH modifies other ValueHandles in its callbacks,
  294. // that shouldn't interfere with non-modified ValueHandles receiving
  295. // their appropriate callbacks.
  296. //
  297. // We create the active CallbackVH in the middle of a palindromic
  298. // arrangement of other VHs so that the bad behavior would be
  299. // triggered in whichever order callbacks run.
  300. class DestroyingVH final : public CallbackVH {
  301. public:
  302. std::unique_ptr<WeakTrackingVH> ToClear[2];
  303. DestroyingVH(Value *V) {
  304. ToClear[0].reset(new WeakTrackingVH(V));
  305. setValPtr(V);
  306. ToClear[1].reset(new WeakTrackingVH(V));
  307. }
  308. void deleted() override {
  309. ToClear[0].reset();
  310. ToClear[1].reset();
  311. CallbackVH::deleted();
  312. }
  313. void allUsesReplacedWith(Value *) override {
  314. ToClear[0].reset();
  315. ToClear[1].reset();
  316. }
  317. };
  318. {
  319. WeakTrackingVH ShouldBeVisited1(BitcastV.get());
  320. DestroyingVH C(BitcastV.get());
  321. WeakTrackingVH ShouldBeVisited2(BitcastV.get());
  322. BitcastV->replaceAllUsesWith(ConstantV);
  323. EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
  324. EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
  325. }
  326. {
  327. WeakTrackingVH ShouldBeVisited1(BitcastV.get());
  328. DestroyingVH C(BitcastV.get());
  329. WeakTrackingVH ShouldBeVisited2(BitcastV.get());
  330. BitcastV.reset();
  331. EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited1));
  332. EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited2));
  333. }
  334. }
  335. TEST_F(ValueHandle, AssertingVHCheckedLast) {
  336. // If a CallbackVH exists to clear out a group of AssertingVHs on
  337. // Value deletion, the CallbackVH should get a chance to do so
  338. // before the AssertingVHs assert.
  339. class ClearingVH final : public CallbackVH {
  340. public:
  341. AssertingVH<Value> *ToClear[2];
  342. ClearingVH(Value *V,
  343. AssertingVH<Value> &A0, AssertingVH<Value> &A1)
  344. : CallbackVH(V) {
  345. ToClear[0] = &A0;
  346. ToClear[1] = &A1;
  347. }
  348. void deleted() override {
  349. *ToClear[0] = nullptr;
  350. *ToClear[1] = nullptr;
  351. CallbackVH::deleted();
  352. }
  353. };
  354. AssertingVH<Value> A1, A2;
  355. A1 = BitcastV.get();
  356. ClearingVH C(BitcastV.get(), A1, A2);
  357. A2 = BitcastV.get();
  358. // C.deleted() should run first, clearing the two AssertingVHs,
  359. // which should prevent them from asserting.
  360. BitcastV.reset();
  361. }
  362. TEST_F(ValueHandle, PoisoningVH_BasicOperation) {
  363. PoisoningVH<CastInst> VH(BitcastV.get());
  364. CastInst *implicit_to_exact_type = VH;
  365. (void)implicit_to_exact_type; // Avoid warning.
  366. PoisoningVH<Value> GenericVH(BitcastV.get());
  367. EXPECT_EQ(BitcastV.get(), GenericVH);
  368. GenericVH = ConstantV;
  369. EXPECT_EQ(ConstantV, GenericVH);
  370. // Make sure I can call a method on the underlying CastInst. It
  371. // doesn't matter which method.
  372. EXPECT_FALSE(VH->mayWriteToMemory());
  373. EXPECT_FALSE((*VH).mayWriteToMemory());
  374. }
  375. TEST_F(ValueHandle, PoisoningVH_Const) {
  376. const CastInst *ConstBitcast = BitcastV.get();
  377. PoisoningVH<const CastInst> VH(ConstBitcast);
  378. const CastInst *implicit_to_exact_type = VH;
  379. (void)implicit_to_exact_type; // Avoid warning.
  380. }
  381. TEST_F(ValueHandle, PoisoningVH_Comparisons) {
  382. PoisoningVH<Value> BitcastVH(BitcastV.get());
  383. PoisoningVH<Value> ConstantVH(ConstantV);
  384. EXPECT_TRUE(BitcastVH == BitcastVH);
  385. EXPECT_TRUE(BitcastV.get() == BitcastVH);
  386. EXPECT_TRUE(BitcastVH == BitcastV.get());
  387. EXPECT_FALSE(BitcastVH == ConstantVH);
  388. EXPECT_TRUE(BitcastVH != ConstantVH);
  389. EXPECT_TRUE(BitcastV.get() != ConstantVH);
  390. EXPECT_TRUE(BitcastVH != ConstantV);
  391. EXPECT_FALSE(BitcastVH != BitcastVH);
  392. // Cast to Value* so comparisons work.
  393. Value *BV = BitcastV.get();
  394. Value *CV = ConstantV;
  395. EXPECT_EQ(BV < CV, BitcastVH < ConstantVH);
  396. EXPECT_EQ(BV <= CV, BitcastVH <= ConstantVH);
  397. EXPECT_EQ(BV > CV, BitcastVH > ConstantVH);
  398. EXPECT_EQ(BV >= CV, BitcastVH >= ConstantVH);
  399. EXPECT_EQ(BV < CV, BitcastV.get() < ConstantVH);
  400. EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantVH);
  401. EXPECT_EQ(BV > CV, BitcastV.get() > ConstantVH);
  402. EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantVH);
  403. EXPECT_EQ(BV < CV, BitcastVH < ConstantV);
  404. EXPECT_EQ(BV <= CV, BitcastVH <= ConstantV);
  405. EXPECT_EQ(BV > CV, BitcastVH > ConstantV);
  406. EXPECT_EQ(BV >= CV, BitcastVH >= ConstantV);
  407. }
  408. TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {
  409. PoisoningVH<Value> VH(BitcastV.get());
  410. BitcastV->replaceAllUsesWith(ConstantV);
  411. EXPECT_TRUE(DenseMapInfo<PoisoningVH<Value>>::isEqual(VH, BitcastV.get()));
  412. }
  413. #ifdef NDEBUG
  414. TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {
  415. EXPECT_EQ(sizeof(CastInst *), sizeof(PoisoningVH<CastInst>));
  416. }
  417. #else // !NDEBUG
  418. TEST_F(ValueHandle, TrackingVH_Tracks) {
  419. TrackingVH<Value> VH(BitcastV.get());
  420. BitcastV->replaceAllUsesWith(ConstantV);
  421. EXPECT_EQ(VH, ConstantV);
  422. }
  423. #ifdef GTEST_HAS_DEATH_TEST
  424. TEST_F(ValueHandle, PoisoningVH_Asserts) {
  425. PoisoningVH<Value> VH(BitcastV.get());
  426. // The poisoned handle shouldn't assert when the value is deleted.
  427. BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
  428. // But should when we access the handle.
  429. EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
  430. // Now check that poison catches RAUW.
  431. VH = BitcastV.get();
  432. // The replace doesn't trigger anything immediately.
  433. BitcastV->replaceAllUsesWith(ConstantV);
  434. // But a use does.
  435. EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
  436. // Don't clear anything out here as destroying the handles should be fine.
  437. }
  438. TEST_F(ValueHandle, TrackingVH_Asserts) {
  439. {
  440. TrackingVH<Value> VH(BitcastV.get());
  441. // The tracking handle shouldn't assert when the value is deleted.
  442. BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
  443. // But should when we access the handle.
  444. EXPECT_DEATH((void)*VH,
  445. "TrackingVH must be non-null and valid on dereference!");
  446. }
  447. {
  448. TrackingVH<Instruction> VH(BitcastV.get());
  449. BitcastV->replaceAllUsesWith(ConstantV);
  450. EXPECT_DEATH((void)*VH,
  451. "Tracked Value was replaced by one with an invalid type!");
  452. }
  453. }
  454. #endif // GTEST_HAS_DEATH_TEST
  455. #endif // NDEBUG
  456. }