blob: aeb41738419c4a9874e7bf1ecb1059485ff81004 [file] [log] [blame]
Chris Lattner03453a02002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner77f791d2002-05-07 19:02:48 +00009//
Chris Lattner03453a02002-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 Lattner77f791d2002-05-07 19:02:48 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner1631bcb2006-12-19 22:09:18 +000015#define DEBUG_TYPE "raiseallocs"
Chris Lattner8ef1b882003-09-01 03:14:56 +000016#include "llvm/Transforms/IPO.h"
Chris Lattner771804b2003-12-07 01:42:08 +000017#include "llvm/Constants.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000018#include "llvm/DerivedTypes.h"
Chris Lattner771804b2003-12-07 01:42:08 +000019#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000021#include "llvm/Pass.h"
Chris Lattner8ef1b882003-09-01 03:14:56 +000022#include "llvm/Support/CallSite.h"
Reid Spencer557ab152007-02-05 23:32:05 +000023#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner1631bcb2006-12-19 22:09:18 +000027STATISTIC(NumRaised, "Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000028
Chris Lattner1631bcb2006-12-19 22:09:18 +000029namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000030 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
31 // instruction.
Chris Lattner77f791d2002-05-07 19:02:48 +000032 //
Reid Spencer557ab152007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN RaiseAllocations : public ModulePass {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000034 Function *MallocFunc; // Functions in the module we are processing
35 Function *FreeFunc; // Initialized by doPassInitializationVirt
36 public:
37 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000038
Chris Lattnerbf3a0992002-10-01 22:38:41 +000039 // doPassInitialization - For the raise allocations pass, this finds a
40 // declaration for malloc and free if they exist.
41 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000042 void doInitialization(Module &M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000043
Chris Lattner8ef1b882003-09-01 03:14:56 +000044 // run - This method does the actual work of converting instructions over.
Chris Lattnerbf3a0992002-10-01 22:38:41 +000045 //
Chris Lattner4f2cf032004-09-20 04:48:05 +000046 bool runOnModule(Module &M);
Chris Lattnerbf3a0992002-10-01 22:38:41 +000047 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000048
Chris Lattnerc2d3d312006-08-27 22:42:52 +000049 RegisterPass<RaiseAllocations>
Chris Lattnerc8b70922002-07-26 21:12:46 +000050 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattner77f791d2002-05-07 19:02:48 +000051} // end anonymous namespace
52
53
54// createRaiseAllocationsPass - The interface to this file...
Chris Lattner4f2cf032004-09-20 04:48:05 +000055ModulePass *llvm::createRaiseAllocationsPass() {
Chris Lattner77f791d2002-05-07 19:02:48 +000056 return new RaiseAllocations();
57}
58
59
Chris Lattner8ef1b882003-09-01 03:14:56 +000060// If the module has a symbol table, they might be referring to the malloc and
61// free functions. If this is the case, grab the method pointers that the
62// module is using.
63//
64// Lookup %malloc and %free in the symbol table, for later use. If they don't
65// exist, or are not external, we do not worry about converting calls to that
66// function into the appropriate instruction.
67//
68void RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000069
Chris Lattnerdfe04182002-07-18 00:18:01 +000070 // Get Malloc and free prototypes if they exist!
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000071 MallocFunc = M.getFunction("malloc");
72 if (MallocFunc) {
73 const FunctionType* TyWeHave = MallocFunc->getFunctionType();
Chris Lattner77f791d2002-05-07 19:02:48 +000074
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000075 // Get the expected prototype for malloc
76 const FunctionType *Malloc1Type =
77 FunctionType::get(PointerType::get(Type::Int8Ty),
78 std::vector<const Type*>(1, Type::Int64Ty), false);
79
80 // Chck to see if we got the expected malloc
81 if (TyWeHave != Malloc1Type) {
82 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
83 // This handles the common declaration of: 'void *malloc(unsigned);'
84 const FunctionType *Malloc2Type =
85 FunctionType::get(PointerType::get(Type::Int8Ty),
86 std::vector<const Type*>(1, Type::Int32Ty), false);
87 if (TyWeHave != Malloc2Type) {
88 // Check to see if the prototype is missing, giving us
89 // sbyte*(...) * malloc
90 // This handles the common declaration of: 'void *malloc();'
91 const FunctionType *Malloc3Type =
92 FunctionType::get(PointerType::get(Type::Int8Ty),
93 std::vector<const Type*>(), true);
94 if (TyWeHave != Malloc3Type)
95 // Give up
96 MallocFunc = 0;
97 }
98 }
Chris Lattnerdfe04182002-07-18 00:18:01 +000099 }
100
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000101 FreeFunc = M.getFunction("free");
102 if (FreeFunc) {
103 const FunctionType* TyWeHave = FreeFunc->getFunctionType();
104
105 // Get the expected prototype for void free(i8*)
106 const FunctionType *Free1Type = FunctionType::get(Type::VoidTy,
107 std::vector<const Type*>(1, PointerType::get(Type::Int8Ty)), false);
Chris Lattnere3da2982002-05-24 20:29:18 +0000108
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000109 if (TyWeHave != Free1Type) {
110 // Check to see if the prototype was forgotten, giving us
111 // void (...) * free
112 // This handles the common forward declaration of: 'void free();'
113 const FunctionType* Free2Type = FunctionType::get(Type::VoidTy,
114 std::vector<const Type*>(),true);
Chris Lattnere3da2982002-05-24 20:29:18 +0000115
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000116 if (TyWeHave != Free2Type) {
117 // One last try, check to see if we can find free as
118 // int (...)* free. This handles the case where NOTHING was declared.
119 const FunctionType* Free3Type = FunctionType::get(Type::Int32Ty,
120 std::vector<const Type*>(),true);
121
122 if (TyWeHave != Free3Type) {
123 // Give up.
124 FreeFunc = 0;
125 }
126 }
127 }
Chris Lattner603e0072003-08-11 15:05:08 +0000128 }
129
Chris Lattner77f791d2002-05-07 19:02:48 +0000130 // Don't mess with locally defined versions of these functions...
Reid Spencer5301e7c2007-01-30 20:08:39 +0000131 if (MallocFunc && !MallocFunc->isDeclaration()) MallocFunc = 0;
132 if (FreeFunc && !FreeFunc->isDeclaration()) FreeFunc = 0;
Chris Lattner77f791d2002-05-07 19:02:48 +0000133}
134
Chris Lattner8ef1b882003-09-01 03:14:56 +0000135// run - Transform calls into instructions...
Chris Lattner77f791d2002-05-07 19:02:48 +0000136//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000137bool RaiseAllocations::runOnModule(Module &M) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000138 // Find the malloc/free prototypes...
139 doInitialization(M);
140
Chris Lattner77f791d2002-05-07 19:02:48 +0000141 bool Changed = false;
Chris Lattner77f791d2002-05-07 19:02:48 +0000142
Chris Lattner8ef1b882003-09-01 03:14:56 +0000143 // First, process all of the malloc calls...
144 if (MallocFunc) {
145 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
Chris Lattner771804b2003-12-07 01:42:08 +0000146 std::vector<Value*> EqPointers; // Values equal to MallocFunc
Chris Lattner8ef1b882003-09-01 03:14:56 +0000147 while (!Users.empty()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000148 User *U = Users.back();
149 Users.pop_back();
150
151 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000152 CallSite CS = CallSite::get(I);
Chris Lattner771804b2003-12-07 01:42:08 +0000153 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
154 (CS.getCalledFunction() == MallocFunc ||
155 std::find(EqPointers.begin(), EqPointers.end(),
156 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157
Chris Lattner8ef1b882003-09-01 03:14:56 +0000158 Value *Source = *CS.arg_begin();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159
Chris Lattner8ef1b882003-09-01 03:14:56 +0000160 // If no prototype was provided for malloc, we may need to cast the
161 // source size.
Reid Spencerc635f472006-12-31 05:48:39 +0000162 if (Source->getType() != Type::Int32Ty)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000163 Source =
Reid Spencerc635f472006-12-31 05:48:39 +0000164 CastInst::createIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000165 "MallocAmtCast", I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000166
Chris Lattner8d4c36b2007-02-11 01:08:35 +0000167 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
168 MI->takeName(I);
Chris Lattner8ef1b882003-09-01 03:14:56 +0000169 I->replaceAllUsesWith(MI);
Chris Lattner39f398b2003-09-16 19:42:21 +0000170
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))
174 new BranchInst(II->getNormalDest(), I);
175
176 // Delete the old call site
Chris Lattner8ef1b882003-09-01 03:14:56 +0000177 MI->getParent()->getInstList().erase(I);
178 Changed = true;
179 ++NumRaised;
180 }
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000181 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
182 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
183 EqPointers.push_back(GV);
Chris Lattner771804b2003-12-07 01:42:08 +0000184 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000185 if (CE->isCast()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000186 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
187 EqPointers.push_back(CE);
188 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000189 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000190 }
191 }
192
193 // Next, process all free calls...
194 if (FreeFunc) {
195 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
Chris Lattner771804b2003-12-07 01:42:08 +0000196 std::vector<Value*> EqPointers; // Values equal to FreeFunc
Chris Lattner8ef1b882003-09-01 03:14:56 +0000197
198 while (!Users.empty()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000199 User *U = Users.back();
200 Users.pop_back();
201
202 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000203 CallSite CS = CallSite::get(I);
Chris Lattner771804b2003-12-07 01:42:08 +0000204 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
205 (CS.getCalledFunction() == FreeFunc ||
206 std::find(EqPointers.begin(), EqPointers.end(),
207 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000208
Chris Lattner8ef1b882003-09-01 03:14:56 +0000209 // If no prototype was provided for free, we may need to cast the
210 // source pointer. This should be really uncommon, but it's necessary
Chris Lattner0ce80cd2005-02-27 06:18:25 +0000211 // just in case we are dealing with weird code like this:
Chris Lattner8ef1b882003-09-01 03:14:56 +0000212 // free((long)ptr);
213 //
214 Value *Source = *CS.arg_begin();
215 if (!isa<PointerType>(Source->getType()))
Reid Spencerc635f472006-12-31 05:48:39 +0000216 Source = new IntToPtrInst(Source, PointerType::get(Type::Int8Ty),
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000217 "FreePtrCast", I);
Chris Lattner8ef1b882003-09-01 03:14:56 +0000218 new FreeInst(Source, I);
Chris Lattner39f398b2003-09-16 19:42:21 +0000219
220 // If the old instruction was an invoke, add an unconditional branch
221 // before the invoke, which will become the new terminator.
222 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
223 new BranchInst(II->getNormalDest(), I);
224
225 // Delete the old call site
Chris Lattner1f0a97c2004-11-09 05:10:56 +0000226 if (I->getType() != Type::VoidTy)
227 I->replaceAllUsesWith(UndefValue::get(I->getType()));
228 I->eraseFromParent();
Chris Lattner8ef1b882003-09-01 03:14:56 +0000229 Changed = true;
230 ++NumRaised;
231 }
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000232 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
233 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
234 EqPointers.push_back(GV);
Chris Lattner771804b2003-12-07 01:42:08 +0000235 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000236 if (CE->isCast()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000237 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
238 EqPointers.push_back(CE);
239 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000240 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000241 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000242 }
243
244 return Changed;
245}