Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 1 | //===- LowerAllocations.cpp - Reduce free insts to calls ------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | // The LowerAllocations transformation is a target-dependent tranformation |
| 11 | // because it depends on the size of data types and alignment constraints. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "lowerallocs" |
| 16 | #include "llvm/Transforms/Scalar.h" |
| 17 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Constants.h" |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumLowered, "Number of allocations lowered"); |
| 29 | |
| 30 | namespace { |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 31 | /// LowerAllocations - Turn free instructions into @free calls. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | /// |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 33 | class LowerAllocations : public BasicBlockPass { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 34 | Constant *FreeFunc; // Functions in the module we are processing |
| 35 | // Initialized by doInitialization |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | public: |
| 37 | static char ID; // Pass ID, replacement for typeid |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 38 | explicit LowerAllocations() |
| 39 | : BasicBlockPass(&ID), FreeFunc(0) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | |
| 41 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 42 | AU.addRequired<TargetData>(); |
| 43 | AU.setPreservesCFG(); |
| 44 | |
| 45 | // This is a cluster of orthogonal Transforms: |
| 46 | AU.addPreserved<UnifyFunctionExitNodes>(); |
| 47 | AU.addPreservedID(PromoteMemoryToRegisterID); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | AU.addPreservedID(LowerSwitchID); |
| 49 | AU.addPreservedID(LowerInvokePassID); |
| 50 | } |
| 51 | |
| 52 | /// doPassInitialization - For the lower allocations pass, this ensures that |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 53 | /// a module contains a declaration for a free function. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | /// |
| 55 | bool doInitialization(Module &M); |
| 56 | |
| 57 | virtual bool doInitialization(Function &F) { |
Devang Patel | 35d69b0 | 2008-11-18 18:43:07 +0000 | [diff] [blame] | 58 | return doInitialization(*F.getParent()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// runOnBasicBlock - This method does the actual work of converting |
| 62 | /// instructions over, assuming that the pass has already been initialized. |
| 63 | /// |
| 64 | bool runOnBasicBlock(BasicBlock &BB); |
| 65 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | char LowerAllocations::ID = 0; |
| 69 | static RegisterPass<LowerAllocations> |
| 70 | X("lowerallocs", "Lower allocations from instructions to calls"); |
| 71 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | // Publically exposed interface to pass... |
Dan Gohman | 66a636e | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 73 | const PassInfo *const llvm::LowerAllocationsID = &X; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | // createLowerAllocationsPass - Interface to this file... |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 75 | Pass *llvm::createLowerAllocationsPass() { |
| 76 | return new LowerAllocations(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | // doInitialization - For the lower allocations pass, this ensures that a |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 81 | // module contains a declaration for a free function. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | // |
| 83 | // This function is always successful. |
| 84 | // |
| 85 | bool LowerAllocations::doInitialization(Module &M) { |
Duncan Sands | f2519d6 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 86 | const Type *BPTy = Type::getInt8PtrTy(M.getContext()); |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 87 | FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()), |
| 88 | BPTy, (Type *)0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
| 92 | // runOnBasicBlock - This method does the actual work of converting |
| 93 | // instructions over, assuming that the pass has already been initialized. |
| 94 | // |
| 95 | bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { |
| 96 | bool Changed = false; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 97 | assert(FreeFunc && "Pass not initialized!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 98 | |
| 99 | BasicBlock::InstListType &BBIL = BB.getInstList(); |
| 100 | |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 101 | // Loop over all of the instructions, looking for free instructions |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 103 | if (FreeInst *FI = dyn_cast<FreeInst>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | // Insert a call to the free function... |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 105 | CallInst::CreateFree(FI->getOperand(0), I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | |
| 107 | // Delete the old free instruction |
| 108 | I = --BBIL.erase(I); |
| 109 | Changed = true; |
| 110 | ++NumLowered; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return Changed; |
| 115 | } |
| 116 | |