2009-10-27 21:05:49 +01:00
|
|
|
//===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===//
|
2009-09-10 06:36:43 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-10-27 21:05:49 +01:00
|
|
|
// This family of functions identifies calls to builtin functions that allocate
|
2013-07-23 01:52:23 +02:00
|
|
|
// or free memory.
|
2009-09-10 06:36:43 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-27 21:05:49 +01:00
|
|
|
#ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
|
|
|
|
#define LLVM_ANALYSIS_MEMORYBUILTINS_H
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-03-04 12:59:06 +01:00
|
|
|
#include "llvm/Analysis/TargetFolder.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-06 04:23:41 +01:00
|
|
|
#include "llvm/IR/InstVisitor.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Operator.h"
|
2014-03-04 12:17:44 +01:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2012-06-21 17:45:28 +02:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
|
2009-09-10 06:36:43 +02:00
|
|
|
namespace llvm {
|
|
|
|
class CallInst;
|
|
|
|
class PointerType;
|
2012-10-08 18:38:25 +02:00
|
|
|
class DataLayout;
|
2012-08-29 17:32:21 +02:00
|
|
|
class TargetLibraryInfo;
|
2009-09-10 06:36:43 +02:00
|
|
|
class Type;
|
|
|
|
class Value;
|
|
|
|
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
|
|
|
|
/// like).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a function that returns a
|
2012-06-28 18:34:03 +02:00
|
|
|
/// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// allocates uninitialized memory (such as malloc).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// allocates zero-filled memory (such as calloc).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// allocates memory (either malloc, calloc, or strdup like).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-06-21 23:25:05 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// reallocates memory (such as realloc).
|
2012-08-29 17:32:21 +02:00
|
|
|
bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2013-09-24 18:37:51 +02:00
|
|
|
/// \brief Tests if a value is a call or invoke to a library function that
|
|
|
|
/// allocates memory and never returns null (such as operator new).
|
|
|
|
bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
|
|
|
|
bool LookThroughBitCast = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2009-09-10 06:36:43 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// malloc Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// extractMallocCall - Returns the corresponding CallInst if the instruction
|
|
|
|
/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
|
|
|
|
/// ignore InvokeInst here.
|
2012-08-29 17:32:21 +02:00
|
|
|
const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
|
|
|
|
static inline CallInst *extractMallocCall(Value *I,
|
|
|
|
const TargetLibraryInfo *TLI) {
|
|
|
|
return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
|
2012-06-21 17:45:28 +02:00
|
|
|
}
|
2009-09-10 06:36:43 +02:00
|
|
|
|
|
|
|
/// getMallocType - Returns the PointerType resulting from the malloc call.
|
2009-11-07 01:16:28 +01:00
|
|
|
/// The PointerType depends on the number of bitcast uses of the malloc call:
|
|
|
|
/// 0: PointerType is the malloc calls' return type.
|
|
|
|
/// 1: PointerType is the bitcast's result type.
|
|
|
|
/// >1: Unique PointerType cannot be determined, return NULL.
|
2012-08-29 17:32:21 +02:00
|
|
|
PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2009-11-07 01:16:28 +01:00
|
|
|
/// getMallocAllocatedType - Returns the Type allocated by malloc call.
|
|
|
|
/// The Type depends on the number of bitcast uses of the malloc call:
|
|
|
|
/// 0: PointerType is the malloc calls' return type.
|
|
|
|
/// 1: PointerType is the bitcast's result type.
|
|
|
|
/// >1: Unique PointerType cannot be determined, return NULL.
|
2012-08-29 17:32:21 +02:00
|
|
|
Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2013-07-23 01:52:23 +02:00
|
|
|
/// getMallocArraySize - Returns the array size of a malloc call. If the
|
2009-10-28 21:18:55 +01:00
|
|
|
/// argument passed to malloc is a multiple of the size of the malloced type,
|
|
|
|
/// then return that multiple. For non-array mallocs, the multiple is
|
|
|
|
/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
|
2009-10-15 22:14:52 +02:00
|
|
|
/// determined.
|
2015-03-10 03:37:25 +01:00
|
|
|
Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
|
2012-08-29 17:32:21 +02:00
|
|
|
const TargetLibraryInfo *TLI,
|
2009-11-10 09:32:25 +01:00
|
|
|
bool LookThroughSExt = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
2012-05-03 23:19:58 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// calloc Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// extractCallocCall - Returns the corresponding CallInst if the instruction
|
|
|
|
/// is a calloc call.
|
2012-08-29 17:32:21 +02:00
|
|
|
const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
|
|
|
|
static inline CallInst *extractCallocCall(Value *I,
|
|
|
|
const TargetLibraryInfo *TLI) {
|
|
|
|
return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
|
2012-06-21 17:45:28 +02:00
|
|
|
}
|
2012-05-03 23:19:58 +02:00
|
|
|
|
|
|
|
|
2009-10-24 06:23:03 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// free Call Utility Functions.
|
|
|
|
//
|
|
|
|
|
2010-06-23 23:51:12 +02:00
|
|
|
/// isFreeCall - Returns non-null if the value is a call to the builtin free()
|
2012-08-29 17:32:21 +02:00
|
|
|
const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
|
2013-07-23 01:52:23 +02:00
|
|
|
|
2012-08-29 17:32:21 +02:00
|
|
|
static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
|
|
|
|
return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
|
2010-11-30 02:28:33 +01:00
|
|
|
}
|
2009-09-10 06:36:43 +02:00
|
|
|
|
2013-07-23 01:52:23 +02:00
|
|
|
|
2012-06-21 17:45:28 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions to compute size of objects.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// \brief Compute the size of the object pointed by Ptr. Returns true and the
|
2013-03-05 20:04:12 +01:00
|
|
|
/// object size in Size if successful, and false otherwise. In this context, by
|
|
|
|
/// object we mean the region of memory starting at Ptr to the end of the
|
|
|
|
/// underlying object pointed to by Ptr.
|
2012-06-21 17:45:28 +02:00
|
|
|
/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
|
|
|
|
/// byval arguments, and global variables.
|
2015-03-10 03:37:25 +01:00
|
|
|
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
|
2012-08-29 17:32:21 +02:00
|
|
|
const TargetLibraryInfo *TLI, bool RoundToAlign = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
|
|
|
typedef std::pair<APInt, APInt> SizeOffsetType;
|
|
|
|
|
2013-07-23 01:52:23 +02:00
|
|
|
/// \brief Evaluate the size and offset of an object pointed to by a Value*
|
2012-06-21 17:45:28 +02:00
|
|
|
/// statically. Fails if size or offset are not known at compile time.
|
|
|
|
class ObjectSizeOffsetVisitor
|
|
|
|
: public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
|
|
|
|
|
2015-03-10 03:37:25 +01:00
|
|
|
const DataLayout &DL;
|
2012-08-29 17:32:21 +02:00
|
|
|
const TargetLibraryInfo *TLI;
|
2012-06-21 17:45:28 +02:00
|
|
|
bool RoundToAlign;
|
|
|
|
unsigned IntTyBits;
|
|
|
|
APInt Zero;
|
2013-04-09 20:16:05 +02:00
|
|
|
SmallPtrSet<Instruction *, 8> SeenInsts;
|
2012-06-21 17:45:28 +02:00
|
|
|
|
|
|
|
APInt align(APInt Size, uint64_t Align);
|
|
|
|
|
|
|
|
SizeOffsetType unknown() {
|
|
|
|
return std::make_pair(APInt(), APInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-03-10 03:37:25 +01:00
|
|
|
ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
|
2012-11-01 09:07:29 +01:00
|
|
|
LLVMContext &Context, bool RoundToAlign = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
|
|
|
|
SizeOffsetType compute(Value *V);
|
|
|
|
|
|
|
|
bool knownSize(SizeOffsetType &SizeOffset) {
|
|
|
|
return SizeOffset.first.getBitWidth() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool knownOffset(SizeOffsetType &SizeOffset) {
|
|
|
|
return SizeOffset.second.getBitWidth() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bothKnown(SizeOffsetType &SizeOffset) {
|
|
|
|
return knownSize(SizeOffset) && knownOffset(SizeOffset);
|
|
|
|
}
|
|
|
|
|
2013-12-14 01:27:48 +01:00
|
|
|
// These are "private", except they can't actually be made private. Only
|
|
|
|
// compute() should be used by external users.
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetType visitAllocaInst(AllocaInst &I);
|
|
|
|
SizeOffsetType visitArgument(Argument &A);
|
|
|
|
SizeOffsetType visitCallSite(CallSite CS);
|
|
|
|
SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
|
2012-06-28 18:34:03 +02:00
|
|
|
SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
|
|
|
|
SizeOffsetType visitGEPOperator(GEPOperator &GEP);
|
2012-12-31 17:23:48 +01:00
|
|
|
SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
|
|
|
|
SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
|
|
|
|
SizeOffsetType visitLoadInst(LoadInst &I);
|
|
|
|
SizeOffsetType visitPHINode(PHINode&);
|
|
|
|
SizeOffsetType visitSelectInst(SelectInst &I);
|
|
|
|
SizeOffsetType visitUndefValue(UndefValue&);
|
|
|
|
SizeOffsetType visitInstruction(Instruction &I);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<Value*, Value*> SizeOffsetEvalType;
|
|
|
|
|
|
|
|
|
2013-07-23 01:52:23 +02:00
|
|
|
/// \brief Evaluate the size and offset of an object pointed to by a Value*.
|
2012-06-21 17:45:28 +02:00
|
|
|
/// May create code to compute the result at run-time.
|
|
|
|
class ObjectSizeOffsetEvaluator
|
|
|
|
: public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
|
|
|
|
|
2012-06-21 18:58:41 +02:00
|
|
|
typedef IRBuilder<true, TargetFolder> BuilderTy;
|
2012-07-03 19:13:25 +02:00
|
|
|
typedef std::pair<WeakVH, WeakVH> WeakEvalType;
|
|
|
|
typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
|
2012-06-21 18:58:41 +02:00
|
|
|
typedef SmallPtrSet<const Value*, 8> PtrSetTy;
|
|
|
|
|
2015-03-10 03:37:25 +01:00
|
|
|
const DataLayout &DL;
|
2012-08-29 17:32:21 +02:00
|
|
|
const TargetLibraryInfo *TLI;
|
2012-06-21 17:45:28 +02:00
|
|
|
LLVMContext &Context;
|
|
|
|
BuilderTy Builder;
|
|
|
|
IntegerType *IntTy;
|
|
|
|
Value *Zero;
|
|
|
|
CacheMapTy CacheMap;
|
|
|
|
PtrSetTy SeenVals;
|
2013-10-24 11:17:24 +02:00
|
|
|
bool RoundToAlign;
|
2012-06-21 17:45:28 +02:00
|
|
|
|
|
|
|
SizeOffsetEvalType unknown() {
|
2014-04-15 06:59:12 +02:00
|
|
|
return std::make_pair(nullptr, nullptr);
|
2012-06-21 17:45:28 +02:00
|
|
|
}
|
|
|
|
SizeOffsetEvalType compute_(Value *V);
|
|
|
|
|
|
|
|
public:
|
2015-03-10 03:37:25 +01:00
|
|
|
ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
|
2013-10-24 11:17:24 +02:00
|
|
|
LLVMContext &Context, bool RoundToAlign = false);
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetEvalType compute(Value *V);
|
|
|
|
|
2012-07-03 19:13:25 +02:00
|
|
|
bool knownSize(SizeOffsetEvalType SizeOffset) {
|
2012-06-21 17:45:28 +02:00
|
|
|
return SizeOffset.first;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:13:25 +02:00
|
|
|
bool knownOffset(SizeOffsetEvalType SizeOffset) {
|
2012-06-21 17:45:28 +02:00
|
|
|
return SizeOffset.second;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:13:25 +02:00
|
|
|
bool anyKnown(SizeOffsetEvalType SizeOffset) {
|
2012-06-21 17:45:28 +02:00
|
|
|
return knownSize(SizeOffset) || knownOffset(SizeOffset);
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:13:25 +02:00
|
|
|
bool bothKnown(SizeOffsetEvalType SizeOffset) {
|
2012-06-21 17:45:28 +02:00
|
|
|
return knownSize(SizeOffset) && knownOffset(SizeOffset);
|
|
|
|
}
|
|
|
|
|
2013-12-14 01:27:48 +01:00
|
|
|
// The individual instruction visitors should be treated as private.
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
|
|
|
|
SizeOffsetEvalType visitCallSite(CallSite CS);
|
2012-06-28 18:34:03 +02:00
|
|
|
SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
|
|
|
|
SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
|
2012-06-21 17:45:28 +02:00
|
|
|
SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
|
|
|
|
SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
|
|
|
|
SizeOffsetEvalType visitLoadInst(LoadInst &I);
|
|
|
|
SizeOffsetEvalType visitPHINode(PHINode &PHI);
|
|
|
|
SizeOffsetEvalType visitSelectInst(SelectInst &I);
|
|
|
|
SizeOffsetEvalType visitInstruction(Instruction &I);
|
|
|
|
};
|
|
|
|
|
2009-09-10 06:36:43 +02:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|