blob: 99003689fb1f52b225152f53b02cf66ebb997a25 [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"
19#include "llvm/Module.h"
20#include "llvm/Instructions.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/CallSite.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/ADT/Statistic.h"
25#include <algorithm>
26using namespace llvm;
27
28STATISTIC(NumRaised, "Number of allocations raised");
29
30namespace {
Dan Gohman2e4337e2007-08-27 16:11:48 +000031 // RaiseAllocations - Turn @malloc and @free calls into the appropriate
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 // instruction.
33 //
34 class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
35 Function *MallocFunc; // Functions in the module we are processing
36 Function *FreeFunc; // Initialized by doPassInitializationVirt
37 public:
38 static char ID; // Pass identification, replacement for typeid
39 RaiseAllocations()
Dan Gohman26f8c272008-09-04 17:05:41 +000040 : ModulePass(&ID), MallocFunc(0), FreeFunc(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
42 // doPassInitialization - For the raise allocations pass, this finds a
43 // declaration for malloc and free if they exist.
44 //
45 void doInitialization(Module &M);
46
47 // run - This method does the actual work of converting instructions over.
48 //
49 bool runOnModule(Module &M);
50 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051} // end anonymous namespace
52
Dan Gohman089efff2008-05-13 00:00:25 +000053char RaiseAllocations::ID = 0;
54static RegisterPass<RaiseAllocations>
55X("raiseallocs", "Raise allocations from calls to instructions");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57// createRaiseAllocationsPass - The interface to this file...
58ModulePass *llvm::createRaiseAllocationsPass() {
59 return new RaiseAllocations();
60}
61
62
63// If the module has a symbol table, they might be referring to the malloc and
64// free functions. If this is the case, grab the method pointers that the
65// module is using.
66//
Dan Gohman2e4337e2007-08-27 16:11:48 +000067// Lookup @malloc and @free in the symbol table, for later use. If they don't
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068// exist, or are not external, we do not worry about converting calls to that
69// function into the appropriate instruction.
70//
71void RaiseAllocations::doInitialization(Module &M) {
72
73 // Get Malloc and free prototypes if they exist!
74 MallocFunc = M.getFunction("malloc");
75 if (MallocFunc) {
76 const FunctionType* TyWeHave = MallocFunc->getFunctionType();
77
78 // Get the expected prototype for malloc
79 const FunctionType *Malloc1Type =
Christopher Lambbb2f2222007-12-17 01:12:55 +000080 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 std::vector<const Type*>(1, Type::Int64Ty), false);
82
83 // Chck to see if we got the expected malloc
84 if (TyWeHave != Malloc1Type) {
Dan Gohman9e1657f2009-06-14 23:30:43 +000085 // Check to see if the prototype is wrong, giving us i8*(i32) * malloc
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 // This handles the common declaration of: 'void *malloc(unsigned);'
87 const FunctionType *Malloc2Type =
Christopher Lambbb2f2222007-12-17 01:12:55 +000088 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 std::vector<const Type*>(1, Type::Int32Ty), false);
90 if (TyWeHave != Malloc2Type) {
91 // Check to see if the prototype is missing, giving us
Dan Gohman9e1657f2009-06-14 23:30:43 +000092 // i8*(...) * malloc
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 // This handles the common declaration of: 'void *malloc();'
94 const FunctionType *Malloc3Type =
Chris Lattner3fd51c02009-07-01 04:13:31 +000095 FunctionType::get(PointerType::getUnqual(Type::Int8Ty), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 if (TyWeHave != Malloc3Type)
97 // Give up
98 MallocFunc = 0;
99 }
100 }
101 }
102
103 FreeFunc = M.getFunction("free");
104 if (FreeFunc) {
105 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
106
107 // Get the expected prototype for void free(i8*)
108 const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
Christopher Lambbb2f2222007-12-17 01:12:55 +0000109 std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111 if (TyWeHave != Free1Type) {
112 // Check to see if the prototype was forgotten, giving us
113 // void (...) * free
114 // This handles the common forward declaration of: 'void free();'
Chris Lattner3fd51c02009-07-01 04:13:31 +0000115 const FunctionType* Free2Type = FunctionType::get(Type::VoidTy, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 if (TyWeHave != Free2Type) {
118 // One last try, check to see if we can find free as
119 // int (...)* free. This handles the case where NOTHING was declared.
Chris Lattner3fd51c02009-07-01 04:13:31 +0000120 const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 if (TyWeHave != Free3Type) {
123 // Give up.
124 FreeFunc = 0;
125 }
126 }
127 }
128 }
129
130 // Don't mess with locally defined versions of these functions...
131 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
132 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
133}
134
135// run - Transform calls into instructions...
136//
137bool RaiseAllocations::runOnModule(Module &M) {
138 // Find the malloc/free prototypes...
139 doInitialization(M);
140
141 bool Changed = false;
142
143 // First, process all of the malloc calls...
144 if (MallocFunc) {
145 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
146 std::vector<Value*> EqPointers; // Values equal to MallocFunc
147 while (!Users.empty()) {
148 User *U = Users.back();
149 Users.pop_back();
150
151 if (Instruction *I = dyn_cast<Instruction>(U)) {
152 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000153 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 (CS.getCalledFunction() == MallocFunc ||
155 std::find(EqPointers.begin(), EqPointers.end(),
156 CS.getCalledValue()) != EqPointers.end())) {
157
158 Value *Source = *CS.arg_begin();
159
160 // If no prototype was provided for malloc, we may need to cast the
161 // source size.
162 if (Source->getType() != Type::Int32Ty)
163 Source =
Gabor Greifa645dd32008-05-16 19:29:10 +0000164 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 "MallocAmtCast", I);
166
167 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
168 MI->takeName(I);
169 I->replaceAllUsesWith(MI);
170
171 // If the old instruction was an invoke, add an unconditional branch
172 // before the invoke, which will become the new terminator.
173 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000174 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
176 // Delete the old call site
Dan Gohmande087372008-06-21 22:08:46 +0000177 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 Changed = true;
179 ++NumRaised;
180 }
181 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
182 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
183 EqPointers.push_back(GV);
184 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
185 if (CE->isCast()) {
186 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
187 EqPointers.push_back(CE);
188 }
189 }
190 }
191 }
192
193 // Next, process all free calls...
194 if (FreeFunc) {
195 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
196 std::vector<Value*> EqPointers; // Values equal to FreeFunc
197
198 while (!Users.empty()) {
199 User *U = Users.back();
200 Users.pop_back();
201
202 if (Instruction *I = dyn_cast<Instruction>(U)) {
Devang Patelea682b32007-10-17 20:12:58 +0000203 if (isa<InvokeInst>(I))
204 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000206 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 (CS.getCalledFunction() == FreeFunc ||
208 std::find(EqPointers.begin(), EqPointers.end(),
209 CS.getCalledValue()) != EqPointers.end())) {
210
211 // If no prototype was provided for free, we may need to cast the
212 // source pointer. This should be really uncommon, but it's necessary
213 // just in case we are dealing with weird code like this:
214 // free((long)ptr);
215 //
216 Value *Source = *CS.arg_begin();
217 if (!isa<PointerType>(Source->getType()))
Christopher Lambbb2f2222007-12-17 01:12:55 +0000218 Source = new IntToPtrInst(Source,
219 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 "FreePtrCast", I);
221 new FreeInst(Source, I);
222
223 // If the old instruction was an invoke, add an unconditional branch
224 // before the invoke, which will become the new terminator.
225 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000226 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227
228 // Delete the old call site
229 if (I->getType() != Type::VoidTy)
230 I->replaceAllUsesWith(UndefValue::get(I->getType()));
231 I->eraseFromParent();
232 Changed = true;
233 ++NumRaised;
234 }
235 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
236 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
237 EqPointers.push_back(GV);
238 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
239 if (CE->isCast()) {
240 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
241 EqPointers.push_back(CE);
242 }
243 }
244 }
245 }
246
247 return Changed;
248}