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

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 15:54:55 PDT 2024


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

The CastInst subclasses all have pretty much the same implementation. Add a helper templated class to help stamp out the subclasses more succinctly.

>From 006afa6a527cd78ec2904311d16a6bf71d9c68fb Mon Sep 17 00:00:00 2001
From: Arthur Eubanks <aeubanks at google.com>
Date: Wed, 31 Jul 2024 22:53:12 +0000
Subject: [PATCH] [SandboxIR][NFC] Introduce templated CastInstImpl to simplify
 subclasses

The CastInst subclasses all have pretty much the same implementation.
Add a helper templated class to help stamp out the subclasses more
succinctly.
---
 llvm/include/llvm/SandboxIR/SandboxIR.h | 144 +++--------------
 llvm/lib/SandboxIR/SandboxIR.cpp        | 204 ------------------------
 2 files changed, 18 insertions(+), 330 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 38c2586f9d73c..1a96331381e3d 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -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
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
-};
-
-class FPToSIInst 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::FPToSI;
-    return false;
+                       Context &Ctx, const Twine &Name = "") {
+    return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
   }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
-};
-
-class IntToPtrInst 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::IntToPtr;
+      return I->getOpcode() == Op;
     return false;
   }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
 };
 
-class PtrToIntInst 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) {
-    return isa<Instruction>(From) &&
-           cast<Instruction>(From)->getOpcode() == Opcode::PtrToInt;
-  }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
+class SIToFPInst final : public CastInstImpl<Instruction::Opcode::SIToFP> {};
+class FPToUIInst final : public CastInstImpl<Instruction::Opcode::FPToUI> {};
+class FPToSIInst final : public CastInstImpl<Instruction::Opcode::FPToSI> {};
+class IntToPtrInst final : public CastInstImpl<Instruction::Opcode::IntToPtr> {
 };
-
-class BitCastInst : 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() == Instruction::Opcode::BitCast;
-    return false;
-  }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const override;
-  LLVM_DUMP_METHOD void dump() const override;
-#endif
+class PtrToIntInst final : public CastInstImpl<Instruction::Opcode::PtrToInt> {
 };
-
-class AddrSpaceCastInst : public CastInst {
+class BitCastInst final : public CastInstImpl<Instruction::Opcode::BitCast> {};
+class AddrSpaceCastInst final
+    : public CastInstImpl<Instruction::Opcode::AddrSpaceCast> {
 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::AddrSpaceCast;
-    return false;
-  }
   /// \Returns the pointer operand.
   Value *getPointerOperand() { return getOperand(0); }
   /// \Returns the pointer operand.
@@ -1587,10 +1483,6 @@ class AddrSpaceCastInst : public CastInst {
   unsigned getDestAddressSpace() const {
     return getType()->getPointerAddressSpace();
   }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const override;
-  LLVM_DUMP_METHOD void dump() const override;
-#endif
 };
 
 /// An LLLVM Instruction that has no SandboxIR equivalent class gets mapped to
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 1ea22c3a8b48e..a21e5a96011e6 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -1141,210 +1141,6 @@ void CastInst::dump() const {
   dump(dbgs());
   dbgs() << "\n";
 }
-#endif // NDEBUG
-
-Value *SIToFPInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                          BasicBlock *WhereBB, Context &Ctx,
-                          const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::SIToFP, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-Value *SIToFPInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertBefore->getIterator(),
-                InsertBefore->getParent(), Ctx, Name);
-}
-Value *SIToFPInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void SIToFPInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void SIToFPInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *FPToUIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                          BasicBlock *WhereBB, Context &Ctx,
-                          const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::FPToUI, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-Value *FPToUIInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertBefore->getIterator(),
-                InsertBefore->getParent(), Ctx, Name);
-}
-Value *FPToUIInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void FPToUIInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void FPToUIInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *FPToSIInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                          BasicBlock *WhereBB, Context &Ctx,
-                          const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::FPToSI, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-Value *FPToSIInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertBefore->getIterator(),
-                InsertBefore->getParent(), Ctx, Name);
-}
-Value *FPToSIInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                          Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void FPToSIInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void FPToSIInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *IntToPtrInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                            BasicBlock *WhereBB, Context &Ctx,
-                            const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::IntToPtr, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-Value *IntToPtrInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                            Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertBefore->getIterator(),
-                InsertBefore->getParent(), Ctx, Name);
-}
-Value *IntToPtrInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                            Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void IntToPtrInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void IntToPtrInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *PtrToIntInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                            BasicBlock *WhereBB, Context &Ctx,
-                            const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::PtrToInt, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-Value *PtrToIntInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                            Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertBefore->getIterator(),
-                InsertBefore->getParent(), Ctx, Name);
-}
-Value *PtrToIntInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                            Context &Ctx, const Twine &Name) {
-  return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void PtrToIntInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void PtrToIntInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *BitCastInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                           BasicBlock *WhereBB, Context &Ctx,
-                           const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::BitCast, Src, WhereIt,
-                          WhereBB, Ctx, Name);
-}
-
-Value *BitCastInst::create(Value *Src, Type *DestTy, Instruction *InsertBefore,
-                           Context &Ctx, const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::BitCast, Src,
-                          InsertBefore, Ctx, Name);
-}
-
-Value *BitCastInst::create(Value *Src, Type *DestTy, BasicBlock *InsertAtEnd,
-                           Context &Ctx, const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::BitCast, Src,
-                          InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void BitCastInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void BitCastInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
-#endif // NDEBUG
-
-Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy, BBIterator WhereIt,
-                                 BasicBlock *WhereBB, Context &Ctx,
-                                 const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
-                          WhereIt, WhereBB, Ctx, Name);
-}
-
-Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy,
-                                 Instruction *InsertBefore, Context &Ctx,
-                                 const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
-                          InsertBefore, Ctx, Name);
-}
-
-Value *AddrSpaceCastInst::create(Value *Src, Type *DestTy,
-                                 BasicBlock *InsertAtEnd, Context &Ctx,
-                                 const Twine &Name) {
-  return CastInst::create(DestTy, Instruction::Opcode::AddrSpaceCast, Src,
-                          InsertAtEnd, Ctx, Name);
-}
-
-#ifndef NDEBUG
-void AddrSpaceCastInst::dump(raw_ostream &OS) const {
-  dumpCommonPrefix(OS);
-  dumpCommonSuffix(OS);
-}
-
-void AddrSpaceCastInst::dump() const {
-  dump(dbgs());
-  dbgs() << "\n";
-}
 
 void OpaqueInst::dump(raw_ostream &OS) const {
   dumpCommonPrefix(OS);



More information about the llvm-commits mailing list