1 //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
3 // This file defines the RaiseAllocations pass which convert malloc and free
4 // calls to malloc and free instructions.
6 //===----------------------------------------------------------------------===//
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 #include "Support/StatisticReporter.h"
18 static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
22 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
25 class RaiseAllocations : public BasicBlockPass {
26 Function *MallocFunc; // Functions in the module we are processing
27 Function *FreeFunc; // Initialized by doPassInitializationVirt
29 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
31 const char *getPassName() const { return "Raise Allocations"; }
33 // doPassInitialization - For the raise allocations pass, this finds a
34 // declaration for malloc and free if they exist.
36 bool doInitialization(Module *M);
38 // runOnBasicBlock - This method does the actual work of converting
39 // instructions over, assuming that the pass has already been initialized.
41 bool runOnBasicBlock(BasicBlock *BB);
44 } // end anonymous namespace
47 // createRaiseAllocationsPass - The interface to this file...
48 Pass *createRaiseAllocationsPass() {
49 return new RaiseAllocations();
53 bool RaiseAllocations::doInitialization(Module *M) {
54 // If the module has a symbol table, they might be referring to the malloc
55 // and free functions. If this is the case, grab the method pointers that
56 // the module is using.
58 // Lookup %malloc and %free in the symbol table, for later use. If they
59 // don't exist, or are not external, we do not worry about converting calls
60 // to that function into the appropriate instruction.
62 const FunctionType *MallocType = // Get the type for malloc
63 FunctionType::get(PointerType::get(Type::SByteTy),
64 std::vector<const Type*>(1, Type::UIntTy), false);
66 const FunctionType *FreeType = // Get the type for free
67 FunctionType::get(Type::VoidTy,
68 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
71 MallocFunc = M->getFunction("malloc", MallocType);
72 FreeFunc = M->getFunction("free" , FreeType);
74 // Don't mess with locally defined versions of these functions...
75 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
76 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
80 // runOnBasicBlock - Process a basic block, fixing it up...
82 bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
84 BasicBlock::InstListType &BIL = BB->getInstList();
86 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
89 if (CallInst *CI = dyn_cast<CallInst>(I)) {
90 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
91 const Type *PtrSByte = PointerType::get(Type::SByteTy);
92 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
95 ReplaceInstWithInst(BIL, BI, MallocI);
98 continue; // Skip the ++BI
99 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
100 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
102 continue; // Skip the ++BI