blob: 0ef0991637983878ebfe70ed2a13bae70016f732 [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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 // 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 =
Owen Anderson6b6e2d92009-07-29 22:17:13 +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 =
Owen Anderson6b6e2d92009-07-29 22:17:13 +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 =
Owen Anderson6b6e2d92009-07-29 22:17:13 +000095 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Owen Anderson086ea052009-07-06 01:34:54 +000096 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 if (TyWeHave != Malloc3Type)
98 // Give up
99 MallocFunc = 0;
100 }
101 }
102 }
103
104 FreeFunc = M.getFunction("free");
105 if (FreeFunc) {
106 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
107
108 // Get the expected prototype for void free(i8*)
Owen Anderson6b6e2d92009-07-29 22:17:13 +0000109 const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
110 std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)),
Owen Anderson086ea052009-07-06 01:34:54 +0000111 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 if (TyWeHave != Free1Type) {
114 // Check to see if the prototype was forgotten, giving us
115 // void (...) * free
116 // This handles the common forward declaration of: 'void free();'
Owen Anderson6b6e2d92009-07-29 22:17:13 +0000117 const FunctionType* Free2Type = FunctionType::get(Type::VoidTy,
Owen Anderson086ea052009-07-06 01:34:54 +0000118 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
120 if (TyWeHave != Free2Type) {
121 // One last try, check to see if we can find free as
122 // int (...)* free. This handles the case where NOTHING was declared.
Owen Anderson6b6e2d92009-07-29 22:17:13 +0000123 const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty,
Owen Anderson086ea052009-07-06 01:34:54 +0000124 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
126 if (TyWeHave != Free3Type) {
127 // Give up.
128 FreeFunc = 0;
129 }
130 }
131 }
132 }
133
134 // Don't mess with locally defined versions of these functions...
135 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
136 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
137}
138
139// run - Transform calls into instructions...
140//
141bool RaiseAllocations::runOnModule(Module &M) {
142 // Find the malloc/free prototypes...
143 doInitialization(M);
Owen Anderson175b6542009-07-22 00:24:57 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 bool Changed = false;
146
147 // First, process all of the malloc calls...
148 if (MallocFunc) {
149 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
150 std::vector<Value*> EqPointers; // Values equal to MallocFunc
151 while (!Users.empty()) {
152 User *U = Users.back();
153 Users.pop_back();
154
155 if (Instruction *I = dyn_cast<Instruction>(U)) {
156 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000157 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 (CS.getCalledFunction() == MallocFunc ||
159 std::find(EqPointers.begin(), EqPointers.end(),
160 CS.getCalledValue()) != EqPointers.end())) {
161
162 Value *Source = *CS.arg_begin();
163
164 // If no prototype was provided for malloc, we may need to cast the
165 // source size.
166 if (Source->getType() != Type::Int32Ty)
167 Source =
Gabor Greifa645dd32008-05-16 19:29:10 +0000168 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 "MallocAmtCast", I);
170
Owen Anderson140166d2009-07-15 23:53:25 +0000171 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 MI->takeName(I);
173 I->replaceAllUsesWith(MI);
174
175 // If the old instruction was an invoke, add an unconditional branch
176 // before the invoke, which will become the new terminator.
177 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000178 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
180 // Delete the old call site
Dan Gohmande087372008-06-21 22:08:46 +0000181 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 Changed = true;
183 ++NumRaised;
184 }
185 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
186 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
187 EqPointers.push_back(GV);
188 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
189 if (CE->isCast()) {
190 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
191 EqPointers.push_back(CE);
192 }
193 }
194 }
195 }
196
197 // Next, process all free calls...
198 if (FreeFunc) {
199 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
200 std::vector<Value*> EqPointers; // Values equal to FreeFunc
201
202 while (!Users.empty()) {
203 User *U = Users.back();
204 Users.pop_back();
205
206 if (Instruction *I = dyn_cast<Instruction>(U)) {
Devang Patelea682b32007-10-17 20:12:58 +0000207 if (isa<InvokeInst>(I))
208 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 CallSite CS = CallSite::get(I);
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000210 if (CS.getInstruction() && !CS.arg_empty() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 (CS.getCalledFunction() == FreeFunc ||
212 std::find(EqPointers.begin(), EqPointers.end(),
213 CS.getCalledValue()) != EqPointers.end())) {
214
215 // If no prototype was provided for free, we may need to cast the
216 // source pointer. This should be really uncommon, but it's necessary
217 // just in case we are dealing with weird code like this:
218 // free((long)ptr);
219 //
220 Value *Source = *CS.arg_begin();
221 if (!isa<PointerType>(Source->getType()))
Christopher Lambbb2f2222007-12-17 01:12:55 +0000222 Source = new IntToPtrInst(Source,
Owen Anderson6b6e2d92009-07-29 22:17:13 +0000223 PointerType::getUnqual(Type::Int8Ty),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 "FreePtrCast", I);
225 new FreeInst(Source, I);
226
227 // If the old instruction was an invoke, add an unconditional branch
228 // before the invoke, which will become the new terminator.
229 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greifd6da1d02008-04-06 20:25:17 +0000230 BranchInst::Create(II->getNormalDest(), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231
232 // Delete the old call site
233 if (I->getType() != Type::VoidTy)
Owen Andersonb99ecca2009-07-30 23:03:37 +0000234 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 I->eraseFromParent();
236 Changed = true;
237 ++NumRaised;
238 }
239 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
240 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
241 EqPointers.push_back(GV);
242 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
243 if (CE->isCast()) {
244 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
245 EqPointers.push_back(CE);
246 }
247 }
248 }
249 }
250
251 return Changed;
252}