1 //===- 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/IR/ValueHandle.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "gtest/gtest.h"
15 #include <memory>
16 
17 using namespace llvm;
18 
19 namespace {
20 
21 class ValueHandle : public testing::Test {
22 protected:
23   Constant *ConstantV;
24   std::auto_ptr<BitCastInst> BitcastV;
25 
ValueHandle()26   ValueHandle() :
27     ConstantV(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0)),
28     BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(getGlobalContext()))) {
29   }
30 };
31 
32 class ConcreteCallbackVH : public CallbackVH {
33 public:
ConcreteCallbackVH(Value * V)34   ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
35 };
36 
TEST_F(ValueHandle,WeakVH_BasicOperation)37 TEST_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(getGlobalContext()), WVH->getType());
46   EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*WVH).getType());
47 }
48 
TEST_F(ValueHandle,WeakVH_Comparisons)49 TEST_F(ValueHandle, WeakVH_Comparisons) {
50   WeakVH BitcastWVH(BitcastV.get());
51   WeakVH ConstantWVH(ConstantV);
52 
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 
TEST_F(ValueHandle,WeakVH_FollowsRAUW)82 TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
83   WeakVH WVH(BitcastV.get());
84   WeakVH WVH_Copy(WVH);
85   WeakVH WVH_Recreated(BitcastV.get());
86   BitcastV->replaceAllUsesWith(ConstantV);
87   EXPECT_EQ(ConstantV, WVH);
88   EXPECT_EQ(ConstantV, WVH_Copy);
89   EXPECT_EQ(ConstantV, WVH_Recreated);
90 }
91 
TEST_F(ValueHandle,WeakVH_NullOnDeletion)92 TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
93   WeakVH WVH(BitcastV.get());
94   WeakVH WVH_Copy(WVH);
95   WeakVH WVH_Recreated(BitcastV.get());
96   BitcastV.reset();
97   Value *null_value = nullptr;
98   EXPECT_EQ(null_value, WVH);
99   EXPECT_EQ(null_value, WVH_Copy);
100   EXPECT_EQ(null_value, WVH_Recreated);
101 }
102 
103 
TEST_F(ValueHandle,AssertingVH_BasicOperation)104 TEST_F(ValueHandle, AssertingVH_BasicOperation) {
105   AssertingVH<CastInst> AVH(BitcastV.get());
106   CastInst *implicit_to_exact_type = AVH;
107   (void)implicit_to_exact_type;  // Avoid warning.
108 
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 
TEST_F(ValueHandle,AssertingVH_Const)120 TEST_F(ValueHandle, AssertingVH_Const) {
121   const CastInst *ConstBitcast = BitcastV.get();
122   AssertingVH<const CastInst> AVH(ConstBitcast);
123   const CastInst *implicit_to_exact_type = AVH;
124   (void)implicit_to_exact_type;  // Avoid warning.
125 }
126 
TEST_F(ValueHandle,AssertingVH_Comparisons)127 TEST_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 
TEST_F(ValueHandle,AssertingVH_DoesNotFollowRAUW)160 TEST_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 
TEST_F(ValueHandle,AssertingVH_ReducesToPointer)168 TEST_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 
TEST_F(ValueHandle,AssertingVH_Asserts)176 TEST_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);
181   AVH = nullptr;
182   EXPECT_DEATH({BitcastV.reset();},
183                "An asserting value handle still pointed to this value!");
184   Copy = nullptr;
185   BitcastV.reset();
186 }
187 
188 #endif  // GTEST_HAS_DEATH_TEST
189 
190 #endif  // NDEBUG
191 
TEST_F(ValueHandle,CallbackVH_BasicOperation)192 TEST_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.
200   EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), CVH->getType());
201   EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (*CVH).getType());
202 }
203 
TEST_F(ValueHandle,CallbackVH_Comparisons)204 TEST_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 
TEST_F(ValueHandle,CallbackVH_CallbackOnDeletion)237 TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
238   class RecordingVH : public CallbackVH {
239   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:
247     void deleted() override {
248       DeletedCalls++;
249       CallbackVH::deleted();
250     }
251     void allUsesReplacedWith(Value *) override { AURWCalls++; }
252   };
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 
TEST_F(ValueHandle,CallbackVH_CallbackOnRAUW)263 TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
264   class RecordingVH : public CallbackVH {
265   public:
266     int DeletedCalls;
267     Value *AURWArgument;
268 
269     RecordingVH() : DeletedCalls(0), AURWArgument(nullptr) {}
270     RecordingVH(Value *V)
271       : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr) {}
272 
273   private:
274     void deleted() override {
275       DeletedCalls++;
276       CallbackVH::deleted();
277     }
278     void allUsesReplacedWith(Value *new_value) override {
279       EXPECT_EQ(nullptr, AURWArgument);
280       AURWArgument = new_value;
281     }
282   };
283 
284   RecordingVH RVH;
285   RVH = BitcastV.get();
286   EXPECT_EQ(0, RVH.DeletedCalls);
287   EXPECT_EQ(nullptr, RVH.AURWArgument);
288   BitcastV->replaceAllUsesWith(ConstantV);
289   EXPECT_EQ(0, RVH.DeletedCalls);
290   EXPECT_EQ(ConstantV, RVH.AURWArgument);
291 }
292 
TEST_F(ValueHandle,CallbackVH_DeletionCanRAUW)293 TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
294   class RecoveringVH : public CallbackVH {
295   public:
296     int DeletedCalls;
297     Value *AURWArgument;
298     LLVMContext *Context;
299 
300     RecoveringVH() : DeletedCalls(0), AURWArgument(nullptr),
301                      Context(&getGlobalContext()) {}
302     RecoveringVH(Value *V)
303       : CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr),
304         Context(&getGlobalContext()) {}
305 
306   private:
307     void deleted() override {
308       getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())));
309       setValPtr(nullptr);
310     }
311     void allUsesReplacedWith(Value *new_value) override {
312       ASSERT_TRUE(nullptr != getValPtr());
313       EXPECT_EQ(1U, getValPtr()->getNumUses());
314       EXPECT_EQ(nullptr, AURWArgument);
315       AURWArgument = new_value;
316     }
317   };
318 
319   // Normally, if a value has uses, deleting it will crash.  However, we can use
320   // a CallbackVH to remove the uses before the check for no uses.
321   RecoveringVH RVH;
322   RVH = BitcastV.get();
323   std::auto_ptr<BinaryOperator> BitcastUser(
324     BinaryOperator::CreateAdd(RVH,
325                               Constant::getNullValue(Type::getInt32Ty(getGlobalContext()))));
326   EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
327   BitcastV.reset();  // Would crash without the ValueHandler.
328   EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())), RVH.AURWArgument);
329   EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(getGlobalContext())),
330             BitcastUser->getOperand(0));
331 }
332 
TEST_F(ValueHandle,DestroyingOtherVHOnSameValueDoesntBreakIteration)333 TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
334   // When a CallbackVH modifies other ValueHandles in its callbacks,
335   // that shouldn't interfere with non-modified ValueHandles receiving
336   // their appropriate callbacks.
337   //
338   // We create the active CallbackVH in the middle of a palindromic
339   // arrangement of other VHs so that the bad behavior would be
340   // triggered in whichever order callbacks run.
341 
342   class DestroyingVH : public CallbackVH {
343   public:
344     std::unique_ptr<WeakVH> ToClear[2];
345     DestroyingVH(Value *V) {
346       ToClear[0].reset(new WeakVH(V));
347       setValPtr(V);
348       ToClear[1].reset(new WeakVH(V));
349     }
350     void deleted() override {
351       ToClear[0].reset();
352       ToClear[1].reset();
353       CallbackVH::deleted();
354     }
355     void allUsesReplacedWith(Value *) override {
356       ToClear[0].reset();
357       ToClear[1].reset();
358     }
359   };
360 
361   {
362     WeakVH ShouldBeVisited1(BitcastV.get());
363     DestroyingVH C(BitcastV.get());
364     WeakVH ShouldBeVisited2(BitcastV.get());
365 
366     BitcastV->replaceAllUsesWith(ConstantV);
367     EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
368     EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
369   }
370 
371   {
372     WeakVH ShouldBeVisited1(BitcastV.get());
373     DestroyingVH C(BitcastV.get());
374     WeakVH ShouldBeVisited2(BitcastV.get());
375 
376     BitcastV.reset();
377     EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited1));
378     EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited2));
379   }
380 }
381 
TEST_F(ValueHandle,AssertingVHCheckedLast)382 TEST_F(ValueHandle, AssertingVHCheckedLast) {
383   // If a CallbackVH exists to clear out a group of AssertingVHs on
384   // Value deletion, the CallbackVH should get a chance to do so
385   // before the AssertingVHs assert.
386 
387   class ClearingVH : public CallbackVH {
388   public:
389     AssertingVH<Value> *ToClear[2];
390     ClearingVH(Value *V,
391                AssertingVH<Value> &A0, AssertingVH<Value> &A1)
392       : CallbackVH(V) {
393       ToClear[0] = &A0;
394       ToClear[1] = &A1;
395     }
396 
397     void deleted() override {
398       *ToClear[0] = nullptr;
399       *ToClear[1] = nullptr;
400       CallbackVH::deleted();
401     }
402   };
403 
404   AssertingVH<Value> A1, A2;
405   A1 = BitcastV.get();
406   ClearingVH C(BitcastV.get(), A1, A2);
407   A2 = BitcastV.get();
408   // C.deleted() should run first, clearing the two AssertingVHs,
409   // which should prevent them from asserting.
410   BitcastV.reset();
411 }
412 
413 }
414