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/StatisticReporter.h"
17 static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
21 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
24 class RaiseAllocations : public BasicBlockPass {
25 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doPassInitializationVirt
28 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
30 // doPassInitialization - For the raise allocations pass, this finds a
31 // declaration for malloc and free if they exist.
33 bool doInitialization(Module &M);
35 // runOnBasicBlock - This method does the actual work of converting
36 // instructions over, assuming that the pass has already been initialized.
38 bool runOnBasicBlock(BasicBlock &BB);
41 RegisterOpt<RaiseAllocations>
42 X("raiseallocs", "Raise allocations from calls to instructions");
43 } // end anonymous namespace
46 // createRaiseAllocationsPass - The interface to this file...
47 Pass *createRaiseAllocationsPass() {
48 return new RaiseAllocations();
52 bool RaiseAllocations::doInitialization(Module &M) {
53 // If the module has a symbol table, they might be referring to the malloc
54 // and free functions. If this is the case, grab the method pointers that
55 // the module is using.
57 // Lookup %malloc and %free in the symbol table, for later use. If they
58 // don't exist, or are not external, we do not worry about converting calls
59 // to that function into the appropriate instruction.
61 const FunctionType *MallocType = // Get the type for malloc
62 FunctionType::get(PointerType::get(Type::SByteTy),
63 std::vector<const Type*>(1, Type::ULongTy), false);
65 const FunctionType *FreeType = // Get the type for free
66 FunctionType::get(Type::VoidTy,
67 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
70 // Get Malloc and free prototypes if they exist!
71 MallocFunc = M.getFunction("malloc", MallocType);
72 FreeFunc = M.getFunction("free" , FreeType);
74 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
75 // This handles the common declaration of: 'void *malloc(unsigned);'
76 if (MallocFunc == 0) {
77 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
78 std::vector<const Type*>(1, Type::UIntTy), false);
79 MallocFunc = M.getFunction("malloc", MallocType);
82 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
83 // This handles the common declaration of: 'void *malloc();'
84 if (MallocFunc == 0) {
85 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
86 std::vector<const Type*>(), true);
87 MallocFunc = M.getFunction("malloc", MallocType);
90 // Check to see if the prototype was forgotten, giving us void (...) * free
91 // This handles the common forward declaration of: 'void free();'
93 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
94 FreeFunc = M.getFunction("free", FreeType);
98 // Don't mess with locally defined versions of these functions...
99 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
100 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
104 // runOnBasicBlock - Process a basic block, fixing it up...
106 bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
107 bool Changed = false;
108 BasicBlock::InstListType &BIL = BB.getInstList();
110 for (BasicBlock::iterator BI = BB.begin(); BI != BB.end(); ++BI) {
113 if (CallInst *CI = dyn_cast<CallInst>(I)) {
114 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
115 Value *Source = CI->getOperand(1);
117 // If no prototype was provided for malloc, we may need to cast the
119 if (Source->getType() != Type::UIntTy)
120 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", BI);
122 std::string Name(CI->getName()); CI->setName("");
123 BI = new MallocInst(Type::SByteTy, Source, Name, BI);
124 CI->replaceAllUsesWith(BI);
128 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
129 // If no prototype was provided for free, we may need to cast the
130 // source pointer. This should be really uncommon, but it's neccesary
131 // just in case we are dealing with wierd code like this:
134 Value *Source = CI->getOperand(1);
135 if (!isa<PointerType>(Source->getType()))
136 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
138 BI = new FreeInst(Source, BI);