[llvm] f0197a7 - [SandboxIR] Implement IntToPtrInst (#101359)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 31 09:46:02 PDT 2024
Author: vporpo
Date: 2024-07-31T09:45:58-07:00
New Revision: f0197a75e83d45514201432999616267ae0954e2
URL: https://github.com/llvm/llvm-project/commit/f0197a75e83d45514201432999616267ae0954e2
DIFF: https://github.com/llvm/llvm-project/commit/f0197a75e83d45514201432999616267ae0954e2.diff
LOG: [SandboxIR] Implement IntToPtrInst (#101359)
This patch implements sandboxir::IntToPtrInst which mirrors
llvm::IntToPtrInst.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index b4a69885393fa..1940608d77d6a 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -32,6 +32,8 @@
// | |
// | +- BitCastInst
// | |
+// | +- IntToPtrInst
+// | |
// | +- PtrToIntInst
// |
// +- CallBase -----------+- CallBrInst
@@ -1372,6 +1374,27 @@ class CastInst : public Instruction {
#endif
};
+class IntToPtrInst final : public CastInst {
+public:
+ static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &Name = "");
+ static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
+ Context &Ctx, const Twine &Name = "");
+ static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
+ Context &Ctx, const Twine &Name = "");
+
+ static bool classof(const Value *From) {
+ if (auto *I = dyn_cast<Instruction>(From))
+ return I->getOpcode() == Opcode::IntToPtr;
+ return false;
+ }
+#ifndef NDEBUG
+ void dump(raw_ostream &OS) const final;
+ LLVM_DUMP_METHOD void dump() const final;
+#endif // NDEBUG
+};
+
class PtrToIntInst final : public CastInst {
public:
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 56ffd22ee3bcd..2ef53d857011a 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -1143,6 +1143,34 @@ void CastInst::dump() const {
}
#endif // NDEBUG
+Value *IntToPtrInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &Name) {
+ return CastInst::create(DestTy, Instruction::Opcode::IntToPtr, Src, WhereIt,
+ WhereBB, Ctx, Name);
+}
+Value *IntToPtrInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
+ Context &Ctx, const Twine &Name) {
+ return create(Src, DestTy, InsertBefore->getIterator(),
+ InsertBefore->getParent(), Ctx, Name);
+}
+Value *IntToPtrInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
+ Context &Ctx, const Twine &Name) {
+ return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
+}
+
+#ifndef NDEBUG
+void IntToPtrInst::dump(raw_ostream &OS) const {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+}
+
+void IntToPtrInst::dump() const {
+ dump(dbgs());
+ dbgs() << "\n";
+}
+#endif // NDEBUG
+
Value *PtrToIntInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
const Twine &Name) {
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 72af73d9c2f10..158ace1873880 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -1520,6 +1520,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
auto *IntToPtr = cast<sandboxir::CastInst>(&*It++);
+ EXPECT_TRUE(isa<sandboxir::IntToPtrInst>(IntToPtr));
EXPECT_EQ(IntToPtr->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
EXPECT_EQ(IntToPtr->getSrcTy(), Ti32);
EXPECT_EQ(IntToPtr->getDestTy(), Tptr);
@@ -1616,6 +1617,75 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
}
}
+TEST_F(SandboxIRTest, IntToPtrInst) {
+ parseIR(C, R"IR(
+define void @foo(i32 %arg) {
+ %inttoptr = inttoptr i32 %arg to ptr
+ ret void
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(&LLVMF);
+ unsigned ArgIdx = 0;
+ auto *Arg = F->getArg(ArgIdx++);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ Type *Ti32 = Type::getInt32Ty(C);
+ Type *Tptr = Ti32->getPointerTo();
+
+ auto *IntToPtr = cast<sandboxir::IntToPtrInst>(&*It++);
+ EXPECT_EQ(IntToPtr->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
+ EXPECT_EQ(IntToPtr->getSrcTy(), Ti32);
+ EXPECT_EQ(IntToPtr->getDestTy(), Tptr);
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ {
+ // Check create() WhereIt, WhereBB
+ auto *NewI = cast<sandboxir::IntToPtrInst>(
+ sandboxir::IntToPtrInst::create(Arg, Tptr, /*WhereIt=*/BB->end(),
+ /*WhereBB=*/BB, Ctx, "IntToPtr"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), nullptr);
+ EXPECT_EQ(NewI->getPrevNode(), Ret);
+ }
+ {
+ // Check create() InsertBefore.
+ auto *NewI = cast<sandboxir::IntToPtrInst>(
+ sandboxir::IntToPtrInst::create(Arg, Tptr,
+ /*InsertBefore=*/Ret, Ctx, "IntToPtr"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), Ret);
+ }
+ {
+ // Check create() InsertAtEnd.
+ auto *NewI = cast<sandboxir::IntToPtrInst>(
+ sandboxir::IntToPtrInst::create(Arg, Tptr,
+ /*InsertAtEnd=*/BB, Ctx, "IntToPtr"));
+ // Check getOpcode().
+ EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::IntToPtr);
+ // Check getSrcTy().
+ EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
+ // Check getDestTy().
+ EXPECT_EQ(NewI->getDestTy(), Tptr);
+ // Check instr position.
+ EXPECT_EQ(NewI->getNextNode(), nullptr);
+ EXPECT_EQ(NewI->getParent(), BB);
+ }
+}
+
TEST_F(SandboxIRTest, PtrToIntInst) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
More information about the llvm-commits
mailing list