Add a simple pass to make sure that all (non-library) calls to malloc and free
authorAndrew Lenharth <andrewl@lenharth.org>
Mon, 10 Apr 2006 19:26:09 +0000 (19:26 +0000)
committerAndrew Lenharth <andrewl@lenharth.org>
Mon, 10 Apr 2006 19:26:09 +0000 (19:26 +0000)
are visible to analysis as intrinsics.  That is, make sure someone doesn't pass
free around by address in some struct (as happens in say 176.gcc).

This doesn't get rid of any indirect calls, just ensure calls to free and malloc
are always direct.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27560 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Transforms/IPO.h
include/llvm/Transforms/LinkAllPasses.h
lib/Transforms/IPO/IndMemRemoval.cpp [new file with mode: 0644]

index ca5959ce4be3bd46c5e2e697f5180b4c546b2b47..eba40b8516bdd1b487358ecd559411aaaa035b0a 100644 (file)
@@ -169,6 +169,11 @@ ModulePass *createBlockExtractorPass(std::vector<BasicBlock*> &BTNE);
 // specific well-known (library) functions.
 ModulePass *createSimplifyLibCallsPass();
 
+
+// createIndMemRemPass - This pass removes potential indirect calls of
+// malloc and free
+ModulePass *createIndMemRemPass();
+
 } // End llvm namespace
 
 #endif
index cc9621fefabb69110ac34edb5fed99e44d23f4e7..ac64f855ecfcc2575c0d7b7e76f4575a9b8f7d62 100644 (file)
@@ -107,7 +107,7 @@ namespace {
       (void) llvm::createCondPropagationPass();
       (void) llvm::createNullProfilerRSPass();
       (void) llvm::createRSProfilingPass();
-
+      (void) llvm::createIndMemRemPass();
     }
   } ForcePassLinking;
 }
diff --git a/lib/Transforms/IPO/IndMemRemoval.cpp b/lib/Transforms/IPO/IndMemRemoval.cpp
new file mode 100644 (file)
index 0000000..84a4307
--- /dev/null
@@ -0,0 +1,92 @@
+//===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===//
+//
+//                     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 pass finds places where memory allocation functions may escape into
+// indirect land.  Some transforms are much easier (aka possible) only if free 
+// or malloc are not called indirectly.
+// Thus find places where the address of memory functions are taken and construct
+// bounce functions with direct calls of those functions.
+//
+//===----------------------------------------------------------------------===//
+
+#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/ADT/Statistic.h"
+#include <fstream>
+#include <iostream>
+#include <set>
+using namespace llvm;
+
+namespace {
+  Statistic<> NumBounceSites("indmemrem", "Number of sites modified");
+  Statistic<> NumBounce  ("indmemrem", "Number of bounce functions created");
+
+  class IndMemRemPass : public ModulePass {
+
+  public:
+    IndMemRemPass();
+    virtual bool runOnModule(Module &M);
+  };
+  RegisterOpt<IndMemRemPass> X("indmemrem", "Indirect Malloc and Free Removal");
+} // end anonymous namespace
+
+
+IndMemRemPass::IndMemRemPass()
+{
+}
+
+bool IndMemRemPass::runOnModule(Module &M) {
+  //in Theory, all direct calls of malloc and free should be promoted
+  //to intrinsics.  Therefor, this goes through and finds where the
+  //address of free or malloc are taken and replaces those with bounce
+  //functions, ensuring that all malloc and free that might happen
+  //happens through intrinsics.
+  bool changed = false;
+  if (Function* F = M.getNamedFunction("free")) {
+    assert(F->isExternal() && "free not external?");
+    if (F->getNumUses()) {
+      Function* FN = new Function(F->getFunctionType(), 
+                                 GlobalValue::LinkOnceLinkage, 
+                                 "free_llvm_bounce", &M);
+      BasicBlock* bb = new BasicBlock("entry",FN);
+      Instruction* R = new ReturnInst(bb);
+      new FreeInst(FN->arg_begin(), R);
+      ++NumBounce;
+      NumBounceSites += F->getNumUses();
+      F->replaceAllUsesWith(FN);
+      changed = true;
+    }
+  }
+  if (Function* F = M.getNamedFunction("malloc")) {
+    assert(F->isExternal() && "malloc not external?");
+    if (F->getNumUses()) {
+      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);
+      Instruction* R = new ReturnInst(a, bb);
+      ++NumBounce;
+      NumBounceSites += F->getNumUses();
+      F->replaceAllUsesWith(FN);
+      changed = true;
+    }
+  }
+  return changed;
+}
+
+ModulePass *llvm::createIndMemRemPass() {
+  return new IndMemRemPass();
+}