blob: 7b4ad27694136158c89d519075969e6dae74ebff [file] [log] [blame]
Dan Gohmane3784952007-08-27 16:11:48 +00001//===- RaiseAllocations.cpp - Convert @malloc & @free calls to insts ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerade686e2002-05-07 19:02:48 +00009//
Chris Lattner65e96e52002-05-07 19:04:39 +000010// This file defines the RaiseAllocations pass which convert malloc and free
11// calls to malloc and free instructions.
Chris Lattnerade686e2002-05-07 19:02:48 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner86453c52006-12-19 22:09:18 +000015#define DEBUG_TYPE "raiseallocs"
Chris Lattner2dbfa032003-09-01 03:14:56 +000016#include "llvm/Transforms/IPO.h"
Chris Lattnereb12cd62003-12-07 01:42:08 +000017#include "llvm/Constants.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000018#include "llvm/DerivedTypes.h"
Owen Anderson14ce9ef2009-07-06 01:34:54 +000019#include "llvm/LLVMContext.h"
Chris Lattnereb12cd62003-12-07 01:42:08 +000020#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000021#include "llvm/Instructions.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000022#include "llvm/Pass.h"
Chris Lattner2dbfa032003-09-01 03:14:56 +000023#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000026#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner86453c52006-12-19 22:09:18 +000029STATISTIC(NumRaised, "Number of allocations raised");
Chris Lattnerade686e2002-05-07 19:02:48 +000030
Chris Lattner86453c52006-12-19 22:09:18 +000031namespace {
Dan Gohmane3784952007-08-27 16:11:48 +000032 // RaiseAllocations - Turn @malloc and @free calls into the appropriate
Chris Lattnera92f6962002-10-01 22:38:41 +000033 // instruction.
Chris Lattnerade686e2002-05-07 19:02:48 +000034 //
Reid Spencer9133fe22007-02-05 23:32:05 +000035 class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
Chris Lattnera92f6962002-10-01 22:38:41 +000036 Function *MallocFunc; // Functions in the module we are processing
37 Function *FreeFunc; // Initialized by doPassInitializationVirt
38 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000040 RaiseAllocations()
Dan Gohmanae73dc12008-09-04 17:05:41 +000041 : ModulePass(&ID), MallocFunc(0), FreeFunc(0) {}
Misha Brukmanfd939082005-04-21 23:48:37 +000042
Chris Lattnera92f6962002-10-01 22:38:41 +000043 // doPassInitialization - For the raise allocations pass, this finds a
44 // declaration for malloc and free if they exist.
45 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000046 void doInitialization(Module &M);
Misha Brukmanfd939082005-04-21 23:48:37 +000047
Chris Lattner2dbfa032003-09-01 03:14:56 +000048 // run - This method does the actual work of converting instructions over.
Chris Lattnera92f6962002-10-01 22:38:41 +000049 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000050 bool runOnModule(Module &M);
Chris Lattnera92f6962002-10-01 22:38:41 +000051 };
Chris Lattnerade686e2002-05-07 19:02:48 +000052} // end anonymous namespace
53
Dan Gohman844731a2008-05-13 00:00:25 +000054char RaiseAllocations::ID = 0;
55static RegisterPass<RaiseAllocations>
56X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattnerade686e2002-05-07 19:02:48 +000057
58// createRaiseAllocationsPass - The interface to this file...
Chris Lattnerb12914b2004-09-20 04:48:05 +000059ModulePass *llvm::createRaiseAllocationsPass() {
Chris Lattnerade686e2002-05-07 19:02:48 +000060 return new RaiseAllocations();
61}
62
63
Chris Lattner2dbfa032003-09-01 03:14:56 +000064// 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 Gohmane3784952007-08-27 16:11:48 +000068// Lookup @malloc and @free in the symbol table, for later use. If they don't
Chris Lattner2dbfa032003-09-01 03:14:56 +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) {
Chris Lattner0b5909e2002-07-18 00:18:01 +000073 // Get Malloc and free prototypes if they exist!
Reid Spenceref9b9a72007-02-05 20:47:22 +000074 MallocFunc = M.getFunction("malloc");
75 if (MallocFunc) {
76 const FunctionType* TyWeHave = MallocFunc->getFunctionType();
Chris Lattnerade686e2002-05-07 19:02:48 +000077
Reid Spenceref9b9a72007-02-05 20:47:22 +000078 // Get the expected prototype for malloc
79 const FunctionType *Malloc1Type =
Owen Anderson1d0be152009-08-13 21:58:54 +000080 FunctionType::get(PointerType::getUnqual(Type::getInt8Ty(M.getContext())),
81 std::vector<const Type*>(1,
82 Type::getInt64Ty(M.getContext())), false);
Reid Spenceref9b9a72007-02-05 20:47:22 +000083
84 // Chck to see if we got the expected malloc
85 if (TyWeHave != Malloc1Type) {
Dan Gohmana119de82009-06-14 23:30:43 +000086 // Check to see if the prototype is wrong, giving us i8*(i32) * malloc
Reid Spenceref9b9a72007-02-05 20:47:22 +000087 // This handles the common declaration of: 'void *malloc(unsigned);'
88 const FunctionType *Malloc2Type =
Owen Anderson1d0be152009-08-13 21:58:54 +000089 FunctionType::get(PointerType::getUnqual(
90 Type::getInt8Ty(M.getContext())),
91 std::vector<const Type*>(1,
92 Type::getInt32Ty(M.getContext())), false);
Reid Spenceref9b9a72007-02-05 20:47:22 +000093 if (TyWeHave != Malloc2Type) {
94 // Check to see if the prototype is missing, giving us
Dan Gohmana119de82009-06-14 23:30:43 +000095 // i8*(...) * malloc
Reid Spenceref9b9a72007-02-05 20:47:22 +000096 // This handles the common declaration of: 'void *malloc();'
97 const FunctionType *Malloc3Type =
Owen Anderson1d0be152009-08-13 21:58:54 +000098 FunctionType::get(PointerType::getUnqual(
99 Type::getInt8Ty(M.getContext())),
Owen Anderson14ce9ef2009-07-06 01:34:54 +0000100 true);
Reid Spenceref9b9a72007-02-05 20:47:22 +0000101 if (TyWeHave != Malloc3Type)
102 // Give up
103 MallocFunc = 0;
104 }
105 }
Chris Lattner0b5909e2002-07-18 00:18:01 +0000106 }
107
Reid Spenceref9b9a72007-02-05 20:47:22 +0000108 FreeFunc = M.getFunction("free");
109 if (FreeFunc) {
110 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
111
112 // Get the expected prototype for void free(i8*)
Owen Anderson1d0be152009-08-13 21:58:54 +0000113 const FunctionType *Free1Type =
114 FunctionType::get(Type::getVoidTy(M.getContext()),
115 std::vector<const Type*>(1, PointerType::getUnqual(
116 Type::getInt8Ty(M.getContext()))),
117 false);
Chris Lattner47e0f3a2002-05-24 20:29:18 +0000118
Reid Spenceref9b9a72007-02-05 20:47:22 +0000119 if (TyWeHave != Free1Type) {
120 // Check to see if the prototype was forgotten, giving us
121 // void (...) * free
122 // This handles the common forward declaration of: 'void free();'
Owen Anderson1d0be152009-08-13 21:58:54 +0000123 const FunctionType* Free2Type =
124 FunctionType::get(Type::getVoidTy(M.getContext()), true);
Chris Lattner47e0f3a2002-05-24 20:29:18 +0000125
Reid Spenceref9b9a72007-02-05 20:47:22 +0000126 if (TyWeHave != Free2Type) {
127 // One last try, check to see if we can find free as
128 // int (...)* free. This handles the case where NOTHING was declared.
Owen Anderson1d0be152009-08-13 21:58:54 +0000129 const FunctionType* Free3Type =
130 FunctionType::get(Type::getInt32Ty(M.getContext()), true);
Reid Spenceref9b9a72007-02-05 20:47:22 +0000131
132 if (TyWeHave != Free3Type) {
133 // Give up.
134 FreeFunc = 0;
135 }
136 }
137 }
Chris Lattner1f28e8c2003-08-11 15:05:08 +0000138 }
139
Chris Lattnerade686e2002-05-07 19:02:48 +0000140 // Don't mess with locally defined versions of these functions...
Reid Spencer5cbf9852007-01-30 20:08:39 +0000141 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
142 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
Chris Lattnerade686e2002-05-07 19:02:48 +0000143}
144
Chris Lattner2dbfa032003-09-01 03:14:56 +0000145// run - Transform calls into instructions...
Chris Lattnerade686e2002-05-07 19:02:48 +0000146//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000147bool RaiseAllocations::runOnModule(Module &M) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000148 // Find the malloc/free prototypes...
149 doInitialization(M);
Owen Andersone922c022009-07-22 00:24:57 +0000150
Chris Lattnerade686e2002-05-07 19:02:48 +0000151 bool Changed = false;
Chris Lattnerade686e2002-05-07 19:02:48 +0000152
Chris Lattner2dbfa032003-09-01 03:14:56 +0000153 // First, process all of the malloc calls...
154 if (MallocFunc) {
155 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000156 std::vector<Value*> EqPointers; // Values equal to MallocFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000157 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000158 User *U = Users.back();
159 Users.pop_back();
160
161 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000162 CallSite CS = CallSite::get(I);
Dan Gohmancb406c22007-10-03 19:26:29 +0000163 if (CS.getInstruction() && !CS.arg_empty() &&
Chris Lattnereb12cd62003-12-07 01:42:08 +0000164 (CS.getCalledFunction() == MallocFunc ||
165 std::find(EqPointers.begin(), EqPointers.end(),
166 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000167
Chris Lattner2dbfa032003-09-01 03:14:56 +0000168 Value *Source = *CS.arg_begin();
Misha Brukmanfd939082005-04-21 23:48:37 +0000169
Chris Lattner2dbfa032003-09-01 03:14:56 +0000170 // If no prototype was provided for malloc, we may need to cast the
171 // source size.
Owen Anderson1d0be152009-08-13 21:58:54 +0000172 if (Source->getType() != Type::getInt32Ty(M.getContext()))
Reid Spencer3da59db2006-11-27 01:05:10 +0000173 Source =
Owen Anderson1d0be152009-08-13 21:58:54 +0000174 CastInst::CreateIntegerCast(Source,
175 Type::getInt32Ty(M.getContext()),
176 false/*ZExt*/,
Reid Spencer7b06bd52006-12-13 00:50:17 +0000177 "MallocAmtCast", I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000178
Owen Anderson1d0be152009-08-13 21:58:54 +0000179 MallocInst *MI = new MallocInst(Type::getInt8Ty(M.getContext()),
180 Source, "", I);
Chris Lattner046800a2007-02-11 01:08:35 +0000181 MI->takeName(I);
Chris Lattner2dbfa032003-09-01 03:14:56 +0000182 I->replaceAllUsesWith(MI);
Chris Lattnercc838342003-09-16 19:42:21 +0000183
184 // If the old instruction was an invoke, add an unconditional branch
185 // before the invoke, which will become the new terminator.
186 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greif051a9502008-04-06 20:25:17 +0000187 BranchInst::Create(II->getNormalDest(), I);
Chris Lattnercc838342003-09-16 19:42:21 +0000188
189 // Delete the old call site
Dan Gohman1adec832008-06-21 22:08:46 +0000190 I->eraseFromParent();
Chris Lattner2dbfa032003-09-01 03:14:56 +0000191 Changed = true;
192 ++NumRaised;
193 }
Reid Spencer518310c2004-07-18 00:44:37 +0000194 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
195 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
196 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000197 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000198 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000199 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
200 EqPointers.push_back(CE);
201 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000202 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000203 }
204 }
205
206 // Next, process all free calls...
207 if (FreeFunc) {
208 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000209 std::vector<Value*> EqPointers; // Values equal to FreeFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000210
211 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000212 User *U = Users.back();
213 Users.pop_back();
214
215 if (Instruction *I = dyn_cast<Instruction>(U)) {
Devang Patel84458322007-10-17 20:12:58 +0000216 if (isa<InvokeInst>(I))
217 continue;
Chris Lattner2dbfa032003-09-01 03:14:56 +0000218 CallSite CS = CallSite::get(I);
Dan Gohmancb406c22007-10-03 19:26:29 +0000219 if (CS.getInstruction() && !CS.arg_empty() &&
Chris Lattnereb12cd62003-12-07 01:42:08 +0000220 (CS.getCalledFunction() == FreeFunc ||
221 std::find(EqPointers.begin(), EqPointers.end(),
222 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000223
Chris Lattner2dbfa032003-09-01 03:14:56 +0000224 // If no prototype was provided for free, we may need to cast the
225 // source pointer. This should be really uncommon, but it's necessary
Chris Lattnerda895d62005-02-27 06:18:25 +0000226 // just in case we are dealing with weird code like this:
Chris Lattner2dbfa032003-09-01 03:14:56 +0000227 // free((long)ptr);
228 //
229 Value *Source = *CS.arg_begin();
230 if (!isa<PointerType>(Source->getType()))
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000231 Source = new IntToPtrInst(Source,
Owen Anderson1d0be152009-08-13 21:58:54 +0000232 PointerType::getUnqual(Type::getInt8Ty(M.getContext())),
Reid Spencer7b06bd52006-12-13 00:50:17 +0000233 "FreePtrCast", I);
Chris Lattner2dbfa032003-09-01 03:14:56 +0000234 new FreeInst(Source, I);
Chris Lattnercc838342003-09-16 19:42:21 +0000235
236 // If the old instruction was an invoke, add an unconditional branch
237 // before the invoke, which will become the new terminator.
238 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greif051a9502008-04-06 20:25:17 +0000239 BranchInst::Create(II->getNormalDest(), I);
Chris Lattnercc838342003-09-16 19:42:21 +0000240
241 // Delete the old call site
Owen Anderson1d0be152009-08-13 21:58:54 +0000242 if (I->getType() != Type::getVoidTy(M.getContext()))
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000243 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Chris Lattner52f20f82004-11-09 05:10:56 +0000244 I->eraseFromParent();
Chris Lattner2dbfa032003-09-01 03:14:56 +0000245 Changed = true;
246 ++NumRaised;
247 }
Reid Spencer518310c2004-07-18 00:44:37 +0000248 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
249 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
250 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000251 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000252 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000253 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
254 EqPointers.push_back(CE);
255 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000256 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000257 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000258 }
259
260 return Changed;
261}