Forgot to commit these.
[oota-llvm.git] / lib / Analysis / MallocHelper.cpp
index 41fdab9012ff1c2c5ecd42f88c508a0e8fb5664c..70afa88ac6c26ec718994620615027ac21b827c7 100644 (file)
@@ -1,4 +1,4 @@
-//===-- MallocHelper.cpp - Functions to identify malloc calls -------------===//
+//===-- MallocFreeHelper.cpp - Identify calls to malloc and free builtins -===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,7 +8,8 @@
 //===----------------------------------------------------------------------===//
 //
 // This family of functions identifies calls to malloc, bitcasts of malloc
-// calls, and the types and array sizes associated with them.
+// calls, and the types and array sizes associated with them.  It also
+// identifies calls to the free builtin.
 //
 //===----------------------------------------------------------------------===//
 
@@ -234,7 +235,7 @@ static bool isConstantOne(Value *val) {
 /// determined.
 Value* llvm::getMallocArraySize(CallInst* CI, LLVMContext &Context,
                                 const TargetData* TD) {
-  if (isSafeToGetMallocArraySize(CI, Context, TD))
+  if (!isSafeToGetMallocArraySize(CI, Context, TD))
     return NULL;
 
   // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
@@ -263,3 +264,33 @@ Value* llvm::getMallocArraySize(CallInst* CI, LLVMContext &Context,
   assert(BO && "getMallocArraySize not constant but not multiplication either");
   return BO->getOperand(0);
 }
+
+//===----------------------------------------------------------------------===//
+//  free Call Utility Functions.
+//
+
+/// isFreeCall - Returns true if the the value is a call to the builtin free()
+bool llvm::isFreeCall(const Value* I) {
+  const CallInst *CI = dyn_cast<CallInst>(I);
+  if (!CI)
+    return false;
+
+  const Module* M = CI->getParent()->getParent()->getParent();
+  Function *FreeFunc = M->getFunction("free");
+
+  if (CI->getOperand(0) != FreeFunc)
+    return false;
+
+  // Check free prototype.
+  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 
+  // attribute will exist.
+  const FunctionType *FTy = FreeFunc->getFunctionType();
+  if (FTy->getReturnType() != Type::getVoidTy(M->getContext()))
+    return false;
+  if (FTy->getNumParams() != 1)
+    return false;
+  if (FTy->param_begin()->get() != Type::getInt8PtrTy(M->getContext()))
+    return false;
+
+  return true;
+}