[libc] [clang] [lldb] [llvm] [mlir] [NFC][ObjectSizeOffset] Use classes instead of std::pair (PR #76882)
bwendling wrote: > I am unsure I understand why we do the base template rather than inheritance. > Where is the base template used? If we do inheritance we could avoid > duplicating of members and provide default impls that work with many things, > e.g., all operator== are the same. I made these templates because they're dealing with different types. I can try to avoid duplicating the comparison functions, but I don't know if we'll get much out of inheritance that we don't get with templates...or really just different structs. https://github.com/llvm/llvm-project/pull/76882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[libc] [clang] [lldb] [llvm] [mlir] [NFC][ObjectSizeOffset] Use classes instead of std::pair (PR #76882)
https://github.com/bwendling edited https://github.com/llvm/llvm-project/pull/76882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[libc] [clang] [lldb] [llvm] [mlir] [NFC][ObjectSizeOffset] Use classes instead of std::pair (PR #76882)
@@ -187,80 +187,147 @@ Value *lowerObjectSizeCall( const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed, SmallVectorImpl *InsertedInstructions = nullptr); -using SizeOffsetType = std::pair; +/// SizeOffsetType - A base template class for the object size visitors. Used +/// here as a self-documenting way to handle the values rather than using a +/// \p std::pair. +template struct SizeOffsetType { + T Size; + T Offset; + + bool knownSize() const; + bool knownOffset() const; + bool anyKnown() const; + bool bothKnown() const; +}; + +/// SizeOffsetType - Used by \p ObjectSizeOffsetVisitor, which works +/// with \p APInts. +template <> struct SizeOffsetType { + APInt Size; + APInt Offset; + + SizeOffsetType() = default; + SizeOffsetType(APInt Size, APInt Offset) : Size(Size), Offset(Offset) {} + + bool knownSize() const { return Size.getBitWidth() > 1; } + bool knownOffset() const { return Offset.getBitWidth() > 1; } + bool anyKnown() const { return knownSize() || knownOffset(); } + bool bothKnown() const { return knownSize() && knownOffset(); } + + bool operator==(const SizeOffsetType ) { +return Size == RHS.Size && Offset == RHS.Offset; + } + bool operator!=(const SizeOffsetType ) { return !(*this == RHS); } +}; +using SizeOffsetAPInt = SizeOffsetType; /// Evaluate the size and offset of an object pointed to by a Value* /// statically. Fails if size or offset are not known at compile time. class ObjectSizeOffsetVisitor - : public InstVisitor { +: public InstVisitor { const DataLayout const TargetLibraryInfo *TLI; ObjectSizeOpts Options; unsigned IntTyBits; APInt Zero; - SmallDenseMap SeenInsts; + SmallDenseMap SeenInsts; unsigned InstructionsVisited; APInt align(APInt Size, MaybeAlign Align); - SizeOffsetType unknown() { -return std::make_pair(APInt(), APInt()); - } + static SizeOffsetAPInt unknown; public: ObjectSizeOffsetVisitor(const DataLayout , const TargetLibraryInfo *TLI, LLVMContext , ObjectSizeOpts Options = {}); - SizeOffsetType compute(Value *V); - - static bool knownSize(const SizeOffsetType ) { -return SizeOffset.first.getBitWidth() > 1; - } - - static bool knownOffset(const SizeOffsetType ) { -return SizeOffset.second.getBitWidth() > 1; - } - - static bool bothKnown(const SizeOffsetType ) { -return knownSize(SizeOffset) && knownOffset(SizeOffset); - } + SizeOffsetAPInt compute(Value *V); // These are "private", except they can't actually be made private. Only // compute() should be used by external users. - SizeOffsetType visitAllocaInst(AllocaInst ); - SizeOffsetType visitArgument(Argument ); - SizeOffsetType visitCallBase(CallBase ); - SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); - SizeOffsetType visitExtractElementInst(ExtractElementInst ); - SizeOffsetType visitExtractValueInst(ExtractValueInst ); - SizeOffsetType visitGlobalAlias(GlobalAlias ); - SizeOffsetType visitGlobalVariable(GlobalVariable ); - SizeOffsetType visitIntToPtrInst(IntToPtrInst&); - SizeOffsetType visitLoadInst(LoadInst ); - SizeOffsetType visitPHINode(PHINode&); - SizeOffsetType visitSelectInst(SelectInst ); - SizeOffsetType visitUndefValue(UndefValue&); - SizeOffsetType visitInstruction(Instruction ); + SizeOffsetAPInt visitAllocaInst(AllocaInst ); + SizeOffsetAPInt visitArgument(Argument ); + SizeOffsetAPInt visitCallBase(CallBase ); + SizeOffsetAPInt visitConstantPointerNull(ConstantPointerNull &); + SizeOffsetAPInt visitExtractElementInst(ExtractElementInst ); + SizeOffsetAPInt visitExtractValueInst(ExtractValueInst ); + SizeOffsetAPInt visitGlobalAlias(GlobalAlias ); + SizeOffsetAPInt visitGlobalVariable(GlobalVariable ); + SizeOffsetAPInt visitIntToPtrInst(IntToPtrInst &); + SizeOffsetAPInt visitLoadInst(LoadInst ); + SizeOffsetAPInt visitPHINode(PHINode &); + SizeOffsetAPInt visitSelectInst(SelectInst ); + SizeOffsetAPInt visitUndefValue(UndefValue &); + SizeOffsetAPInt visitInstruction(Instruction ); private: - SizeOffsetType findLoadSizeOffset( + SizeOffsetAPInt findLoadSizeOffset( LoadInst , BasicBlock , BasicBlock::iterator From, - SmallDenseMap , + SmallDenseMap , unsigned ); - SizeOffsetType combineSizeOffset(SizeOffsetType LHS, SizeOffsetType RHS); - SizeOffsetType computeImpl(Value *V); - SizeOffsetType computeValue(Value *V); + SizeOffsetAPInt combineSizeOffset(SizeOffsetAPInt LHS, SizeOffsetAPInt RHS); + SizeOffsetAPInt computeImpl(Value *V); + SizeOffsetAPInt computeValue(Value *V); bool CheckedZextOrTrunc(APInt ); }; -using SizeOffsetEvalType = std::pair; +template <> struct SizeOffsetType; + +/// SizeOffsetType - Used by \p ObjectSizeOffsetEvaluator, which works +/// with \p Values. +template <> struct SizeOffsetType { + Value *Size; + Value *Offset; + + SizeOffsetType() = default; + SizeOffsetType(Value *Size, Value *Offset) : Size(Size),
[libc] [clang] [lldb] [llvm] [mlir] [NFC][ObjectSizeOffset] Use classes instead of std::pair (PR #76882)
@@ -187,80 +187,147 @@ Value *lowerObjectSizeCall( const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed, SmallVectorImpl *InsertedInstructions = nullptr); -using SizeOffsetType = std::pair; +/// SizeOffsetType - A base template class for the object size visitors. Used +/// here as a self-documenting way to handle the values rather than using a +/// \p std::pair. +template struct SizeOffsetType { + T Size; + T Offset; + + bool knownSize() const; + bool knownOffset() const; + bool anyKnown() const; + bool bothKnown() const; +}; + +/// SizeOffsetType - Used by \p ObjectSizeOffsetVisitor, which works +/// with \p APInts. +template <> struct SizeOffsetType { + APInt Size; + APInt Offset; + + SizeOffsetType() = default; + SizeOffsetType(APInt Size, APInt Offset) : Size(Size), Offset(Offset) {} + + bool knownSize() const { return Size.getBitWidth() > 1; } + bool knownOffset() const { return Offset.getBitWidth() > 1; } + bool anyKnown() const { return knownSize() || knownOffset(); } + bool bothKnown() const { return knownSize() && knownOffset(); } + + bool operator==(const SizeOffsetType ) { +return Size == RHS.Size && Offset == RHS.Offset; + } + bool operator!=(const SizeOffsetType ) { return !(*this == RHS); } +}; +using SizeOffsetAPInt = SizeOffsetType; /// Evaluate the size and offset of an object pointed to by a Value* /// statically. Fails if size or offset are not known at compile time. class ObjectSizeOffsetVisitor - : public InstVisitor { +: public InstVisitor { const DataLayout const TargetLibraryInfo *TLI; ObjectSizeOpts Options; unsigned IntTyBits; APInt Zero; - SmallDenseMap SeenInsts; + SmallDenseMap SeenInsts; unsigned InstructionsVisited; APInt align(APInt Size, MaybeAlign Align); - SizeOffsetType unknown() { -return std::make_pair(APInt(), APInt()); - } + static SizeOffsetAPInt unknown; public: ObjectSizeOffsetVisitor(const DataLayout , const TargetLibraryInfo *TLI, LLVMContext , ObjectSizeOpts Options = {}); - SizeOffsetType compute(Value *V); - - static bool knownSize(const SizeOffsetType ) { -return SizeOffset.first.getBitWidth() > 1; - } - - static bool knownOffset(const SizeOffsetType ) { -return SizeOffset.second.getBitWidth() > 1; - } - - static bool bothKnown(const SizeOffsetType ) { -return knownSize(SizeOffset) && knownOffset(SizeOffset); - } + SizeOffsetAPInt compute(Value *V); // These are "private", except they can't actually be made private. Only // compute() should be used by external users. - SizeOffsetType visitAllocaInst(AllocaInst ); - SizeOffsetType visitArgument(Argument ); - SizeOffsetType visitCallBase(CallBase ); - SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); - SizeOffsetType visitExtractElementInst(ExtractElementInst ); - SizeOffsetType visitExtractValueInst(ExtractValueInst ); - SizeOffsetType visitGlobalAlias(GlobalAlias ); - SizeOffsetType visitGlobalVariable(GlobalVariable ); - SizeOffsetType visitIntToPtrInst(IntToPtrInst&); - SizeOffsetType visitLoadInst(LoadInst ); - SizeOffsetType visitPHINode(PHINode&); - SizeOffsetType visitSelectInst(SelectInst ); - SizeOffsetType visitUndefValue(UndefValue&); - SizeOffsetType visitInstruction(Instruction ); + SizeOffsetAPInt visitAllocaInst(AllocaInst ); + SizeOffsetAPInt visitArgument(Argument ); + SizeOffsetAPInt visitCallBase(CallBase ); + SizeOffsetAPInt visitConstantPointerNull(ConstantPointerNull &); + SizeOffsetAPInt visitExtractElementInst(ExtractElementInst ); + SizeOffsetAPInt visitExtractValueInst(ExtractValueInst ); + SizeOffsetAPInt visitGlobalAlias(GlobalAlias ); + SizeOffsetAPInt visitGlobalVariable(GlobalVariable ); + SizeOffsetAPInt visitIntToPtrInst(IntToPtrInst &); + SizeOffsetAPInt visitLoadInst(LoadInst ); + SizeOffsetAPInt visitPHINode(PHINode &); + SizeOffsetAPInt visitSelectInst(SelectInst ); + SizeOffsetAPInt visitUndefValue(UndefValue &); + SizeOffsetAPInt visitInstruction(Instruction ); private: - SizeOffsetType findLoadSizeOffset( + SizeOffsetAPInt findLoadSizeOffset( LoadInst , BasicBlock , BasicBlock::iterator From, - SmallDenseMap , + SmallDenseMap , unsigned ); - SizeOffsetType combineSizeOffset(SizeOffsetType LHS, SizeOffsetType RHS); - SizeOffsetType computeImpl(Value *V); - SizeOffsetType computeValue(Value *V); + SizeOffsetAPInt combineSizeOffset(SizeOffsetAPInt LHS, SizeOffsetAPInt RHS); + SizeOffsetAPInt computeImpl(Value *V); + SizeOffsetAPInt computeValue(Value *V); bool CheckedZextOrTrunc(APInt ); }; -using SizeOffsetEvalType = std::pair; +template <> struct SizeOffsetType; + +/// SizeOffsetType - Used by \p ObjectSizeOffsetEvaluator, which works +/// with \p Values. +template <> struct SizeOffsetType { + Value *Size; + Value *Offset; + + SizeOffsetType() = default; + SizeOffsetType(Value *Size, Value *Offset) : Size(Size),