[MemoryBuiltins] Default to trivial mapper in getAllocSize() (NFC)
authorNikita Popov <npopov@redhat.com>
Thu, 21 Jul 2022 09:40:35 +0000 (11:40 +0200)
committerNikita Popov <npopov@redhat.com>
Thu, 21 Jul 2022 09:43:48 +0000 (11:43 +0200)
Default getAllocSize() to use the trivial mapper. Also switch
from using std::function to function_ref.

Furthermore, update the doc comment to point out a subtle difference
between getAllocSize() and getObjectSize(): The latter may also
return something for calls that return their argument (via "returned"
attribute or special intrinsics like invariant groups).

llvm/include/llvm/Analysis/MemoryBuiltins.h
llvm/lib/Analysis/MemoryBuiltins.cpp

index b0ff7f969484aa599188f517bfc4cdab9348e102..457a9d330ae24a589b8f4d020b9bf51a4509204f 100644 (file)
@@ -108,13 +108,16 @@ bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI);
 /// the definition of the allocalign attribute.
 Value *getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI);
 
-/// Return the size of the requested allocation.  With a trivial mapper, this is
-/// identical to calling getObjectSize(..., Exact).  A mapper function can be
-/// used to replace one Value* (operand to the allocation) with another.  This
-/// is useful when doing abstract interpretation.
-Optional<APInt> getAllocSize(const CallBase *CB,
-                             const TargetLibraryInfo *TLI,
-                             std::function<const Value*(const Value*)> Mapper);
+/// Return the size of the requested allocation. With a trivial mapper, this is
+/// similar to calling getObjectSize(..., Exact), but without looking through
+/// calls that return their argument. A mapper function can be used to replace
+/// one Value* (operand to the allocation) with another. This is useful when
+/// doing abstract interpretation.
+Optional<APInt> getAllocSize(
+    const CallBase *CB, const TargetLibraryInfo *TLI,
+    function_ref<const Value *(const Value *)> Mapper = [](const Value *V) {
+      return V;
+    });
 
 /// If this is a call to an allocation function that initializes memory to a
 /// fixed value, return said value in the requested type.  Otherwise, return
index 50256a2629d4c079d7fdddaa68072a97032cc1ef..bf81d3b4dbe1f4acda4fbdaa94195011f9da8d1f 100644 (file)
@@ -361,9 +361,8 @@ static bool CheckedZextOrTrunc(APInt &I, unsigned IntTyBits) {
 }
 
 Optional<APInt>
-llvm::getAllocSize(const CallBase *CB,
-                   const TargetLibraryInfo *TLI,
-                   std::function<const Value*(const Value*)> Mapper) {
+llvm::getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI,
+                   function_ref<const Value *(const Value *)> Mapper) {
   // Note: This handles both explicitly listed allocation functions and
   // allocsize.  The code structure could stand to be cleaned up a bit.
   Optional<AllocFnsTy> FnData = getAllocationSize(CB, TLI);
@@ -769,8 +768,7 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
 }
 
 SizeOffsetType ObjectSizeOffsetVisitor::visitCallBase(CallBase &CB) {
-  auto Mapper = [](const Value *V) { return V; };
-  if (Optional<APInt> Size = getAllocSize(&CB, TLI, Mapper))
+  if (Optional<APInt> Size = getAllocSize(&CB, TLI))
     return std::make_pair(*Size, Zero);
   return unknown();
 }