1 //===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ValueHandle.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Instructions.h"
14 #include "llvm/LLVMContext.h"
15 #include "llvm/ADT/OwningPtr.h"
17 #include "gtest/gtest.h"
25 class ValueHandle : public testing::Test {
28 std::auto_ptr<BitCastInst> BitcastV;
31 ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
32 BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) {
36 class ConcreteCallbackVH : public CallbackVH {
38 ConcreteCallbackVH() : CallbackVH() {}
39 ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
42 TEST_F(ValueHandle, WeakVH_BasicOperation) {
43 WeakVH WVH(BitcastV.get());
44 EXPECT_EQ(BitcastV.get(), WVH);
46 EXPECT_EQ(ConstantV, WVH);
48 // Make sure I can call a method on the underlying Value. It
49 // doesn't matter which method.
50 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), WVH->getType());
51 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
54 TEST_F(ValueHandle, WeakVH_Comparisons) {
55 WeakVH BitcastWVH(BitcastV.get());
56 WeakVH ConstantWVH(ConstantV);
58 EXPECT_TRUE(BitcastWVH == BitcastWVH);
59 EXPECT_TRUE(BitcastV.get() == BitcastWVH);
60 EXPECT_TRUE(BitcastWVH == BitcastV.get());
61 EXPECT_FALSE(BitcastWVH == ConstantWVH);
63 EXPECT_TRUE(BitcastWVH != ConstantWVH);
64 EXPECT_TRUE(BitcastV.get() != ConstantWVH);
65 EXPECT_TRUE(BitcastWVH != ConstantV);
66 EXPECT_FALSE(BitcastWVH != BitcastWVH);
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);
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);
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);
87 TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
88 WeakVH WVH(BitcastV.get());
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);
97 TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
98 WeakVH WVH(BitcastV.get());
100 WeakVH WVH_Recreated(BitcastV.get());
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);
109 TEST_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.
114 AssertingVH<Value> GenericAVH(BitcastV.get());
115 EXPECT_EQ(BitcastV.get(), GenericAVH);
116 GenericAVH = ConstantV;
117 EXPECT_EQ(ConstantV, GenericAVH);
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());
125 TEST_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.
132 TEST_F(ValueHandle, AssertingVH_Comparisons) {
133 AssertingVH<Value> BitcastAVH(BitcastV.get());
134 AssertingVH<Value> ConstantAVH(ConstantV);
136 EXPECT_TRUE(BitcastAVH == BitcastAVH);
137 EXPECT_TRUE(BitcastV.get() == BitcastAVH);
138 EXPECT_TRUE(BitcastAVH == BitcastV.get());
139 EXPECT_FALSE(BitcastAVH == ConstantAVH);
141 EXPECT_TRUE(BitcastAVH != ConstantAVH);
142 EXPECT_TRUE(BitcastV.get() != ConstantAVH);
143 EXPECT_TRUE(BitcastAVH != ConstantV);
144 EXPECT_FALSE(BitcastAVH != BitcastAVH);
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);
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);
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);
165 TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
166 AssertingVH<Value> AVH(BitcastV.get());
167 BitcastV->replaceAllUsesWith(ConstantV);
168 EXPECT_EQ(BitcastV.get(), AVH);
173 TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
174 EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
179 #ifdef GTEST_HAS_DEATH_TEST
181 TEST_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);
187 EXPECT_DEATH({BitcastV.reset();},
188 "An asserting value handle still pointed to this value!");
193 #endif // GTEST_HAS_DEATH_TEST
197 TEST_F(ValueHandle, CallbackVH_BasicOperation) {
198 ConcreteCallbackVH CVH(BitcastV.get());
199 EXPECT_EQ(BitcastV.get(), CVH);
201 EXPECT_EQ(ConstantV, CVH);
203 // Make sure I can call a method on the underlying Value. It
204 // doesn't matter which method.
205 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType());
206 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType());
209 TEST_F(ValueHandle, CallbackVH_Comparisons) {
210 ConcreteCallbackVH BitcastCVH(BitcastV.get());
211 ConcreteCallbackVH ConstantCVH(ConstantV);
213 EXPECT_TRUE(BitcastCVH == BitcastCVH);
214 EXPECT_TRUE(BitcastV.get() == BitcastCVH);
215 EXPECT_TRUE(BitcastCVH == BitcastV.get());
216 EXPECT_FALSE(BitcastCVH == ConstantCVH);
218 EXPECT_TRUE(BitcastCVH != ConstantCVH);
219 EXPECT_TRUE(BitcastV.get() != ConstantCVH);
220 EXPECT_TRUE(BitcastCVH != ConstantV);
221 EXPECT_FALSE(BitcastCVH != BitcastCVH);
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);
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);
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);
242 TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
243 class RecordingVH : public CallbackVH {
248 RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
249 RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
252 virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
253 virtual void allUsesReplacedWith(Value *) { AURWCalls++; }
257 RVH = BitcastV.get();
258 EXPECT_EQ(0, RVH.DeletedCalls);
259 EXPECT_EQ(0, RVH.AURWCalls);
261 EXPECT_EQ(1, RVH.DeletedCalls);
262 EXPECT_EQ(0, RVH.AURWCalls);
265 TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
266 class RecordingVH : public CallbackVH {
271 RecordingVH() : DeletedCalls(0), AURWArgument(NULL) {}
272 RecordingVH(Value *V)
273 : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {}
276 virtual void deleted() { DeletedCalls++; CallbackVH::deleted(); }
277 virtual void allUsesReplacedWith(Value *new_value) {
278 EXPECT_EQ(NULL, AURWArgument);
279 AURWArgument = new_value;
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);
292 TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
293 class RecoveringVH : public CallbackVH {
297 LLVMContext *Context;
299 RecoveringVH() : DeletedCalls(0), AURWArgument(NULL),
300 Context(&getGlobalContext()) {}
301 RecoveringVH(Value *V)
302 : CallbackVH(V), DeletedCalls(0), AURWArgument(NULL),
303 Context(&getGlobalContext()) {}
306 virtual void deleted() {
307 getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())));
310 virtual void allUsesReplacedWith(Value *new_value) {
311 ASSERT_TRUE(NULL != getValPtr());
312 EXPECT_EQ(1U, getValPtr()->getNumUses());
313 EXPECT_EQ(NULL, AURWArgument);
314 AURWArgument = new_value;
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.
321 RVH = BitcastV.get();
322 std::auto_ptr<BinaryOperator> BitcastUser(
323 BinaryOperator::CreateAdd(RVH,
324 Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))));
325 EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
326 BitcastV.reset(); // Would crash without the ValueHandler.
327 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument);
328 EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())),
329 BitcastUser->getOperand(0));
332 TEST_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.
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.
341 class DestroyingVH : public CallbackVH {
343 OwningPtr<WeakVH> ToClear[2];
344 DestroyingVH(Value *V) {
345 ToClear[0].reset(new WeakVH(V));
347 ToClear[1].reset(new WeakVH(V));
349 virtual void deleted() {
352 CallbackVH::deleted();
354 virtual void allUsesReplacedWith(Value *) {
361 WeakVH ShouldBeVisited1(BitcastV.get());
362 DestroyingVH C(BitcastV.get());
363 WeakVH ShouldBeVisited2(BitcastV.get());
365 BitcastV->replaceAllUsesWith(ConstantV);
366 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
367 EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
371 WeakVH ShouldBeVisited1(BitcastV.get());
372 DestroyingVH C(BitcastV.get());
373 WeakVH ShouldBeVisited2(BitcastV.get());
376 EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited1));
377 EXPECT_EQ(NULL, static_cast<Value*>(ShouldBeVisited2));
381 TEST_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.
386 class ClearingVH : public CallbackVH {
388 AssertingVH<Value> *ToClear[2];
390 AssertingVH<Value> &A0, AssertingVH<Value> &A1)
396 virtual void deleted() {
399 CallbackVH::deleted();
403 AssertingVH<Value> A1, A2;
405 ClearingVH C(BitcastV.get(), A1, A2);
407 // C.deleted() should run first, clearing the two AssertingVHs,
408 // which should prevent them from asserting.