Autoupgrade malloc insts to malloc calls.
[oota-llvm.git] / lib / Transforms / IPO / IndMemRemoval.cpp
1 //===-- IndMemRemoval.cpp - Remove indirect allocations and frees ---------===//
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 pass finds places where memory allocation functions may escape into
11 // indirect land.  Some transforms are much easier (aka possible) only if free 
12 // or malloc are not called indirectly.
13 // Thus find places where the address of memory functions are taken and 
14 // construct bounce functions with direct calls of those functions.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "indmemrem"
19 #include "llvm/Transforms/IPO.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Module.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Type.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Target/TargetData.h"
28 using namespace llvm;
29
30 STATISTIC(NumBounceSites, "Number of sites modified");
31 STATISTIC(NumBounce     , "Number of bounce functions created");
32
33 namespace {
34   class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
35   public:
36     static char ID; // Pass identification, replacement for typeid
37     IndMemRemPass() : ModulePass(&ID) {}
38
39     virtual bool runOnModule(Module &M);
40   };
41 } // end anonymous namespace
42
43 char IndMemRemPass::ID = 0;
44 static RegisterPass<IndMemRemPass>
45 X("indmemrem","Indirect Malloc and Free Removal");
46
47 bool IndMemRemPass::runOnModule(Module &M) {
48   // In theory, all direct calls of malloc and free should be promoted
49   // to intrinsics.  Therefore, this goes through and finds where the
50   // address of free or malloc are taken and replaces those with bounce
51   // functions, ensuring that all malloc and free that might happen
52   // happen through intrinsics.
53   bool changed = false;
54   if (Function* F = M.getFunction("free")) {
55     if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
56       Function* FN = Function::Create(F->getFunctionType(),
57                                       GlobalValue::LinkOnceAnyLinkage,
58                                       "free_llvm_bounce", &M);
59       BasicBlock* bb = BasicBlock::Create(M.getContext(), "entry",FN);
60       Instruction* R = ReturnInst::Create(M.getContext(), bb);
61       new FreeInst(FN->arg_begin(), R);
62       ++NumBounce;
63       NumBounceSites += F->getNumUses();
64       F->replaceAllUsesWith(FN);
65       changed = true;
66     }
67   }
68   if (Function* F = M.getFunction("malloc")) {
69     if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
70       TargetData* TD = getAnalysisIfAvailable<TargetData>();
71       if (TD) { 
72         Function* FN = Function::Create(F->getFunctionType(), 
73                                         GlobalValue::LinkOnceAnyLinkage,
74                                         "malloc_llvm_bounce", &M);
75         F->replaceAllUsesWith(FN);
76         FN->setDoesNotAlias(0);
77         BasicBlock* bb = BasicBlock::Create(M.getContext(), "entry", FN);
78         const Type* IntPtrTy = TD->getIntPtrType(M.getContext());
79         Value* c = FN->arg_begin();
80         if (FN->arg_begin()->getType() != IntPtrTy)
81           c = CastInst::CreateIntegerCast(FN->arg_begin(), IntPtrTy, false,
82                                           "c", bb);
83         Value* a = CallInst::CreateMalloc(bb, IntPtrTy,
84                                           Type::getInt8Ty(M.getContext()),
85                                           c, NULL, "m");
86         bb->getInstList().push_back(cast<Instruction>(a));
87         ReturnInst::Create(M.getContext(), a, bb);
88         ++NumBounce;
89         NumBounceSites += F->getNumUses();
90         changed = true;
91       }
92     }
93   }
94   return changed;
95 }
96
97 ModulePass *llvm::createIndMemRemPass() {
98   return new IndMemRemPass();
99 }