diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h index 4c452ce0b4a61..cf1246951ecc1 100644 --- a/llvm/include/llvm/SandboxIR/SandboxIR.h +++ b/llvm/include/llvm/SandboxIR/SandboxIR.h @@ -356,11 +356,11 @@ class Value { void dumpCommonSuffix(raw_ostream &OS) const; void printAsOperandCommon(raw_ostream &OS) const; friend raw_ostream &operator<<(raw_ostream &OS, const sandboxir::Value &V) { - V.dump(OS); + V.dumpOS(OS); return OS; } - virtual void dump(raw_ostream &OS) const = 0; - LLVM_DUMP_METHOD virtual void dump() const = 0; + virtual void dumpOS(raw_ostream &OS) const = 0; + LLVM_DUMP_METHOD void dump() const; #endif }; @@ -378,14 +378,8 @@ class Argument : public sandboxir::Value { void verify() const final { assert(isa(Val) && "Expected Argument!"); } - friend raw_ostream &operator<<(raw_ostream &OS, - const sandboxir::Argument &TArg) { - TArg.dump(OS); - return OS; - } void printAsOperand(raw_ostream &OS) const; - void dump(raw_ostream &OS) const final; - LLVM_DUMP_METHOD void dump() const final; + void dumpOS(raw_ostream &OS) const final; #endif }; @@ -474,10 +468,7 @@ class User : public Value { assert(isa(Val) && "Expected User!"); } void dumpCommonHeader(raw_ostream &OS) const final; - void dump(raw_ostream &OS) const override { - // TODO: Remove this tmp implementation once we get the Instruction classes. - } - LLVM_DUMP_METHOD void dump() const override { + void dumpOS(raw_ostream &OS) const override { // TODO: Remove this tmp implementation once we get the Instruction classes. } #endif @@ -510,13 +501,7 @@ class Constant : public sandboxir::User { void verify() const override { assert(isa(Val) && "Expected Constant!"); } - friend raw_ostream &operator<<(raw_ostream &OS, - const sandboxir::Constant &SBC) { - SBC.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; + void dumpOS(raw_ostream &OS) const override; #endif }; @@ -605,12 +590,7 @@ class BasicBlock : public Value { void verify() const final { assert(isa(Val) && "Expected BasicBlock!"); } - friend raw_ostream &operator<<(raw_ostream &OS, const BasicBlock &SBBB) { - SBBB.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const final; - LLVM_DUMP_METHOD void dump() const final; + void dumpOS(raw_ostream &OS) const final; #endif }; @@ -657,12 +637,6 @@ class Instruction : public sandboxir::User { public: static const char *getOpcodeName(Opcode Opc); -#ifndef NDEBUG - friend raw_ostream &operator<<(raw_ostream &OS, Opcode Opc) { - OS << getOpcodeName(Opc); - return OS; - } -#endif /// This is used by BasicBlock::iterator. virtual unsigned getNumOfIRInstrs() const = 0; /// \Returns a BasicBlock::iterator for this Instruction. @@ -703,37 +677,54 @@ class Instruction : public sandboxir::User { static bool classof(const sandboxir::Value *From); #ifndef NDEBUG - friend raw_ostream &operator<<(raw_ostream &OS, - const sandboxir::Instruction &SBI) { - SBI.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; + void dumpOS(raw_ostream &OS) const override; #endif }; -class SelectInst : public Instruction { - /// Use Context::createSelectInst(). Don't call the - /// constructor directly. - SelectInst(llvm::SelectInst *CI, Context &Ctx) - : Instruction(ClassID::Select, Opcode::Select, CI, Ctx) {} - friend Context; // for SelectInst() +/// Instructions that contain a single LLVM Instruction can inherit from this. +template class SingleLLVMInstructionImpl : public Instruction { + SingleLLVMInstructionImpl(ClassID ID, Opcode Opc, llvm::Instruction *I, + sandboxir::Context &SBCtx) + : Instruction(ID, Opc, I, SBCtx) {} + + // All instructions are friends with this so they can call the constructor. +#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; +#include "llvm/SandboxIR/SandboxIRValues.def" + friend class UnaryInstruction; + friend class CallBase; + Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { return getOperandUseDefault(OpIdx, Verify); } SmallVector getLLVMInstrs() const final { return {cast(Val)}; } - static Value *createCommon(Value *Cond, Value *True, Value *False, - const Twine &Name, IRBuilder<> &Builder, - Context &Ctx); public: unsigned getUseOperandNo(const Use &Use) const final { return getUseOperandNoDefault(Use); } unsigned getNumOfIRInstrs() const final { return 1u; } +#ifndef NDEBUG + void verify() const final { assert(isa(Val) && "Expected LLVMT!"); } + void dumpOS(raw_ostream &OS) const override { + dumpCommonPrefix(OS); + dumpCommonSuffix(OS); + } +#endif +}; + +class SelectInst : public SingleLLVMInstructionImpl { + /// Use Context::createSelectInst(). Don't call the + /// constructor directly. + SelectInst(llvm::SelectInst *CI, Context &Ctx) + : SingleLLVMInstructionImpl(ClassID::Select, Opcode::Select, CI, Ctx) {} + friend Context; // for SelectInst() + static Value *createCommon(Value *Cond, Value *True, Value *False, + const Twine &Name, IRBuilder<> &Builder, + Context &Ctx); + +public: static Value *create(Value *Cond, Value *True, Value *False, Instruction *InsertBefore, Context &Ctx, const Twine &Name = ""); @@ -750,27 +741,15 @@ class SelectInst : public Instruction { void swapValues() { cast(Val)->swapValues(); } /// For isa/dyn_cast. static bool classof(const Value *From); -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected SelectInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class InsertElementInst final : public Instruction { +class InsertElementInst final + : public SingleLLVMInstructionImpl { /// Use Context::createInsertElementInst() instead. InsertElementInst(llvm::Instruction *I, Context &Ctx) - : Instruction(ClassID::InsertElement, Opcode::InsertElement, I, Ctx) {} - friend class Context; // For accessing the constructor in - // create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } + : SingleLLVMInstructionImpl(ClassID::InsertElement, Opcode::InsertElement, + I, Ctx) {} + friend class Context; // For accessing the constructor in create*() public: static Value *create(Value *Vec, Value *NewElt, Value *Idx, @@ -787,41 +766,15 @@ class InsertElementInst final : public Instruction { return llvm::InsertElementInst::isValidOperands(Vec->Val, NewElt->Val, Idx->Val); } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected InsertElementInst"); - } - friend raw_ostream &operator<<(raw_ostream &OS, - const InsertElementInst &IEI) { - IEI.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class BranchInst : public Instruction { +class BranchInst : public SingleLLVMInstructionImpl { /// Use Context::createBranchInst(). Don't call the constructor directly. BranchInst(llvm::BranchInst *BI, Context &Ctx) - : Instruction(ClassID::Br, Opcode::Br, BI, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::Br, Opcode::Br, BI, Ctx) {} friend Context; // for BranchInst() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } static BranchInst *create(BasicBlock *IfTrue, Instruction *InsertBefore, Context &Ctx); static BranchInst *create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd, @@ -885,26 +838,15 @@ class BranchInst : public Instruction { map_iterator(ConstLLVMRange.end(), ConstBBMap); return make_range(ConstMappedBegin, ConstMappedEnd); } - -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected BranchInst!"); - } - friend raw_ostream &operator<<(raw_ostream &OS, const BranchInst &BI) { - BI.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; /// An abstract class, parent of unary instructions. -class UnaryInstruction : public Instruction { +class UnaryInstruction + : public SingleLLVMInstructionImpl { protected: UnaryInstruction(ClassID ID, Opcode Opc, llvm::Instruction *LLVMI, Context &Ctx) - : Instruction(ID, Opc, LLVMI, Ctx) {} + : SingleLLVMInstructionImpl(ID, Opc, LLVMI, Ctx) {} public: static bool classof(const Instruction *I) { @@ -920,23 +862,13 @@ class LoadInst final : public UnaryInstruction { LoadInst(llvm::LoadInst *LI, Context &Ctx) : UnaryInstruction(ClassID::Load, Opcode::Load, LI, Ctx) {} friend Context; // for LoadInst() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: /// Return true if this is a load from a volatile memory location. bool isVolatile() const { return cast(Val)->isVolatile(); } /// Specify whether this is a volatile load or not. void setVolatile(bool V); - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx, const Twine &Name = ""); @@ -956,36 +888,20 @@ class LoadInst final : public UnaryInstruction { Align getAlign() const { return cast(Val)->getAlign(); } bool isUnordered() const { return cast(Val)->isUnordered(); } bool isSimple() const { return cast(Val)->isSimple(); } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected LoadInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class StoreInst final : public Instruction { +class StoreInst final : public SingleLLVMInstructionImpl { /// Use StoreInst::create(). StoreInst(llvm::StoreInst *SI, Context &Ctx) - : Instruction(ClassID::Store, Opcode::Store, SI, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::Store, Opcode::Store, SI, Ctx) {} friend Context; // for StoreInst() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: /// Return true if this is a store from a volatile memory location. bool isVolatile() const { return cast(Val)->isVolatile(); } /// Specify whether this is a volatile store or not. void setVolatile(bool V); - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } + static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align, Instruction *InsertBefore, Context &Ctx); static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align, @@ -1003,13 +919,6 @@ class StoreInst final : public Instruction { Align getAlign() const { return cast(Val)->getAlign(); } bool isSimple() const { return cast(Val)->isSimple(); } bool isUnordered() const { return cast(Val)->isUnordered(); } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected StoreInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class UnreachableInst final : public Instruction { @@ -1033,28 +942,15 @@ class UnreachableInst final : public Instruction { llvm_unreachable("UnreachableInst has no operands!"); } unsigned getNumOfIRInstrs() const final { return 1u; } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected UnreachableInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class ReturnInst final : public Instruction { +class ReturnInst final : public SingleLLVMInstructionImpl { /// Use ReturnInst::create() instead of calling the constructor. ReturnInst(llvm::Instruction *I, Context &Ctx) - : Instruction(ClassID::Ret, Opcode::Ret, I, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::Ret, Opcode::Ret, I, Ctx) {} ReturnInst(ClassID SubclassID, llvm::Instruction *I, Context &Ctx) - : Instruction(SubclassID, Opcode::Ret, I, Ctx) {} + : SingleLLVMInstructionImpl(SubclassID, Opcode::Ret, I, Ctx) {} friend class Context; // For accessing the constructor in create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } static ReturnInst *createCommon(Value *RetVal, IRBuilder<> &Builder, Context &Ctx); @@ -1066,22 +962,13 @@ class ReturnInst final : public Instruction { static bool classof(const Value *From) { return From->getSubclassID() == ClassID::Ret; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } /// \Returns null if there is no return value. Value *getReturnValue() const; -#ifndef NDEBUG - void verify() const final {} - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class CallBase : public Instruction { +class CallBase : public SingleLLVMInstructionImpl { CallBase(ClassID ID, Opcode Opc, llvm::Instruction *I, Context &Ctx) - : Instruction(ID, Opc, I, Ctx) {} + : SingleLLVMInstructionImpl(ID, Opc, I, Ctx) {} friend class CallInst; // For constructor. friend class InvokeInst; // For constructor. friend class CallBrInst; // For constructor. @@ -1219,12 +1106,6 @@ class CallInst final : public CallBase { : CallBase(ClassID::Call, Opcode::Call, I, Ctx) {} friend class Context; // For accessing the constructor in // create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: static CallInst *create(FunctionType *FTy, Value *Func, @@ -1241,15 +1122,6 @@ class CallInst final : public CallBase { static bool classof(const Value *From) { return From->getSubclassID() == ClassID::Call; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } -#ifndef NDEBUG - void verify() const final {} - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class InvokeInst final : public CallBase { @@ -1259,12 +1131,6 @@ class InvokeInst final : public CallBase { : CallBase(ClassID::Invoke, Opcode::Invoke, I, Ctx) {} friend class Context; // For accessing the constructor in // create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: static InvokeInst *create(FunctionType *FTy, Value *Func, @@ -1284,10 +1150,6 @@ class InvokeInst final : public CallBase { static bool classof(const Value *From) { return From->getSubclassID() == ClassID::Invoke; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } BasicBlock *getNormalDest() const; BasicBlock *getUnwindDest() const; void setNormalDest(BasicBlock *BB); @@ -1305,15 +1167,6 @@ class InvokeInst final : public CallBase { unsigned getNumSuccessors() const { return cast(Val)->getNumSuccessors(); } -#ifndef NDEBUG - void verify() const final {} - friend raw_ostream &operator<<(raw_ostream &OS, const InvokeInst &I) { - I.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class CallBrInst final : public CallBase { @@ -1323,12 +1176,6 @@ class CallBrInst final : public CallBase { : CallBase(ClassID::CallBr, Opcode::CallBr, I, Ctx) {} friend class Context; // For accessing the constructor in // create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: static CallBrInst *create(FunctionType *FTy, Value *Func, @@ -1350,10 +1197,6 @@ class CallBrInst final : public CallBase { static bool classof(const Value *From) { return From->getSubclassID() == ClassID::CallBr; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } unsigned getNumIndirectDests() const { return cast(Val)->getNumIndirectDests(); } @@ -1368,32 +1211,19 @@ class CallBrInst final : public CallBase { unsigned getNumSuccessors() const { return cast(Val)->getNumSuccessors(); } -#ifndef NDEBUG - void verify() const final {} - friend raw_ostream &operator<<(raw_ostream &OS, const CallBrInst &I) { - I.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; -class GetElementPtrInst final : public Instruction { +class GetElementPtrInst final + : public SingleLLVMInstructionImpl { /// Use Context::createGetElementPtrInst(). Don't call /// the constructor directly. GetElementPtrInst(llvm::Instruction *I, Context &Ctx) - : Instruction(ClassID::GetElementPtr, Opcode::GetElementPtr, I, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::GetElementPtr, Opcode::GetElementPtr, + I, Ctx) {} GetElementPtrInst(ClassID SubclassID, llvm::Instruction *I, Context &Ctx) - : Instruction(SubclassID, Opcode::GetElementPtr, I, Ctx) {} + : SingleLLVMInstructionImpl(SubclassID, Opcode::GetElementPtr, I, Ctx) {} friend class Context; // For accessing the constructor in // create*() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: static Value *create(Type *Ty, Value *Ptr, ArrayRef IdxList, @@ -1409,10 +1239,6 @@ class GetElementPtrInst final : public Instruction { static bool classof(const Value *From) { return From->getSubclassID() == ClassID::GetElementPtr; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } Type *getSourceElementType() const { return cast(Val)->getSourceElementType(); @@ -1475,22 +1301,9 @@ class GetElementPtrInst final : public Instruction { Offset); } // TODO: Add missing member functions. - -#ifndef NDEBUG - void verify() const final {} - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class AllocaInst final : public UnaryInstruction { - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } - AllocaInst(llvm::AllocaInst *AI, Context &Ctx) : UnaryInstruction(ClassID::Alloca, Instruction::Opcode::Alloca, AI, Ctx) {} @@ -1507,11 +1320,6 @@ class AllocaInst final : public UnaryInstruction { BasicBlock *InsertAtEnd, Context &Ctx, Value *ArraySize = nullptr, const Twine &Name = ""); - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } - /// Return true if there is an allocation size parameter to the allocation /// instruction that is not 1. bool isArrayAllocation() const { @@ -1571,13 +1379,6 @@ class AllocaInst final : public UnaryInstruction { return I->getSubclassID() == Instruction::ClassID::Alloca; return false; } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected AllocaInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class CastInst : public UnaryInstruction { @@ -1620,18 +1421,8 @@ class CastInst : public UnaryInstruction { : UnaryInstruction(ClassID::Cast, getCastOpcode(CI->getOpcode()), CI, Ctx) {} friend Context; // for SBCastInstruction() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } static Value *create(Type *DestTy, Opcode Op, Value *Operand, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name = ""); @@ -1645,13 +1436,6 @@ class CastInst : public UnaryInstruction { static bool classof(const Value *From); Type *getSrcTy() const { return cast(Val)->getSrcTy(); } Type *getDestTy() const { return cast(Val)->getDestTy(); } -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected CastInst!"); - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; // Helper class to simplify stamping out CastInst subclasses. @@ -1714,17 +1498,11 @@ class AddrSpaceCastInst final } }; -class PHINode final : public Instruction { +class PHINode final : public SingleLLVMInstructionImpl { /// Use Context::createPHINode(). Don't call the constructor directly. PHINode(llvm::PHINode *PHI, Context &Ctx) - : Instruction(ClassID::PHI, Opcode::PHI, PHI, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::PHI, Opcode::PHI, PHI, Ctx) {} friend Context; // for PHINode() - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } /// Helper for mapped_iterator. struct LLVMBBToBB { Context &Ctx; @@ -1733,10 +1511,6 @@ class PHINode final : public Instruction { }; public: - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } static PHINode *create(Type *Ty, unsigned NumReservedValues, Instruction *InsertBefore, Context &Ctx, const Twine &Name = ""); @@ -1799,50 +1573,21 @@ class PHINode final : public Instruction { // TODO: Implement // void copyIncomingBlocks(iterator_range BBRange, // uint32_t ToIdx = 0) -#ifndef NDEBUG - void verify() const final { - assert(isa(Val) && "Expected PHINode!"); - } - 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. -class OpaqueInst : public sandboxir::Instruction { +class OpaqueInst : public SingleLLVMInstructionImpl { OpaqueInst(llvm::Instruction *I, sandboxir::Context &Ctx) - : sandboxir::Instruction(ClassID::Opaque, Opcode::Opaque, I, Ctx) {} + : SingleLLVMInstructionImpl(ClassID::Opaque, Opcode::Opaque, I, Ctx) {} OpaqueInst(ClassID SubclassID, llvm::Instruction *I, sandboxir::Context &Ctx) - : sandboxir::Instruction(SubclassID, Opcode::Opaque, I, Ctx) {} + : SingleLLVMInstructionImpl(SubclassID, Opcode::Opaque, I, Ctx) {} friend class Context; // For constructor. - Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final { - return getOperandUseDefault(OpIdx, Verify); - } - SmallVector getLLVMInstrs() const final { - return {cast(Val)}; - } public: static bool classof(const sandboxir::Value *From) { return From->getSubclassID() == ClassID::Opaque; } - unsigned getUseOperandNo(const Use &Use) const final { - return getUseOperandNoDefault(Use); - } - unsigned getNumOfIRInstrs() const final { return 1u; } -#ifndef NDEBUG - void verify() const final { - // Nothing to do - } - friend raw_ostream &operator<<(raw_ostream &OS, - const sandboxir::OpaqueInst &OI) { - OI.dump(OS); - return OS; - } - void dump(raw_ostream &OS) const override; - LLVM_DUMP_METHOD void dump() const override; -#endif }; class Context { @@ -1996,8 +1741,7 @@ class Function : public Constant { assert(isa(Val) && "Expected Function!"); } void dumpNameAndArgs(raw_ostream &OS) const; - void dump(raw_ostream &OS) const final; - LLVM_DUMP_METHOD void dump() const final; + void dumpOS(raw_ostream &OS) const final; #endif }; diff --git a/llvm/include/llvm/SandboxIR/Use.h b/llvm/include/llvm/SandboxIR/Use.h index 5b55337b12bc2..c4a774aa3a89e 100644 --- a/llvm/include/llvm/SandboxIR/Use.h +++ b/llvm/include/llvm/SandboxIR/Use.h @@ -61,7 +61,7 @@ class Use { } bool operator!=(const Use &Other) const { return !(*this == Other); } #ifndef NDEBUG - void dump(raw_ostream &OS) const; + void dumpOS(raw_ostream &OS) const; void dump() const; #endif // NDEBUG }; diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp index f810a93461477..2b61a3723d483 100644 --- a/llvm/lib/SandboxIR/SandboxIR.cpp +++ b/llvm/lib/SandboxIR/SandboxIR.cpp @@ -30,7 +30,7 @@ void Use::swap(Use &OtherUse) { } #ifndef NDEBUG -void Use::dump(raw_ostream &OS) const { +void Use::dumpOS(raw_ostream &OS) const { Value *Def = nullptr; if (LLVMUse == nullptr) OS << " LLVM Use! "; @@ -58,7 +58,7 @@ void Use::dump(raw_ostream &OS) const { OS << "\n"; } -void Use::dump() const { dump(dbgs()); } +void Use::dump() const { dumpOS(dbgs()); } #endif // NDEBUG Use OperandUseIterator::operator*() const { return Use; } @@ -203,17 +203,18 @@ void Value::printAsOperandCommon(raw_ostream &OS) const { OS << "NULL "; } +void Value::dump() const { + dumpOS(dbgs()); + dbgs() << "\n"; +} + void Argument::printAsOperand(raw_ostream &OS) const { printAsOperandCommon(OS); } -void Argument::dump(raw_ostream &OS) const { +void Argument::dumpOS(raw_ostream &OS) const { dumpCommonPrefix(OS); dumpCommonSuffix(OS); } -void Argument::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} #endif // NDEBUG Use User::getOperandUseDefault(unsigned OpIdx, bool Verify) const { @@ -472,13 +473,9 @@ bool Instruction::classof(const sandboxir::Value *From) { } #ifndef NDEBUG -void Instruction::dump(raw_ostream &OS) const { +void Instruction::dumpOS(raw_ostream &OS) const { OS << "Unimplemented! Please override dump()."; } -void Instruction::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} #endif // NDEBUG Value *SelectInst::createCommon(Value *Cond, Value *True, Value *False, @@ -514,18 +511,6 @@ bool SelectInst::classof(const Value *From) { return From->getSubclassID() == ClassID::Select; } -#ifndef NDEBUG -void SelectInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void SelectInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - BranchInst *BranchInst::create(BasicBlock *IfTrue, Instruction *InsertBefore, Context &Ctx) { auto &Builder = Ctx.getLLVMIRBuilder(); @@ -596,16 +581,6 @@ const BasicBlock * BranchInst::ConstLLVMBBToSBBB::operator()(const llvm::BasicBlock *BB) const { return cast(Ctx.getValue(BB)); } -#ifndef NDEBUG -void BranchInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} -void BranchInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG void LoadInst::setVolatile(bool V) { Ctx.getTracker() @@ -657,18 +632,6 @@ Value *LoadInst::getPointerOperand() const { return Ctx.getValue(cast(Val)->getPointerOperand()); } -#ifndef NDEBUG -void LoadInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void LoadInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - void StoreInst::setVolatile(bool V) { Ctx.getTracker() .emplaceIfTracking< @@ -720,18 +683,6 @@ Value *StoreInst::getPointerOperand() const { return Ctx.getValue(cast(Val)->getPointerOperand()); } -#ifndef NDEBUG -void StoreInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void StoreInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - UnreachableInst *UnreachableInst::create(Instruction *InsertBefore, Context &Ctx) { auto &Builder = Ctx.getLLVMIRBuilder(); @@ -753,18 +704,6 @@ bool UnreachableInst::classof(const Value *From) { return From->getSubclassID() == ClassID::Unreachable; } -#ifndef NDEBUG -void UnreachableInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void UnreachableInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - ReturnInst *ReturnInst::createCommon(Value *RetVal, IRBuilder<> &Builder, Context &Ctx) { llvm::ReturnInst *NewRI; @@ -795,18 +734,6 @@ Value *ReturnInst::getReturnValue() const { return LLVMRetVal != nullptr ? Ctx.getValue(LLVMRetVal) : nullptr; } -#ifndef NDEBUG -void ReturnInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void ReturnInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Value *CallBase::getCalledOperand() const { return Ctx.getValue(cast(Val)->getCalledOperand()); } @@ -867,18 +794,6 @@ CallInst *CallInst::create(FunctionType *FTy, Value *Func, NameStr); } -#ifndef NDEBUG -void CallInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void CallInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - InvokeInst *InvokeInst::create(FunctionType *FTy, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef Args, BBIterator WhereIt, @@ -943,17 +858,6 @@ BasicBlock *InvokeInst::getSuccessor(unsigned SuccIdx) const { Ctx.getValue(cast(Val)->getSuccessor(SuccIdx))); } -#ifndef NDEBUG -void InvokeInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} -void InvokeInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - CallBrInst *CallBrInst::create(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, ArrayRef IndirectDests, @@ -1039,17 +943,6 @@ BasicBlock *CallBrInst::getSuccessor(unsigned Idx) const { Ctx.getValue(cast(Val)->getSuccessor(Idx))); } -#ifndef NDEBUG -void CallBrInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} -void CallBrInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Value *GetElementPtrInst::create(Type *Ty, Value *Ptr, ArrayRef IdxList, BasicBlock::iterator WhereIt, @@ -1092,18 +985,6 @@ Value *GetElementPtrInst::getPointerOperand() const { return Ctx.getValue(cast(Val)->getPointerOperand()); } -#ifndef NDEBUG -void GetElementPtrInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void GetElementPtrInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const { return cast(Ctx.getValue(LLVMBB)); } @@ -1299,18 +1180,6 @@ Value *AllocaInst::getArraySize() { return Ctx.getValue(cast(Val)->getArraySize()); } -#ifndef NDEBUG -void AllocaInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void AllocaInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand, BBIterator WhereIt, BasicBlock *WhereBB, Context &Ctx, const Twine &Name) { @@ -1346,38 +1215,6 @@ bool CastInst::classof(const Value *From) { return From->getSubclassID() == ClassID::Cast; } -#ifndef NDEBUG -void CastInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void CastInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} - -void PHINode::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void PHINode::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} - -void OpaqueInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void OpaqueInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx, Instruction *InsertBefore, Context &Ctx, const Twine &Name) { @@ -1404,18 +1241,6 @@ Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx, return Ctx.getOrCreateConstant(cast(NewV)); } -#ifndef NDEBUG -void InsertElementInst::dump(raw_ostream &OS) const { - dumpCommonPrefix(OS); - dumpCommonSuffix(OS); -} - -void InsertElementInst::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} -#endif // NDEBUG - Constant *Constant::createInt(Type *Ty, uint64_t V, Context &Ctx, bool IsSigned) { llvm::Constant *LLVMC = llvm::ConstantInt::get(Ty, V, IsSigned); @@ -1423,16 +1248,11 @@ Constant *Constant::createInt(Type *Ty, uint64_t V, Context &Ctx, } #ifndef NDEBUG -void Constant::dump(raw_ostream &OS) const { +void Constant::dumpOS(raw_ostream &OS) const { dumpCommonPrefix(OS); dumpCommonSuffix(OS); } -void Constant::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} - void Function::dumpNameAndArgs(raw_ostream &OS) const { auto *F = cast(Val); OS << *F->getReturnType() << " @" << F->getName() << "("; @@ -1448,7 +1268,7 @@ void Function::dumpNameAndArgs(raw_ostream &OS) const { [&] { OS << ", "; }); OS << ")"; } -void Function::dump(raw_ostream &OS) const { +void Function::dumpOS(raw_ostream &OS) const { dumpNameAndArgs(OS); OS << " {\n"; auto *LLVMF = cast(Val); @@ -1464,10 +1284,6 @@ void Function::dump(raw_ostream &OS) const { [&OS] { OS << "\n"; }); OS << "}\n"; } -void Function::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} #endif // NDEBUG BasicBlock::iterator::pointer @@ -1804,7 +1620,7 @@ Instruction &BasicBlock::back() const { } #ifndef NDEBUG -void BasicBlock::dump(raw_ostream &OS) const { +void BasicBlock::dumpOS(raw_ostream &OS) const { llvm::BasicBlock *BB = cast(Val); const auto &Name = BB->getName(); OS << Name; @@ -1833,13 +1649,9 @@ void BasicBlock::dump(raw_ostream &OS) const { } } else { for (auto &SBI : *this) { - SBI.dump(OS); + SBI.dumpOS(OS); OS << "\n"; } } } -void BasicBlock::dump() const { - dump(dbgs()); - dbgs() << "\n"; -} #endif // NDEBUG diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index 3e52b05ad2e94..f98a60b49ecab 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -102,12 +102,12 @@ define void @foo(i32 %v1) { #ifndef NDEBUG std::string Buff; raw_string_ostream BS(Buff); - F->dump(BS); - Arg0->dump(BS); - BB->dump(BS); - AddI->dump(BS); - Const0->dump(BS); - OpaqueI->dump(BS); + F->dumpOS(BS); + Arg0->dumpOS(BS); + BB->dumpOS(BS); + AddI->dumpOS(BS); + Const0->dumpOS(BS); + OpaqueI->dumpOS(BS); #endif } @@ -176,11 +176,11 @@ define i32 @foo(i32 %v0, i32 %v1) { EXPECT_EQ(Ret->getOperand(0), I0); #ifndef NDEBUG - // Check Use.dump() + // Check Use.dump(() std::string Buff; raw_string_ostream BS(Buff); BS << "\n"; - I0->getOperandUse(0).dump(BS); + I0->getOperandUse(0).dumpOS(BS); EXPECT_EQ(Buff, R"IR( Def: i32 %v0 ; SB2. (Argument) User: %add0 = add i32 %v0, %v1 ; SB5. (Opaque) @@ -396,7 +396,7 @@ define void @foo(i32 %arg0, i32 %arg1) { std::string Buff; raw_string_ostream BS(Buff); BS << "\n"; - F->dump(BS); + F->dumpOS(BS); EXPECT_EQ(Buff, R"IR( void @foo(i32 %arg0, i32 %arg1) { bb0: @@ -465,7 +465,7 @@ define void @foo(i32 %v1) { std::string Buff; raw_string_ostream BS(Buff); BS << "\n"; - BB0.dump(BS); + BB0.dumpOS(BS); EXPECT_EQ(Buff, R"IR( bb0: br label %bb1 ; SB3. (Br)