Andrew Lenharth | a9cdcca | 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 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "indmemrem" |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/IPO.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include "llvm/Module.h" |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Type.h" |
Reid Spencer | a94d394 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 28 | STATISTIC(NumBounceSites, "Number of sites modified"); |
| 29 | STATISTIC(NumBounce , "Number of bounce functions created"); |
| 30 | |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 31 | namespace { |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 32 | class IndMemRemPass : public ModulePass { |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 33 | public: |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 34 | virtual bool runOnModule(Module &M); |
| 35 | }; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 36 | RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal"); |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 37 | } // end anonymous namespace |
| 38 | |
| 39 | |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 40 | bool IndMemRemPass::runOnModule(Module &M) { |
| 41 | //in Theory, all direct calls of malloc and free should be promoted |
| 42 | //to intrinsics. Therefor, this goes through and finds where the |
| 43 | //address of free or malloc are taken and replaces those with bounce |
| 44 | //functions, ensuring that all malloc and free that might happen |
Andrew Lenharth | 92cf71f | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 45 | //happen through intrinsics. |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 46 | bool changed = false; |
| 47 | if (Function* F = M.getNamedFunction("free")) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 48 | assert(F->isDeclaration() && "free not external?"); |
Andrew Lenharth | 92cf71f | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 49 | if (!F->use_empty()) { |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 50 | Function* FN = new Function(F->getFunctionType(), |
| 51 | GlobalValue::LinkOnceLinkage, |
| 52 | "free_llvm_bounce", &M); |
| 53 | BasicBlock* bb = new BasicBlock("entry",FN); |
| 54 | Instruction* R = new ReturnInst(bb); |
| 55 | new FreeInst(FN->arg_begin(), R); |
| 56 | ++NumBounce; |
| 57 | NumBounceSites += F->getNumUses(); |
| 58 | F->replaceAllUsesWith(FN); |
| 59 | changed = true; |
| 60 | } |
| 61 | } |
| 62 | if (Function* F = M.getNamedFunction("malloc")) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 63 | assert(F->isDeclaration() && "malloc not external?"); |
Andrew Lenharth | 92cf71f | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 64 | if (!F->use_empty()) { |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 65 | Function* FN = new Function(F->getFunctionType(), |
| 66 | GlobalValue::LinkOnceLinkage, |
| 67 | "malloc_llvm_bounce", &M); |
| 68 | BasicBlock* bb = new BasicBlock("entry",FN); |
Reid Spencer | bfe26ff | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 69 | Instruction* c = CastInst::createIntegerCast( |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 70 | FN->arg_begin(), Type::Int32Ty, false, "c", bb); |
| 71 | Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb); |
Reid Spencer | de46e48 | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 72 | new ReturnInst(a, bb); |
Andrew Lenharth | a9cdcca | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 73 | ++NumBounce; |
| 74 | NumBounceSites += F->getNumUses(); |
| 75 | F->replaceAllUsesWith(FN); |
| 76 | changed = true; |
| 77 | } |
| 78 | } |
| 79 | return changed; |
| 80 | } |
| 81 | |
| 82 | ModulePass *llvm::createIndMemRemPass() { |
| 83 | return new IndMemRemPass(); |
| 84 | } |