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
26 //===----------------------------------------------------------------------===//
27 // malloc Call Utility Functions.
30 /// isMalloc - Returns true if the value is either a malloc call or a bitcast of
31 /// the result of a malloc call
32 bool isMalloc(const Value* I);
34 /// extractMallocCall - Returns the corresponding CallInst if the instruction
35 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
36 /// ignore InvokeInst here.
37 const CallInst* extractMallocCall(const Value* I);
38 CallInst* extractMallocCall(Value* I);
40 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
41 /// instruction is a bitcast of the result of a malloc call.
42 const CallInst* extractMallocCallFromBitCast(const Value* I);
43 CallInst* extractMallocCallFromBitCast(Value* I);
45 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
46 /// matches the malloc call IR generated by CallInst::CreateMalloc(). This
47 /// means that it is a malloc call with one bitcast use AND the malloc call's
49 /// 1. a constant not equal to the size of the malloced type
51 /// 2. the result of a multiplication by the size of the malloced type
52 /// Otherwise it returns NULL.
53 /// The unique bitcast is needed to determine the type/size of the array
55 CallInst* isArrayMalloc(Value* I, LLVMContext &Context, const TargetData* TD);
56 const CallInst* isArrayMalloc(const Value* I, LLVMContext &Context,
57 const TargetData* TD);
59 /// getMallocType - Returns the PointerType resulting from the malloc call.
60 /// This PointerType is the result type of the call's only bitcast use.
61 /// If there is no unique bitcast use, then return NULL.
62 const PointerType* getMallocType(const CallInst* CI);
64 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
65 /// Type is the result type of the call's only bitcast use. If there is no
66 /// unique bitcast use, then return NULL.
67 const Type* getMallocAllocatedType(const CallInst* CI);
69 /// getMallocArraySize - Returns the array size of a malloc call. For array
70 /// mallocs, the size is computated in 1 of 3 ways:
71 /// 1. If the element type is of size 1, then array size is the argument to
73 /// 2. Else if the malloc's argument is a constant, the array size is that
74 /// argument divided by the element type's size.
75 /// 3. Else the malloc argument must be a multiplication and the array size is
76 /// the first operand of the multiplication.
77 /// For non-array mallocs, the computed size is constant 1.
78 /// This function returns NULL for all mallocs whose array size cannot be
80 Value* getMallocArraySize(CallInst* CI, LLVMContext &Context,
81 const TargetData* TD);
83 //===----------------------------------------------------------------------===//
84 // free Call Utility Functions.
87 /// isFreeCall - Returns true if the the value is a call to the builtin free()
88 bool isFreeCall(const Value* I);
90 } // End llvm namespace