blob: 6a6528fbddfbbe91e27468702ed43252e28c2709 [file] [log] [blame]
Jeffrey Yasskin487fa012009-04-27 20:32:07 +00001//===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===//
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
10#include "llvm/Support/ValueHandle.h"
11
12#include "llvm/Constants.h"
13#include "llvm/Instructions.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000014#include "llvm/LLVMContext.h"
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +000015#include "llvm/ADT/OwningPtr.h"
Jeffrey Yasskin487fa012009-04-27 20:32:07 +000016
17#include "gtest/gtest.h"
18
19#include <memory>
20
21using namespace llvm;
22
23namespace {
24
25class ValueHandle : public testing::Test {
26protected:
27 Constant *ConstantV;
28 std::auto_ptr<BitCastInst> BitcastV;
29
Owen Anderson9adc0ab2009-07-14 23:09:55 +000030 ValueHandle() :
Owen Anderson1d0be152009-08-13 21:58:54 +000031 ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
32 BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) {
Jeffrey Yasskin487fa012009-04-27 20:32:07 +000033 }
34};
35
Dan Gohmanc09b12c2009-05-02 21:10:48 +000036class ConcreteCallbackVH : public CallbackVH {
37public:
38 ConcreteCallbackVH() : CallbackVH() {}
39 ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
40};
41
Jeffrey Yasskin487fa012009-04-27 20:32:07 +000042TEST_F(ValueHandle, WeakVH_BasicOperation) {
43 WeakVH WVH(BitcastV.get());
44 EXPECT_EQ(BitcastV.get(), WVH);
45 WVH = ConstantV;
46 EXPECT_EQ(ConstantV, WVH);
47
48 // Make sure I can call a method on the underlying Value. It
49 // doesn't matter which method.
Owen Anderson1d0be152009-08-13 21:58:54 +000050 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType());
51 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
Jeffrey Yasskin487fa012009-04-27 20:32:07 +000052}
53
54TEST_F(ValueHandle, WeakVH_Comparisons) {
55 WeakVH BitcastWVH(BitcastV.get());
56 WeakVH ConstantWVH(ConstantV);
57
58 EXPECT_TRUE(BitcastWVH == BitcastWVH);
59 EXPECT_TRUE(BitcastV.get() == BitcastWVH);
60 EXPECT_TRUE(BitcastWVH == BitcastV.get());
61 EXPECT_FALSE(BitcastWVH == ConstantWVH);
62
63 EXPECT_TRUE(BitcastWVH != ConstantWVH);
64 EXPECT_TRUE(BitcastV.get() != ConstantWVH);
65 EXPECT_TRUE(BitcastWVH != ConstantV);
66 EXPECT_FALSE(BitcastWVH != BitcastWVH);
67
68 // Cast to Value* so comparisons work.
69 Value *BV = BitcastV.get();
70 Value *CV = ConstantV;
71 EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
72 EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
73 EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
74 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
75
76 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
77 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
78 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
79 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
80
81 EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
82 EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
83 EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
84 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
85}
86
87TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
88 WeakVH WVH(BitcastV.get());
89 WeakVH WVH_Copy(WVH);
90 WeakVH WVH_Recreated(BitcastV.get());
91 BitcastV->replaceAllUsesWith(ConstantV);
92 EXPECT_EQ(ConstantV, WVH);
93 EXPECT_EQ(ConstantV, WVH_Copy);
94 EXPECT_EQ(ConstantV, WVH_Recreated);
95}
96
97TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
98 WeakVH WVH(BitcastV.get());
99 WeakVH WVH_Copy(WVH);
100 WeakVH WVH_Recreated(BitcastV.get());
101 BitcastV.reset();
102 Value *null_value = NULL;
103 EXPECT_EQ(null_value, WVH);
104 EXPECT_EQ(null_value, WVH_Copy);
105 EXPECT_EQ(null_value, WVH_Recreated);
106}
107
108
109TEST_F(ValueHandle, AssertingVH_BasicOperation) {
110 AssertingVH<CastInst> AVH(BitcastV.get());
111 CastInst *implicit_to_exact_type = AVH;
112 implicit_to_exact_type = implicit_to_exact_type; // Avoid warning.
113
114 AssertingVH<Value> GenericAVH(BitcastV.get());
115 EXPECT_EQ(BitcastV.get(), GenericAVH);
116 GenericAVH = ConstantV;
117 EXPECT_EQ(ConstantV, GenericAVH);
118
119 // Make sure I can call a method on the underlying CastInst. It
120 // doesn't matter which method.
121 EXPECT_FALSE(AVH->mayWriteToMemory());
122 EXPECT_FALSE((*AVH).mayWriteToMemory());
123}
124
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000125TEST_F(ValueHandle, AssertingVH_Const) {
126 const CastInst *ConstBitcast = BitcastV.get();
127 AssertingVH<const CastInst> AVH(ConstBitcast);
128 const CastInst *implicit_to_exact_type = AVH;
129 implicit_to_exact_type = implicit_to_exact_type; // Avoid warning.
130}
131
Jeffrey Yasskin487fa012009-04-27 20:32:07 +0000132TEST_F(ValueHandle, AssertingVH_Comparisons) {
133 AssertingVH<Value> BitcastAVH(BitcastV.get());
134 AssertingVH<Value> ConstantAVH(ConstantV);
135
136 EXPECT_TRUE(BitcastAVH == BitcastAVH);
137 EXPECT_TRUE(BitcastV.get() == BitcastAVH);
138 EXPECT_TRUE(BitcastAVH == BitcastV.get());
139 EXPECT_FALSE(BitcastAVH == ConstantAVH);
140
141 EXPECT_TRUE(BitcastAVH != ConstantAVH);
142 EXPECT_TRUE(BitcastV.get() != ConstantAVH);
143 EXPECT_TRUE(BitcastAVH != ConstantV);
144 EXPECT_FALSE(BitcastAVH != BitcastAVH);
145
146 // Cast to Value* so comparisons work.
147 Value *BV = BitcastV.get();
148 Value *CV = ConstantV;
149 EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
150 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
151 EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
152 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
153
154 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
155 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
156 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
157 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
158
159 EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
160 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
161 EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
162 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
163}
164
165TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
166 AssertingVH<Value> AVH(BitcastV.get());
167 BitcastV->replaceAllUsesWith(ConstantV);
168 EXPECT_EQ(BitcastV.get(), AVH);
169}
170
171#ifdef NDEBUG
172
173TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
174 EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
175}
176
177#else // !NDEBUG
178
179#ifdef GTEST_HAS_DEATH_TEST
180
181TEST_F(ValueHandle, AssertingVH_Asserts) {
182 AssertingVH<Value> AVH(BitcastV.get());
183 EXPECT_DEATH({BitcastV.reset();},
184 "An asserting value handle still pointed to this value!");
185 AssertingVH<Value> Copy(AVH);
186 AVH = NULL;
187 EXPECT_DEATH({BitcastV.reset();},
188 "An asserting value handle still pointed to this value!");
189 Copy = NULL;
190 BitcastV.reset();
191}
192
193#endif // GTEST_HAS_DEATH_TEST
194
195#endif // NDEBUG
196
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000197TEST_F(ValueHandle, CallbackVH_BasicOperation) {
198 ConcreteCallbackVH CVH(BitcastV.get());
199 EXPECT_EQ(BitcastV.get(), CVH);
200 CVH = ConstantV;
201 EXPECT_EQ(ConstantV, CVH);
202
203 // Make sure I can call a method on the underlying Value. It
204 // doesn't matter which method.
Owen Anderson1d0be152009-08-13 21:58:54 +0000205 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType());
206 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType());
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000207}
208
209TEST_F(ValueHandle, CallbackVH_Comparisons) {
210 ConcreteCallbackVH BitcastCVH(BitcastV.get());
211 ConcreteCallbackVH ConstantCVH(ConstantV);
212
213 EXPECT_TRUE(BitcastCVH == BitcastCVH);
214 EXPECT_TRUE(BitcastV.get() == BitcastCVH);
215 EXPECT_TRUE(BitcastCVH == BitcastV.get());
216 EXPECT_FALSE(BitcastCVH == ConstantCVH);
217
218 EXPECT_TRUE(BitcastCVH != ConstantCVH);
219 EXPECT_TRUE(BitcastV.get() != ConstantCVH);
220 EXPECT_TRUE(BitcastCVH != ConstantV);
221 EXPECT_FALSE(BitcastCVH != BitcastCVH);
222
223 // Cast to Value* so comparisons work.
224 Value *BV = BitcastV.get();
225 Value *CV = ConstantV;
226 EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
227 EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
228 EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
229 EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
230
231 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
232 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
233 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
234 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
235
236 EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
237 EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
238 EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
239 EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
240}
241
242TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
243 class RecordingVH : public CallbackVH {
244 public:
245 int DeletedCalls;
246 int AURWCalls;
247
248 RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
249 RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
250
251 private:
252 virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
253 virtual void allUsesReplacedWith(Value *) { AURWCalls++; }
254 };
255
256 RecordingVH RVH;
257 RVH = BitcastV.get();
258 EXPECT_EQ(0, RVH.DeletedCalls);
259 EXPECT_EQ(0, RVH.AURWCalls);
260 BitcastV.reset();
261 EXPECT_EQ(1, RVH.DeletedCalls);
262 EXPECT_EQ(0, RVH.AURWCalls);
263}
264
265TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
266 class RecordingVH : public CallbackVH {
267 public:
268 int DeletedCalls;
269 Value *AURWArgument;
270
271 RecordingVH() : DeletedCalls(0), AURWArgument(NULL) {}
272 RecordingVH(Value *V)
273 : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {}
274
275 private:
276 virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
277 virtual void allUsesReplacedWith(Value *new_value) {
278 EXPECT_EQ(NULL, AURWArgument);
279 AURWArgument = new_value;
280 }
281 };
282
283 RecordingVH RVH;
284 RVH = BitcastV.get();
285 EXPECT_EQ(0, RVH.DeletedCalls);
286 EXPECT_EQ(NULL, RVH.AURWArgument);
287 BitcastV->replaceAllUsesWith(ConstantV);
288 EXPECT_EQ(0, RVH.DeletedCalls);
289 EXPECT_EQ(ConstantV, RVH.AURWArgument);
290}
291
292TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
293 class RecoveringVH : public CallbackVH {
294 public:
295 int DeletedCalls;
296 Value *AURWArgument;
Owen Anderson0a5372e2009-07-13 04:09:18 +0000297 LLVMContext *Context;
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000298
Owen Anderson0a5372e2009-07-13 04:09:18 +0000299 RecoveringVH() : DeletedCalls(0), AURWArgument(NULL),
300 Context(&getGlobalContext()) {}
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000301 RecoveringVH(Value *V)
Owen Anderson0a5372e2009-07-13 04:09:18 +0000302 : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL),
303 Context(&getGlobalContext()) {}
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000304
305 private:
306 virtual void deleted() {
Owen Anderson1d0be152009-08-13 21:58:54 +0000307 getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())));
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000308 setValPtr(NULL);
309 }
310 virtual void allUsesReplacedWith(Value *new_value) {
311 ASSERT_TRUE(NULL != getValPtr());
Daniel Dunbar7ce55362009-05-18 03:44:24 +0000312 EXPECT_EQ(1U, getValPtr()->getNumUses());
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000313 EXPECT_EQ(NULL, AURWArgument);
314 AURWArgument = new_value;
315 }
316 };
317
318 // Normally, if a value has uses, deleting it will crash. However, we can use
319 // a CallbackVH to remove the uses before the check for no uses.
320 RecoveringVH RVH;
321 RVH = BitcastV.get();
322 std::auto_ptr<BinaryOperator> BitcastUser(
Owen Anderson0a5372e2009-07-13 04:09:18 +0000323 BinaryOperator::CreateAdd(RVH,
Owen Anderson1d0be152009-08-13 21:58:54 +0000324 Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))));
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000325 EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
326 BitcastV.reset(); // Would crash without the ValueHandler.
Owen Anderson1d0be152009-08-13 21:58:54 +0000327 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument);
328 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())),
Owen Anderson0a5372e2009-07-13 04:09:18 +0000329 BitcastUser->getOperand(0));
Dan Gohmanc09b12c2009-05-02 21:10:48 +0000330}
331
Jeffrey Yasskin6a9291a2009-10-12 17:43:32 +0000332TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
333 // When a CallbackVH modifies other ValueHandles in its callbacks,
334 // that shouldn't interfere with non-modified ValueHandles receiving
335 // their appropriate callbacks.
336 //
337 // We create the active CallbackVH in the middle of a palindromic
338 // arrangement of other VHs so that the bad behavior would be
339 // triggered in whichever order callbacks run.
340
341 class DestroyingVH : public CallbackVH {
342 public:
343 OwningPtr<WeakVH> ToClear[2];
344 DestroyingVH(Value *V) {
345 ToClear[0].reset(new WeakVH(V));
346 setValPtr(V);
347 ToClear[1].reset(new WeakVH(V));
348 }
349 virtual void deleted() {
350 ToClear[0].reset();
351 ToClear[1].reset();
352 CallbackVH::deleted();
353 }
354 virtual void allUsesReplacedWith(Value *) {
355 ToClear[0].reset();
356 ToClear[1].reset();
357 }
358 };
359
360 {
361 WeakVH ShouldBeVisited1(BitcastV.get());
362 DestroyingVH C(BitcastV.get());
363 WeakVH ShouldBeVisited2(BitcastV.get());
364
365 BitcastV->replaceAllUsesWith(ConstantV);
366 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
367 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
368 }
369
370 {
371 WeakVH ShouldBeVisited1(BitcastV.get());
372 DestroyingVH C(BitcastV.get());
373 WeakVH ShouldBeVisited2(BitcastV.get());
374
375 BitcastV.reset();
376 EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited1));
377 EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited2));
378 }
379}
380
381TEST_F(ValueHandle, AssertingVHCheckedLast) {
382 // If a CallbackVH exists to clear out a group of AssertingVHs on
383 // Value deletion, the CallbackVH should get a chance to do so
384 // before the AssertingVHs assert.
385
386 class ClearingVH : public CallbackVH {
387 public:
388 AssertingVH<Value> *ToClear[2];
389 ClearingVH(Value *V,
390 AssertingVH<Value> &A0, AssertingVH<Value> &A1)
391 : CallbackVH(V) {
392 ToClear[0] = &A0;
393 ToClear[1] = &A1;
394 }
395
396 virtual void deleted() {
397 *ToClear[0] = 0;
398 *ToClear[1] = 0;
399 CallbackVH::deleted();
400 }
401 };
402
403 AssertingVH<Value> A1, A2;
404 A1 = BitcastV.get();
405 ClearingVH C(BitcastV.get(), A1, A2);
406 A2 = BitcastV.get();
407 // C.deleted() should run first, clearing the two AssertingVHs,
408 // which should prevent them from asserting.
409 BitcastV.reset();
410}
411
Jeffrey Yasskin487fa012009-04-27 20:32:07 +0000412}