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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 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 | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "indmemrem" |
Andrew Lenharth | 3433141 | 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 | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Type.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 29 | STATISTIC(NumBounceSites, "Number of sites modified"); |
| 30 | STATISTIC(NumBounce , "Number of bounce functions created"); |
| 31 | |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 32 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 33 | class VISIBILITY_HIDDEN IndMemRemPass : public ModulePass { |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 34 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 35 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 36 | IndMemRemPass() : ModulePass((intptr_t)&ID) {} |
| 37 | |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 38 | virtual bool runOnModule(Module &M); |
| 39 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 40 | char IndMemRemPass::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 41 | RegisterPass<IndMemRemPass> X("indmemrem","Indirect Malloc and Free Removal"); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 42 | } // end anonymous namespace |
| 43 | |
| 44 | |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 45 | bool IndMemRemPass::runOnModule(Module &M) { |
| 46 | //in Theory, all direct calls of malloc and free should be promoted |
| 47 | //to intrinsics. Therefor, this goes through and finds where the |
| 48 | //address of free or malloc are taken and replaces those with bounce |
| 49 | //functions, ensuring that all malloc and free that might happen |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 50 | //happen through intrinsics. |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 51 | bool changed = false; |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 52 | if (Function* F = M.getFunction("free")) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 53 | assert(F->isDeclaration() && "free not external?"); |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 54 | if (!F->use_empty()) { |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 55 | Function* FN = new Function(F->getFunctionType(), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 56 | GlobalValue::LinkOnceLinkage, |
| 57 | "free_llvm_bounce", &M); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 58 | BasicBlock* bb = new BasicBlock("entry",FN); |
| 59 | Instruction* R = new ReturnInst(bb); |
| 60 | new FreeInst(FN->arg_begin(), R); |
| 61 | ++NumBounce; |
| 62 | NumBounceSites += F->getNumUses(); |
| 63 | F->replaceAllUsesWith(FN); |
| 64 | changed = true; |
| 65 | } |
| 66 | } |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 67 | if (Function* F = M.getFunction("malloc")) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 68 | assert(F->isDeclaration() && "malloc not external?"); |
Andrew Lenharth | 61e99c9 | 2006-04-13 13:43:31 +0000 | [diff] [blame] | 69 | if (!F->use_empty()) { |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 70 | Function* FN = new Function(F->getFunctionType(), |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 71 | GlobalValue::LinkOnceLinkage, |
| 72 | "malloc_llvm_bounce", &M); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 73 | BasicBlock* bb = new BasicBlock("entry",FN); |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 74 | Instruction* c = CastInst::createIntegerCast( |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 75 | FN->arg_begin(), Type::Int32Ty, false, "c", bb); |
| 76 | Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb); |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 77 | new ReturnInst(a, bb); |
Andrew Lenharth | 3433141 | 2006-04-10 19:26:09 +0000 | [diff] [blame] | 78 | ++NumBounce; |
| 79 | NumBounceSites += F->getNumUses(); |
| 80 | F->replaceAllUsesWith(FN); |
| 81 | changed = true; |
| 82 | } |
| 83 | } |
| 84 | return changed; |
| 85 | } |
| 86 | |
| 87 | ModulePass *llvm::createIndMemRemPass() { |
| 88 | return new IndMemRemPass(); |
| 89 | } |