2014-03-04 12:17:44 +01:00
|
|
|
//===- ValueHandleTest.cpp - ValueHandle tests ----------------------------===//
|
2009-04-27 22:32:07 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-04-27 22:32:07 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:17:44 +01:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2009-04-27 22:32:07 +02:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ValueHandle : public testing::Test {
|
|
|
|
protected:
|
2016-04-14 23:59:01 +02:00
|
|
|
LLVMContext Context;
|
2009-04-27 22:32:07 +02:00
|
|
|
Constant *ConstantV;
|
2015-04-22 06:39:13 +02:00
|
|
|
std::unique_ptr<BitCastInst> BitcastV;
|
2009-04-27 22:32:07 +02:00
|
|
|
|
2016-04-14 23:59:01 +02:00
|
|
|
ValueHandle()
|
|
|
|
: ConstantV(ConstantInt::get(Type::getInt32Ty(Context), 0)),
|
|
|
|
BitcastV(new BitCastInst(ConstantV, Type::getInt32Ty(Context))) {}
|
2009-04-27 22:32:07 +02:00
|
|
|
};
|
|
|
|
|
2015-08-04 00:30:24 +02:00
|
|
|
class ConcreteCallbackVH final : public CallbackVH {
|
2009-05-02 23:10:48 +02:00
|
|
|
public:
|
|
|
|
ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
|
|
|
|
};
|
|
|
|
|
2017-05-01 19:07:54 +02:00
|
|
|
TEST_F(ValueHandle, WeakVH_BasicOperation) {
|
|
|
|
WeakVH WVH(BitcastV.get());
|
|
|
|
EXPECT_EQ(BitcastV.get(), WVH);
|
|
|
|
WVH = ConstantV;
|
|
|
|
EXPECT_EQ(ConstantV, WVH);
|
|
|
|
|
|
|
|
// Make sure I can call a method on the underlying Value. It
|
|
|
|
// doesn't matter which method.
|
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
|
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
|
|
|
|
|
|
|
|
WVH = BitcastV.get();
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(WVH, BitcastV.get());
|
|
|
|
BitcastV.reset();
|
|
|
|
EXPECT_EQ(WVH, nullptr);
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
|
|
|
|
WeakTrackingVH WVH(BitcastV.get());
|
2017-04-26 18:20:59 +02:00
|
|
|
EXPECT_EQ(BitcastV.get(), WVH);
|
|
|
|
WVH = ConstantV;
|
|
|
|
EXPECT_EQ(ConstantV, WVH);
|
|
|
|
|
|
|
|
// Make sure I can call a method on the underlying Value. It
|
|
|
|
// doesn't matter which method.
|
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), WVH->getType());
|
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
TEST_F(ValueHandle, WeakTrackingVH_Comparisons) {
|
|
|
|
WeakTrackingVH BitcastWVH(BitcastV.get());
|
|
|
|
WeakTrackingVH ConstantWVH(ConstantV);
|
2009-04-27 22:32:07 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastWVH == BitcastWVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() == BitcastWVH);
|
|
|
|
EXPECT_TRUE(BitcastWVH == BitcastV.get());
|
|
|
|
EXPECT_FALSE(BitcastWVH == ConstantWVH);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastWVH != ConstantWVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() != ConstantWVH);
|
|
|
|
EXPECT_TRUE(BitcastWVH != ConstantV);
|
|
|
|
EXPECT_FALSE(BitcastWVH != BitcastWVH);
|
|
|
|
|
|
|
|
// Cast to Value* so comparisons work.
|
|
|
|
Value *BV = BitcastV.get();
|
|
|
|
Value *CV = ConstantV;
|
|
|
|
EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
TEST_F(ValueHandle, WeakTrackingVH_FollowsRAUW) {
|
|
|
|
WeakTrackingVH WVH(BitcastV.get());
|
|
|
|
WeakTrackingVH WVH_Copy(WVH);
|
|
|
|
WeakTrackingVH WVH_Recreated(BitcastV.get());
|
2009-04-27 22:32:07 +02:00
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(ConstantV, WVH);
|
|
|
|
EXPECT_EQ(ConstantV, WVH_Copy);
|
|
|
|
EXPECT_EQ(ConstantV, WVH_Recreated);
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:07:49 +02:00
|
|
|
TEST_F(ValueHandle, WeakTrackingVH_NullOnDeletion) {
|
|
|
|
WeakTrackingVH WVH(BitcastV.get());
|
|
|
|
WeakTrackingVH WVH_Copy(WVH);
|
|
|
|
WeakTrackingVH WVH_Recreated(BitcastV.get());
|
2009-04-27 22:32:07 +02:00
|
|
|
BitcastV.reset();
|
2014-06-09 00:29:17 +02:00
|
|
|
Value *null_value = nullptr;
|
2009-04-27 22:32:07 +02:00
|
|
|
EXPECT_EQ(null_value, WVH);
|
|
|
|
EXPECT_EQ(null_value, WVH_Copy);
|
|
|
|
EXPECT_EQ(null_value, WVH_Recreated);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, AssertingVH_BasicOperation) {
|
|
|
|
AssertingVH<CastInst> AVH(BitcastV.get());
|
|
|
|
CastInst *implicit_to_exact_type = AVH;
|
2010-12-23 01:58:24 +01:00
|
|
|
(void)implicit_to_exact_type; // Avoid warning.
|
2009-04-27 22:32:07 +02:00
|
|
|
|
|
|
|
AssertingVH<Value> GenericAVH(BitcastV.get());
|
|
|
|
EXPECT_EQ(BitcastV.get(), GenericAVH);
|
|
|
|
GenericAVH = ConstantV;
|
|
|
|
EXPECT_EQ(ConstantV, GenericAVH);
|
|
|
|
|
|
|
|
// Make sure I can call a method on the underlying CastInst. It
|
|
|
|
// doesn't matter which method.
|
|
|
|
EXPECT_FALSE(AVH->mayWriteToMemory());
|
|
|
|
EXPECT_FALSE((*AVH).mayWriteToMemory());
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:54:29 +02:00
|
|
|
TEST_F(ValueHandle, AssertingVH_Const) {
|
|
|
|
const CastInst *ConstBitcast = BitcastV.get();
|
|
|
|
AssertingVH<const CastInst> AVH(ConstBitcast);
|
|
|
|
const CastInst *implicit_to_exact_type = AVH;
|
2010-12-23 01:58:24 +01:00
|
|
|
(void)implicit_to_exact_type; // Avoid warning.
|
2009-08-07 21:54:29 +02:00
|
|
|
}
|
|
|
|
|
2009-04-27 22:32:07 +02:00
|
|
|
TEST_F(ValueHandle, AssertingVH_Comparisons) {
|
|
|
|
AssertingVH<Value> BitcastAVH(BitcastV.get());
|
|
|
|
AssertingVH<Value> ConstantAVH(ConstantV);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastAVH == BitcastAVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() == BitcastAVH);
|
|
|
|
EXPECT_TRUE(BitcastAVH == BitcastV.get());
|
|
|
|
EXPECT_FALSE(BitcastAVH == ConstantAVH);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastAVH != ConstantAVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() != ConstantAVH);
|
|
|
|
EXPECT_TRUE(BitcastAVH != ConstantV);
|
|
|
|
EXPECT_FALSE(BitcastAVH != BitcastAVH);
|
|
|
|
|
|
|
|
// Cast to Value* so comparisons work.
|
|
|
|
Value *BV = BitcastV.get();
|
|
|
|
Value *CV = ConstantV;
|
|
|
|
EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
|
|
|
|
AssertingVH<Value> AVH(BitcastV.get());
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(BitcastV.get(), AVH);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
|
|
|
|
EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:17:24 +01:00
|
|
|
#elif LLVM_ENABLE_ABI_BREAKING_CHECKS // && !NDEBUG
|
2009-04-27 22:32:07 +02:00
|
|
|
|
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, AssertingVH_Asserts) {
|
|
|
|
AssertingVH<Value> AVH(BitcastV.get());
|
|
|
|
EXPECT_DEATH({BitcastV.reset();},
|
|
|
|
"An asserting value handle still pointed to this value!");
|
|
|
|
AssertingVH<Value> Copy(AVH);
|
2014-06-09 00:29:17 +02:00
|
|
|
AVH = nullptr;
|
2009-04-27 22:32:07 +02:00
|
|
|
EXPECT_DEATH({BitcastV.reset();},
|
|
|
|
"An asserting value handle still pointed to this value!");
|
2014-06-09 00:29:17 +02:00
|
|
|
Copy = nullptr;
|
2009-04-27 22:32:07 +02:00
|
|
|
BitcastV.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
2009-05-02 23:10:48 +02:00
|
|
|
TEST_F(ValueHandle, CallbackVH_BasicOperation) {
|
|
|
|
ConcreteCallbackVH CVH(BitcastV.get());
|
|
|
|
EXPECT_EQ(BitcastV.get(), CVH);
|
|
|
|
CVH = ConstantV;
|
|
|
|
EXPECT_EQ(ConstantV, CVH);
|
|
|
|
|
|
|
|
// Make sure I can call a method on the underlying Value. It
|
|
|
|
// doesn't matter which method.
|
2016-04-14 23:59:01 +02:00
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), CVH->getType());
|
|
|
|
EXPECT_EQ(Type::getInt32Ty(Context), (*CVH).getType());
|
2009-05-02 23:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, CallbackVH_Comparisons) {
|
|
|
|
ConcreteCallbackVH BitcastCVH(BitcastV.get());
|
|
|
|
ConcreteCallbackVH ConstantCVH(ConstantV);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastCVH == BitcastCVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() == BitcastCVH);
|
|
|
|
EXPECT_TRUE(BitcastCVH == BitcastV.get());
|
|
|
|
EXPECT_FALSE(BitcastCVH == ConstantCVH);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastCVH != ConstantCVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() != ConstantCVH);
|
|
|
|
EXPECT_TRUE(BitcastCVH != ConstantV);
|
|
|
|
EXPECT_FALSE(BitcastCVH != BitcastCVH);
|
|
|
|
|
|
|
|
// Cast to Value* so comparisons work.
|
|
|
|
Value *BV = BitcastV.get();
|
|
|
|
Value *CV = ConstantV;
|
|
|
|
EXPECT_EQ(BV < CV, BitcastCVH < ConstantCVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantCVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastCVH > ConstantCVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantCVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantCVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantCVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantCVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantCVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastCVH < ConstantV);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastCVH <= ConstantV);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastCVH > ConstantV);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastCVH >= ConstantV);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {
|
2015-08-04 00:30:24 +02:00
|
|
|
class RecordingVH final : public CallbackVH {
|
2009-05-02 23:10:48 +02:00
|
|
|
public:
|
|
|
|
int DeletedCalls;
|
|
|
|
int AURWCalls;
|
|
|
|
|
|
|
|
RecordingVH() : DeletedCalls(0), AURWCalls(0) {}
|
|
|
|
RecordingVH(Value *V) : CallbackVH(V), DeletedCalls(0), AURWCalls(0) {}
|
|
|
|
|
|
|
|
private:
|
2015-04-11 04:11:45 +02:00
|
|
|
void deleted() override {
|
|
|
|
DeletedCalls++;
|
|
|
|
CallbackVH::deleted();
|
|
|
|
}
|
|
|
|
void allUsesReplacedWith(Value *) override { AURWCalls++; }
|
2009-05-02 23:10:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
RecordingVH RVH;
|
|
|
|
RVH = BitcastV.get();
|
|
|
|
EXPECT_EQ(0, RVH.DeletedCalls);
|
|
|
|
EXPECT_EQ(0, RVH.AURWCalls);
|
|
|
|
BitcastV.reset();
|
|
|
|
EXPECT_EQ(1, RVH.DeletedCalls);
|
|
|
|
EXPECT_EQ(0, RVH.AURWCalls);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {
|
2015-08-04 00:30:24 +02:00
|
|
|
class RecordingVH final : public CallbackVH {
|
2009-05-02 23:10:48 +02:00
|
|
|
public:
|
|
|
|
int DeletedCalls;
|
|
|
|
Value *AURWArgument;
|
|
|
|
|
2014-06-09 00:29:17 +02:00
|
|
|
RecordingVH() : DeletedCalls(0), AURWArgument(nullptr) {}
|
2009-05-02 23:10:48 +02:00
|
|
|
RecordingVH(Value *V)
|
2014-06-09 00:29:17 +02:00
|
|
|
: CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr) {}
|
2009-05-02 23:10:48 +02:00
|
|
|
|
|
|
|
private:
|
2015-04-11 04:11:45 +02:00
|
|
|
void deleted() override {
|
|
|
|
DeletedCalls++;
|
|
|
|
CallbackVH::deleted();
|
|
|
|
}
|
|
|
|
void allUsesReplacedWith(Value *new_value) override {
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, AURWArgument);
|
2009-05-02 23:10:48 +02:00
|
|
|
AURWArgument = new_value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RecordingVH RVH;
|
|
|
|
RVH = BitcastV.get();
|
|
|
|
EXPECT_EQ(0, RVH.DeletedCalls);
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, RVH.AURWArgument);
|
2009-05-02 23:10:48 +02:00
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(0, RVH.DeletedCalls);
|
|
|
|
EXPECT_EQ(ConstantV, RVH.AURWArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
|
2015-08-04 00:30:24 +02:00
|
|
|
class RecoveringVH final : public CallbackVH {
|
2009-05-02 23:10:48 +02:00
|
|
|
public:
|
|
|
|
int DeletedCalls;
|
|
|
|
Value *AURWArgument;
|
2009-07-13 06:09:18 +02:00
|
|
|
LLVMContext *Context;
|
2009-05-02 23:10:48 +02:00
|
|
|
|
2016-04-14 23:59:01 +02:00
|
|
|
RecoveringVH(LLVMContext &TheContext)
|
|
|
|
: DeletedCalls(0), AURWArgument(nullptr), Context(&TheContext) {}
|
|
|
|
|
|
|
|
RecoveringVH(LLVMContext &TheContext, Value *V)
|
|
|
|
: CallbackVH(V), DeletedCalls(0), AURWArgument(nullptr),
|
|
|
|
Context(&TheContext) {}
|
2009-05-02 23:10:48 +02:00
|
|
|
|
|
|
|
private:
|
2015-04-11 04:11:45 +02:00
|
|
|
void deleted() override {
|
2016-04-14 23:59:01 +02:00
|
|
|
getValPtr()->replaceAllUsesWith(
|
|
|
|
Constant::getNullValue(Type::getInt32Ty(*Context)));
|
2014-06-09 00:29:17 +02:00
|
|
|
setValPtr(nullptr);
|
2009-05-02 23:10:48 +02:00
|
|
|
}
|
2015-04-11 04:11:45 +02:00
|
|
|
void allUsesReplacedWith(Value *new_value) override {
|
2014-06-09 00:29:17 +02:00
|
|
|
ASSERT_TRUE(nullptr != getValPtr());
|
2009-05-18 05:44:24 +02:00
|
|
|
EXPECT_EQ(1U, getValPtr()->getNumUses());
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, AURWArgument);
|
2009-05-02 23:10:48 +02:00
|
|
|
AURWArgument = new_value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Normally, if a value has uses, deleting it will crash. However, we can use
|
|
|
|
// a CallbackVH to remove the uses before the check for no uses.
|
2016-04-14 23:59:01 +02:00
|
|
|
RecoveringVH RVH(Context);
|
|
|
|
RVH = RecoveringVH(Context, BitcastV.get());
|
|
|
|
std::unique_ptr<BinaryOperator> BitcastUser(BinaryOperator::CreateAdd(
|
|
|
|
RVH, Constant::getNullValue(Type::getInt32Ty(Context))));
|
2009-05-02 23:10:48 +02:00
|
|
|
EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
|
|
|
|
BitcastV.reset(); // Would crash without the ValueHandler.
|
2016-04-14 23:59:01 +02:00
|
|
|
EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
|
|
|
|
RVH.AURWArgument);
|
|
|
|
EXPECT_EQ(Constant::getNullValue(Type::getInt32Ty(Context)),
|
2009-07-13 06:09:18 +02:00
|
|
|
BitcastUser->getOperand(0));
|
2009-05-02 23:10:48 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 19:43:32 +02:00
|
|
|
TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {
|
|
|
|
// When a CallbackVH modifies other ValueHandles in its callbacks,
|
|
|
|
// that shouldn't interfere with non-modified ValueHandles receiving
|
|
|
|
// their appropriate callbacks.
|
|
|
|
//
|
|
|
|
// We create the active CallbackVH in the middle of a palindromic
|
|
|
|
// arrangement of other VHs so that the bad behavior would be
|
|
|
|
// triggered in whichever order callbacks run.
|
|
|
|
|
2015-08-04 00:30:24 +02:00
|
|
|
class DestroyingVH final : public CallbackVH {
|
2009-10-12 19:43:32 +02:00
|
|
|
public:
|
2017-05-01 19:07:49 +02:00
|
|
|
std::unique_ptr<WeakTrackingVH> ToClear[2];
|
2009-10-12 19:43:32 +02:00
|
|
|
DestroyingVH(Value *V) {
|
2017-05-01 19:07:49 +02:00
|
|
|
ToClear[0].reset(new WeakTrackingVH(V));
|
2009-10-12 19:43:32 +02:00
|
|
|
setValPtr(V);
|
2017-05-01 19:07:49 +02:00
|
|
|
ToClear[1].reset(new WeakTrackingVH(V));
|
2009-10-12 19:43:32 +02:00
|
|
|
}
|
2015-04-11 04:11:45 +02:00
|
|
|
void deleted() override {
|
2009-10-12 19:43:32 +02:00
|
|
|
ToClear[0].reset();
|
|
|
|
ToClear[1].reset();
|
|
|
|
CallbackVH::deleted();
|
|
|
|
}
|
2015-04-11 04:11:45 +02:00
|
|
|
void allUsesReplacedWith(Value *) override {
|
2009-10-12 19:43:32 +02:00
|
|
|
ToClear[0].reset();
|
|
|
|
ToClear[1].reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH ShouldBeVisited1(BitcastV.get());
|
2009-10-12 19:43:32 +02:00
|
|
|
DestroyingVH C(BitcastV.get());
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH ShouldBeVisited2(BitcastV.get());
|
2009-10-12 19:43:32 +02:00
|
|
|
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited1));
|
|
|
|
EXPECT_EQ(ConstantV, static_cast<Value*>(ShouldBeVisited2));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH ShouldBeVisited1(BitcastV.get());
|
2009-10-12 19:43:32 +02:00
|
|
|
DestroyingVH C(BitcastV.get());
|
2017-05-01 19:07:49 +02:00
|
|
|
WeakTrackingVH ShouldBeVisited2(BitcastV.get());
|
2009-10-12 19:43:32 +02:00
|
|
|
|
|
|
|
BitcastV.reset();
|
2014-06-09 00:29:17 +02:00
|
|
|
EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited1));
|
|
|
|
EXPECT_EQ(nullptr, static_cast<Value*>(ShouldBeVisited2));
|
2009-10-12 19:43:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, AssertingVHCheckedLast) {
|
|
|
|
// If a CallbackVH exists to clear out a group of AssertingVHs on
|
|
|
|
// Value deletion, the CallbackVH should get a chance to do so
|
|
|
|
// before the AssertingVHs assert.
|
|
|
|
|
2015-08-04 00:30:24 +02:00
|
|
|
class ClearingVH final : public CallbackVH {
|
2009-10-12 19:43:32 +02:00
|
|
|
public:
|
|
|
|
AssertingVH<Value> *ToClear[2];
|
|
|
|
ClearingVH(Value *V,
|
|
|
|
AssertingVH<Value> &A0, AssertingVH<Value> &A1)
|
|
|
|
: CallbackVH(V) {
|
|
|
|
ToClear[0] = &A0;
|
|
|
|
ToClear[1] = &A1;
|
|
|
|
}
|
|
|
|
|
2015-04-11 04:11:45 +02:00
|
|
|
void deleted() override {
|
2014-06-09 00:29:17 +02:00
|
|
|
*ToClear[0] = nullptr;
|
|
|
|
*ToClear[1] = nullptr;
|
2009-10-12 19:43:32 +02:00
|
|
|
CallbackVH::deleted();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AssertingVH<Value> A1, A2;
|
|
|
|
A1 = BitcastV.get();
|
|
|
|
ClearingVH C(BitcastV.get(), A1, A2);
|
|
|
|
A2 = BitcastV.get();
|
|
|
|
// C.deleted() should run first, clearing the two AssertingVHs,
|
|
|
|
// which should prevent them from asserting.
|
|
|
|
BitcastV.reset();
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
TEST_F(ValueHandle, PoisoningVH_BasicOperation) {
|
|
|
|
PoisoningVH<CastInst> VH(BitcastV.get());
|
|
|
|
CastInst *implicit_to_exact_type = VH;
|
|
|
|
(void)implicit_to_exact_type; // Avoid warning.
|
|
|
|
|
|
|
|
PoisoningVH<Value> GenericVH(BitcastV.get());
|
|
|
|
EXPECT_EQ(BitcastV.get(), GenericVH);
|
|
|
|
GenericVH = ConstantV;
|
|
|
|
EXPECT_EQ(ConstantV, GenericVH);
|
|
|
|
|
|
|
|
// Make sure I can call a method on the underlying CastInst. It
|
|
|
|
// doesn't matter which method.
|
|
|
|
EXPECT_FALSE(VH->mayWriteToMemory());
|
|
|
|
EXPECT_FALSE((*VH).mayWriteToMemory());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_Const) {
|
|
|
|
const CastInst *ConstBitcast = BitcastV.get();
|
|
|
|
PoisoningVH<const CastInst> VH(ConstBitcast);
|
|
|
|
const CastInst *implicit_to_exact_type = VH;
|
|
|
|
(void)implicit_to_exact_type; // Avoid warning.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_Comparisons) {
|
|
|
|
PoisoningVH<Value> BitcastVH(BitcastV.get());
|
|
|
|
PoisoningVH<Value> ConstantVH(ConstantV);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastVH == BitcastVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() == BitcastVH);
|
|
|
|
EXPECT_TRUE(BitcastVH == BitcastV.get());
|
|
|
|
EXPECT_FALSE(BitcastVH == ConstantVH);
|
|
|
|
|
|
|
|
EXPECT_TRUE(BitcastVH != ConstantVH);
|
|
|
|
EXPECT_TRUE(BitcastV.get() != ConstantVH);
|
|
|
|
EXPECT_TRUE(BitcastVH != ConstantV);
|
|
|
|
EXPECT_FALSE(BitcastVH != BitcastVH);
|
|
|
|
|
|
|
|
// Cast to Value* so comparisons work.
|
|
|
|
Value *BV = BitcastV.get();
|
|
|
|
Value *CV = ConstantV;
|
|
|
|
EXPECT_EQ(BV < CV, BitcastVH < ConstantVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastVH <= ConstantVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastVH > ConstantVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastVH >= ConstantVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastV.get() < ConstantVH);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantVH);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastV.get() > ConstantVH);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantVH);
|
|
|
|
|
|
|
|
EXPECT_EQ(BV < CV, BitcastVH < ConstantV);
|
|
|
|
EXPECT_EQ(BV <= CV, BitcastVH <= ConstantV);
|
|
|
|
EXPECT_EQ(BV > CV, BitcastVH > ConstantV);
|
|
|
|
EXPECT_EQ(BV >= CV, BitcastVH >= ConstantV);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {
|
|
|
|
PoisoningVH<Value> VH(BitcastV.get());
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_TRUE(DenseMapInfo<PoisoningVH<Value>>::isEqual(VH, BitcastV.get()));
|
|
|
|
}
|
|
|
|
|
2020-06-13 21:10:22 +02:00
|
|
|
TEST_F(ValueHandle, AssertingVH_DenseMap) {
|
|
|
|
DenseMap<AssertingVH<Value>, int> Map;
|
|
|
|
Map.insert({BitcastV.get(), 1});
|
|
|
|
Map.insert({ConstantV, 2});
|
|
|
|
// These will create a temporary AssertingVH during lookup.
|
|
|
|
EXPECT_TRUE(Map.find(BitcastV.get()) != Map.end());
|
|
|
|
EXPECT_TRUE(Map.find(ConstantV) != Map.end());
|
|
|
|
// These will not create a temporary AssertingVH.
|
|
|
|
EXPECT_TRUE(Map.find_as(BitcastV.get()) != Map.end());
|
|
|
|
EXPECT_TRUE(Map.find_as(ConstantV) != Map.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_DenseMap) {
|
|
|
|
DenseMap<PoisoningVH<Value>, int> Map;
|
|
|
|
Map.insert({BitcastV.get(), 1});
|
|
|
|
Map.insert({ConstantV, 2});
|
|
|
|
// These will create a temporary PoisoningVH during lookup.
|
|
|
|
EXPECT_TRUE(Map.find(BitcastV.get()) != Map.end());
|
|
|
|
EXPECT_TRUE(Map.find(ConstantV) != Map.end());
|
|
|
|
// These will not create a temporary PoisoningVH.
|
|
|
|
EXPECT_TRUE(Map.find_as(BitcastV.get()) != Map.end());
|
|
|
|
EXPECT_TRUE(Map.find_as(ConstantV) != Map.end());
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {
|
|
|
|
EXPECT_EQ(sizeof(CastInst *), sizeof(PoisoningVH<CastInst>));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // !NDEBUG
|
|
|
|
|
2017-05-01 18:28:58 +02:00
|
|
|
TEST_F(ValueHandle, TrackingVH_Tracks) {
|
|
|
|
TrackingVH<Value> VH(BitcastV.get());
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_EQ(VH, ConstantV);
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
2021-01-07 23:17:24 +01:00
|
|
|
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
|
2017-01-24 13:34:47 +01:00
|
|
|
|
|
|
|
TEST_F(ValueHandle, PoisoningVH_Asserts) {
|
|
|
|
PoisoningVH<Value> VH(BitcastV.get());
|
|
|
|
|
|
|
|
// The poisoned handle shouldn't assert when the value is deleted.
|
|
|
|
BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
|
|
|
|
// But should when we access the handle.
|
|
|
|
EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
|
|
|
|
|
|
|
|
// Now check that poison catches RAUW.
|
|
|
|
VH = BitcastV.get();
|
|
|
|
// The replace doesn't trigger anything immediately.
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
// But a use does.
|
|
|
|
EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");
|
|
|
|
|
|
|
|
// Don't clear anything out here as destroying the handles should be fine.
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:17:24 +01:00
|
|
|
#endif // LLVM_ENABLE_ABI_BREAKING_CHECKS
|
|
|
|
|
2017-05-01 18:28:58 +02:00
|
|
|
TEST_F(ValueHandle, TrackingVH_Asserts) {
|
|
|
|
{
|
|
|
|
TrackingVH<Value> VH(BitcastV.get());
|
|
|
|
|
|
|
|
// The tracking handle shouldn't assert when the value is deleted.
|
|
|
|
BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
|
|
|
|
// But should when we access the handle.
|
|
|
|
EXPECT_DEATH((void)*VH,
|
|
|
|
"TrackingVH must be non-null and valid on dereference!");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TrackingVH<Instruction> VH(BitcastV.get());
|
|
|
|
|
|
|
|
BitcastV->replaceAllUsesWith(ConstantV);
|
|
|
|
EXPECT_DEATH((void)*VH,
|
|
|
|
"Tracked Value was replaced by one with an invalid type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:34:47 +01:00
|
|
|
#endif // GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
|
|
#endif // NDEBUG
|
2009-04-27 22:32:07 +02:00
|
|
|
}
|