blob: e19b3a639d32981951d3ab2bab083727e53dc631 [file] [log] [blame]
Chandler Carruth4220e9c2014-03-04 11:17:44 +00001//===- ValueHandleTest.cpp - ValueHandle tests ----------------------------===//
Jeffrey Yasskin41f24772009-04-27 20:32:07 +00002//
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
Chandler Carruth4220e9c2014-03-04 11:17:44 +000010#include "llvm/IR/ValueHandle.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
12#include "llvm/IR/Instructions.h"
13#include "llvm/IR/LLVMContext.h"
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000014#include "gtest/gtest.h"
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000015#include <memory>
16
17using namespace llvm;
18
19namespace {
20
21class ValueHandle : public testing::Test {
22protected:
Mehdi Amini03b42e42016-04-14 21:59:01 +000023 LLVMContext Context;
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000024 Constant *ConstantV;
David Blaikie17ab82c2015-04-22 04:39:13 +000025 std::unique_ptr<BitCastInst> BitcastV;
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000026
Mehdi Amini03b42e42016-04-14 21:59:01 +000027 ValueHandle()
28 : ConstantV(ConstantInt::get(Type::getInt32Ty(Context), 0)),
29 BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(Context))) {}
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000030};
31
David Blaikie774b5842015-08-03 22:30:24 +000032class ConcreteCallbackVH final : public CallbackVH {
Dan Gohman745ad442009-05-02 21:10:48 +000033public:
Dan Gohman745ad442009-05-02 21:10:48 +000034 ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
35};
36
Sanjoy Das7de051b2017-04-26 16:20:59 +000037TEST_F(ValueHandle, WeakVH_BasicOperation) {
38 WeakVH WVH(BitcastV.get());
39 EXPECT_EQ(BitcastV.get(), WVH);
40 WVH = ConstantV;
41 EXPECT_EQ(ConstantV, WVH);
42
43 // Make sure I can call a method on the underlying Value. It
44 // doesn't matter which method.
45 EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
46 EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
Sanjoy Das7de051b2017-04-26 16:20:59 +000047}
48
Sanjoy Das2cbeb002017-04-26 16:37:05 +000049TEST_F(ValueHandle, WeakVH_Comparisons) {
50 WeakVH BitcastWVH(BitcastV.get());
51 WeakVH ConstantWVH(ConstantV);
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000052
53 EXPECT_TRUE(BitcastWVH == BitcastWVH);
54 EXPECT_TRUE(BitcastV.get() == BitcastWVH);
55 EXPECT_TRUE(BitcastWVH == BitcastV.get());
56 EXPECT_FALSE(BitcastWVH == ConstantWVH);
57
58 EXPECT_TRUE(BitcastWVH != ConstantWVH);
59 EXPECT_TRUE(BitcastV.get() != ConstantWVH);
60 EXPECT_TRUE(BitcastWVH != ConstantV);
61 EXPECT_FALSE(BitcastWVH != BitcastWVH);
62
63 // Cast to Value* so comparisons work.
64 Value *BV = BitcastV.get();
65 Value *CV = ConstantV;
66 EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
67 EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
68 EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
69 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
70
71 EXPECT_EQ(BV < CV, BitcastV.get() < 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
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
Sanjoy Das2cbeb002017-04-26 16:37:05 +000082TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
83 WeakVH WVH(BitcastV.get());
84 WeakVH WVH_Copy(WVH);
85 WeakVH WVH_Recreated(BitcastV.get());
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000086 BitcastV->replaceAllUsesWith(ConstantV);
87 EXPECT_EQ(ConstantV, WVH);
88 EXPECT_EQ(ConstantV, WVH_Copy);
89 EXPECT_EQ(ConstantV, WVH_Recreated);
90}
91
Sanjoy Das2cbeb002017-04-26 16:37:05 +000092TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
93 WeakVH WVH(BitcastV.get());
94 WeakVH WVH_Copy(WVH);
95 WeakVH WVH_Recreated(BitcastV.get());
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000096 BitcastV.reset();
Craig Topper66f09ad2014-06-08 22:29:17 +000097 Value *null_value = nullptr;
Jeffrey Yasskin41f24772009-04-27 20:32:07 +000098 EXPECT_EQ(null_value, WVH);
99 EXPECT_EQ(null_value, WVH_Copy);
100 EXPECT_EQ(null_value, WVH_Recreated);
101}
102
103
104TEST_F(ValueHandle, AssertingVH_BasicOperation) {
105 AssertingVH<CastInst> AVH(BitcastV.get());
106 CastInst *implicit_to_exact_type = AVH;
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000107 (void)implicit_to_exact_type; // Avoid warning.
Jeffrey Yasskin41f24772009-04-27 20:32:07 +0000108
109 AssertingVH<Value> GenericAVH(BitcastV.get());
110 EXPECT_EQ(BitcastV.get(), GenericAVH);
111 GenericAVH = ConstantV;
112 EXPECT_EQ(ConstantV, GenericAVH);
113
114 // Make sure I can call a method on the underlying CastInst. It
115 // doesn't matter which method.
116 EXPECT_FALSE(AVH->mayWriteToMemory());
117 EXPECT_FALSE((*AVH).mayWriteToMemory());
118}
119
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000120TEST_F(ValueHandle, AssertingVH_Const) {
121 const CastInst *ConstBitcast = BitcastV.get();
122 AssertingVH<const CastInst> AVH(ConstBitcast);
123 const CastInst *implicit_to_exact_type = AVH;
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000124 (void)implicit_to_exact_type; // Avoid warning.
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000125}
126
Jeffrey Yasskin41f24772009-04-27 20:32:07 +0000127TEST_F(ValueHandle, AssertingVH_Comparisons) {
128 AssertingVH<Value> BitcastAVH(BitcastV.get());
129 AssertingVH<Value> ConstantAVH(ConstantV);
130
131 EXPECT_TRUE(BitcastAVH == BitcastAVH);
132 EXPECT_TRUE(BitcastV.get() == BitcastAVH);
133 EXPECT_TRUE(BitcastAVH == BitcastV.get());
134 EXPECT_FALSE(BitcastAVH == ConstantAVH);
135
136 EXPECT_TRUE(BitcastAVH != ConstantAVH);
137 EXPECT_TRUE(BitcastV.get() != ConstantAVH);
138 EXPECT_TRUE(BitcastAVH != ConstantV);
139 EXPECT_FALSE(BitcastAVH != BitcastAVH);
140
141 // Cast to Value* so comparisons work.
142 Value *BV = BitcastV.get();
143 Value *CV = ConstantV;
144 EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
145 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
146 EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
147 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
148
149 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
150 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
151 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
152 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
153
154 EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
155 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
156 EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
157 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
158}
159
160TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
161 AssertingVH<Value> AVH(BitcastV.get());
162 BitcastV->replaceAllUsesWith(ConstantV);
163 EXPECT_EQ(BitcastV.get(), AVH);
164}
165
166#ifdef NDEBUG
167
168TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
169 EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
170}
171
172#else // !NDEBUG
173
174#ifdef GTEST_HAS_DEATH_TEST
175
176TEST_F(ValueHandle, AssertingVH_Asserts) {
177 AssertingVH<Value> AVH(BitcastV.get());
178 EXPECT_DEATH({BitcastV.reset();},
179 "An asserting value handle still pointed to this value!");
180 AssertingVH<Value> Copy(AVH);
Craig Topper66f09ad2014-06-08 22:29:17 +0000181 AVH = nullptr;
Jeffrey Yasskin41f24772009-04-27 20:32:07 +0000182 EXPECT_DEATH({BitcastV.reset();},
183 "An asserting value handle still pointed to this value!");
Craig Topper66f09ad2014-06-08 22:29:17 +0000184 Copy = nullptr;
Jeffrey Yasskin41f24772009-04-27 20:32:07 +0000185 BitcastV.reset();
186}
187
188#endif // GTEST_HAS_DEATH_TEST
189
190#endif // NDEBUG
191
Dan Gohman745ad442009-05-02 21:10:48 +0000192TEST_F(ValueHandle, CallbackVH_BasicOperation) {
193 ConcreteCallbackVH CVH(BitcastV.get());
194 EXPECT_EQ(BitcastV.get(), CVH);
195 CVH = ConstantV;
196 EXPECT_EQ(ConstantV, CVH);
197
198 // Make sure I can call a method on the underlying Value. It
199 // doesn't matter which method.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000200 EXPECT_EQ(Type::getInt32Ty(Context), CVH->getType());
201 EXPECT_EQ(Type::getInt32Ty(Context), (*CVH).getType());
Dan Gohman745ad442009-05-02 21:10:48 +0000202}
203
204TEST_F(ValueHandle, CallbackVH_Comparisons) {
205 ConcreteCallbackVH BitcastCVH(BitcastV.get());
206 ConcreteCallbackVH ConstantCVH(ConstantV);
207
208 EXPECT_TRUE(BitcastCVH == BitcastCVH);
209 EXPECT_TRUE(BitcastV.get() == BitcastCVH);
210 EXPECT_TRUE(BitcastCVH == BitcastV.get());
211 EXPECT_FALSE(BitcastCVH == ConstantCVH);
212
213 EXPECT_TRUE(BitcastCVH != ConstantCVH);
214 EXPECT_TRUE(BitcastV.get() != ConstantCVH);
215 EXPECT_TRUE(BitcastCVH != ConstantV);
216 EXPECT_FALSE(BitcastCVH != BitcastCVH);
217
218 // Cast to Value* so comparisons work.
219 Value *BV = BitcastV.get();
220 Value *CV = ConstantV;
221 EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
222 EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
223 EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
224 EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
225
226 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
227 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
228 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
229 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
230
231 EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
232 EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
233 EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
234 EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
235}
236
237TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
David Blaikie774b5842015-08-03 22:30:24 +0000238 class RecordingVH final : public CallbackVH {
Dan Gohman745ad442009-05-02 21:10:48 +0000239 public:
240 int DeletedCalls;
241 int AURWCalls;
242
243 RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
244 RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
245
246 private:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000247 void deleted() override {
248 DeletedCalls++;
249 CallbackVH::deleted();
250 }
251 void allUsesReplacedWith(Value *) override { AURWCalls++; }
Dan Gohman745ad442009-05-02 21:10:48 +0000252 };
253
254 RecordingVH RVH;
255 RVH = BitcastV.get();
256 EXPECT_EQ(0, RVH.DeletedCalls);
257 EXPECT_EQ(0, RVH.AURWCalls);
258 BitcastV.reset();
259 EXPECT_EQ(1, RVH.DeletedCalls);
260 EXPECT_EQ(0, RVH.AURWCalls);
261}
262
263TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
David Blaikie774b5842015-08-03 22:30:24 +0000264 class RecordingVH final : public CallbackVH {
Dan Gohman745ad442009-05-02 21:10:48 +0000265 public:
266 int DeletedCalls;
267 Value *AURWArgument;
268
Craig Topper66f09ad2014-06-08 22:29:17 +0000269 RecordingVH() : DeletedCalls(0), AURWArgument(nullptr) {}
Dan Gohman745ad442009-05-02 21:10:48 +0000270 RecordingVH(Value *V)
Craig Topper66f09ad2014-06-08 22:29:17 +0000271 : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr) {}
Dan Gohman745ad442009-05-02 21:10:48 +0000272
273 private:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000274 void deleted() override {
275 DeletedCalls++;
276 CallbackVH::deleted();
277 }
278 void allUsesReplacedWith(Value *new_value) override {
Craig Topper66f09ad2014-06-08 22:29:17 +0000279 EXPECT_EQ(nullptr, AURWArgument);
Dan Gohman745ad442009-05-02 21:10:48 +0000280 AURWArgument = new_value;
281 }
282 };
283
284 RecordingVH RVH;
285 RVH = BitcastV.get();
286 EXPECT_EQ(0, RVH.DeletedCalls);
Craig Topper66f09ad2014-06-08 22:29:17 +0000287 EXPECT_EQ(nullptr, RVH.AURWArgument);
Dan Gohman745ad442009-05-02 21:10:48 +0000288 BitcastV->replaceAllUsesWith(ConstantV);
289 EXPECT_EQ(0, RVH.DeletedCalls);
290 EXPECT_EQ(ConstantV, RVH.AURWArgument);
291}
292
293TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
David Blaikie774b5842015-08-03 22:30:24 +0000294 class RecoveringVH final : public CallbackVH {
Dan Gohman745ad442009-05-02 21:10:48 +0000295 public:
296 int DeletedCalls;
297 Value *AURWArgument;
Owen Anderson53a52212009-07-13 04:09:18 +0000298 LLVMContext *Context;
Dan Gohman745ad442009-05-02 21:10:48 +0000299
Mehdi Amini03b42e42016-04-14 21:59:01 +0000300 RecoveringVH(LLVMContext &TheContext)
301 : DeletedCalls(0), AURWArgument(nullptr), Context(&TheContext) {}
302
303 RecoveringVH(LLVMContext &TheContext, Value *V)
304 : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr),
305 Context(&TheContext) {}
Dan Gohman745ad442009-05-02 21:10:48 +0000306
307 private:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000308 void deleted() override {
Mehdi Amini03b42e42016-04-14 21:59:01 +0000309 getValPtr()->replaceAllUsesWith(
310 Constant::getNullValue(Type::getInt32Ty(*Context)));
Craig Topper66f09ad2014-06-08 22:29:17 +0000311 setValPtr(nullptr);
Dan Gohman745ad442009-05-02 21:10:48 +0000312 }
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000313 void allUsesReplacedWith(Value *new_value) override {
Craig Topper66f09ad2014-06-08 22:29:17 +0000314 ASSERT_TRUE(nullptr != getValPtr());
Daniel Dunbar5d816542009-05-18 03:44:24 +0000315 EXPECT_EQ(1U, getValPtr()->getNumUses());
Craig Topper66f09ad2014-06-08 22:29:17 +0000316 EXPECT_EQ(nullptr, AURWArgument);
Dan Gohman745ad442009-05-02 21:10:48 +0000317 AURWArgument = new_value;
318 }
319 };
320
321 // Normally, if a value has uses, deleting it will crash. However, we can use
322 // a CallbackVH to remove the uses before the check for no uses.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000323 RecoveringVH RVH(Context);
324 RVH = RecoveringVH(Context, BitcastV.get());
325 std::unique_ptr<BinaryOperator> BitcastUser(BinaryOperator::CreateAdd(
326 RVH, Constant::getNullValue(Type::getInt32Ty(Context))));
Dan Gohman745ad442009-05-02 21:10:48 +0000327 EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
328 BitcastV.reset(); // Would crash without the ValueHandler.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000329 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
330 RVH.AURWArgument);
331 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
Owen Anderson53a52212009-07-13 04:09:18 +0000332 BitcastUser->getOperand(0));
Dan Gohman745ad442009-05-02 21:10:48 +0000333}
334
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000335TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
336 // When a CallbackVH modifies other ValueHandles in its callbacks,
337 // that shouldn't interfere with non-modified ValueHandles receiving
338 // their appropriate callbacks.
339 //
340 // We create the active CallbackVH in the middle of a palindromic
341 // arrangement of other VHs so that the bad behavior would be
342 // triggered in whichever order callbacks run.
343
David Blaikie774b5842015-08-03 22:30:24 +0000344 class DestroyingVH final : public CallbackVH {
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000345 public:
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000346 std::unique_ptr<WeakVH> ToClear[2];
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000347 DestroyingVH(Value *V) {
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000348 ToClear[0].reset(new WeakVH(V));
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000349 setValPtr(V);
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000350 ToClear[1].reset(new WeakVH(V));
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000351 }
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000352 void deleted() override {
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000353 ToClear[0].reset();
354 ToClear[1].reset();
355 CallbackVH::deleted();
356 }
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000357 void allUsesReplacedWith(Value *) override {
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000358 ToClear[0].reset();
359 ToClear[1].reset();
360 }
361 };
362
363 {
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000364 WeakVH ShouldBeVisited1(BitcastV.get());
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000365 DestroyingVH C(BitcastV.get());
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000366 WeakVH ShouldBeVisited2(BitcastV.get());
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000367
368 BitcastV->replaceAllUsesWith(ConstantV);
369 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
370 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
371 }
372
373 {
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000374 WeakVH ShouldBeVisited1(BitcastV.get());
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000375 DestroyingVH C(BitcastV.get());
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000376 WeakVH ShouldBeVisited2(BitcastV.get());
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000377
378 BitcastV.reset();
Craig Topper66f09ad2014-06-08 22:29:17 +0000379 EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited1));
380 EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited2));
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000381 }
382}
383
384TEST_F(ValueHandle, AssertingVHCheckedLast) {
385 // If a CallbackVH exists to clear out a group of AssertingVHs on
386 // Value deletion, the CallbackVH should get a chance to do so
387 // before the AssertingVHs assert.
388
David Blaikie774b5842015-08-03 22:30:24 +0000389 class ClearingVH final : public CallbackVH {
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000390 public:
391 AssertingVH<Value> *ToClear[2];
392 ClearingVH(Value *V,
393 AssertingVH<Value> &A0, AssertingVH<Value> &A1)
394 : CallbackVH(V) {
395 ToClear[0] = &A0;
396 ToClear[1] = &A1;
397 }
398
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000399 void deleted() override {
Craig Topper66f09ad2014-06-08 22:29:17 +0000400 *ToClear[0] = nullptr;
401 *ToClear[1] = nullptr;
Jeffrey Yasskin406ac812009-10-12 17:43:32 +0000402 CallbackVH::deleted();
403 }
404 };
405
406 AssertingVH<Value> A1, A2;
407 A1 = BitcastV.get();
408 ClearingVH C(BitcastV.get(), A1, A2);
409 A2 = BitcastV.get();
410 // C.deleted() should run first, clearing the two AssertingVHs,
411 // which should prevent them from asserting.
412 BitcastV.reset();
413}
414
Chandler Carruth942c3142017-01-24 12:34:47 +0000415TEST_F(ValueHandle, PoisoningVH_BasicOperation) {
416 PoisoningVH<CastInst> VH(BitcastV.get());
417 CastInst *implicit_to_exact_type = VH;
418 (void)implicit_to_exact_type; // Avoid warning.
419
420 PoisoningVH<Value> GenericVH(BitcastV.get());
421 EXPECT_EQ(BitcastV.get(), GenericVH);
422 GenericVH = ConstantV;
423 EXPECT_EQ(ConstantV, GenericVH);
424
425 // Make sure I can call a method on the underlying CastInst. It
426 // doesn't matter which method.
427 EXPECT_FALSE(VH->mayWriteToMemory());
428 EXPECT_FALSE((*VH).mayWriteToMemory());
429}
430
431TEST_F(ValueHandle, PoisoningVH_Const) {
432 const CastInst *ConstBitcast = BitcastV.get();
433 PoisoningVH<const CastInst> VH(ConstBitcast);
434 const CastInst *implicit_to_exact_type = VH;
435 (void)implicit_to_exact_type; // Avoid warning.
436}
437
438TEST_F(ValueHandle, PoisoningVH_Comparisons) {
439 PoisoningVH<Value> BitcastVH(BitcastV.get());
440 PoisoningVH<Value> ConstantVH(ConstantV);
441
442 EXPECT_TRUE(BitcastVH == BitcastVH);
443 EXPECT_TRUE(BitcastV.get() == BitcastVH);
444 EXPECT_TRUE(BitcastVH == BitcastV.get());
445 EXPECT_FALSE(BitcastVH == ConstantVH);
446
447 EXPECT_TRUE(BitcastVH != ConstantVH);
448 EXPECT_TRUE(BitcastV.get() != ConstantVH);
449 EXPECT_TRUE(BitcastVH != ConstantV);
450 EXPECT_FALSE(BitcastVH != BitcastVH);
451
452 // Cast to Value* so comparisons work.
453 Value *BV = BitcastV.get();
454 Value *CV = ConstantV;
455 EXPECT_EQ(BV < CV, BitcastVH < ConstantVH);
456 EXPECT_EQ(BV <= CV, BitcastVH <= ConstantVH);
457 EXPECT_EQ(BV > CV, BitcastVH > ConstantVH);
458 EXPECT_EQ(BV >= CV, BitcastVH >= ConstantVH);
459
460 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantVH);
461 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantVH);
462 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantVH);
463 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantVH);
464
465 EXPECT_EQ(BV < CV, BitcastVH < ConstantV);
466 EXPECT_EQ(BV <= CV, BitcastVH <= ConstantV);
467 EXPECT_EQ(BV > CV, BitcastVH > ConstantV);
468 EXPECT_EQ(BV >= CV, BitcastVH >= ConstantV);
469}
470
471TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {
472 PoisoningVH<Value> VH(BitcastV.get());
473 BitcastV->replaceAllUsesWith(ConstantV);
474 EXPECT_TRUE(DenseMapInfo<PoisoningVH<Value>>::isEqual(VH, BitcastV.get()));
475}
476
477#ifdef NDEBUG
478
479TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {
480 EXPECT_EQ(sizeof(CastInst *), sizeof(PoisoningVH<CastInst>));
481}
482
483#else // !NDEBUG
484
Sanjoy Das8a6238202017-05-01 16:28:58 +0000485TEST_F(ValueHandle, TrackingVH_Tracks) {
486 TrackingVH<Value> VH(BitcastV.get());
487 BitcastV->replaceAllUsesWith(ConstantV);
488 EXPECT_EQ(VH, ConstantV);
489}
490
Chandler Carruth942c3142017-01-24 12:34:47 +0000491#ifdef GTEST_HAS_DEATH_TEST
492
493TEST_F(ValueHandle, PoisoningVH_Asserts) {
494 PoisoningVH<Value> VH(BitcastV.get());
495
496 // The poisoned handle shouldn't assert when the value is deleted.
497 BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
498 // But should when we access the handle.
499 EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
500
501 // Now check that poison catches RAUW.
502 VH = BitcastV.get();
503 // The replace doesn't trigger anything immediately.
504 BitcastV->replaceAllUsesWith(ConstantV);
505 // But a use does.
506 EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
507
508 // Don't clear anything out here as destroying the handles should be fine.
509}
510
Sanjoy Das8a6238202017-05-01 16:28:58 +0000511TEST_F(ValueHandle, TrackingVH_Asserts) {
512 {
513 TrackingVH<Value> VH(BitcastV.get());
514
515 // The tracking handle shouldn't assert when the value is deleted.
516 BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
517 // But should when we access the handle.
518 EXPECT_DEATH((void)*VH,
519 "TrackingVH must be non-null and valid on dereference!");
520 }
521
522 {
523 TrackingVH<Instruction> VH(BitcastV.get());
524
525 BitcastV->replaceAllUsesWith(ConstantV);
526 EXPECT_DEATH((void)*VH,
527 "Tracked Value was replaced by one with an invalid type!");
528 }
529}
530
Chandler Carruth942c3142017-01-24 12:34:47 +0000531#endif // GTEST_HAS_DEATH_TEST
532
533#endif // NDEBUG
Jeffrey Yasskin41f24772009-04-27 20:32:07 +0000534}