Don't treat malloc calls with non-matching prototype as malloc.
[oota-llvm.git] / lib / Analysis / MallocHelper.cpp
index ab6239e55ad433ec651e00f2ac90fd2bf23b476d..1a8665b6f6ff03b58cb4058ee505d5c6bbd284e7 100644 (file)
@@ -34,12 +34,23 @@ static bool isMallocCall(const CallInst *CI) {
     return false;
 
   const Module* M = CI->getParent()->getParent()->getParent();
-  Constant *MallocFunc = M->getFunction("malloc");
+  Function *MallocFunc = M->getFunction("malloc");
 
   if (CI->getOperand(0) != MallocFunc)
     return false;
 
-  return true;
+  // Check malloc prototype.
+  // FIXME: this will be obsolete when nobuiltin attribute will exist.
+  const FunctionType *FTy = MallocFunc->getFunctionType();
+  if (FTy->getNumParams() != 1)
+    return false;
+  if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
+    if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
+      return false;
+    return true;
+  }
+
+  return false;
 }
 
 /// extractMallocCall - Returns the corresponding CallInst if the instruction