Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 1 | //===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 construct |
| 14 | // bounce functions with direct calls of those functions. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm/Transforms/IPO.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Type.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
| 26 | #include <fstream> |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 27 | #include <set> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | Statistic<> NumBounceSites("indmemrem", "Number of sites modified"); |
| 32 | Statistic<> NumBounce ("indmemrem", "Number of bounce functions created"); |
| 33 | |
| 34 | class IndMemRemPass : public ModulePass { |
| 35 | |
| 36 | public: |
| 37 | IndMemRemPass(); |
| 38 | virtual bool runOnModule(Module &M); |
| 39 | }; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 40 | RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal"); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 41 | } // end anonymous namespace |
| 42 | |
| 43 | |
| 44 | IndMemRemPass::IndMemRemPass() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | bool IndMemRemPass::runOnModule(Module &M) { |
| 49 | //in Theory, all direct calls of malloc and free should be promoted |
| 50 | //to intrinsics. Therefor, 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 |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 53 | //happen through intrinsics. |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 54 | bool changed = false; |
| 55 | if (Function* F = M.getNamedFunction("free")) { |
| 56 | assert(F->isExternal() && "free not external?"); |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 57 | if (!F->use_empty()) { |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 58 | Function* FN = new Function(F->getFunctionType(), |
| 59 | GlobalValue::LinkOnceLinkage, |
| 60 | "free_llvm_bounce", &M); |
| 61 | BasicBlock* bb = new BasicBlock("entry",FN); |
| 62 | Instruction* R = new ReturnInst(bb); |
| 63 | new FreeInst(FN->arg_begin(), R); |
| 64 | ++NumBounce; |
| 65 | NumBounceSites += F->getNumUses(); |
| 66 | F->replaceAllUsesWith(FN); |
| 67 | changed = true; |
| 68 | } |
| 69 | } |
| 70 | if (Function* F = M.getNamedFunction("malloc")) { |
| 71 | assert(F->isExternal() && "malloc not external?"); |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 72 | if (!F->use_empty()) { |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 73 | Function* FN = new Function(F->getFunctionType(), |
| 74 | GlobalValue::LinkOnceLinkage, |
| 75 | "malloc_llvm_bounce", &M); |
| 76 | BasicBlock* bb = new BasicBlock("entry",FN); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 77 | Instruction* c = |
| 78 | CastInst::createInferredCast(FN->arg_begin(), Type::UIntTy, "c", bb); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 79 | Instruction* a = new MallocInst(Type::SByteTy, c, "m", bb); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 80 | new ReturnInst(a, bb); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 81 | ++NumBounce; |
| 82 | NumBounceSites += F->getNumUses(); |
| 83 | F->replaceAllUsesWith(FN); |
| 84 | changed = true; |
| 85 | } |
| 86 | } |
| 87 | return changed; |
| 88 | } |
| 89 | |
| 90 | ModulePass *llvm::createIndMemRemPass() { |
| 91 | return new IndMemRemPass(); |
| 92 | } |