Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame^] | 1 | //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===// |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame^] | 3 | // This file defines the RaiseAllocations pass which convert malloc and free |
| 4 | // calls to malloc and free instructions. |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #include "llvm/Transforms/ChangeAllocations.h" |
| 9 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 10 | #include "llvm/Module.h" |
| 11 | #include "llvm/Function.h" |
| 12 | #include "llvm/DerivedTypes.h" |
| 13 | #include "llvm/iMemory.h" |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/Pass.h" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // RaiseAllocations - Turn %malloc and %free calls into the appropriate |
| 20 | // instruction. |
| 21 | // |
| 22 | class RaiseAllocations : public BasicBlockPass { |
| 23 | Function *MallocFunc; // Functions in the module we are processing |
| 24 | Function *FreeFunc; // Initialized by doPassInitializationVirt |
| 25 | public: |
| 26 | inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {} |
| 27 | |
| 28 | const char *getPassName() const { return "Raise Allocations"; } |
| 29 | |
| 30 | // doPassInitialization - For the raise allocations pass, this finds a |
| 31 | // declaration for malloc and free if they exist. |
| 32 | // |
| 33 | bool doInitialization(Module *M); |
| 34 | |
| 35 | // runOnBasicBlock - This method does the actual work of converting |
| 36 | // instructions over, assuming that the pass has already been initialized. |
| 37 | // |
| 38 | bool runOnBasicBlock(BasicBlock *BB); |
| 39 | }; |
| 40 | |
| 41 | } // end anonymous namespace |
| 42 | |
| 43 | |
| 44 | // createRaiseAllocationsPass - The interface to this file... |
| 45 | Pass *createRaiseAllocationsPass() { |
| 46 | return new RaiseAllocations(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | bool RaiseAllocations::doInitialization(Module *M) { |
| 51 | // If the module has a symbol table, they might be referring to the malloc |
| 52 | // and free functions. If this is the case, grab the method pointers that |
| 53 | // the module is using. |
| 54 | // |
| 55 | // Lookup %malloc and %free in the symbol table, for later use. If they |
| 56 | // don't exist, or are not external, we do not worry about converting calls |
| 57 | // to that function into the appropriate instruction. |
| 58 | // |
| 59 | const FunctionType *MallocType = // Get the type for malloc |
| 60 | FunctionType::get(PointerType::get(Type::SByteTy), |
| 61 | std::vector<const Type*>(1, Type::UIntTy), false); |
| 62 | |
| 63 | const FunctionType *FreeType = // Get the type for free |
| 64 | FunctionType::get(Type::VoidTy, |
| 65 | std::vector<const Type*>(1, PointerType::get(Type::SByteTy)), |
| 66 | false); |
| 67 | |
| 68 | MallocFunc = M->getFunction("malloc", MallocType); |
| 69 | FreeFunc = M->getFunction("free" , FreeType); |
| 70 | |
| 71 | // Don't mess with locally defined versions of these functions... |
| 72 | if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0; |
| 73 | if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0; |
| 74 | return false; |
| 75 | } |
| 76 | |
Chris Lattner | 03453a0 | 2002-05-07 19:04:39 +0000 | [diff] [blame^] | 77 | // runOnBasicBlock - Process a basic block, fixing it up... |
Chris Lattner | 77f791d | 2002-05-07 19:02:48 +0000 | [diff] [blame] | 78 | // |
| 79 | bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) { |
| 80 | bool Changed = false; |
| 81 | BasicBlock::InstListType &BIL = BB->getInstList(); |
| 82 | |
| 83 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
| 84 | Instruction *I = *BI; |
| 85 | |
| 86 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 87 | if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc? |
| 88 | const Type *PtrSByte = PointerType::get(Type::SByteTy); |
| 89 | MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1), |
| 90 | CI->getName()); |
| 91 | CI->setName(""); |
| 92 | ReplaceInstWithInst(BIL, BI, MallocI); |
| 93 | Changed = true; |
| 94 | continue; // Skip the ++BI |
| 95 | } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free? |
| 96 | ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1))); |
| 97 | Changed = true; |
| 98 | continue; // Skip the ++BI |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ++BI; |
| 103 | } |
| 104 | |
| 105 | return Changed; |
| 106 | } |