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/IPO.h"
9 #include "llvm/Module.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/iMemory.h"
12 #include "llvm/iTerminators.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Support/CallSite.h"
16 #include "Support/Statistic.h"
19 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
21 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
24 class RaiseAllocations : public Pass {
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 void doInitialization(Module &M);
35 // run - This method does the actual work of converting instructions over.
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 // If the module has a symbol table, they might be referring to the malloc and
52 // free functions. If this is the case, grab the method pointers that the
55 // Lookup %malloc and %free in the symbol table, for later use. If they don't
56 // exist, or are not external, we do not worry about converting calls to that
57 // function into the appropriate instruction.
59 void RaiseAllocations::doInitialization(Module &M) {
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);
96 // One last try, check to see if we can find free as 'int (...)* free'. This
97 // handles the case where NOTHING was declared.
99 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
100 FreeFunc = M.getFunction("free", FreeType);
103 // Don't mess with locally defined versions of these functions...
104 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
105 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
108 // run - Transform calls into instructions...
110 bool RaiseAllocations::run(Module &M) {
111 // Find the malloc/free prototypes...
114 bool Changed = false;
116 // First, process all of the malloc calls...
118 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
119 while (!Users.empty()) {
120 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
121 CallSite CS = CallSite::get(I);
122 if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
123 CS.arg_begin() != CS.arg_end()) {
124 Value *Source = *CS.arg_begin();
126 // If no prototype was provided for malloc, we may need to cast the
128 if (Source->getType() != Type::UIntTy)
129 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
131 std::string Name(I->getName()); I->setName("");
132 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
133 I->replaceAllUsesWith(MI);
135 // If the old instruction was an invoke, add an unconditional branch
136 // before the invoke, which will become the new terminator.
137 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
138 new BranchInst(II->getNormalDest(), I);
140 // Delete the old call site
141 MI->getParent()->getInstList().erase(I);
151 // Next, process all free calls...
153 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
155 while (!Users.empty()) {
156 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
157 CallSite CS = CallSite::get(I);
158 if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
159 CS.arg_begin() != CS.arg_end()) {
161 // If no prototype was provided for free, we may need to cast the
162 // source pointer. This should be really uncommon, but it's necessary
163 // just in case we are dealing with wierd code like this:
166 Value *Source = *CS.arg_begin();
167 if (!isa<PointerType>(Source->getType()))
168 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
170 new FreeInst(Source, I);
172 // If the old instruction was an invoke, add an unconditional branch
173 // before the invoke, which will become the new terminator.
174 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
175 new BranchInst(II->getNormalDest(), I);
177 // Delete the old call site
178 I->getParent()->getInstList().erase(I);