[SandboxIR] Implement the remaining CastInst sub-classes (#101537)
This patch implements:
sandboxir::UIToFPInst
sandboxir::FPExtInst
sandboxir::FPTruncInst
sandboxir::SExtInst
sandboxir::ZExtInst
sandboxir::TruncInst
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 0079404..0bd6cda 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -32,15 +32,27 @@
// | |
// | +- BitCastInst
// | |
+// | +- FPExtInst
+// | |
// | +- FPToSIInst
// | |
// | +- FPToUIInst
// | |
+// | +- FPTruncInst
+// | |
// | +- IntToPtrInst
// | |
// | +- PtrToIntInst
// | |
+// | +- SExtInst
+// | |
// | +- SIToFPInst
+// | |
+// | +- TruncInst
+// | |
+// | +- UIToFPInst
+// | |
+// | +- ZExtInst
// |
// +- CallBase -----------+- CallBrInst
// | |
@@ -1458,6 +1470,12 @@
}
};
+class TruncInst final : public CastInstImpl<Instruction::Opcode::Trunc> {};
+class ZExtInst final : public CastInstImpl<Instruction::Opcode::ZExt> {};
+class SExtInst final : public CastInstImpl<Instruction::Opcode::SExt> {};
+class FPTruncInst final : public CastInstImpl<Instruction::Opcode::FPTrunc> {};
+class FPExtInst final : public CastInstImpl<Instruction::Opcode::FPExt> {};
+class UIToFPInst final : public CastInstImpl<Instruction::Opcode::UIToFP> {};
class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index cbf3952..5f858b4 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -1489,11 +1489,13 @@
// Check classof(), getOpcode(), getSrcTy(), getDstTy()
auto *ZExt = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::ZExtInst>(ZExt));
EXPECT_EQ(ZExt->getOpcode(), sandboxir::Instruction::Opcode::ZExt);
EXPECT_EQ(ZExt->getSrcTy(), Ti32);
EXPECT_EQ(ZExt->getDestTy(), Ti64);
auto *SExt = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::SExtInst>(SExt));
EXPECT_EQ(SExt->getOpcode(), sandboxir::Instruction::Opcode::SExt);
EXPECT_EQ(SExt->getSrcTy(), Ti32);
EXPECT_EQ(SExt->getDestTy(), Ti64);
@@ -1511,6 +1513,7 @@
EXPECT_EQ(FPToSI->getDestTy(), Ti32);
auto *FPExt = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::FPExtInst>(FPExt));
EXPECT_EQ(FPExt->getOpcode(), sandboxir::Instruction::Opcode::FPExt);
EXPECT_EQ(FPExt->getSrcTy(), Tfloat);
EXPECT_EQ(FPExt->getDestTy(), Tdouble);
@@ -1534,16 +1537,19 @@
EXPECT_EQ(SIToFP->getDestTy(), Tfloat);
auto *UIToFP = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::UIToFPInst>(UIToFP));
EXPECT_EQ(UIToFP->getOpcode(), sandboxir::Instruction::Opcode::UIToFP);
EXPECT_EQ(UIToFP->getSrcTy(), Ti32);
EXPECT_EQ(UIToFP->getDestTy(), Tfloat);
auto *Trunc = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::TruncInst>(Trunc));
EXPECT_EQ(Trunc->getOpcode(), sandboxir::Instruction::Opcode::Trunc);
EXPECT_EQ(Trunc->getSrcTy(), Ti32);
EXPECT_EQ(Trunc->getDestTy(), Ti16);
auto *FPTrunc = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::FPTruncInst>(FPTrunc));
EXPECT_EQ(FPTrunc->getOpcode(), sandboxir::Instruction::Opcode::FPTrunc);
EXPECT_EQ(FPTrunc->getSrcTy(), Tdouble);
EXPECT_EQ(FPTrunc->getDestTy(), Tfloat);
@@ -1686,6 +1692,78 @@
}
}
+TEST_F(SandboxIRTest, TruncInst) {
+ parseIR(C, R"IR(
+define void @foo(i64 %arg) {
+ %trunc = trunc i64 %arg to i32
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::TruncInst, sandboxir::Instruction::Opcode::Trunc>(
+ *M,
+ /*SrcTy=*/Type::getInt64Ty(C), /*DstTy=*/Type::getInt32Ty(C));
+}
+
+TEST_F(SandboxIRTest, ZExtInst) {
+ parseIR(C, R"IR(
+define void @foo(i32 %arg) {
+ %zext = zext i32 %arg to i64
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::ZExtInst, sandboxir::Instruction::Opcode::ZExt>(
+ *M,
+ /*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
+}
+
+TEST_F(SandboxIRTest, SExtInst) {
+ parseIR(C, R"IR(
+define void @foo(i32 %arg) {
+ %sext = sext i32 %arg to i64
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::SExtInst, sandboxir::Instruction::Opcode::SExt>(
+ *M,
+ /*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getInt64Ty(C));
+}
+
+TEST_F(SandboxIRTest, FPTruncInst) {
+ parseIR(C, R"IR(
+define void @foo(double %arg) {
+ %fptrunc = fptrunc double %arg to float
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::FPTruncInst, sandboxir::Instruction::Opcode::FPTrunc>(
+ *M,
+ /*SrcTy=*/Type::getDoubleTy(C), /*DstTy=*/Type::getFloatTy(C));
+}
+
+TEST_F(SandboxIRTest, FPExtInst) {
+ parseIR(C, R"IR(
+define void @foo(float %arg) {
+ %fpext = fpext float %arg to double
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::FPExtInst, sandboxir::Instruction::Opcode::FPExt>(
+ *M,
+ /*SrcTy=*/Type::getFloatTy(C), /*DstTy=*/Type::getDoubleTy(C));
+}
+
+TEST_F(SandboxIRTest, UIToFPInst) {
+ parseIR(C, R"IR(
+define void @foo(i32 %arg) {
+ %uitofp = uitofp i32 %arg to float
+ ret void
+}
+)IR");
+ testCastInst<sandboxir::UIToFPInst, sandboxir::Instruction::Opcode::UIToFP>(
+ *M,
+ /*SrcTy=*/Type::getInt32Ty(C), /*DstTy=*/Type::getFloatTy(C));
+}
+
TEST_F(SandboxIRTest, SIToFPInst) {
parseIR(C, R"IR(
define void @foo(i32 %arg) {