blob: 8d2a9cbdd5a4cbb2621ef260ba362997b90154c9 [file] [log] [blame]
Dan Gohman2e4337e2007-08-27 16:11:48 +00001//===- RaiseAllocations.cpp - Convert @malloc & @free calls to insts ------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the RaiseAllocations pass which convert malloc and free
11// calls to malloc and free instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "raiseallocs"
16#include "llvm/Transforms/IPO.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Owen Anderson086ea052009-07-06 01:34:54 +000019#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Module.h"
21#include "llvm/Instructions.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/CallSite.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/ADT/Statistic.h"
26#include <algorithm>
27using namespace llvm;
28
29STATISTIC(NumRaised, "Number of allocations raised");
30
31namespace {
Dan Gohman2e4337e2007-08-27 16:11:48 +000032 // RaiseAllocations - Turn @malloc and @free calls into the appropriate
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 // instruction.
34 //
35 class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
36 Function *MallocFunc; // Functions in the module we are processing
37 Function *FreeFunc; // Initialized by doPassInitializationVirt
38 public:
39 static char ID; // Pass identification, replacement for typeid
40 RaiseAllocations()
Dan Gohman26f8c272008-09-04 17:05:41 +000041 : ModulePass(&ID), MallocFunc(0), FreeFunc(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
43 // doPassInitialization - For the raise allocations pass, this finds a
44 // declaration for malloc and free if they exist.
45 //
46 void doInitialization(Module &M);
47
48 // run - This method does the actual work of converting instructions over.
49 //
50 bool runOnModule(Module &M);
51 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052} // end anonymous namespace
53
Dan Gohman089efff2008-05-13 00:00:25 +000054char RaiseAllocations::ID = 0;
55static RegisterPass<RaiseAllocations>
56X("raiseallocs", "Raise allocations from calls to instructions");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58// createRaiseAllocationsPass - The interface to this file...
59ModulePass *llvm::createRaiseAllocationsPass() {
60 return new RaiseAllocations();
61}
62
63
64// If the module has a symbol table, they might be referring to the malloc and
65// free functions. If this is the case, grab the method pointers that the
66// module is using.
67//
Dan Gohman2e4337e2007-08-27 16:11:48 +000068// Lookup @malloc and @free in the symbol table, for later use. If they don't
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069// exist, or are not external, we do not worry about converting calls to that
70// function into the appropriate instruction.
71//
72void RaiseAllocations::doInitialization(Module &M) {
Owen Andersone1f1f822009-07-16 18:04:31 +000073 Context = &M.getContext();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 // Get Malloc and free prototypes if they exist!
76 MallocFunc = M.getFunction("malloc");
77 if (MallocFunc) {
78 const FunctionType* TyWeHave = MallocFunc->getFunctionType();
79
80 // Get the expected prototype for malloc
81 const FunctionType *Malloc1Type =
Owen Anderson086ea052009-07-06 01:34:54 +000082 Context->getFunctionType(Context->getPointerTypeUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 std::vector<const Type*>(1, Type::Int64Ty), false);
84
85 // Chck to see if we got the expected malloc
86 if (TyWeHave != Malloc1Type) {
Dan Gohman9e1657f2009-06-14 23:30:43 +000087 // Check to see if the prototype is wrong, giving us i8*(i32) * malloc
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 // This handles the common declaration of: 'void *malloc(unsigned);'
89 const FunctionType *Malloc2Type =
Owen Anderson086ea052009-07-06 01:34:54 +000090 Context->getFunctionType(Context->getPointerTypeUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 std::vector<const Type*>(1, Type::Int32Ty), false);
92 if (TyWeHave != Malloc2Type) {
93 // Check to see if the prototype is missing, giving us
Dan Gohman9e1657f2009-06-14 23:30:43 +000094 // i8*(...) * malloc
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 // This handles the common declaration of: 'void *malloc();'
96 const FunctionType *Malloc3Type =
Owen Anderson086ea052009-07-06 01:34:54 +000097 Context->getFunctionType(Context->getPointerTypeUnqual(Type::Int8Ty),
98 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 if (TyWeHave != Malloc3Type)
100 // Give up
101 MallocFunc = 0;
102 }
103 }
104 }
105
106 FreeFunc = M.getFunction("free");
107 if (FreeFunc) {
108 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
109
110 // Get the expected prototype for void free(i8*)
Owen Anderson086ea052009-07-06 01:34:54 +0000111 const FunctionType *Free1Type = Context->getFunctionType(Type::VoidTy,
112 std::vector<const Type*>(1, Context->getPointerTypeUnqual(Type::Int8Ty)),
113 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 if (TyWeHave != Free1Type) {
116 // Check to see if the prototype was forgotten, giving us
117 // void (...) * free
118 // This handles the common forward declaration of: 'void free();'
Owen Anderson086ea052009-07-06 01:34:54 +0000119 const FunctionType* Free2Type = Context->getFunctionType(Type::VoidTy,
120 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 if (TyWeHave != Free2Type) {
123 // One last try, check to see if we can find free as
124 // int (...)* free. This handles the case where NOTHING was declared.
Owen Anderson086ea052009-07-06 01:34:54 +0000125 const FunctionType* Free3Type = Context->getFunctionType(Type::Int32Ty,
126 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
128 if (TyWeHave != Free3Type) {
129 // Give up.
130 FreeFunc = 0;
131 }
132 }
133 }
134 }
135
136 // Don't mess with locally defined versions of these functions...
137 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
138 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
139}
140
141// run - Transform calls into instructions...
142//
143bool RaiseAllocations::runOnModule(Module &M) {
144 // Find the malloc/free prototypes...
145 doInitialization(M);
146
147 bool Changed = false;
148
149 // First, process all of the malloc calls...
150 if (MallocFunc) {
151 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
152 std::vector<Value*> EqPointers; // Values equal to MallocFunc
153 while (!Users.empty()) {
154 User *U = Users.back();
155 Users.pop_back();
156
157 if (Instruction *I = dyn_cast<Instruction>(U)) {
158 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000159 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 (CS.getCalledFunction() == MallocFunc ||
161 std::find(EqPointers.begin(), EqPointers.end(),
162 CS.getCalledValue()) != EqPointers.end())) {
163
164 Value *Source = *CS.arg_begin();
165
166 // If no prototype was provided for malloc, we may need to cast the
167 // source size.
168 if (Source->getType() != Type::Int32Ty)
169 Source =
Gabor Greifa645dd32008-05-16 19:29:10 +0000170 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 "MallocAmtCast", I);
172
Owen Anderson140166d2009-07-15 23:53:25 +0000173 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 MI->takeName(I);
175 I->replaceAllUsesWith(MI);
176
177 // If the old instruction was an invoke, add an unconditional branch
178 // before the invoke, which will become the new terminator.
179 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000180 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 // Delete the old call site
Dan Gohmande087372008-06-21 22:08:46 +0000183 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 Changed = true;
185 ++NumRaised;
186 }
187 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
188 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
189 EqPointers.push_back(GV);
190 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
191 if (CE->isCast()) {
192 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
193 EqPointers.push_back(CE);
194 }
195 }
196 }
197 }
198
199 // Next, process all free calls...
200 if (FreeFunc) {
201 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
202 std::vector<Value*> EqPointers; // Values equal to FreeFunc
203
204 while (!Users.empty()) {
205 User *U = Users.back();
206 Users.pop_back();
207
208 if (Instruction *I = dyn_cast<Instruction>(U)) {
Devang Patelea682b32007-10-17 20:12:58 +0000209 if (isa<InvokeInst>(I))
210 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000212 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 (CS.getCalledFunction() == FreeFunc ||
214 std::find(EqPointers.begin(), EqPointers.end(),
215 CS.getCalledValue()) != EqPointers.end())) {
216
217 // If no prototype was provided for free, we may need to cast the
218 // source pointer. This should be really uncommon, but it's necessary
219 // just in case we are dealing with weird code like this:
220 // free((long)ptr);
221 //
222 Value *Source = *CS.arg_begin();
223 if (!isa<PointerType>(Source->getType()))
Christopher Lambbb2f2222007-12-17 01:12:55 +0000224 Source = new IntToPtrInst(Source,
Owen Anderson086ea052009-07-06 01:34:54 +0000225 Context->getPointerTypeUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 "FreePtrCast", I);
227 new FreeInst(Source, I);
228
229 // If the old instruction was an invoke, add an unconditional branch
230 // before the invoke, which will become the new terminator.
231 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000232 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
234 // Delete the old call site
235 if (I->getType() != Type::VoidTy)
Owen Anderson086ea052009-07-06 01:34:54 +0000236 I->replaceAllUsesWith(Context->getUndef(I->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 I->eraseFromParent();
238 Changed = true;
239 ++NumRaised;
240 }
241 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
242 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
243 EqPointers.push_back(GV);
244 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
245 if (CE->isCast()) {
246 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
247 EqPointers.push_back(CE);
248 }
249 }
250 }
251 }
252
253 return Changed;
254}