• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   LLVMContext Context;
24   Constant *ConstantV;
25   std::unique_ptr<BitCastInst> BitcastV;
26 
ValueHandle()27   ValueHandle()
28       : ConstantV(ConstantInt::get(Type::getInt32Ty(Context), 0)),
29         BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(Context))) {}
30 };
31 
32 class ConcreteCallbackVH final : 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(Context), WVH->getType());
46   EXPECT_EQ(Type::getInt32Ty(Context), (*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(Context), CVH->getType());
201   EXPECT_EQ(Type::getInt32Ty(Context), (*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 final : 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 final : 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 final : public CallbackVH {
295   public:
296     int DeletedCalls;
297     Value *AURWArgument;
298     LLVMContext *Context;
299 
300     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) {}
306 
307   private:
308     void deleted() override {
309       getValPtr()->replaceAllUsesWith(
310           Constant::getNullValue(Type::getInt32Ty(*Context)));
311       setValPtr(nullptr);
312     }
313     void allUsesReplacedWith(Value *new_value) override {
314       ASSERT_TRUE(nullptr != getValPtr());
315       EXPECT_EQ(1U, getValPtr()->getNumUses());
316       EXPECT_EQ(nullptr, AURWArgument);
317       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.
323   RecoveringVH RVH(Context);
324   RVH = RecoveringVH(Context, BitcastV.get());
325   std::unique_ptr<BinaryOperator> BitcastUser(BinaryOperator::CreateAdd(
326       RVH, Constant::getNullValue(Type::getInt32Ty(Context))));
327   EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
328   BitcastV.reset();  // Would crash without the ValueHandler.
329   EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
330             RVH.AURWArgument);
331   EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
332             BitcastUser->getOperand(0));
333 }
334 
TEST_F(ValueHandle,DestroyingOtherVHOnSameValueDoesntBreakIteration)335 TEST_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 
344   class DestroyingVH final : public CallbackVH {
345   public:
346     std::unique_ptr<WeakVH> ToClear[2];
347     DestroyingVH(Value *V) {
348       ToClear[0].reset(new WeakVH(V));
349       setValPtr(V);
350       ToClear[1].reset(new WeakVH(V));
351     }
352     void deleted() override {
353       ToClear[0].reset();
354       ToClear[1].reset();
355       CallbackVH::deleted();
356     }
357     void allUsesReplacedWith(Value *) override {
358       ToClear[0].reset();
359       ToClear[1].reset();
360     }
361   };
362 
363   {
364     WeakVH ShouldBeVisited1(BitcastV.get());
365     DestroyingVH C(BitcastV.get());
366     WeakVH ShouldBeVisited2(BitcastV.get());
367 
368     BitcastV->replaceAllUsesWith(ConstantV);
369     EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
370     EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
371   }
372 
373   {
374     WeakVH ShouldBeVisited1(BitcastV.get());
375     DestroyingVH C(BitcastV.get());
376     WeakVH ShouldBeVisited2(BitcastV.get());
377 
378     BitcastV.reset();
379     EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited1));
380     EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited2));
381   }
382 }
383 
TEST_F(ValueHandle,AssertingVHCheckedLast)384 TEST_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 
389   class ClearingVH final : public CallbackVH {
390   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 
399     void deleted() override {
400       *ToClear[0] = nullptr;
401       *ToClear[1] = nullptr;
402       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 
415 }
416