[libc-commits] [lldb] [libc] [llvm] [mlir] [clang] [NFC][ObjectSizeOffset] Use classes instead of std::pair (PR #76882)

Johannes Doerfert via libc-commits libc-commits at lists.llvm.org
Fri Jan 5 10:33:07 PST 2024


================
@@ -187,80 +187,132 @@ Value *lowerObjectSizeCall(
     const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
     SmallVectorImpl<Instruction *> *InsertedInstructions = nullptr);
 
-using SizeOffsetType = std::pair<APInt, APInt>;
+/// 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 <typename T, class C> class SizeOffsetType {
+public:
+  T Size;
+  T Offset;
+
+  SizeOffsetType() = default;
+  SizeOffsetType(T Size, T Offset) : Size(Size), Offset(Offset) {}
+
+  bool anyKnown() const { return C::known(Size) || C::known(Offset); }
+  bool bothKnown() const { return C::known(Size) && C::known(Offset); }
+
+  bool operator==(const SizeOffsetType<T, C> &RHS) const {
+    return Size == RHS.Size && Offset == RHS.Offset;
+  }
+  bool operator!=(const SizeOffsetType<T, C> &RHS) const {
+    return !(*this == RHS);
+  }
+};
+
+/// SizeOffsetAPInt - Used by \p ObjectSizeOffsetVisitor, which works with
+/// \p APInts.
+class SizeOffsetAPInt : public SizeOffsetType<APInt, SizeOffsetAPInt> {
+  friend class SizeOffsetType;
+  static bool known(APInt V) { return V.getBitWidth() > 1; }
+
+public:
+  SizeOffsetAPInt() = default;
+  SizeOffsetAPInt(APInt Size, APInt Offset) : SizeOffsetType(Size, Offset) {}
+
+  bool knownSize() const { return SizeOffsetAPInt::known(Size); }
+  bool knownOffset() const { return SizeOffsetAPInt::known(Offset); }
----------------
jdoerfert wrote:

I thought this can move to the base class as well, no?

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


More information about the libc-commits mailing list