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/Scalar.h"
9 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
10 #include "llvm/Module.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Pass.h"
15 #include "Support/Statistic.h"
18 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
20 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
23 class RaiseAllocations : public BasicBlockPass {
24 Function *MallocFunc; // Functions in the module we are processing
25 Function *FreeFunc; // Initialized by doPassInitializationVirt
27 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
29 // doPassInitialization - For the raise allocations pass, this finds a
30 // declaration for malloc and free if they exist.
32 bool doInitialization(Module &M);
34 // runOnBasicBlock - This method does the actual work of converting
35 // instructions over, assuming that the pass has already been initialized.
37 bool runOnBasicBlock(BasicBlock &BB);
40 RegisterOpt<RaiseAllocations>
41 X("raiseallocs", "Raise allocations from calls to instructions");
42 } // end anonymous namespace
45 // createRaiseAllocationsPass - The interface to this file...
46 Pass *createRaiseAllocationsPass() {
47 return new RaiseAllocations();
51 bool RaiseAllocations::doInitialization(Module &M) {
52 // If the module has a symbol table, they might be referring to the malloc
53 // and free functions. If this is the case, grab the method pointers that
54 // the module is using.
56 // Lookup %malloc and %free in the symbol table, for later use. If they
57 // don't exist, or are not external, we do not worry about converting calls
58 // to that function into the appropriate instruction.
60 const FunctionType *MallocType = // Get the type for malloc
61 FunctionType::get(PointerType::get(Type::SByteTy),
62 std::vector<const Type*>(1, Type::ULongTy), false);
64 const FunctionType *FreeType = // Get the type for free
65 FunctionType::get(Type::VoidTy,
66 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
69 // Get Malloc and free prototypes if they exist!
70 MallocFunc = M.getFunction("malloc", MallocType);
71 FreeFunc = M.getFunction("free" , FreeType);
73 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
74 // This handles the common declaration of: 'void *malloc(unsigned);'
75 if (MallocFunc == 0) {
76 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
77 std::vector<const Type*>(1, Type::UIntTy), false);
78 MallocFunc = M.getFunction("malloc", MallocType);
81 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
82 // This handles the common declaration of: 'void *malloc();'
83 if (MallocFunc == 0) {
84 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
85 std::vector<const Type*>(), true);
86 MallocFunc = M.getFunction("malloc", MallocType);
89 // Check to see if the prototype was forgotten, giving us void (...) * free
90 // This handles the common forward declaration of: 'void free();'
92 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
93 FreeFunc = M.getFunction("free", FreeType);
97 // Don't mess with locally defined versions of these functions...
98 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
99 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
103 // runOnBasicBlock - Process a basic block, fixing it up...
105 bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
106 bool Changed = false;
107 BasicBlock::InstListType &BIL = BB.getInstList();
109 for (BasicBlock::iterator BI = BB.begin(); BI != BB.end(); ++BI) {
112 if (CallInst *CI = dyn_cast<CallInst>(I)) {
113 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
114 Value *Source = CI->getOperand(1);
116 // If no prototype was provided for malloc, we may need to cast the
118 if (Source->getType() != Type::UIntTy)
119 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", BI);
121 std::string Name(CI->getName()); CI->setName("");
122 BI = new MallocInst(Type::SByteTy, Source, Name, BI);
123 CI->replaceAllUsesWith(BI);
127 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
128 // If no prototype was provided for free, we may need to cast the
129 // source pointer. This should be really uncommon, but it's neccesary
130 // just in case we are dealing with wierd code like this:
133 Value *Source = CI->getOperand(1);
134 if (!isa<PointerType>(Source->getType()))
135 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
137 BI = new FreeInst(Source, BI);