LLVM  15.0.0git
Classes | Macros | Enumerations | Functions | Variables
MemoryBuiltins.cpp File Reference
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.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/Debug.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdint>
#include <iterator>
#include <numeric>
#include <type_traits>
#include <utility>
Include dependency graph for MemoryBuiltins.cpp:

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, AlignedAllocLike = 1<<2, CallocLike = 1<<3,
  ReallocLike = 1<<4, StrDupLike = 1<<5, MallocOrOpNewLike = MallocLike | OpNewLike, MallocOrCallocLike = MallocLike | OpNewLike | CallocLike | AlignedAllocLike,
  AllocLike = MallocOrCallocLike | StrDupLike, AnyAlloc = AllocLike | ReallocLike
}
 
enum  MallocFamily {
  MallocFamily::Malloc, MallocFamily::CPPNew, MallocFamily::CPPNewAligned, MallocFamily::CPPNewArray,
  MallocFamily::CPPNewArrayAligned, MallocFamily::MSVCNew, MallocFamily::MSVCArrayNew, MallocFamily::VecMalloc,
  MallocFamily::KmpcAllocShared
}
 

Functions

StringRef mangledNameForMallocFamily (const MallocFamily &Family)
 
static const FunctiongetCalledFunction (const Value *V, bool &IsNoBuiltin)
 
static 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. More...
 
static Optional< AllocFnsTygetAllocationData (const Value *V, AllocType AllocTy, const TargetLibraryInfo *TLI)
 
static Optional< AllocFnsTygetAllocationData (const Value *V, AllocType AllocTy, function_ref< const TargetLibraryInfo &(Function &)> GetTLI)
 
static Optional< AllocFnsTygetAllocationSize (const Value *V, const TargetLibraryInfo *TLI)
 
static bool isMallocLikeFn (const Value *V, const TargetLibraryInfo *TLI)
 Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such as malloc). More...
 
static bool isAlignedAllocLikeFn (const Value *V, const TargetLibraryInfo *TLI)
 Tests if a value is a call or invoke to a library function that allocates uninitialized memory with alignment (such as aligned_alloc). More...
 
static bool isCallocLikeFn (const Value *V, const TargetLibraryInfo *TLI)
 Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as calloc). More...
 
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. More...
 
Optional< FreeFnsTygetFreeFunctionDataForFunction (const Function *Callee, const LibFunc TLIFn)
 
static APInt getSizeWithOverflow (const SizeOffsetType &Data)
 
 STATISTIC (ObjectVisitorArgument, "Number of arguments with unsolved size and offset")
 
 STATISTIC (ObjectVisitorLoad, "Number of load instructions with unsolved size and offset")
 

Variables

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 
AlignedAllocLike 
CallocLike 
ReallocLike 
StrDupLike 
MallocOrOpNewLike 
MallocOrCallocLike 
AllocLike 
AnyAlloc 

Definition at line 54 of file MemoryBuiltins.cpp.

◆ MallocFamily

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

Definition at line 67 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 348 of file MemoryBuiltins.cpp.

References I.

Referenced by llvm::getAllocSize().

◆ getAllocationData() [1/2]

static Optional<AllocFnsTy> getAllocationData ( const Value V,
AllocType  AllocTy,
const TargetLibraryInfo TLI 
)
static

◆ getAllocationData() [2/2]

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

◆ getAllocationDataForFunction()

static Optional<AllocFnsTy> getAllocationDataForFunction ( const Function Callee,
AllocType  AllocTy,
const TargetLibraryInfo TLI 
)
static

◆ getAllocationSize()

static Optional<AllocFnsTy> getAllocationSize ( const Value V,
const TargetLibraryInfo TLI 
)
static

◆ getCalledFunction()

static const Function* getCalledFunction ( const Value V,
bool &  IsNoBuiltin 
)
static

◆ getFreeFunctionDataForFunction()

Optional<FreeFnsTy> getFreeFunctionDataForFunction ( const Function Callee,
const LibFunc  TLIFn 
)

◆ getSizeWithOverflow()

static APInt getSizeWithOverflow ( const SizeOffsetType Data)
static

Definition at line 549 of file MemoryBuiltins.cpp.

References llvm::Data.

Referenced by llvm::getObjectSize().

◆ isAlignedAllocLikeFn()

static bool isAlignedAllocLikeFn ( const Value V,
const TargetLibraryInfo TLI 
)
static

Tests if a value is a call or invoke to a library function that allocates uninitialized memory with alignment (such as aligned_alloc).

Definition at line 288 of file MemoryBuiltins.cpp.

References AlignedAllocLike, and getAllocationData().

Referenced by llvm::getInitialValueOfAllocation().

◆ isCallocLikeFn()

static bool isCallocLikeFn ( const Value V,
const TargetLibraryInfo TLI 
)
static

Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as calloc).

Definition at line 294 of file MemoryBuiltins.cpp.

References CallocLike, and getAllocationData().

Referenced by llvm::getInitialValueOfAllocation().

◆ isMallocLikeFn()

static bool isMallocLikeFn ( const Value V,
const TargetLibraryInfo TLI 
)
static

Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such as malloc).

Definition at line 282 of file MemoryBuiltins.cpp.

References getAllocationData(), and MallocOrOpNewLike.

Referenced by llvm::getInitialValueOfAllocation().

◆ 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 117 of file MemoryBuiltins.cpp.

Referenced by getAllocationDataForFunction().

◆ FreeFnData

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

Definition at line 447 of file MemoryBuiltins.cpp.

Referenced by getFreeFunctionDataForFunction().