diff options
Diffstat (limited to 'llvm/unittests/SandboxIR/SandboxIRTest.cpp')
-rw-r--r-- | llvm/unittests/SandboxIR/SandboxIRTest.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index 9eeac9b..2ad3365 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -6081,6 +6081,72 @@ TEST_F(SandboxIRTest, InstructionCallbacks) { EXPECT_THAT(Moved, testing::IsEmpty()); } +// Check callbacks when we set a Use. +TEST_F(SandboxIRTest, SetUseCallbacks) { + parseIR(C, R"IR( +define void @foo(i8 %v0, i8 %v1) { + %add0 = add i8 %v0, %v1 + %add1 = add i8 %add0, %v1 + ret void +} +)IR"); + llvm::Function *LLVMF = &*M->getFunction("foo"); + sandboxir::Context Ctx(C); + auto *F = Ctx.createFunction(LLVMF); + auto *Arg0 = F->getArg(0); + auto *BB = &*F->begin(); + auto It = BB->begin(); + auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++); + auto *Add1 = cast<sandboxir::BinaryOperator>(&*It++); + + SmallVector<std::pair<sandboxir::Use, sandboxir::Value *>> UsesSet; + auto Id = Ctx.registerSetUseCallback( + [&UsesSet](sandboxir::Use U, sandboxir::Value *NewSrc) { + UsesSet.push_back({U, NewSrc}); + }); + + // Now change %add1 operand to not use %add0. + Add1->setOperand(0, Arg0); + EXPECT_EQ(UsesSet.size(), 1u); + EXPECT_EQ(UsesSet[0].first.get(), Add1->getOperandUse(0).get()); + EXPECT_EQ(UsesSet[0].second, Arg0); + // Restore to previous state. + Add1->setOperand(0, Add0); + UsesSet.clear(); + + // RAUW + Add0->replaceAllUsesWith(Arg0); + EXPECT_EQ(UsesSet.size(), 1u); + EXPECT_EQ(UsesSet[0].first.get(), Add1->getOperandUse(0).get()); + EXPECT_EQ(UsesSet[0].second, Arg0); + // Restore to previous state. + Add1->setOperand(0, Add0); + UsesSet.clear(); + + // RUWIf + Add0->replaceUsesWithIf(Arg0, [](const auto &U) { return true; }); + EXPECT_EQ(UsesSet.size(), 1u); + EXPECT_EQ(UsesSet[0].first.get(), Add1->getOperandUse(0).get()); + EXPECT_EQ(UsesSet[0].second, Arg0); + // Restore to previous state. + Add1->setOperand(0, Add0); + UsesSet.clear(); + + // RUOW + Add1->replaceUsesOfWith(Add0, Arg0); + EXPECT_EQ(UsesSet.size(), 1u); + EXPECT_EQ(UsesSet[0].first.get(), Add1->getOperandUse(0).get()); + EXPECT_EQ(UsesSet[0].second, Arg0); + // Restore to previous state. + Add1->setOperand(0, Add0); + UsesSet.clear(); + + // Check unregister. + Ctx.unregisterSetUseCallback(Id); + Add0->replaceAllUsesWith(Arg0); + EXPECT_TRUE(UsesSet.empty()); +} + TEST_F(SandboxIRTest, FunctionObjectAlreadyExists) { parseIR(C, R"IR( define void @foo() { |