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