LLVM 20.0.0git
Classes | Macros | Enumerations | Functions | Variables
MemoryBuiltins.cpp File Reference
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/TargetFolder.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/Utils/Local.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdint>
#include <iterator>
#include <numeric>
#include <optional>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

struct  AllocFnsTy
 
struct  FreeFnsTy
 

Macros

#define DEBUG_TYPE   "memory-builtins"
 

Enumerations

enum  AllocType : uint8_t {
  OpNewLike = 1<<0 , MallocLike = 1<<1 , StrDupLike = 1<<2 , MallocOrOpNewLike = MallocLike | OpNewLike ,
  AllocLike = MallocOrOpNewLike | StrDupLike , AnyAlloc = AllocLike
}
 
enum class  MallocFamily {
  Malloc , CPPNew , CPPNewAligned , CPPNewArray ,
  CPPNewArrayAligned , MSVCNew , MSVCArrayNew , VecMalloc ,
  KmpcAllocShared
}
 

Functions

StringRef mangledNameForMallocFamily (const MallocFamily &Family)
 
static const FunctiongetCalledFunction (const Value *V)
 
static std::optional< AllocFnsTygetAllocationDataForFunction (const Function *Callee, AllocType AllocTy, const TargetLibraryInfo *TLI)
 Returns the allocation data for the given value if it's a call to a known allocation function.
 
static std::optional< AllocFnsTygetAllocationData (const Value *V, AllocType AllocTy, const TargetLibraryInfo *TLI)
 
static std::optional< AllocFnsTygetAllocationData (const Value *V, AllocType AllocTy, function_ref< const TargetLibraryInfo &(Function &)> GetTLI)
 
static std::optional< AllocFnsTygetAllocationSize (const CallBase *CB, const TargetLibraryInfo *TLI)
 
static AllocFnKind getAllocFnKind (const Value *V)
 
static AllocFnKind getAllocFnKind (const Function *F)
 
static bool checkFnAllocKind (const Value *V, AllocFnKind Wanted)
 
static bool checkFnAllocKind (const Function *F, AllocFnKind Wanted)
 
static bool CheckedZextOrTrunc (APInt &I, unsigned IntTyBits)
 When we're compiling N-bit code, and the user uses parameters that are greater than N bits (e.g.
 
std::optional< FreeFnsTygetFreeFunctionDataForFunction (const Function *Callee, const LibFunc TLIFn)
 
static APInt getSizeWithOverflow (const SizeOffsetAPInt &Data)
 
 STATISTIC (ObjectVisitorArgument, "Number of arguments with unsolved size and offset")
 
 STATISTIC (ObjectVisitorLoad, "Number of load instructions with unsolved size and offset")
 

Variables

static cl::opt< unsignedObjectSizeOffsetVisitorMaxVisitInstructions ("object-size-offset-visitor-max-visit-instructions", cl::desc("Maximum number of instructions for ObjectSizeOffsetVisitor to " "look at"), cl::init(100))
 
static const std::pair< LibFunc, AllocFnsTyAllocationFnData []
 
static const std::pair< LibFunc, FreeFnsTyFreeFnData []
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "memory-builtins"

Definition at line 52 of file MemoryBuiltins.cpp.

Enumeration Type Documentation

◆ AllocType

enum AllocType : uint8_t
Enumerator
OpNewLike 
MallocLike 
StrDupLike 
MallocOrOpNewLike 
AllocLike 
AnyAlloc 

Definition at line 60 of file MemoryBuiltins.cpp.

◆ MallocFamily

enum class MallocFamily
strong
Enumerator
Malloc 
CPPNew 
CPPNewAligned 
CPPNewArray 
CPPNewArrayAligned 
MSVCNew 
MSVCArrayNew 
VecMalloc 
KmpcAllocShared 

Definition at line 69 of file MemoryBuiltins.cpp.

Function Documentation

◆ CheckedZextOrTrunc()

static bool CheckedZextOrTrunc ( APInt I,
unsigned  IntTyBits 
)
static

When we're compiling N-bit code, and the user uses parameters that are greater than N bits (e.g.

uint64_t on a 32-bit build), we can run into trouble with APInt size issues. This function handles resizing + overflow checks for us. Check and zext or trunc I depending on IntTyBits and I's value.

Definition at line 356 of file MemoryBuiltins.cpp.

References I.

Referenced by llvm::getAllocSize().

◆ checkFnAllocKind() [1/2]

static bool checkFnAllocKind ( const Function F,
AllocFnKind  Wanted 
)
static

Definition at line 281 of file MemoryBuiltins.cpp.

References F, and getAllocFnKind().

◆ checkFnAllocKind() [2/2]

static bool checkFnAllocKind ( const Value V,
AllocFnKind  Wanted 
)
static

◆ getAllocationData() [1/2]

static std::optional< AllocFnsTy > getAllocationData ( const Value V,
AllocType  AllocTy,
const TargetLibraryInfo TLI 
)
static

◆ getAllocationData() [2/2]

static std::optional< AllocFnsTy > getAllocationData ( const Value V,
AllocType  AllocTy,
function_ref< const TargetLibraryInfo &(Function &)>  GetTLI 
)
static

Definition at line 228 of file MemoryBuiltins.cpp.

References getAllocationDataForFunction(), and getCalledFunction().

◆ getAllocationDataForFunction()

static std::optional< AllocFnsTy > getAllocationDataForFunction ( const Function Callee,
AllocType  AllocTy,
const TargetLibraryInfo TLI 
)
static

Returns the allocation data for the given value if it's a call to a known allocation function.

Definition at line 178 of file MemoryBuiltins.cpp.

References AllocationFnData, AllocFnsTy::AllocTy, llvm::find_if(), AllocFnsTy::FstParam, llvm::TargetLibraryInfo::getLibFunc(), llvm::TargetLibraryInfo::has(), AllocFnsTy::NumParams, P, and AllocFnsTy::SndParam.

Referenced by getAllocationData(), llvm::getAllocationFamily(), and getAllocationSize().

◆ getAllocationSize()

static std::optional< AllocFnsTy > getAllocationSize ( const CallBase CB,
const TargetLibraryInfo TLI 
)
static

◆ getAllocFnKind() [1/2]

static AllocFnKind getAllocFnKind ( const Function F)
static

Definition at line 273 of file MemoryBuiltins.cpp.

References F.

◆ getAllocFnKind() [2/2]

static AllocFnKind getAllocFnKind ( const Value V)
static

◆ getCalledFunction()

static const Function * getCalledFunction ( const Value V)
static

◆ getFreeFunctionDataForFunction()

std::optional< FreeFnsTy > getFreeFunctionDataForFunction ( const Function Callee,
const LibFunc  TLIFn 
)

Definition at line 489 of file MemoryBuiltins.cpp.

References llvm::find_if(), FreeFnData, and P.

Referenced by llvm::getAllocationFamily(), and llvm::isLibFreeFunction().

◆ getSizeWithOverflow()

static APInt getSizeWithOverflow ( const SizeOffsetAPInt Data)
static

Definition at line 565 of file MemoryBuiltins.cpp.

References llvm::Offset, and Size.

Referenced by llvm::getObjectSize().

◆ mangledNameForMallocFamily()

StringRef mangledNameForMallocFamily ( const MallocFamily Family)

◆ STATISTIC() [1/2]

STATISTIC ( ObjectVisitorArgument  ,
"Number of arguments with unsolved size and offset"   
)

◆ STATISTIC() [2/2]

STATISTIC ( ObjectVisitorLoad  ,
"Number of load instructions with unsolved size and offset"   
)

Variable Documentation

◆ AllocationFnData

const std::pair<LibFunc, AllocFnsTy> AllocationFnData[]
static

Definition at line 119 of file MemoryBuiltins.cpp.

Referenced by getAllocationDataForFunction().

◆ FreeFnData

const std::pair<LibFunc, FreeFnsTy> FreeFnData[]
static

Definition at line 456 of file MemoryBuiltins.cpp.

Referenced by getFreeFunctionDataForFunction().

◆ ObjectSizeOffsetVisitorMaxVisitInstructions

cl::opt< unsigned > ObjectSizeOffsetVisitorMaxVisitInstructions("object-size-offset-visitor-max-visit-instructions", cl::desc("Maximum number of instructions for ObjectSizeOffsetVisitor to " "look at"), cl::init(100)) ( "object-size-offset-visitor-max-visit-instructions"  ,
cl::desc("Maximum number of instructions for ObjectSizeOffsetVisitor to " "look at")  ,
cl::init(100)   
)
static