Remove FreeInst.
[oota-llvm.git] / include / llvm / Analysis / MallocHelper.h
1 //===- llvm/Analysis/MallocFreeHelper.h - Identify malloc/free --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions identifies calls to malloc, bitcasts of malloc
11 // calls, and the types and array sizes associated with them.  It also
12 // identifies calls to the free builtin.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ANALYSIS_MALLOCHELPER_H
17 #define LLVM_ANALYSIS_MALLOCHELPER_H
18
19 namespace llvm {
20 class CallInst;
21 class LLVMContext;
22 class PointerType;
23 class TargetData;
24 class Type;
25 class Value;
26
27 //===----------------------------------------------------------------------===//
28 //  malloc Call Utility Functions.
29 //
30
31 /// isMalloc - Returns true if the value is either a malloc call or a bitcast of 
32 /// the result of a malloc call
33 bool isMalloc(const Value* I);
34
35 /// extractMallocCall - Returns the corresponding CallInst if the instruction
36 /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
37 /// ignore InvokeInst here.
38 const CallInst* extractMallocCall(const Value* I);
39 CallInst* extractMallocCall(Value* I);
40
41 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
42 /// instruction is a bitcast of the result of a malloc call.
43 const CallInst* extractMallocCallFromBitCast(const Value* I);
44 CallInst* extractMallocCallFromBitCast(Value* I);
45
46 /// isArrayMalloc - Returns the corresponding CallInst if the instruction 
47 /// matches the malloc call IR generated by CallInst::CreateMalloc().  This 
48 /// means that it is a malloc call with one bitcast use AND the malloc call's 
49 /// size argument is:
50 ///  1. a constant not equal to the size of the malloced type
51 /// or
52 ///  2. the result of a multiplication by the size of the malloced type
53 /// Otherwise it returns NULL.
54 /// The unique bitcast is needed to determine the type/size of the array
55 /// allocation.
56 CallInst* isArrayMalloc(Value* I, LLVMContext &Context, const TargetData* TD);
57 const CallInst* isArrayMalloc(const Value* I, LLVMContext &Context,
58                               const TargetData* TD);
59
60 /// getMallocType - Returns the PointerType resulting from the malloc call.
61 /// This PointerType is the result type of the call's only bitcast use.
62 /// If there is no unique bitcast use, then return NULL.
63 const PointerType* getMallocType(const CallInst* CI);
64
65 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
66 /// Type is the result type of the call's only bitcast use. If there is no
67 /// unique bitcast use, then return NULL.
68 const Type* getMallocAllocatedType(const CallInst* CI);
69
70 /// getMallocArraySize - Returns the array size of a malloc call.  For array
71 /// mallocs, the size is computated in 1 of 3 ways:
72 ///  1. If the element type is of size 1, then array size is the argument to 
73 ///     malloc.
74 ///  2. Else if the malloc's argument is a constant, the array size is that
75 ///     argument divided by the element type's size.
76 ///  3. Else the malloc argument must be a multiplication and the array size is
77 ///     the first operand of the multiplication.
78 /// For non-array mallocs, the computed size is constant 1. 
79 /// This function returns NULL for all mallocs whose array size cannot be
80 /// determined.
81 Value* getMallocArraySize(CallInst* CI, LLVMContext &Context,
82                           const TargetData* TD);
83                           
84 //===----------------------------------------------------------------------===//
85 //  free Call Utility Functions.
86 //
87
88 /// isFreeCall - Returns true if the the value is a call to the builtin free()
89 bool isFreeCall(const Value* I);
90
91 } // End llvm namespace
92
93 #endif