blob: 8c97b5d17c08fcfa741519250d8369f5a24d5c86 [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"
Chris Lattnereb12cd62003-12-07 01:42:08 +000019#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000021#include "llvm/Pass.h"
Chris Lattner2dbfa032003-09-01 03:14:56 +000022#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000023#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000025#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner86453c52006-12-19 22:09:18 +000028STATISTIC(NumRaised, "Number of allocations raised");
Chris Lattnerade686e2002-05-07 19:02:48 +000029
Chris Lattner86453c52006-12-19 22:09:18 +000030namespace {
Dan Gohmane3784952007-08-27 16:11:48 +000031 // RaiseAllocations - Turn @malloc and @free calls into the appropriate
Chris Lattnera92f6962002-10-01 22:38:41 +000032 // instruction.
Chris Lattnerade686e2002-05-07 19:02:48 +000033 //
Reid Spencer9133fe22007-02-05 23:32:05 +000034 class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
Chris Lattnera92f6962002-10-01 22:38:41 +000035 Function *MallocFunc; // Functions in the module we are processing
36 Function *FreeFunc; // Initialized by doPassInitializationVirt
37 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000039 RaiseAllocations()
Dan Gohmanae73dc12008-09-04 17:05:41 +000040 : ModulePass(&ID), MallocFunc(0), FreeFunc(0) {}
Misha Brukmanfd939082005-04-21 23:48:37 +000041
Chris Lattnera92f6962002-10-01 22:38:41 +000042 // doPassInitialization - For the raise allocations pass, this finds a
43 // declaration for malloc and free if they exist.
44 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000045 void doInitialization(Module &M);
Misha Brukmanfd939082005-04-21 23:48:37 +000046
Chris Lattner2dbfa032003-09-01 03:14:56 +000047 // run - This method does the actual work of converting instructions over.
Chris Lattnera92f6962002-10-01 22:38:41 +000048 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000049 bool runOnModule(Module &M);
Chris Lattnera92f6962002-10-01 22:38:41 +000050 };
Chris Lattnerade686e2002-05-07 19:02:48 +000051} // end anonymous namespace
52
Dan Gohman844731a2008-05-13 00:00:25 +000053char RaiseAllocations::ID = 0;
54static RegisterPass<RaiseAllocations>
55X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattnerade686e2002-05-07 19:02:48 +000056
57// createRaiseAllocationsPass - The interface to this file...
Chris Lattnerb12914b2004-09-20 04:48:05 +000058ModulePass *llvm::createRaiseAllocationsPass() {
Chris Lattnerade686e2002-05-07 19:02:48 +000059 return new RaiseAllocations();
60}
61
62
Chris Lattner2dbfa032003-09-01 03:14:56 +000063// 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 Gohmane3784952007-08-27 16:11:48 +000067// Lookup @malloc and @free in the symbol table, for later use. If they don't
Chris Lattner2dbfa032003-09-01 03:14:56 +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) {
Chris Lattnerade686e2002-05-07 19:02:48 +000072
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 =
Christopher Lamb43ad6b32007-12-17 01:12:55 +000080 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Reid Spenceref9b9a72007-02-05 20:47:22 +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 Gohmana119de82009-06-14 23:30:43 +000085 // Check to see if the prototype is wrong, giving us i8*(i32) * malloc
Reid Spenceref9b9a72007-02-05 20:47:22 +000086 // This handles the common declaration of: 'void *malloc(unsigned);'
87 const FunctionType *Malloc2Type =
Christopher Lamb43ad6b32007-12-17 01:12:55 +000088 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Reid Spenceref9b9a72007-02-05 20:47:22 +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 Gohmana119de82009-06-14 23:30:43 +000092 // i8*(...) * malloc
Reid Spenceref9b9a72007-02-05 20:47:22 +000093 // This handles the common declaration of: 'void *malloc();'
94 const FunctionType *Malloc3Type =
Christopher Lamb43ad6b32007-12-17 01:12:55 +000095 FunctionType::get(PointerType::getUnqual(Type::Int8Ty),
Reid Spenceref9b9a72007-02-05 20:47:22 +000096 std::vector<const Type*>(), true);
97 if (TyWeHave != Malloc3Type)
98 // Give up
99 MallocFunc = 0;
100 }
101 }
Chris Lattner0b5909e2002-07-18 00:18:01 +0000102 }
103
Reid Spenceref9b9a72007-02-05 20:47:22 +0000104 FreeFunc = M.getFunction("free");
105 if (FreeFunc) {
106 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
107
108 // Get the expected prototype for void free(i8*)
109 const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000110 std::vector<const Type*>(1, PointerType::getUnqual(Type::Int8Ty)), false);
Chris Lattner47e0f3a2002-05-24 20:29:18 +0000111
Reid Spenceref9b9a72007-02-05 20:47:22 +0000112 if (TyWeHave != Free1Type) {
113 // Check to see if the prototype was forgotten, giving us
114 // void (...) * free
115 // This handles the common forward declaration of: 'void free();'
116 const FunctionType* Free2Type = FunctionType::get(Type::VoidTy,
117 std::vector<const Type*>(),true);
Chris Lattner47e0f3a2002-05-24 20:29:18 +0000118
Reid Spenceref9b9a72007-02-05 20:47:22 +0000119 if (TyWeHave != Free2Type) {
120 // One last try, check to see if we can find free as
121 // int (...)* free. This handles the case where NOTHING was declared.
122 const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty,
123 std::vector<const Type*>(),true);
124
125 if (TyWeHave != Free3Type) {
126 // Give up.
127 FreeFunc = 0;
128 }
129 }
130 }
Chris Lattner1f28e8c2003-08-11 15:05:08 +0000131 }
132
Chris Lattnerade686e2002-05-07 19:02:48 +0000133 // Don't mess with locally defined versions of these functions...
Reid Spencer5cbf9852007-01-30 20:08:39 +0000134 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
135 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
Chris Lattnerade686e2002-05-07 19:02:48 +0000136}
137
Chris Lattner2dbfa032003-09-01 03:14:56 +0000138// run - Transform calls into instructions...
Chris Lattnerade686e2002-05-07 19:02:48 +0000139//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000140bool RaiseAllocations::runOnModule(Module &M) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000141 // Find the malloc/free prototypes...
142 doInitialization(M);
143
Chris Lattnerade686e2002-05-07 19:02:48 +0000144 bool Changed = false;
Chris Lattnerade686e2002-05-07 19:02:48 +0000145
Chris Lattner2dbfa032003-09-01 03:14:56 +0000146 // First, process all of the malloc calls...
147 if (MallocFunc) {
148 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000149 std::vector<Value*> EqPointers; // Values equal to MallocFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000150 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000151 User *U = Users.back();
152 Users.pop_back();
153
154 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000155 CallSite CS = CallSite::get(I);
Dan Gohmancb406c22007-10-03 19:26:29 +0000156 if (CS.getInstruction() && !CS.arg_empty() &&
Chris Lattnereb12cd62003-12-07 01:42:08 +0000157 (CS.getCalledFunction() == MallocFunc ||
158 std::find(EqPointers.begin(), EqPointers.end(),
159 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000160
Chris Lattner2dbfa032003-09-01 03:14:56 +0000161 Value *Source = *CS.arg_begin();
Misha Brukmanfd939082005-04-21 23:48:37 +0000162
Chris Lattner2dbfa032003-09-01 03:14:56 +0000163 // If no prototype was provided for malloc, we may need to cast the
164 // source size.
Reid Spencerc5b206b2006-12-31 05:48:39 +0000165 if (Source->getType() != Type::Int32Ty)
Reid Spencer3da59db2006-11-27 01:05:10 +0000166 Source =
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000167 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
Reid Spencer7b06bd52006-12-13 00:50:17 +0000168 "MallocAmtCast", I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000169
Chris Lattner046800a2007-02-11 01:08:35 +0000170 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
171 MI->takeName(I);
Chris Lattner2dbfa032003-09-01 03:14:56 +0000172 I->replaceAllUsesWith(MI);
Chris Lattnercc838342003-09-16 19:42:21 +0000173
174 // If the old instruction was an invoke, add an unconditional branch
175 // before the invoke, which will become the new terminator.
176 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greif051a9502008-04-06 20:25:17 +0000177 BranchInst::Create(II->getNormalDest(), I);
Chris Lattnercc838342003-09-16 19:42:21 +0000178
179 // Delete the old call site
Dan Gohman1adec832008-06-21 22:08:46 +0000180 I->eraseFromParent();
Chris Lattner2dbfa032003-09-01 03:14:56 +0000181 Changed = true;
182 ++NumRaised;
183 }
Reid Spencer518310c2004-07-18 00:44:37 +0000184 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
185 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
186 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000187 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000188 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000189 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
190 EqPointers.push_back(CE);
191 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000192 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000193 }
194 }
195
196 // Next, process all free calls...
197 if (FreeFunc) {
198 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000199 std::vector<Value*> EqPointers; // Values equal to FreeFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000200
201 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000202 User *U = Users.back();
203 Users.pop_back();
204
205 if (Instruction *I = dyn_cast<Instruction>(U)) {
Devang Patel84458322007-10-17 20:12:58 +0000206 if (isa<InvokeInst>(I))
207 continue;
Chris Lattner2dbfa032003-09-01 03:14:56 +0000208 CallSite CS = CallSite::get(I);
Dan Gohmancb406c22007-10-03 19:26:29 +0000209 if (CS.getInstruction() && !CS.arg_empty() &&
Chris Lattnereb12cd62003-12-07 01:42:08 +0000210 (CS.getCalledFunction() == FreeFunc ||
211 std::find(EqPointers.begin(), EqPointers.end(),
212 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000213
Chris Lattner2dbfa032003-09-01 03:14:56 +0000214 // If no prototype was provided for free, we may need to cast the
215 // source pointer. This should be really uncommon, but it's necessary
Chris Lattnerda895d62005-02-27 06:18:25 +0000216 // just in case we are dealing with weird code like this:
Chris Lattner2dbfa032003-09-01 03:14:56 +0000217 // free((long)ptr);
218 //
219 Value *Source = *CS.arg_begin();
220 if (!isa<PointerType>(Source->getType()))
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000221 Source = new IntToPtrInst(Source,
222 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer7b06bd52006-12-13 00:50:17 +0000223 "FreePtrCast", I);
Chris Lattner2dbfa032003-09-01 03:14:56 +0000224 new FreeInst(Source, I);
Chris Lattnercc838342003-09-16 19:42:21 +0000225
226 // If the old instruction was an invoke, add an unconditional branch
227 // before the invoke, which will become the new terminator.
228 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Gabor Greif051a9502008-04-06 20:25:17 +0000229 BranchInst::Create(II->getNormalDest(), I);
Chris Lattnercc838342003-09-16 19:42:21 +0000230
231 // Delete the old call site
Chris Lattner52f20f82004-11-09 05:10:56 +0000232 if (I->getType() != Type::VoidTy)
233 I->replaceAllUsesWith(UndefValue::get(I->getType()));
234 I->eraseFromParent();
Chris Lattner2dbfa032003-09-01 03:14:56 +0000235 Changed = true;
236 ++NumRaised;
237 }
Reid Spencer518310c2004-07-18 00:44:37 +0000238 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
239 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
240 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000241 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000242 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000243 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
244 EqPointers.push_back(CE);
245 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000246 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000247 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000248 }
249
250 return Changed;
251}