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

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 10:23:08 PDT 2024


Author: Arthur Eubanks
Date: 2024-08-01T10:23:05-07:00
New Revision: d68a4d512006750f2e2e8b31993e2c3d16f252a9

URL: https://github.com/llvm/llvm-project/commit/d68a4d512006750f2e2e8b31993e2c3d16f252a9
DIFF: https://github.com/llvm/llvm-project/commit/d68a4d512006750f2e2e8b31993e2c3d16f252a9.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/SandboxIR/SandboxIR.h
    llvm/lib/SandboxIR/SandboxIR.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 1fd9283313895..00794048c0d7f 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -1433,88 +1433,58 @@ 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 = "");
+                       Context &Ctx, const Twine &Name = "") {
+    return create(Src, DestTy, InsertAtEnd->end(), InsertAtEnd, Ctx, Name);
+  }
 
   static bool classof(const Value *From) {
     if (auto *I = dyn_cast<Instruction>(From))
-      return I->getOpcode() == Opcode::FPToSI;
+      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 IntToPtrInst final : public CastInst {
+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 PtrToIntInst final : public CastInstImpl<Instruction::Opcode::PtrToInt> {
+};
+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::IntToPtr;
-    return false;
+  /// \Returns the pointer operand.
+  Value *getPointerOperand() { return getOperand(0); }
+  /// \Returns the pointer operand.
+  const Value *getPointerOperand() const {
+    return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
+  }
+  /// \Returns the operand index of the pointer operand.
+  static unsigned getPointerOperandIndex() { return 0u; }
+  /// \Returns the address space of the pointer operand.
+  unsigned getSrcAddressSpace() const {
+    return getPointerOperand()->getType()->getPointerAddressSpace();
+  }
+  /// \Returns the address space of the result.
+  unsigned getDestAddressSpace() const {
+    return getType()->getPointerAddressSpace();
   }
-#ifndef NDEBUG
-  void dump(raw_ostream &OS) const final;
-  LLVM_DUMP_METHOD void dump() const final;
-#endif // NDEBUG
 };
 
 class PHINode final : public Instruction {
@@ -1611,83 +1581,6 @@ class PHINode final : public Instruction {
   LLVM_DUMP_METHOD void dump() const override;
 #endif
 };
-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 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 AddrSpaceCastInst : 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::AddrSpaceCast;
-    return false;
-  }
-  /// \Returns the pointer operand.
-  Value *getPointerOperand() { return getOperand(0); }
-  /// \Returns the pointer operand.
-  const Value *getPointerOperand() const {
-    return const_cast<AddrSpaceCastInst *>(this)->getPointerOperand();
-  }
-  /// \Returns the operand index of the pointer operand.
-  static unsigned getPointerOperandIndex() { return 0u; }
-  /// \Returns the address space of the pointer operand.
-  unsigned getSrcAddressSpace() const {
-    return getPointerOperand()->getType()->getPointerAddressSpace();
-  }
-  /// \Returns the address space of the result.
-  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
 /// an OpaqueInstr.

diff  --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 4f12985bb0e63..a67cb2a5bb25a 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -1230,137 +1230,7 @@ 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 PHINode::dump(raw_ostream &OS) const {
   dumpCommonPrefix(OS);
   dumpCommonSuffix(OS);
@@ -1371,80 +1241,6 @@ void PHINode::dump() const {
   dbgs() << "\n";
 }
 
-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);
   dumpCommonSuffix(OS);


        


More information about the llvm-commits mailing list