Large mechanical patch.
[oota-llvm.git] / lib / Transforms / IPO / IndMemRemoval.cpp
index ef27cd61a9a1408b1acbc93d03352e8d0c849511..b251ab4b09c11331673503039d58a8b44c69d1fd 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "indmemrem"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Pass.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
 #include "llvm/Instructions.h"
 #include "llvm/Type.h"
-#include "llvm/Support/Debug.h"
+#include "llvm/DerivedTypes.h"
 #include "llvm/ADT/Statistic.h"
-#include <fstream>
-#include <iostream>
-#include <set>
+#include "llvm/Support/Compiler.h"
 using namespace llvm;
 
-namespace {
-  Statistic<> NumBounceSites("indmemrem", "Number of sites modified");
-  Statistic<> NumBounce  ("indmemrem", "Number of bounce functions created");
-
-  class IndMemRemPass : public ModulePass {
+STATISTIC(NumBounceSites, "Number of sites modified");
+STATISTIC(NumBounce     , "Number of bounce functions created");
 
+namespace {
+  class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
   public:
-    IndMemRemPass();
+    static char ID; // Pass identification, replacement for typeid
+    IndMemRemPass() : ModulePass(&ID) {}
+
     virtual bool runOnModule(Module &M);
   };
-  RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal");
 } // end anonymous namespace
 
-
-IndMemRemPass::IndMemRemPass()
-{
-}
+char IndMemRemPass::ID = 0;
+static RegisterPass<IndMemRemPass>
+X("indmemrem","Indirect Malloc and Free Removal");
 
 bool IndMemRemPass::runOnModule(Module &M) {
   //in Theory, all direct calls of malloc and free should be promoted
@@ -53,14 +50,13 @@ bool IndMemRemPass::runOnModule(Module &M) {
   //functions, ensuring that all malloc and free that might happen
   //happen through intrinsics.
   bool changed = false;
-  if (Function* F = M.getNamedFunction("free")) {
-    assert(F->isExternal() && "free not external?");
-    if (!F->use_empty()) {
-      Function* FN = new Function(F->getFunctionType(), 
-                                 GlobalValue::LinkOnceLinkage, 
-                                 "free_llvm_bounce", &M);
-      BasicBlock* bb = new BasicBlock("entry",FN);
-      Instruction* R = new ReturnInst(bb);
+  if (Function* F = M.getFunction("free")) {
+    if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
+      Function* FN = Function::Create(F->getFunctionType(),
+                                      GlobalValue::LinkOnceLinkage,
+                                      "free_llvm_bounce", &M);
+      BasicBlock* bb = BasicBlock::Create("entry",FN);
+      Instruction* R = ReturnInst::Create(bb);
       new FreeInst(FN->arg_begin(), R);
       ++NumBounce;
       NumBounceSites += F->getNumUses();
@@ -68,16 +64,16 @@ bool IndMemRemPass::runOnModule(Module &M) {
       changed = true;
     }
   }
-  if (Function* F = M.getNamedFunction("malloc")) {
-    assert(F->isExternal() && "malloc not external?");
-    if (!F->use_empty()) {
-      Function* FN = new Function(F->getFunctionType()
-                                 GlobalValue::LinkOnceLinkage, 
-                                 "malloc_llvm_bounce", &M);
-      BasicBlock* bb = new BasicBlock("entry",FN);
-      Instruction* c = new CastInst(FN->arg_begin(), Type::UIntTy, "c", bb);
-      Instruction* a = new MallocInst(Type::SByteTy, c, "m", bb);
-      new ReturnInst(a, bb);
+  if (Function* F = M.getFunction("malloc")) {
+    if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
+      Function* FN = Function::Create(F->getFunctionType(), 
+                                      GlobalValue::LinkOnceLinkage
+                                      "malloc_llvm_bounce", &M);
+      BasicBlock* bb = BasicBlock::Create("entry",FN);
+      Instruction* c = CastInst::CreateIntegerCast(
+          FN->arg_begin(), Type::Int32Ty, false, "c", bb);
+      Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
+      ReturnInst::Create(a, bb);
       ++NumBounce;
       NumBounceSites += F->getNumUses();
       F->replaceAllUsesWith(FN);