Skip to content

Commit 1cc8adb

Browse files
committed
[SandboxIR] CastInst CRTP example
1 parent 6aa723d commit 1cc8adb

File tree

2 files changed

+68
-333
lines changed

2 files changed

+68
-333
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 68 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,57 @@ class BBIterator {
534534
pointer get() const { return getInstr(It); }
535535
};
536536

537+
/// Contains a list of sandboxir::Instruction's.
538+
class BasicBlock : public Value {
539+
/// Builds a graph that contains all values in \p BB in their original form
540+
/// i.e., no vectorization is taking place here.
541+
void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
542+
friend class Context; // For `buildBasicBlockFromIR`
543+
friend class Instruction; // For LLVM Val.
544+
545+
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
546+
: Value(ClassID::Block, BB, SBCtx) {
547+
buildBasicBlockFromLLVMIR(BB);
548+
}
549+
550+
public:
551+
~BasicBlock() = default;
552+
/// For isa/dyn_cast.
553+
static bool classof(const Value *From) {
554+
return From->getSubclassID() == Value::ClassID::Block;
555+
}
556+
Function *getParent() const;
557+
using iterator = BBIterator;
558+
iterator begin() const;
559+
iterator end() const {
560+
auto *BB = cast<llvm::BasicBlock>(Val);
561+
return iterator(BB, BB->end(), &Ctx);
562+
}
563+
std::reverse_iterator<iterator> rbegin() const {
564+
return std::make_reverse_iterator(end());
565+
}
566+
std::reverse_iterator<iterator> rend() const {
567+
return std::make_reverse_iterator(begin());
568+
}
569+
Context &getContext() const { return Ctx; }
570+
Instruction *getTerminator() const;
571+
bool empty() const { return begin() == end(); }
572+
Instruction &front() const;
573+
Instruction &back() const;
574+
575+
#ifndef NDEBUG
576+
void verify() const final {
577+
assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
578+
}
579+
friend raw_ostream &operator<<(raw_ostream &OS, const BasicBlock &SBBB) {
580+
SBBB.dump(OS);
581+
return OS;
582+
}
583+
void dump(raw_ostream &OS) const final;
584+
LLVM_DUMP_METHOD void dump() const final;
585+
#endif
586+
};
587+
537588
/// A sandboxir::User with operands, opcode and linked with previous/next
538589
/// instructions in an instruction list.
539590
class Instruction : public sandboxir::User {
@@ -1378,125 +1429,40 @@ class CastInst : public Instruction {
13781429
#endif
13791430
};
13801431

1381-
class FPToUIInst final : public CastInst {
1432+
template <Instruction::Opcode O> class CastInstCRTP : public CastInst {
13821433
public:
13831434
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
13841435
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;
1436+
const Twine &Name = "") {
1437+
return CastInst::create(DestTy, O, Src, WhereIt, WhereBB, Ctx, Name);
13951438
}
1396-
#ifndef NDEBUG
1397-
void dump(raw_ostream &OS) const final;
1398-
LLVM_DUMP_METHOD void dump() const final;
1399-
#endif // NDEBUG
1400-
};
1401-
1402-
class FPToSIInst final : public CastInst {
1403-
public:
1404-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1405-
BasicBlock *WhereBB, Context &Ctx,
1406-
const Twine &Name = "");
14071439
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1408-
Context &Ctx, const Twine &Name = "");
1409-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1410-
Context &Ctx, const Twine &Name = "");
1411-
1412-
static bool classof(const Value *From) {
1413-
if (auto *I = dyn_cast<Instruction>(From))
1414-
return I->getOpcode() == Opcode::FPToSI;
1415-
return false;
1440+
Context &Ctx, const Twine &Name = "") {
1441+
return create(Src, DestTy, InsertBefore->getIterator(),
1442+
InsertBefore->getParent(), Ctx, Name);
14161443
}
1417-
#ifndef NDEBUG
1418-
void dump(raw_ostream &OS) const final;
1419-
LLVM_DUMP_METHOD void dump() const final;
1420-
#endif // NDEBUG
1421-
};
1422-
1423-
class IntToPtrInst final : public CastInst {
1424-
public:
1425-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1426-
BasicBlock *WhereBB, Context &Ctx,
1427-
const Twine &Name = "");
1428-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1429-
Context &Ctx, const Twine &Name = "");
14301444
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1431-
Context &Ctx, const Twine &Name = "");
1445+
Context &Ctx, const Twine &Name = "") {
1446+
return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
1447+
}
14321448

14331449
static bool classof(const Value *From) {
14341450
if (auto *I = dyn_cast<Instruction>(From))
1435-
return I->getOpcode() == Opcode::IntToPtr;
1451+
return I->getOpcode() == O;
14361452
return false;
14371453
}
1438-
#ifndef NDEBUG
1439-
void dump(raw_ostream &OS) const final;
1440-
LLVM_DUMP_METHOD void dump() const final;
1441-
#endif // NDEBUG
14421454
};
14431455

1444-
class PtrToIntInst final : public CastInst {
1445-
public:
1446-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1447-
BasicBlock *WhereBB, Context &Ctx,
1448-
const Twine &Name = "");
1449-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1450-
Context &Ctx, const Twine &Name = "");
1451-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1452-
Context &Ctx, const Twine &Name = "");
1453-
1454-
static bool classof(const Value *From) {
1455-
return isa<Instruction>(From) &&
1456-
cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt;
1457-
}
1458-
#ifndef NDEBUG
1459-
void dump(raw_ostream &OS) const final;
1460-
LLVM_DUMP_METHOD void dump() const final;
1461-
#endif // NDEBUG
1456+
class FPToUIInst final : public CastInstCRTP<Instruction::Opcode::FPToUI> {};
1457+
class FPToSIInst final : public CastInstCRTP<Instruction::Opcode::FPToSI> {};
1458+
class IntToPtrInst final : public CastInstCRTP<Instruction::Opcode::IntToPtr> {
14621459
};
1463-
1464-
class BitCastInst : public CastInst {
1465-
public:
1466-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1467-
BasicBlock *WhereBB, Context &Ctx,
1468-
const Twine &Name = "");
1469-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1470-
Context &Ctx, const Twine &Name = "");
1471-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1472-
Context &Ctx, const Twine &Name = "");
1473-
1474-
static bool classof(const Value *From) {
1475-
if (auto *I = dyn_cast<Instruction>(From))
1476-
return I->getOpcode() == Instruction::Opcode::BitCast;
1477-
return false;
1478-
}
1479-
#ifndef NDEBUG
1480-
void dump(raw_ostream &OS) const override;
1481-
LLVM_DUMP_METHOD void dump() const override;
1482-
#endif
1460+
class PtrToIntInst final : public CastInstCRTP<Instruction::Opcode::PtrToInt> {
14831461
};
1484-
1485-
class AddrSpaceCastInst : public CastInst {
1462+
class BitCastInst final : public CastInstCRTP<Instruction::Opcode::BitCast> {};
1463+
class AddrSpaceCastInst final
1464+
: public CastInstCRTP<Instruction::Opcode::AddrSpaceCast> {
14861465
public:
1487-
static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
1488-
BasicBlock *WhereBB, Context &Ctx,
1489-
const Twine &Name = "");
1490-
static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
1491-
Context &Ctx, const Twine &Name = "");
1492-
static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
1493-
Context &Ctx, const Twine &Name = "");
1494-
1495-
static bool classof(const Value *From) {
1496-
if (auto *I = dyn_cast<Instruction>(From))
1497-
return I->getOpcode() == Opcode::AddrSpaceCast;
1498-
return false;
1499-
}
15001466
/// \Returns the pointer operand.
15011467
Value *getPointerOperand() { return getOperand(0); }
15021468
/// \Returns the pointer operand.
@@ -1513,10 +1479,6 @@ class AddrSpaceCastInst : public CastInst {
15131479
unsigned getDestAddressSpace() const {
15141480
return getType()->getPointerAddressSpace();
15151481
}
1516-
#ifndef NDEBUG
1517-
void dump(raw_ostream &OS) const override;
1518-
LLVM_DUMP_METHOD void dump() const override;
1519-
#endif
15201482
};
15211483

15221484
/// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
@@ -1556,57 +1518,6 @@ class OpaqueInst : public sandboxir::Instruction {
15561518
#endif
15571519
};
15581520

1559-
/// Contains a list of sandboxir::Instruction's.
1560-
class BasicBlock : public Value {
1561-
/// Builds a graph that contains all values in \p BB in their original form
1562-
/// i.e., no vectorization is taking place here.
1563-
void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
1564-
friend class Context; // For `buildBasicBlockFromIR`
1565-
friend class Instruction; // For LLVM Val.
1566-
1567-
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
1568-
: Value(ClassID::Block, BB, SBCtx) {
1569-
buildBasicBlockFromLLVMIR(BB);
1570-
}
1571-
1572-
public:
1573-
~BasicBlock() = default;
1574-
/// For isa/dyn_cast.
1575-
static bool classof(const Value *From) {
1576-
return From->getSubclassID() == Value::ClassID::Block;
1577-
}
1578-
Function *getParent() const;
1579-
using iterator = BBIterator;
1580-
iterator begin() const;
1581-
iterator end() const {
1582-
auto *BB = cast<llvm::BasicBlock>(Val);
1583-
return iterator(BB, BB->end(), &Ctx);
1584-
}
1585-
std::reverse_iterator<iterator> rbegin() const {
1586-
return std::make_reverse_iterator(end());
1587-
}
1588-
std::reverse_iterator<iterator> rend() const {
1589-
return std::make_reverse_iterator(begin());
1590-
}
1591-
Context &getContext() const { return Ctx; }
1592-
Instruction *getTerminator() const;
1593-
bool empty() const { return begin() == end(); }
1594-
Instruction &front() const;
1595-
Instruction &back() const;
1596-
1597-
#ifndef NDEBUG
1598-
void verify() const final {
1599-
assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
1600-
}
1601-
friend raw_ostream &operator<<(raw_ostream &OS, const BasicBlock &SBBB) {
1602-
SBBB.dump(OS);
1603-
return OS;
1604-
}
1605-
void dump(raw_ostream &OS) const final;
1606-
LLVM_DUMP_METHOD void dump() const final;
1607-
#endif
1608-
};
1609-
16101521
class Context {
16111522
protected:
16121523
LLVMContext &LLVMCtx;

0 commit comments

Comments
 (0)