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/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/ADT/Statistic.h"
26 Statistic NumRaised("raiseallocs", "Number of allocations raised");
28 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
31 class RaiseAllocations : public ModulePass {
32 Function *MallocFunc; // Functions in the module we are processing
33 Function *FreeFunc; // Initialized by doPassInitializationVirt
35 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
37 // doPassInitialization - For the raise allocations pass, this finds a
38 // declaration for malloc and free if they exist.
40 void doInitialization(Module &M);
42 // run - This method does the actual work of converting instructions over.
44 bool runOnModule(Module &M);
47 RegisterPass<RaiseAllocations>
48 X("raiseallocs", "Raise allocations from calls to instructions");
49 } // end anonymous namespace
52 // createRaiseAllocationsPass - The interface to this file...
53 ModulePass *llvm::createRaiseAllocationsPass() {
54 return new RaiseAllocations();
58 // If the module has a symbol table, they might be referring to the malloc and
59 // free functions. If this is the case, grab the method pointers that the
62 // Lookup %malloc and %free in the symbol table, for later use. If they don't
63 // exist, or are not external, we do not worry about converting calls to that
64 // function into the appropriate instruction.
66 void RaiseAllocations::doInitialization(Module &M) {
67 const FunctionType *MallocType = // Get the type for malloc
68 FunctionType::get(PointerType::get(Type::SByteTy),
69 std::vector<const Type*>(1, Type::ULongTy), false);
71 const FunctionType *FreeType = // Get the type for free
72 FunctionType::get(Type::VoidTy,
73 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
76 // Get Malloc and free prototypes if they exist!
77 MallocFunc = M.getFunction("malloc", MallocType);
78 FreeFunc = M.getFunction("free" , FreeType);
80 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
81 // This handles the common declaration of: 'void *malloc(unsigned);'
82 if (MallocFunc == 0) {
83 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
84 std::vector<const Type*>(1, Type::UIntTy), false);
85 MallocFunc = M.getFunction("malloc", MallocType);
88 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
89 // This handles the common declaration of: 'void *malloc();'
90 if (MallocFunc == 0) {
91 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
92 std::vector<const Type*>(), true);
93 MallocFunc = M.getFunction("malloc", MallocType);
96 // Check to see if the prototype was forgotten, giving us void (...) * free
97 // This handles the common forward declaration of: 'void free();'
99 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
100 FreeFunc = M.getFunction("free", FreeType);
103 // One last try, check to see if we can find free as 'int (...)* free'. This
104 // handles the case where NOTHING was declared.
106 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
107 FreeFunc = M.getFunction("free", FreeType);
110 // Don't mess with locally defined versions of these functions...
111 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
112 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
115 // run - Transform calls into instructions...
117 bool RaiseAllocations::runOnModule(Module &M) {
118 // Find the malloc/free prototypes...
121 bool Changed = false;
123 // First, process all of the malloc calls...
125 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
126 std::vector<Value*> EqPointers; // Values equal to MallocFunc
127 while (!Users.empty()) {
128 User *U = Users.back();
131 if (Instruction *I = dyn_cast<Instruction>(U)) {
132 CallSite CS = CallSite::get(I);
133 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
134 (CS.getCalledFunction() == MallocFunc ||
135 std::find(EqPointers.begin(), EqPointers.end(),
136 CS.getCalledValue()) != EqPointers.end())) {
138 Value *Source = *CS.arg_begin();
140 // If no prototype was provided for malloc, we may need to cast the
142 if (Source->getType() != Type::UIntTy)
144 CastInst::createInferredCast(Source, Type::UIntTy,
147 std::string Name(I->getName()); I->setName("");
148 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
149 I->replaceAllUsesWith(MI);
151 // If the old instruction was an invoke, add an unconditional branch
152 // before the invoke, which will become the new terminator.
153 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
154 new BranchInst(II->getNormalDest(), I);
156 // Delete the old call site
157 MI->getParent()->getInstList().erase(I);
161 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
162 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
163 EqPointers.push_back(GV);
164 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
166 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
167 EqPointers.push_back(CE);
173 // Next, process all free calls...
175 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
176 std::vector<Value*> EqPointers; // Values equal to FreeFunc
178 while (!Users.empty()) {
179 User *U = Users.back();
182 if (Instruction *I = dyn_cast<Instruction>(U)) {
183 CallSite CS = CallSite::get(I);
184 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
185 (CS.getCalledFunction() == FreeFunc ||
186 std::find(EqPointers.begin(), EqPointers.end(),
187 CS.getCalledValue()) != EqPointers.end())) {
189 // If no prototype was provided for free, we may need to cast the
190 // source pointer. This should be really uncommon, but it's necessary
191 // just in case we are dealing with weird code like this:
194 Value *Source = *CS.arg_begin();
195 if (!isa<PointerType>(Source->getType()))
196 Source = CastInst::createInferredCast(
197 Source, PointerType::get(Type::SByteTy), "FreePtrCast", I);
198 new FreeInst(Source, I);
200 // If the old instruction was an invoke, add an unconditional branch
201 // before the invoke, which will become the new terminator.
202 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
203 new BranchInst(II->getNormalDest(), I);
205 // Delete the old call site
206 if (I->getType() != Type::VoidTy)
207 I->replaceAllUsesWith(UndefValue::get(I->getType()));
208 I->eraseFromParent();
212 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
213 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
214 EqPointers.push_back(GV);
215 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
217 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
218 EqPointers.push_back(CE);