Skip to content

Commit 3cc288a

Browse files
author
vporpo
authored
[SandboxIR] Implement PtrToIntInst (#101211)
This patch implements sandboxir::PtrToIntInst mirroring llvm::PtrToIntInst.
1 parent 8a4b095 commit 3cc288a

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// |
2929
// +- BranchInst
3030
// |
31-
// +- CastInst
31+
// +- CastInst ------------- PtrToIntInst
3232
// |
3333
// +- CallBase -----------+- CallBrInst
3434
// | |
@@ -95,6 +95,7 @@ class InvokeInst;
9595
class CallBrInst;
9696
class GetElementPtrInst;
9797
class CastInst;
98+
class PtrToIntInst;
9899

99100
/// Iterator for the `Use` edges of a User's operands.
100101
/// \Returns the operand `Use` when dereferenced.
@@ -1331,6 +1332,7 @@ class CastInst : public Instruction {
13311332
CastInst(llvm::CastInst *CI, Context &Ctx)
13321333
: Instruction(ClassID::Cast, getCastOpcode(CI->getOpcode()), CI, Ctx) {}
13331334
friend Context; // for SBCastInstruction()
1335+
friend class PtrToInt; // For constructor.
13341336
Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
13351337
return getOperandUseDefault(OpIdx, Verify);
13361338
}
@@ -1365,6 +1367,25 @@ class CastInst : public Instruction {
13651367
#endif
13661368
};
13671369

1370+
class PtrToIntInst final : public CastInst {
1371+
public:
1372+
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1373+
BasicBlock *WhereBB, Context &Ctx,
1374+
const Twine &Name = "");
1375+
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1376+
Context &Ctx, const Twine &Name = "");
1377+
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1378+
Context &Ctx, const Twine &Name = "");
1379+
static bool classof(const Value *From) {
1380+
return isa<Instruction>(From) &&
1381+
cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt;
1382+
}
1383+
#ifndef NDEBUG
1384+
void dump(raw_ostream &OS) const final;
1385+
LLVM_DUMP_METHOD void dump() const final;
1386+
#endif // NDEBUG
1387+
};
1388+
13681389
/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
13691390
/// an OpaqueInstr.
13701391
class OpaqueInst : public sandboxir::Instruction {

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,34 @@ void CastInst::dump() const {
11411141
dump(dbgs());
11421142
dbgs() << "\n";
11431143
}
1144+
#endif // NDEBUG
1145+
1146+
Value *PtrToIntInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
1147+
BasicBlock *WhereBB, Context &Ctx,
1148+
const Twine &Name) {
1149+
return CastInst::create(DestTy, Instruction::Opcode::PtrToInt, Src, WhereIt,
1150+
WhereBB, Ctx, Name);
1151+
}
1152+
Value *PtrToIntInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1153+
Context &Ctx, const Twine &Name) {
1154+
return create(Src, DestTy, InsertBefore->getIterator(),
1155+
InsertBefore->getParent(), Ctx, Name);
1156+
}
1157+
Value *PtrToIntInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1158+
Context &Ctx, const Twine &Name) {
1159+
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
1160+
}
1161+
1162+
#ifndef NDEBUG
1163+
void PtrToIntInst::dump(raw_ostream &OS) const {
1164+
dumpCommonPrefix(OS);
1165+
dumpCommonSuffix(OS);
1166+
}
1167+
1168+
void PtrToIntInst::dump() const {
1169+
dump(dbgs());
1170+
dbgs() << "\n";
1171+
}
11441172

11451173
void OpaqueInst::dump(raw_ostream &OS) const {
11461174
dumpCommonPrefix(OS);

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
15141514
EXPECT_EQ(FPExt->getDestTy(), Tdouble);
15151515

15161516
auto *PtrToInt = cast<sandboxir::CastInst>(&*It++);
1517+
EXPECT_TRUE(isa<sandboxir::PtrToIntInst>(PtrToInt));
15171518
EXPECT_EQ(PtrToInt->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
15181519
EXPECT_EQ(PtrToInt->getSrcTy(), Tptr);
15191520
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
@@ -1612,3 +1613,72 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
16121613
#endif // NDEBUG
16131614
}
16141615
}
1616+
1617+
TEST_F(SandboxIRTest, PtrToIntInst) {
1618+
parseIR(C, R"IR(
1619+
define void @foo(ptr %ptr) {
1620+
%ptrtoint = ptrtoint ptr %ptr to i32
1621+
ret void
1622+
}
1623+
)IR");
1624+
Function &LLVMF = *M->getFunction("foo");
1625+
sandboxir::Context Ctx(C);
1626+
sandboxir::Function *F = Ctx.createFunction(&LLVMF);
1627+
unsigned ArgIdx = 0;
1628+
auto *Arg = F->getArg(ArgIdx++);
1629+
auto *BB = &*F->begin();
1630+
auto It = BB->begin();
1631+
Type *Ti32 = Type::getInt32Ty(C);
1632+
Type *Tptr = Ti32->getPointerTo();
1633+
1634+
auto *PtrToInt = cast<sandboxir::PtrToIntInst>(&*It++);
1635+
EXPECT_EQ(PtrToInt->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
1636+
EXPECT_EQ(PtrToInt->getSrcTy(), Tptr);
1637+
EXPECT_EQ(PtrToInt->getDestTy(), Ti32);
1638+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
1639+
1640+
{
1641+
// Check create() WhereIt, WhereBB
1642+
auto *NewI = cast<sandboxir::PtrToIntInst>(
1643+
sandboxir::PtrToIntInst::create(Arg, Ti32, /*WhereIt=*/BB->end(),
1644+
/*WhereBB=*/BB, Ctx, "PtrToInt"));
1645+
// Check getOpcode().
1646+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
1647+
// Check getSrcTy().
1648+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1649+
// Check getDestTy().
1650+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1651+
// Check instr position.
1652+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1653+
EXPECT_EQ(NewI->getPrevNode(), Ret);
1654+
}
1655+
{
1656+
// Check create() InsertBefore.
1657+
auto *NewI = cast<sandboxir::PtrToIntInst>(
1658+
sandboxir::PtrToIntInst::create(Arg, Ti32,
1659+
/*InsertBefore=*/Ret, Ctx, "PtrToInt"));
1660+
// Check getOpcode().
1661+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
1662+
// Check getSrcTy().
1663+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1664+
// Check getDestTy().
1665+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1666+
// Check instr position.
1667+
EXPECT_EQ(NewI->getNextNode(), Ret);
1668+
}
1669+
{
1670+
// Check create() InsertAtEnd.
1671+
auto *NewI = cast<sandboxir::PtrToIntInst>(
1672+
sandboxir::PtrToIntInst::create(Arg, Ti32,
1673+
/*InsertAtEnd=*/BB, Ctx, "PtrToInt"));
1674+
// Check getOpcode().
1675+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::PtrToInt);
1676+
// Check getSrcTy().
1677+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1678+
// Check getDestTy().
1679+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1680+
// Check instr position.
1681+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1682+
EXPECT_EQ(NewI->getParent(), BB);
1683+
}
1684+
}

0 commit comments

Comments
 (0)