MemoryBuiltins.h revision 218893
1231650Sluigi//===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===//
2262153Sluigi//
3262153Sluigi//                     The LLVM Compiler Infrastructure
4231650Sluigi//
5262153Sluigi// This file is distributed under the University of Illinois Open Source
6262153Sluigi// License. See LICENSE.TXT for details.
7262153Sluigi//
8231650Sluigi//===----------------------------------------------------------------------===//
9231650Sluigi//
10231650Sluigi// This family of functions identifies calls to builtin functions that allocate
11231650Sluigi// or free memory.
12262153Sluigi//
13262153Sluigi//===----------------------------------------------------------------------===//
14262153Sluigi
15231650Sluigi#ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
16262153Sluigi#define LLVM_ANALYSIS_MEMORYBUILTINS_H
17262153Sluigi
18262153Sluiginamespace llvm {
19262153Sluigiclass CallInst;
20262153Sluigiclass PointerType;
21262153Sluigiclass TargetData;
22262153Sluigiclass Type;
23262153Sluigiclass Value;
24262153Sluigi
25231650Sluigi//===----------------------------------------------------------------------===//
26231650Sluigi//  malloc Call Utility Functions.
27231650Sluigi//
28231650Sluigi
29231650Sluigi/// isMalloc - Returns true if the value is either a malloc call or a bitcast of
30262153Sluigi/// the result of a malloc call
31262153Sluigibool isMalloc(const Value *I);
32231650Sluigi
33235549Sluigi/// extractMallocCall - Returns the corresponding CallInst if the instruction
34235549Sluigi/// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
35231650Sluigi/// ignore InvokeInst here.
36231650Sluigiconst CallInst *extractMallocCall(const Value *I);
37231650SluigiCallInst *extractMallocCall(Value *I);
38231650Sluigi
39231650Sluigi/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
40231650Sluigi/// instruction is a bitcast of the result of a malloc call.
41231650Sluigiconst CallInst *extractMallocCallFromBitCast(const Value *I);
42231650SluigiCallInst *extractMallocCallFromBitCast(Value *I);
43231650Sluigi
44231650Sluigi/// isArrayMalloc - Returns the corresponding CallInst if the instruction
45231650Sluigi/// is a call to malloc whose array size can be determined and the array size
46262153Sluigi/// is not constant 1.  Otherwise, return NULL.
47231650Sluigiconst CallInst *isArrayMalloc(const Value *I, const TargetData *TD);
48257768Sluigi
49257768Sluigi/// getMallocType - Returns the PointerType resulting from the malloc call.
50231650Sluigi/// The PointerType depends on the number of bitcast uses of the malloc call:
51262153Sluigi///   0: PointerType is the malloc calls' return type.
52262153Sluigi///   1: PointerType is the bitcast's result type.
53262153Sluigi///  >1: Unique PointerType cannot be determined, return NULL.
54262153Sluigiconst PointerType *getMallocType(const CallInst *CI);
55262153Sluigi
56262153Sluigi/// getMallocAllocatedType - Returns the Type allocated by malloc call.
57262153Sluigi/// The Type depends on the number of bitcast uses of the malloc call:
58262153Sluigi///   0: PointerType is the malloc calls' return type.
59262153Sluigi///   1: PointerType is the bitcast's result type.
60262153Sluigi///  >1: Unique PointerType cannot be determined, return NULL.
61262153Sluigiconst Type *getMallocAllocatedType(const CallInst *CI);
62262153Sluigi
63231650Sluigi/// getMallocArraySize - Returns the array size of a malloc call.  If the
64231650Sluigi/// argument passed to malloc is a multiple of the size of the malloced type,
65231650Sluigi/// then return that multiple.  For non-array mallocs, the multiple is
66231650Sluigi/// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
67231650Sluigi/// determined.
68262153SluigiValue *getMallocArraySize(CallInst *CI, const TargetData *TD,
69262153Sluigi                          bool LookThroughSExt = false);
70262153Sluigi
71262153Sluigi//===----------------------------------------------------------------------===//
72262153Sluigi//  free Call Utility Functions.
73262153Sluigi//
74262153Sluigi
75262153Sluigi/// isFreeCall - Returns non-null if the value is a call to the builtin free()
76262153Sluigiconst CallInst *isFreeCall(const Value *I);
77262153Sluigi
78262153Sluigistatic inline CallInst *isFreeCall(Value *I) {
79262153Sluigi  return const_cast<CallInst*>(isFreeCall((const Value*)I));
80246355Sluigi}
81246355Sluigi
82231650Sluigi} // End llvm namespace
83262153Sluigi
84231650Sluigi#endif
85246355Sluigi