Skip to content

Commit 8b17b12

Browse files
authored
[SandboxIR] Implement FPToUIInst (#101369)
This patch implements sandboxir::FPToUIInst which mirrors llvm::FPToUIInst.
1 parent 9fe455f commit 8b17b12

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
@@ -34,6 +34,8 @@
3434
// | |
3535
// | +- FPToSIInst
3636
// | |
37+
// | +- FPToUIInst
38+
// | |
3739
// | +- IntToPtrInst
3840
// | |
3941
// | +- PtrToIntInst
@@ -1376,6 +1378,27 @@ class CastInst : public Instruction {
13761378
#endif
13771379
};
13781380

1381+
class FPToUIInst final : public CastInst {
1382+
public:
1383+
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1384+
BasicBlock *WhereBB, Context &Ctx,
1385+
const Twine &Name = "");
1386+
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1387+
Context &Ctx, const Twine &Name = "");
1388+
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1389+
Context &Ctx, const Twine &Name = "");
1390+
1391+
static bool classof(const Value *From) {
1392+
if (auto *I = dyn_cast<Instruction>(From))
1393+
return I->getOpcode() == Opcode::FPToUI;
1394+
return false;
1395+
}
1396+
#ifndef NDEBUG
1397+
void dump(raw_ostream &OS) const final;
1398+
LLVM_DUMP_METHOD void dump() const final;
1399+
#endif // NDEBUG
1400+
};
1401+
13791402
class FPToSIInst final : public CastInst {
13801403
public:
13811404
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 *FPToUIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
1147+
BasicBlock *WhereBB, Context &Ctx,
1148+
const Twine &Name) {
1149+
return CastInst::create(DestTy, Instruction::Opcode::FPToUI, Src, WhereIt,
1150+
WhereBB, Ctx, Name);
1151+
}
1152+
Value *FPToUIInst::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 *FPToUIInst::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 FPToUIInst::dump(raw_ostream &OS) const {
1164+
dumpCommonPrefix(OS);
1165+
dumpCommonSuffix(OS);
1166+
}
1167+
1168+
void FPToUIInst::dump() const {
1169+
dump(dbgs());
1170+
dbgs() << "\n";
1171+
}
1172+
#endif // NDEBUG
1173+
11461174
Value *FPToSIInst::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
@@ -1499,6 +1499,7 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
14991499
EXPECT_EQ(SExt->getDestTy(), Ti64);
15001500

15011501
auto *FPToUI = cast<sandboxir::CastInst>(&*It++);
1502+
EXPECT_TRUE(isa<sandboxir::FPToUIInst>(FPToUI));
15021503
EXPECT_EQ(FPToUI->getOpcode(), sandboxir::Instruction::Opcode::FPToUI);
15031504
EXPECT_EQ(FPToUI->getSrcTy(), Tfloat);
15041505
EXPECT_EQ(FPToUI->getDestTy(), Ti32);
@@ -1618,6 +1619,75 @@ define void @foo(i32 %arg, float %farg, double %darg, ptr %ptr) {
16181619
}
16191620
}
16201621

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

0 commit comments

Comments
 (0)