[llvm] a3fb234 - [BasicAA] Return std::optional from getObjectSize() (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 05:56:48 PDT 2023


Author: Nikita Popov
Date: 2023-10-23T14:56:39+02:00
New Revision: a3fb2348b19167dfb027e20f8e1306ebbf31cfbf

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

LOG: [BasicAA] Return std::optional from getObjectSize() (NFC)

Prefer this over UnknownSize, especially once the return value
switches to something other than uint64_t.

Added: 
    

Modified: 
    llvm/lib/Analysis/BasicAliasAnalysis.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 3e5997081647749..b51659001295716 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -101,17 +101,18 @@ bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
 //===----------------------------------------------------------------------===//
 
 /// Returns the size of the object specified by V or UnknownSize if unknown.
-static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
-                              const TargetLibraryInfo &TLI,
-                              bool NullIsValidLoc,
-                              bool RoundToAlign = false) {
+static std::optional<uint64_t> getObjectSize(const Value *V,
+                                             const DataLayout &DL,
+                                             const TargetLibraryInfo &TLI,
+                                             bool NullIsValidLoc,
+                                             bool RoundToAlign = false) {
   uint64_t Size;
   ObjectSizeOpts Opts;
   Opts.RoundToAlign = RoundToAlign;
   Opts.NullIsUnknownSize = NullIsValidLoc;
   if (getObjectSize(V, Size, DL, &TLI, Opts))
     return Size;
-  return MemoryLocation::UnknownSize;
+  return std::nullopt;
 }
 
 /// Returns true if we can prove that the object specified by V is smaller than
@@ -151,10 +152,10 @@ static bool isObjectSmallerThan(const Value *V, uint64_t Size,
 
   // This function needs to use the aligned object size because we allow
   // reads a bit past the end given sufficient alignment.
-  uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
-                                      /*RoundToAlign*/ true);
+  std::optional<uint64_t> ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
+                                                     /*RoundToAlign*/ true);
 
-  return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
+  return ObjectSize && *ObjectSize < Size;
 }
 
 /// Return the minimal extent from \p V to the end of the underlying object,
@@ -182,8 +183,9 @@ static uint64_t getMinimalExtentFrom(const Value &V,
 /// Returns true if we can prove that the object specified by V has size Size.
 static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
                          const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
-  uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
-  return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
+  std::optional<uint64_t> ObjectSize =
+      getObjectSize(V, DL, TLI, NullIsValidLoc);
+  return ObjectSize && *ObjectSize == Size;
 }
 
 //===----------------------------------------------------------------------===//


        


More information about the llvm-commits mailing list