Skip to content

Commit d68a4d5

Browse files
authored
[SandboxIR][NFC] Introduce templated CastInstImpl to simplify subclasses (#101427)
The CastInst subclasses all have pretty much the same implementation. Add a helper templated class to help stamp out the subclasses more succinctly.
1 parent 0af07c0 commit d68a4d5

File tree

2 files changed

+36
-347
lines changed

2 files changed

+36
-347
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 36 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,88 +1433,58 @@ class CastInst : public Instruction {
14331433
#endif
14341434
};
14351435

1436-
class SIToFPInst final : public CastInst {
1436+
// Helper class to simplify stamping out CastInst subclasses.
1437+
template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
14371438
public:
14381439
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
14391440
BasicBlock *WhereBB, Context &Ctx,
1440-
const Twine &Name = "");
1441-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1442-
Context &Ctx, const Twine &Name = "");
1443-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1444-
Context &Ctx, const Twine &Name = "");
1445-
1446-
static bool classof(const Value *From) {
1447-
if (auto *I = dyn_cast<Instruction>(From))
1448-
return I->getOpcode() == Opcode::SIToFP;
1449-
return false;
1441+
const Twine &Name = "") {
1442+
return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name);
14501443
}
1451-
#ifndef NDEBUG
1452-
void dump(raw_ostream &OS) const final;
1453-
LLVM_DUMP_METHOD void dump() const final;
1454-
#endif // NDEBUG
1455-
};
1456-
1457-
class FPToUIInst final : public CastInst {
1458-
public:
1459-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1460-
BasicBlock *WhereBB, Context &Ctx,
1461-
const Twine &Name = "");
14621444
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1463-
Context &Ctx, const Twine &Name = "");
1464-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1465-
Context &Ctx, const Twine &Name = "");
1466-
1467-
static bool classof(const Value *From) {
1468-
if (auto *I = dyn_cast<Instruction>(From))
1469-
return I->getOpcode() == Opcode::FPToUI;
1470-
return false;
1445+
Context &Ctx, const Twine &Name = "") {
1446+
return create(Src, DestTy, InsertBefore->getIterator(),
1447+
InsertBefore->getParent(), Ctx, Name);
14711448
}
1472-
#ifndef NDEBUG
1473-
void dump(raw_ostream &OS) const final;
1474-
LLVM_DUMP_METHOD void dump() const final;
1475-
#endif // NDEBUG
1476-
};
1477-
1478-
class FPToSIInst final : public CastInst {
1479-
public:
1480-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1481-
BasicBlock *WhereBB, Context &Ctx,
1482-
const Twine &Name = "");
1483-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1484-
Context &Ctx, const Twine &Name = "");
14851449
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1486-
Context &Ctx, const Twine &Name = "");
1450+
Context &Ctx, const Twine &Name = "") {
1451+
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
1452+
}
14871453

14881454
static bool classof(const Value *From) {
14891455
if (auto *I = dyn_cast<Instruction>(From))
1490-
return I->getOpcode() == Opcode::FPToSI;
1456+
return I->getOpcode() == Op;
14911457
return false;
14921458
}
1493-
#ifndef NDEBUG
1494-
void dump(raw_ostream &OS) const final;
1495-
LLVM_DUMP_METHOD void dump() const final;
1496-
#endif // NDEBUG
14971459
};
14981460

1499-
class IntToPtrInst final : public CastInst {
1461+
class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
1462+
class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
1463+
class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};
1464+
class IntToPtrInst final : public CastInstImpl<Instruction::Opcode::IntToPtr> {
1465+
};
1466+
class PtrToIntInst final : public CastInstImpl<Instruction::Opcode::PtrToInt> {
1467+
};
1468+
class BitCastInst final : public CastInstImpl<Instruction::Opcode::BitCast> {};
1469+
class AddrSpaceCastInst final
1470+
: public CastInstImpl<Instruction::Opcode::AddrSpaceCast> {
15001471
public:
1501-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1502-
BasicBlock *WhereBB, Context &Ctx,
1503-
const Twine &Name = "");
1504-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1505-
Context &Ctx, const Twine &Name = "");
1506-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1507-
Context &Ctx, const Twine &Name = "");
1508-
1509-
static bool classof(const Value *From) {
1510-
if (auto *I = dyn_cast<Instruction>(From))
1511-
return I->getOpcode() == Opcode::IntToPtr;
1512-
return false;
1472+
/// \Returns the pointer operand.
1473+
Value *getPointerOperand() { return getOperand(0); }
1474+
/// \Returns the pointer operand.
1475+
const Value *getPointerOperand() const {
1476+
return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
1477+
}
1478+
/// \Returns the operand index of the pointer operand.
1479+
static unsigned getPointerOperandIndex() { return 0u; }
1480+
/// \Returns the address space of the pointer operand.
1481+
unsigned getSrcAddressSpace() const {
1482+
return getPointerOperand()->getType()->getPointerAddressSpace();
1483+
}
1484+
/// \Returns the address space of the result.
1485+
unsigned getDestAddressSpace() const {
1486+
return getType()->getPointerAddressSpace();
15131487
}
1514-
#ifndef NDEBUG
1515-
void dump(raw_ostream &OS) const final;
1516-
LLVM_DUMP_METHOD void dump() const final;
1517-
#endif // NDEBUG
15181488
};
15191489

15201490
class PHINode final : public Instruction {
@@ -1611,83 +1581,6 @@ class PHINode final : public Instruction {
16111581
LLVM_DUMP_METHOD void dump() const override;
16121582
#endif
16131583
};
1614-
class PtrToIntInst final : public CastInst {
1615-
public:
1616-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1617-
BasicBlock *WhereBB, Context &Ctx,
1618-
const Twine &Name = "");
1619-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1620-
Context &Ctx, const Twine &Name = "");
1621-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1622-
Context &Ctx, const Twine &Name = "");
1623-
1624-
static bool classof(const Value *From) {
1625-
return isa<Instruction>(From) &&
1626-
cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt;
1627-
}
1628-
#ifndef NDEBUG
1629-
void dump(raw_ostream &OS) const final;
1630-
LLVM_DUMP_METHOD void dump() const final;
1631-
#endif // NDEBUG
1632-
};
1633-
1634-
class BitCastInst : public CastInst {
1635-
public:
1636-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1637-
BasicBlock *WhereBB, Context &Ctx,
1638-
const Twine &Name = "");
1639-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1640-
Context &Ctx, const Twine &Name = "");
1641-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1642-
Context &Ctx, const Twine &Name = "");
1643-
1644-
static bool classof(const Value *From) {
1645-
if (auto *I = dyn_cast<Instruction>(From))
1646-
return I->getOpcode() == Instruction::Opcode::BitCast;
1647-
return false;
1648-
}
1649-
#ifndef NDEBUG
1650-
void dump(raw_ostream &OS) const override;
1651-
LLVM_DUMP_METHOD void dump() const override;
1652-
#endif
1653-
};
1654-
1655-
class AddrSpaceCastInst : public CastInst {
1656-
public:
1657-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1658-
BasicBlock *WhereBB, Context &Ctx,
1659-
const Twine &Name = "");
1660-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1661-
Context &Ctx, const Twine &Name = "");
1662-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1663-
Context &Ctx, const Twine &Name = "");
1664-
1665-
static bool classof(const Value *From) {
1666-
if (auto *I = dyn_cast<Instruction>(From))
1667-
return I->getOpcode() == Opcode::AddrSpaceCast;
1668-
return false;
1669-
}
1670-
/// \Returns the pointer operand.
1671-
Value *getPointerOperand() { return getOperand(0); }
1672-
/// \Returns the pointer operand.
1673-
const Value *getPointerOperand() const {
1674-
return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
1675-
}
1676-
/// \Returns the operand index of the pointer operand.
1677-
static unsigned getPointerOperandIndex() { return 0u; }
1678-
/// \Returns the address space of the pointer operand.
1679-
unsigned getSrcAddressSpace() const {
1680-
return getPointerOperand()->getType()->getPointerAddressSpace();
1681-
}
1682-
/// \Returns the address space of the result.
1683-
unsigned getDestAddressSpace() const {
1684-
return getType()->getPointerAddressSpace();
1685-
}
1686-
#ifndef NDEBUG
1687-
void dump(raw_ostream &OS) const override;
1688-
LLVM_DUMP_METHOD void dump() const override;
1689-
#endif
1690-
};
16911584

16921585
/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
16931586
/// an OpaqueInstr.

0 commit comments

Comments
 (0)