[llvm] [SandboxIR][NFC] Introduce templated CastInstImpl to simplify subclasses (PR #101427)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 16:33:06 PDT 2024


================
@@ -1431,146 +1431,42 @@ class CastInst : public Instruction {
 #endif
 };
 
-class SIToFPInst final : public CastInst {
+// Helper class to simplify stamping out CastInst subclasses.
+template <Instruction::Opcode Op> class CastInstImpl : public CastInst {
 public:
   static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
                        BasicBlock *WhereBB, Context &Ctx,
-                       const Twine &Name = "");
-  static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                       Context &Ctx, const Twine &Name = "");
-  static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                       Context &Ctx, const Twine &Name = "");
-
-  static bool classof(const Value *From) {
-    if (auto *I = dyn_cast<Instruction>(From))
-      return I->getOpcode() == Opcode::SIToFP;
-    return false;
+                       const Twine &Name = "") {
+    return CastInst::create(DestTy, Op, Src, WhereIt, WhereBB, Ctx, Name);
   }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
-};
-
-class FPToUIInst final : public CastInst {
-public:
-  static Value *create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                       BasicBlock *WhereBB, Context &Ctx,
-                       const Twine &Name = "");
   static Value *create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                       Context &Ctx, const Twine &Name = "");
-  static Value *create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                       Context &Ctx, const Twine &Name = "");
-
-  static bool classof(const Value *From) {
-    if (auto *I = dyn_cast<Instruction>(From))
-      return I->getOpcode() == Opcode::FPToUI;
-    return false;
+                       Context &Ctx, const Twine &Name = "") {
+    return create(Src, DestTy, InsertBefore->getIterator(),
+                  InsertBefore->getParent(), Ctx, Name);
   }
-#ifndef NDEBUG
----------------
vporpo wrote:

I would keep these for now until we find a better fix for all of them. The issue is that gdb won't work with `I->dump()` if `I` is a pointer to a sub-class of CastInst. You can get it to work by casting it to `sandboxir::CastInst *`, like `(gdb) ((sandboxir::CastInst *)I)->dump()` which is annoying. 


https://github.com/llvm/llvm-project/pull/101427


More information about the llvm-commits mailing list