1 //===- llvm/Analysis/MallocHelper.h ---- Identify malloc calls --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to malloc, bitcasts of malloc
11 // calls, and the types and array sizes associated with them.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_MALLOCHELPER_H
16 #define LLVM_ANALYSIS_MALLOCHELPER_H
27 //===----------------------------------------------------------------------===//
28 // malloc Call Utility Functions.
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);
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);
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);
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
51 /// 1. a constant not equal to the malloc's allocated type
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
57 CallInst* isArrayMalloc(Value* I);
58 const CallInst* isArrayMalloc(const Value* I);
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);
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);
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
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
82 Value* getMallocArraySize(CallInst* CI);
84 } // End llvm namespace