1 //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RaiseAllocations pass which convert malloc and free
11 // calls to malloc and free instructions.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iMemory.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/iOther.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CallSite.h"
23 #include "Support/Statistic.h"
28 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
30 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
33 class RaiseAllocations : public Pass {
34 Function *MallocFunc; // Functions in the module we are processing
35 Function *FreeFunc; // Initialized by doPassInitializationVirt
37 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
39 // doPassInitialization - For the raise allocations pass, this finds a
40 // declaration for malloc and free if they exist.
42 void doInitialization(Module &M);
44 // run - This method does the actual work of converting instructions over.
49 RegisterOpt<RaiseAllocations>
50 X("raiseallocs", "Raise allocations from calls to instructions");
51 } // end anonymous namespace
54 // createRaiseAllocationsPass - The interface to this file...
55 Pass *createRaiseAllocationsPass() {
56 return new RaiseAllocations();
60 // If the module has a symbol table, they might be referring to the malloc and
61 // free functions. If this is the case, grab the method pointers that the
64 // Lookup %malloc and %free in the symbol table, for later use. If they don't
65 // exist, or are not external, we do not worry about converting calls to that
66 // function into the appropriate instruction.
68 void RaiseAllocations::doInitialization(Module &M) {
69 const FunctionType *MallocType = // Get the type for malloc
70 FunctionType::get(PointerType::get(Type::SByteTy),
71 std::vector<const Type*>(1, Type::ULongTy), false);
73 const FunctionType *FreeType = // Get the type for free
74 FunctionType::get(Type::VoidTy,
75 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
78 // Get Malloc and free prototypes if they exist!
79 MallocFunc = M.getFunction("malloc", MallocType);
80 FreeFunc = M.getFunction("free" , FreeType);
82 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
83 // This handles the common declaration of: 'void *malloc(unsigned);'
84 if (MallocFunc == 0) {
85 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
86 std::vector<const Type*>(1, Type::UIntTy), false);
87 MallocFunc = M.getFunction("malloc", MallocType);
90 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
91 // This handles the common declaration of: 'void *malloc();'
92 if (MallocFunc == 0) {
93 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
94 std::vector<const Type*>(), true);
95 MallocFunc = M.getFunction("malloc", MallocType);
98 // Check to see if the prototype was forgotten, giving us void (...) * free
99 // This handles the common forward declaration of: 'void free();'
101 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
102 FreeFunc = M.getFunction("free", FreeType);
105 // One last try, check to see if we can find free as 'int (...)* free'. This
106 // handles the case where NOTHING was declared.
108 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
109 FreeFunc = M.getFunction("free", FreeType);
112 // Don't mess with locally defined versions of these functions...
113 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
114 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
117 // run - Transform calls into instructions...
119 bool RaiseAllocations::run(Module &M) {
120 // Find the malloc/free prototypes...
123 bool Changed = false;
125 // First, process all of the malloc calls...
127 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
128 while (!Users.empty()) {
129 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
130 CallSite CS = CallSite::get(I);
131 if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
132 CS.arg_begin() != CS.arg_end()) {
133 Value *Source = *CS.arg_begin();
135 // If no prototype was provided for malloc, we may need to cast the
137 if (Source->getType() != Type::UIntTy)
138 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
140 std::string Name(I->getName()); I->setName("");
141 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
142 I->replaceAllUsesWith(MI);
144 // If the old instruction was an invoke, add an unconditional branch
145 // before the invoke, which will become the new terminator.
146 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
147 new BranchInst(II->getNormalDest(), I);
149 // Delete the old call site
150 MI->getParent()->getInstList().erase(I);
160 // Next, process all free calls...
162 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
164 while (!Users.empty()) {
165 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
166 CallSite CS = CallSite::get(I);
167 if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
168 CS.arg_begin() != CS.arg_end()) {
170 // If no prototype was provided for free, we may need to cast the
171 // source pointer. This should be really uncommon, but it's necessary
172 // just in case we are dealing with wierd code like this:
175 Value *Source = *CS.arg_begin();
176 if (!isa<PointerType>(Source->getType()))
177 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
179 new FreeInst(Source, I);
181 // If the old instruction was an invoke, add an unconditional branch
182 // before the invoke, which will become the new terminator.
183 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
184 new BranchInst(II->getNormalDest(), I);
186 // Delete the old call site
187 I->getParent()->getInstList().erase(I);
200 } // End llvm namespace