this is failing on linux hosts, force a triple.
[llvm/avr.git] / lib / Analysis / MallocHelper.cpp
blob60930d5169b69b373e7781e748dfa39932f1200c
1 //===-- MallocHelper.cpp - Functions to identify malloc calls -------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/MallocHelper.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 using namespace llvm;
21 //===----------------------------------------------------------------------===//
22 // malloc Call Utility Functions.
25 /// isMalloc - Returns true if the the value is either a malloc call or a
26 /// bitcast of the result of a malloc call.
27 bool llvm::isMalloc(Value* I) {
28 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31 bool llvm::isMalloc(const Value* I) {
32 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
35 static bool isMallocCall(const CallInst *CI) {
36 if (!CI)
37 return false;
39 const Module* M = CI->getParent()->getParent()->getParent();
40 Constant *MallocFunc = M->getFunction("malloc");
42 if (CI->getOperand(0) != MallocFunc)
43 return false;
45 return true;
48 /// extractMallocCall - Returns the corresponding CallInst if the instruction
49 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
50 /// ignore InvokeInst here.
51 const CallInst* llvm::extractMallocCall(const Value* I) {
52 const CallInst *CI = dyn_cast<CallInst>(I);
53 return (isMallocCall(CI)) ? CI : NULL;
56 CallInst* llvm::extractMallocCall(Value* I) {
57 CallInst *CI = dyn_cast<CallInst>(I);
58 return (isMallocCall(CI)) ? CI : NULL;
61 static bool isBitCastOfMallocCall(const BitCastInst* BCI) {
62 if (!BCI)
63 return false;
65 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
68 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
69 /// instruction is a bitcast of the result of a malloc call.
70 CallInst* llvm::extractMallocCallFromBitCast(Value* I) {
71 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
72 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
73 : NULL;
76 const CallInst* llvm::extractMallocCallFromBitCast(const Value* I) {
77 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
78 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
79 : NULL;
82 static bool isArrayMallocHelper(const CallInst *CI) {
83 if (!CI)
84 return false;
86 // Only identify array mallocs for mallocs with 1 bitcast use. The unique
87 // bitcast is needed to determine the type/size of the array allocation.
88 if (!CI->hasOneUse()) return false;
90 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
91 UI != E; )
92 if (!isa<BitCastInst>(cast<Instruction>(*UI++)))
93 return false;
95 // malloc arg
96 Value* MallocArg = CI->getOperand(1);
97 // element size
98 const Type* T = getMallocAllocatedType(CI);
99 if (!T) return false;
100 Constant *ElementSize = ConstantExpr::getSizeOf(T);
102 if (isa<ConstantExpr>(MallocArg))
103 return (MallocArg == ElementSize) ? false : true;
105 BinaryOperator *BI = dyn_cast<BinaryOperator>(MallocArg);
106 if (!BI)
107 return false;
109 if (BI->getOpcode() != Instruction::Mul)
110 return false;
112 if (BI->getOperand(1) != ElementSize)
113 return false;
115 return true;
118 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
119 /// matches the malloc call IR generated by CallInst::CreateMalloc(). This
120 /// means that it is a malloc call with one bitcast use AND the malloc call's
121 /// size argument is:
122 /// 1. a constant not equal to the malloc's allocated type
123 /// or
124 /// 2. the result of a multiplication by the malloc's allocated type
125 /// Otherwise it returns NULL.
126 /// The unique bitcast is needed to determine the type/size of the array
127 /// allocation.
128 CallInst* llvm::isArrayMalloc(Value* I) {
129 CallInst *CI = extractMallocCall(I);
130 return (isArrayMallocHelper(CI)) ? CI : NULL;
133 const CallInst* llvm::isArrayMalloc(const Value* I) {
134 const CallInst *CI = extractMallocCall(I);
135 return (isArrayMallocHelper(CI)) ? CI : NULL;
138 /// getMallocType - Returns the PointerType resulting from the malloc call.
139 /// This PointerType is the result type of the call's only bitcast use.
140 /// If there is no unique bitcast use, then return NULL.
141 const PointerType* llvm::getMallocType(const CallInst* CI) {
142 assert(isMalloc(CI) && "GetMallocType and not malloc call");
144 const BitCastInst* BCI = NULL;
146 // Determine type only if there is only 1 bitcast use of CI.
147 if (CI->hasOneUse())
148 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
149 UI != E; )
150 BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++));
152 return BCI ? reinterpret_cast<const PointerType*>(BCI->getDestTy()) : NULL;
155 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
156 /// Type is the result type of the call's only bitcast use. If there is no
157 /// unique bitcast use, then return NULL.
158 const Type* llvm::getMallocAllocatedType(const CallInst* CI) {
159 const PointerType* PT = getMallocType(CI);
160 return PT ? PT->getElementType() : NULL;
163 /// isConstantOne - Return true only if val is constant int 1.
164 static bool isConstantOne(Value *val) {
165 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
168 /// getMallocArraySize - Returns the array size of a malloc call. The array
169 /// size is computated in 1 of 3 ways:
170 /// 1. If the element type if of size 1, then array size is the argument to
171 /// malloc.
172 /// 2. Else if the malloc's argument is a constant, the array size is that
173 /// argument divided by the element type's size.
174 /// 3. Else the malloc argument must be a multiplication and the array size is
175 /// the first operand of the multiplication.
176 /// This function returns constant 1 if:
177 /// 1. The malloc call's allocated type cannot be determined.
178 /// 2. IR wasn't created by a call to CallInst::CreateMalloc() with a non-NULL
179 /// ArraySize.
180 Value* llvm::getMallocArraySize(CallInst* CI) {
181 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
182 if (!isArrayMalloc(CI))
183 return ConstantInt::get(CI->getOperand(1)->getType(), 1);
185 Value* MallocArg = CI->getOperand(1);
186 assert(getMallocAllocatedType(CI) && "getMallocArraySize and no type");
187 Constant *ElementSize = ConstantExpr::getSizeOf(getMallocAllocatedType(CI));
188 ElementSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(ElementSize),
189 MallocArg->getType());
191 Constant* CO = dyn_cast<Constant>(MallocArg);
192 BinaryOperator* BO = dyn_cast<BinaryOperator>(MallocArg);
193 assert((isConstantOne(ElementSize) || CO || BO) &&
194 "getMallocArraySize and malformed malloc IR");
196 if (isConstantOne(ElementSize))
197 return MallocArg;
199 if (CO)
200 return ConstantExpr::getUDiv(CO, ElementSize);
202 assert(BO && "getMallocArraySize not constant but not multiplication either");
203 return BO->getOperand(0);