Skip to content

Commit 9718f3d

Browse files
authored
[SandboxIR] Implement FPToSIInst (#101362)
This patch implements sandboxir::FPToSIInst which mirrors llvm::FPToSIInst.
1 parent a3fb301 commit 9718f3d

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
// | |
3333
// | +- BitCastInst
3434
// | |
35+
// | +- FPToSIInst
36+
// | |
3537
// | +- IntToPtrInst
3638
// | |
3739
// | +- PtrToIntInst
@@ -1374,6 +1376,27 @@ class CastInst : public Instruction {
13741376
#endif
13751377
};
13761378

1379+
class FPToSIInst final : public CastInst {
1380+
public:
1381+
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1382+
BasicBlock *WhereBB, Context &Ctx,
1383+
const Twine &Name = "");
1384+
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1385+
Context &Ctx, const Twine &Name = "");
1386+
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1387+
Context &Ctx, const Twine &Name = "");
1388+
1389+
static bool classof(const Value *From) {
1390+
if (auto *I = dyn_cast<Instruction>(From))
1391+
return I->getOpcode() == Opcode::FPToSI;
1392+
return false;
1393+
}
1394+
#ifndef NDEBUG
1395+
void dump(raw_ostream &OS) const final;
1396+
LLVM_DUMP_METHOD void dump() const final;
1397+
#endif // NDEBUG
1398+
};
1399+
13771400
class IntToPtrInst final : public CastInst {
13781401
public:
13791402
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,34 @@ void CastInst::dump() const {
11431143
}
11441144
#endif // NDEBUG
11451145

1146+
Value *FPToSIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
1147+
BasicBlock *WhereBB, Context &Ctx,
1148+
const Twine &Name) {
1149+
return CastInst::create(DestTy, Instruction::Opcode::FPToSI, Src, WhereIt,
1150+
WhereBB, Ctx, Name);
1151+
}
1152+
Value *FPToSIInst::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 *FPToSIInst::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 FPToSIInst::dump(raw_ostream &OS) const {
1164+
dumpCommonPrefix(OS);
1165+
dumpCommonSuffix(OS);
1166+
}
1167+
1168+
void FPToSIInst::dump() const {
1169+
dump(dbgs());
1170+
dbgs() << "\n";
1171+
}
1172+
#endif // NDEBUG
1173+
11461174
Value *IntToPtrInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
11471175
BasicBlock *WhereBB, Context &Ctx,
11481176
const Twine &Name) {

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
15041504
EXPECT_EQ(FPToUI->getDestTy(), Ti32);
15051505

15061506
auto *FPToSI = cast<sandboxir::CastInst>(&*It++);
1507+
EXPECT_TRUE(isa<sandboxir::FPToSIInst>(FPToSI));
15071508
EXPECT_EQ(FPToSI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
15081509
EXPECT_EQ(FPToSI->getSrcTy(), Tfloat);
15091510
EXPECT_EQ(FPToSI->getDestTy(), Ti32);
@@ -1617,6 +1618,75 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
16171618
}
16181619
}
16191620

1621+
TEST_F(SandboxIRTest, FPToSIInst) {
1622+
parseIR(C, R"IR(
1623+
define void @foo(float %arg) {
1624+
%fptosi = fptosi float %arg to i32
1625+
ret void
1626+
}
1627+
)IR");
1628+
Function &LLVMF = *M->getFunction("foo");
1629+
sandboxir::Context Ctx(C);
1630+
sandboxir::Function *F = Ctx.createFunction(&LLVMF);
1631+
unsigned ArgIdx = 0;
1632+
auto *Arg = F->getArg(ArgIdx++);
1633+
auto *BB = &*F->begin();
1634+
auto It = BB->begin();
1635+
Type *Ti32 = Type::getInt32Ty(C);
1636+
Type *Tfloat = Type::getFloatTy(C);
1637+
1638+
auto *FPToSI = cast<sandboxir::FPToSIInst>(&*It++);
1639+
EXPECT_EQ(FPToSI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
1640+
EXPECT_EQ(FPToSI->getSrcTy(), Tfloat);
1641+
EXPECT_EQ(FPToSI->getDestTy(), Ti32);
1642+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
1643+
1644+
{
1645+
// Check create() WhereIt, WhereBB
1646+
auto *NewI = cast<sandboxir::FPToSIInst>(
1647+
sandboxir::FPToSIInst::create(Arg, Ti32, /*WhereIt=*/BB->end(),
1648+
/*WhereBB=*/BB, Ctx, "FPToSI"));
1649+
// Check getOpcode().
1650+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
1651+
// Check getSrcTy().
1652+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1653+
// Check getDestTy().
1654+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1655+
// Check instr position.
1656+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1657+
EXPECT_EQ(NewI->getPrevNode(), Ret);
1658+
}
1659+
{
1660+
// Check create() InsertBefore.
1661+
auto *NewI = cast<sandboxir::FPToSIInst>(
1662+
sandboxir::FPToSIInst::create(Arg, Ti32,
1663+
/*InsertBefore=*/Ret, Ctx, "FPToSI"));
1664+
// Check getOpcode().
1665+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
1666+
// Check getSrcTy().
1667+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1668+
// Check getDestTy().
1669+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1670+
// Check instr position.
1671+
EXPECT_EQ(NewI->getNextNode(), Ret);
1672+
}
1673+
{
1674+
// Check create() InsertAtEnd.
1675+
auto *NewI = cast<sandboxir::FPToSIInst>(
1676+
sandboxir::FPToSIInst::create(Arg, Ti32,
1677+
/*InsertAtEnd=*/BB, Ctx, "FPToSI"));
1678+
// Check getOpcode().
1679+
EXPECT_EQ(NewI->getOpcode(), sandboxir::Instruction::Opcode::FPToSI);
1680+
// Check getSrcTy().
1681+
EXPECT_EQ(NewI->getSrcTy(), Arg->getType());
1682+
// Check getDestTy().
1683+
EXPECT_EQ(NewI->getDestTy(), Ti32);
1684+
// Check instr position.
1685+
EXPECT_EQ(NewI->getNextNode(), nullptr);
1686+
EXPECT_EQ(NewI->getParent(), BB);
1687+
}
1688+
}
1689+
16201690
TEST_F(SandboxIRTest, IntToPtrInst) {
16211691
parseIR(C, R"IR(
16221692
define void @foo(i32 %arg) {

0 commit comments

Comments
 (0)