@@ -2022,6 +2022,77 @@ define void @foo() {
2022
2022
EXPECT_EQ (CRI->getSuccessor (), Catch);
2023
2023
}
2024
2024
2025
+ TEST_F (SandboxIRTest, CleanupReturnInst) {
2026
+ parseIR (C, R"IR(
2027
+ define void @foo() {
2028
+ dispatch:
2029
+ invoke void @foo()
2030
+ to label %throw unwind label %cleanup
2031
+ throw:
2032
+ ret void
2033
+ cleanup:
2034
+ %cleanuppad = cleanuppad within none []
2035
+ cleanupret from %cleanuppad unwind label %cleanup2
2036
+ cleanup2:
2037
+ %cleanuppad2 = cleanuppad within none []
2038
+ ret void
2039
+ }
2040
+ )IR" );
2041
+ Function &LLVMF = *M->getFunction (" foo" );
2042
+ BasicBlock *LLVMCleanup = getBasicBlockByName (LLVMF, " cleanup" );
2043
+ auto LLVMIt = LLVMCleanup->begin ();
2044
+ [[maybe_unused]] auto *LLVMCP = cast<llvm::CleanupPadInst>(&*LLVMIt++);
2045
+ auto *LLVMCRI = cast<llvm::CleanupReturnInst>(&*LLVMIt++);
2046
+
2047
+ sandboxir::Context Ctx (C);
2048
+ [[maybe_unused]] auto &F = *Ctx.createFunction (&LLVMF);
2049
+ auto *Throw = cast<sandboxir::BasicBlock>(
2050
+ Ctx.getValue (getBasicBlockByName (LLVMF, " throw" )));
2051
+ auto *Cleanup = cast<sandboxir::BasicBlock>(Ctx.getValue (LLVMCleanup));
2052
+ auto *Cleanup2 = cast<sandboxir::BasicBlock>(
2053
+ Ctx.getValue (getBasicBlockByName (LLVMF, " cleanup2" )));
2054
+ auto It = Cleanup->begin ();
2055
+ [[maybe_unused]] auto *CP = cast<sandboxir::CleanupPadInst>(&*It++);
2056
+ auto *CRI = cast<sandboxir::CleanupReturnInst>(&*It++);
2057
+ It = Cleanup2->begin ();
2058
+ auto *CP2 = cast<sandboxir::CleanupPadInst>(&*It++);
2059
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
2060
+
2061
+ // Check hasUnwindDest().
2062
+ EXPECT_EQ (CRI->hasUnwindDest (), LLVMCRI->hasUnwindDest ());
2063
+ // Check unwindsToCaller().
2064
+ EXPECT_EQ (CRI->unwindsToCaller (), LLVMCRI->unwindsToCaller ());
2065
+ // Check getCleanupPad().
2066
+ EXPECT_EQ (CRI->getCleanupPad (), Ctx.getValue (LLVMCRI->getCleanupPad ()));
2067
+ // Check setCleanupPad().
2068
+ auto *OrigCleanupPad = CRI->getCleanupPad ();
2069
+ auto *NewCleanupPad = CP2;
2070
+ EXPECT_NE (NewCleanupPad, OrigCleanupPad);
2071
+ CRI->setCleanupPad (NewCleanupPad);
2072
+ EXPECT_EQ (CRI->getCleanupPad (), NewCleanupPad);
2073
+ CRI->setCleanupPad (OrigCleanupPad);
2074
+ EXPECT_EQ (CRI->getCleanupPad (), OrigCleanupPad);
2075
+ // Check setNumSuccessors().
2076
+ EXPECT_EQ (CRI->getNumSuccessors (), LLVMCRI->getNumSuccessors ());
2077
+ // Check getUnwindDest().
2078
+ EXPECT_EQ (CRI->getUnwindDest (), Ctx.getValue (LLVMCRI->getUnwindDest ()));
2079
+ // Check setUnwindDest().
2080
+ auto *OrigUnwindDest = CRI->getUnwindDest ();
2081
+ auto *NewUnwindDest = Throw;
2082
+ EXPECT_NE (NewUnwindDest, OrigUnwindDest);
2083
+ CRI->setUnwindDest (NewUnwindDest);
2084
+ EXPECT_EQ (CRI->getUnwindDest (), NewUnwindDest);
2085
+ CRI->setUnwindDest (OrigUnwindDest);
2086
+ EXPECT_EQ (CRI->getUnwindDest (), OrigUnwindDest);
2087
+ // Check create().
2088
+ auto *UnwindBB = Cleanup;
2089
+ auto *NewCRI = sandboxir::CleanupReturnInst::create (
2090
+ CP2, UnwindBB, Ret->getIterator (), Ret->getParent (), Ctx);
2091
+ EXPECT_EQ (NewCRI->getCleanupPad (), CP2);
2092
+ EXPECT_EQ (NewCRI->getUnwindDest (), UnwindBB);
2093
+ EXPECT_EQ (NewCRI->getNextNode (), Ret);
2094
+ }
2095
+
2025
2096
TEST_F (SandboxIRTest, GetElementPtrInstruction) {
2026
2097
parseIR (C, R"IR(
2027
2098
define void @foo(ptr %ptr, <2 x ptr> %ptrs) {
0 commit comments