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 #define DEBUG_TYPE "raiseallocs"
16 #include "llvm/Transforms/IPO.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CallSite.h"
23 #include "llvm/ADT/Statistic.h"
26 STATISTIC(NumRaised, "Number of allocations raised");
29 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
32 class RaiseAllocations : public ModulePass {
33 Function *MallocFunc; // Functions in the module we are processing
34 Function *FreeFunc; // Initialized by doPassInitializationVirt
36 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
38 // doPassInitialization - For the raise allocations pass, this finds a
39 // declaration for malloc and free if they exist.
41 void doInitialization(Module &M);
43 // run - This method does the actual work of converting instructions over.
45 bool runOnModule(Module &M);
48 RegisterPass<RaiseAllocations>
49 X("raiseallocs", "Raise allocations from calls to instructions");
50 } // end anonymous namespace
53 // createRaiseAllocationsPass - The interface to this file...
54 ModulePass *llvm::createRaiseAllocationsPass() {
55 return new RaiseAllocations();
59 // If the module has a symbol table, they might be referring to the malloc and
60 // free functions. If this is the case, grab the method pointers that the
63 // Lookup %malloc and %free in the symbol table, for later use. If they don't
64 // exist, or are not external, we do not worry about converting calls to that
65 // function into the appropriate instruction.
67 void RaiseAllocations::doInitialization(Module &M) {
69 // Get Malloc and free prototypes if they exist!
70 MallocFunc = M.getFunction("malloc");
72 const FunctionType* TyWeHave = MallocFunc->getFunctionType();
74 // Get the expected prototype for malloc
75 const FunctionType *Malloc1Type =
76 FunctionType::get(PointerType::get(Type::Int8Ty),
77 std::vector<const Type*>(1, Type::Int64Ty), false);
79 // Chck to see if we got the expected malloc
80 if (TyWeHave != Malloc1Type) {
81 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
82 // This handles the common declaration of: 'void *malloc(unsigned);'
83 const FunctionType *Malloc2Type =
84 FunctionType::get(PointerType::get(Type::Int8Ty),
85 std::vector<const Type*>(1, Type::Int32Ty), false);
86 if (TyWeHave != Malloc2Type) {
87 // Check to see if the prototype is missing, giving us
88 // sbyte*(...) * malloc
89 // This handles the common declaration of: 'void *malloc();'
90 const FunctionType *Malloc3Type =
91 FunctionType::get(PointerType::get(Type::Int8Ty),
92 std::vector<const Type*>(), true);
93 if (TyWeHave != Malloc3Type)
100 FreeFunc = M.getFunction("free");
102 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
104 // Get the expected prototype for void free(i8*)
105 const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
106 std::vector<const Type*>(1, PointerType::get(Type::Int8Ty)), false);
108 if (TyWeHave != Free1Type) {
109 // Check to see if the prototype was forgotten, giving us
111 // This handles the common forward declaration of: 'void free();'
112 const FunctionType* Free2Type = FunctionType::get(Type::VoidTy,
113 std::vector<const Type*>(),true);
115 if (TyWeHave != Free2Type) {
116 // One last try, check to see if we can find free as
117 // int (...)* free. This handles the case where NOTHING was declared.
118 const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty,
119 std::vector<const Type*>(),true);
121 if (TyWeHave != Free3Type) {
129 // Don't mess with locally defined versions of these functions...
130 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
131 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
134 // run - Transform calls into instructions...
136 bool RaiseAllocations::runOnModule(Module &M) {
137 // Find the malloc/free prototypes...
140 bool Changed = false;
142 // First, process all of the malloc calls...
144 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
145 std::vector<Value*> EqPointers; // Values equal to MallocFunc
146 while (!Users.empty()) {
147 User *U = Users.back();
150 if (Instruction *I = dyn_cast<Instruction>(U)) {
151 CallSite CS = CallSite::get(I);
152 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
153 (CS.getCalledFunction() == MallocFunc ||
154 std::find(EqPointers.begin(), EqPointers.end(),
155 CS.getCalledValue()) != EqPointers.end())) {
157 Value *Source = *CS.arg_begin();
159 // If no prototype was provided for malloc, we may need to cast the
161 if (Source->getType() != Type::Int32Ty)
163 CastInst::createIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
166 std::string Name(I->getName()); I->setName("");
167 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, Name, I);
168 I->replaceAllUsesWith(MI);
170 // If the old instruction was an invoke, add an unconditional branch
171 // before the invoke, which will become the new terminator.
172 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
173 new BranchInst(II->getNormalDest(), I);
175 // Delete the old call site
176 MI->getParent()->getInstList().erase(I);
180 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
181 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
182 EqPointers.push_back(GV);
183 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
185 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
186 EqPointers.push_back(CE);
192 // Next, process all free calls...
194 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
195 std::vector<Value*> EqPointers; // Values equal to FreeFunc
197 while (!Users.empty()) {
198 User *U = Users.back();
201 if (Instruction *I = dyn_cast<Instruction>(U)) {
202 CallSite CS = CallSite::get(I);
203 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
204 (CS.getCalledFunction() == FreeFunc ||
205 std::find(EqPointers.begin(), EqPointers.end(),
206 CS.getCalledValue()) != EqPointers.end())) {
208 // If no prototype was provided for free, we may need to cast the
209 // source pointer. This should be really uncommon, but it's necessary
210 // just in case we are dealing with weird code like this:
213 Value *Source = *CS.arg_begin();
214 if (!isa<PointerType>(Source->getType()))
215 Source = new IntToPtrInst(Source, PointerType::get(Type::Int8Ty),
217 new FreeInst(Source, I);
219 // If the old instruction was an invoke, add an unconditional branch
220 // before the invoke, which will become the new terminator.
221 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
222 new BranchInst(II->getNormalDest(), I);
224 // Delete the old call site
225 if (I->getType() != Type::VoidTy)
226 I->replaceAllUsesWith(UndefValue::get(I->getType()));
227 I->eraseFromParent();
231 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
232 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
233 EqPointers.push_back(GV);
234 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
236 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
237 EqPointers.push_back(CE);