Add malloc call utility functions. Patch by Victor Hernandez.
[oota-llvm.git] / include / llvm / Analysis / MallocHelper.h
1 //===- llvm/Analysis/MallocHelper.h ---- Identify malloc calls --*- 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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_MALLOCHELPER_H
16 #define LLVM_ANALYSIS_MALLOCHELPER_H
17
18 namespace llvm {
19 class BitCastInst;
20 class CallInst;
21 class Instruction;
22 class PointerType;
23 class Twine;
24 class Type;
25 class Value;
26
27 //===----------------------------------------------------------------------===//
28 //  malloc Call Utility Functions.
29 //
30
31 /// isMalloc - Returns true if the the value is either a malloc call or a
32 /// bitcast of the result of a malloc call
33 bool isMalloc(const Value* I);
34 bool isMalloc(Value* I);
35
36 /// extractMallocCall - Returns the corresponding CallInst if the instruction
37 /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
38 /// ignore InvokeInst here.
39 const CallInst* extractMallocCall(const Value* I);
40 CallInst* extractMallocCall(Value* I);
41
42 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
43 /// instruction is a bitcast of the result of a malloc call.
44 const CallInst* extractMallocCallFromBitCast(const Value* I);
45 CallInst* extractMallocCallFromBitCast(Value* I);
46
47 /// isArrayMalloc - Returns the corresponding CallInst if the instruction 
48 /// matches the malloc call IR generated by CallInst::CreateMalloc().  This 
49 /// means that it is a malloc call with one bitcast use AND the malloc call's 
50 /// size argument is:
51 ///  1. a constant not equal to the malloc's allocated type
52 /// or
53 ///  2. the result of a multiplication by the malloc's allocated type
54 /// Otherwise it returns NULL.
55 /// The unique bitcast is needed to determine the type/size of the array
56 /// allocation.
57 CallInst* isArrayMalloc(Value* I);
58 const CallInst* isArrayMalloc(const Value* I);
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.  The array
71 /// size is computated in 1 of 3 ways:
72 ///  1. If the element type if 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 /// This function returns constant 1 if:
79 ///  1. The malloc call's allocated type cannot be determined.
80 ///  2. IR wasn't created by a call to CallInst::CreateMalloc() with a non-NULL
81 ///     ArraySize.
82 Value* getMallocArraySize(CallInst* CI);
83
84 } // End llvm namespace
85
86 #endif