Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- IndMemRemoval.cpp - Remove indirect allocations and frees ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | |
| 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 |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 36 | IndMemRemPass() : ModulePass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | |
| 38 | virtual bool runOnModule(Module &M); |
| 39 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | } // end anonymous namespace |
| 41 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | char IndMemRemPass::ID = 0; |
| 43 | static RegisterPass<IndMemRemPass> |
| 44 | X("indmemrem","Indirect Malloc and Free Removal"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 45 | |
| 46 | bool IndMemRemPass::runOnModule(Module &M) { |
| 47 | //in Theory, all direct calls of malloc and free should be promoted |
| 48 | //to intrinsics. Therefor, this goes through and finds where the |
| 49 | //address of free or malloc are taken and replaces those with bounce |
| 50 | //functions, ensuring that all malloc and free that might happen |
| 51 | //happen through intrinsics. |
| 52 | bool changed = false; |
| 53 | if (Function* F = M.getFunction("free")) { |
Nuno Lopes | 959ba4e | 2008-09-06 17:44:06 +0000 | [diff] [blame] | 54 | if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) { |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 55 | Function* FN = Function::Create(F->getFunctionType(), |
| 56 | GlobalValue::LinkOnceLinkage, |
| 57 | "free_llvm_bounce", &M); |
| 58 | BasicBlock* bb = BasicBlock::Create("entry",FN); |
| 59 | Instruction* R = ReturnInst::Create(bb); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | new FreeInst(FN->arg_begin(), R); |
| 61 | ++NumBounce; |
| 62 | NumBounceSites += F->getNumUses(); |
| 63 | F->replaceAllUsesWith(FN); |
| 64 | changed = true; |
| 65 | } |
| 66 | } |
| 67 | if (Function* F = M.getFunction("malloc")) { |
Nuno Lopes | 959ba4e | 2008-09-06 17:44:06 +0000 | [diff] [blame] | 68 | if (F->isDeclaration() && F->arg_size() == 1 && !F->use_empty()) { |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 69 | Function* FN = Function::Create(F->getFunctionType(), |
| 70 | GlobalValue::LinkOnceLinkage, |
| 71 | "malloc_llvm_bounce", &M); |
| 72 | BasicBlock* bb = BasicBlock::Create("entry",FN); |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 73 | Instruction* c = CastInst::CreateIntegerCast( |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | FN->arg_begin(), Type::Int32Ty, false, "c", bb); |
| 75 | Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb); |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 76 | ReturnInst::Create(a, bb); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | ++NumBounce; |
| 78 | NumBounceSites += F->getNumUses(); |
| 79 | F->replaceAllUsesWith(FN); |
| 80 | changed = true; |
| 81 | } |
| 82 | } |
| 83 | return changed; |
| 84 | } |
| 85 | |
| 86 | ModulePass *llvm::createIndMemRemPass() { |
| 87 | return new IndMemRemPass(); |
| 88 | } |