7b9b0ccb8b25bacfa703d59eee9c26cc731b7bd0
[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 using namespace llvm;
28
29 STATISTIC(NumBounceSites, "Number of sites modified");
30 STATISTIC(NumBounce     , "Number of bounce functions created");
31
32 namespace {
33   class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass {
34   public:
35     static char ID; // Pass identification, replacement for typeid
36     IndMemRemPass() : ModulePass(&ID) {}
37
38     virtual bool runOnModule(Module &M);
39   };
40 } // end anonymous namespace
41
42 char IndMemRemPass::ID = 0;
43 static RegisterPass<IndMemRemPass>
44 X("indmemrem","Indirect Malloc and Free Removal");
45
46 bool IndMemRemPass::runOnModule(Module &M) {
47   Context = &M.getContext();
48   
49   // In theory, all direct calls of malloc and free should be promoted
50   // to intrinsics.  Therefore, this goes through and finds where the
51   // address of free or malloc are taken and replaces those with bounce
52   // functions, ensuring that all malloc and free that might happen
53   // happen through intrinsics.
54   bool changed = false;
55   if (Function* F = M.getFunction("free")) {
56     if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
57       Function* FN = Function::Create(F->getFunctionType(),
58                                       GlobalValue::LinkOnceAnyLinkage,
59                                       "free_llvm_bounce", &M);
60       BasicBlock* bb = BasicBlock::Create("entry",FN);
61       Instruction* R = ReturnInst::Create(bb);
62       new FreeInst(FN->arg_begin(), R);
63       ++NumBounce;
64       NumBounceSites += F->getNumUses();
65       F->replaceAllUsesWith(FN);
66       changed = true;
67     }
68   }
69   if (Function* F = M.getFunction("malloc")) {
70     if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) {
71       Function* FN = Function::Create(F->getFunctionType(), 
72                                       GlobalValue::LinkOnceAnyLinkage,
73                                       "malloc_llvm_bounce", &M);
74       FN->setDoesNotAlias(0);
75       BasicBlock* bb = BasicBlock::Create("entry",FN);
76       Instruction* c = CastInst::CreateIntegerCast(
77           FN->arg_begin(), Type::Int32Ty, false, "c", bb);
78       Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
79       ReturnInst::Create(a, bb);
80       ++NumBounce;
81       NumBounceSites += F->getNumUses();
82       F->replaceAllUsesWith(FN);
83       changed = true;
84     }
85   }
86   return changed;
87 }
88
89 ModulePass *llvm::createIndMemRemPass() {
90   return new IndMemRemPass();
91 }