blob: 58f0d1b0a3049cc5d63b3d6a34121d242f9d8b46 [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 Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattnerf52e03c2003-11-21 21:54:22 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner1631bcb2006-12-19 22:09:18 +000026STATISTIC(NumRaised, "Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000027
Chris Lattner1631bcb2006-12-19 22:09:18 +000028namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000029 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
30 // instruction.
Chris Lattner77f791d2002-05-07 19:02:48 +000031 //
Chris Lattner4f2cf032004-09-20 04:48:05 +000032 class RaiseAllocations : public ModulePass {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Function *MallocFunc; // Functions in the module we are processing
34 Function *FreeFunc; // Initialized by doPassInitializationVirt
35 public:
36 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000037
Chris Lattnerbf3a0992002-10-01 22:38:41 +000038 // doPassInitialization - For the raise allocations pass, this finds a
39 // declaration for malloc and free if they exist.
40 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000041 void doInitialization(Module &M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000042
Chris Lattner8ef1b882003-09-01 03:14:56 +000043 // run - This method does the actual work of converting instructions over.
Chris Lattnerbf3a0992002-10-01 22:38:41 +000044 //
Chris Lattner4f2cf032004-09-20 04:48:05 +000045 bool runOnModule(Module &M);
Chris Lattnerbf3a0992002-10-01 22:38:41 +000046 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000047
Chris Lattnerc2d3d312006-08-27 22:42:52 +000048 RegisterPass<RaiseAllocations>
Chris Lattnerc8b70922002-07-26 21:12:46 +000049 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattner77f791d2002-05-07 19:02:48 +000050} // end anonymous namespace
51
52
53// createRaiseAllocationsPass - The interface to this file...
Chris Lattner4f2cf032004-09-20 04:48:05 +000054ModulePass *llvm::createRaiseAllocationsPass() {
Chris Lattner77f791d2002-05-07 19:02:48 +000055 return new RaiseAllocations();
56}
57
58
Chris Lattner8ef1b882003-09-01 03:14:56 +000059// If the module has a symbol table, they might be referring to the malloc and
60// free functions. If this is the case, grab the method pointers that the
61// module is using.
62//
63// Lookup %malloc and %free in the symbol table, for later use. If they don't
64// exist, or are not external, we do not worry about converting calls to that
65// function into the appropriate instruction.
66//
67void RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000068 const FunctionType *MallocType = // Get the type for malloc
69 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattnerdfe04182002-07-18 00:18:01 +000070 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattner77f791d2002-05-07 19:02:48 +000071
72 const FunctionType *FreeType = // Get the type for free
73 FunctionType::get(Type::VoidTy,
74 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
75 false);
76
Chris Lattnerdfe04182002-07-18 00:18:01 +000077 // Get Malloc and free prototypes if they exist!
Chris Lattner113f4f42002-06-25 16:13:24 +000078 MallocFunc = M.getFunction("malloc", MallocType);
79 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattner77f791d2002-05-07 19:02:48 +000080
Chris Lattnerdfe04182002-07-18 00:18:01 +000081 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
82 // This handles the common declaration of: 'void *malloc(unsigned);'
83 if (MallocFunc == 0) {
84 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
85 std::vector<const Type*>(1, Type::UIntTy), false);
86 MallocFunc = M.getFunction("malloc", MallocType);
87 }
88
Chris Lattnere3da2982002-05-24 20:29:18 +000089 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattnerdfe04182002-07-18 00:18:01 +000090 // This handles the common declaration of: 'void *malloc();'
Chris Lattnere3da2982002-05-24 20:29:18 +000091 if (MallocFunc == 0) {
92 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
93 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000094 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000095 }
96
97 // Check to see if the prototype was forgotten, giving us void (...) * free
98 // This handles the common forward declaration of: 'void free();'
99 if (FreeFunc == 0) {
100 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000101 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +0000102 }
103
Chris Lattner603e0072003-08-11 15:05:08 +0000104 // One last try, check to see if we can find free as 'int (...)* free'. This
105 // handles the case where NOTHING was declared.
106 if (FreeFunc == 0) {
107 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
108 FreeFunc = M.getFunction("free", FreeType);
109 }
110
Chris Lattner77f791d2002-05-07 19:02:48 +0000111 // Don't mess with locally defined versions of these functions...
112 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
113 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattner77f791d2002-05-07 19:02:48 +0000114}
115
Chris Lattner8ef1b882003-09-01 03:14:56 +0000116// run - Transform calls into instructions...
Chris Lattner77f791d2002-05-07 19:02:48 +0000117//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000118bool RaiseAllocations::runOnModule(Module &M) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000119 // Find the malloc/free prototypes...
120 doInitialization(M);
121
Chris Lattner77f791d2002-05-07 19:02:48 +0000122 bool Changed = false;
Chris Lattner77f791d2002-05-07 19:02:48 +0000123
Chris Lattner8ef1b882003-09-01 03:14:56 +0000124 // First, process all of the malloc calls...
125 if (MallocFunc) {
126 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
Chris Lattner771804b2003-12-07 01:42:08 +0000127 std::vector<Value*> EqPointers; // Values equal to MallocFunc
Chris Lattner8ef1b882003-09-01 03:14:56 +0000128 while (!Users.empty()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000129 User *U = Users.back();
130 Users.pop_back();
131
132 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000133 CallSite CS = CallSite::get(I);
Chris Lattner771804b2003-12-07 01:42:08 +0000134 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
135 (CS.getCalledFunction() == MallocFunc ||
136 std::find(EqPointers.begin(), EqPointers.end(),
137 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000138
Chris Lattner8ef1b882003-09-01 03:14:56 +0000139 Value *Source = *CS.arg_begin();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000140
Chris Lattner8ef1b882003-09-01 03:14:56 +0000141 // If no prototype was provided for malloc, we may need to cast the
142 // source size.
143 if (Source->getType() != Type::UIntTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000144 Source =
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000145 CastInst::createIntegerCast(Source, Type::UIntTy, false/*ZExt*/,
146 "MallocAmtCast", I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147
Chris Lattner8ef1b882003-09-01 03:14:56 +0000148 std::string Name(I->getName()); I->setName("");
149 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
150 I->replaceAllUsesWith(MI);
Chris Lattner39f398b2003-09-16 19:42:21 +0000151
152 // If the old instruction was an invoke, add an unconditional branch
153 // before the invoke, which will become the new terminator.
154 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
155 new BranchInst(II->getNormalDest(), I);
156
157 // Delete the old call site
Chris Lattner8ef1b882003-09-01 03:14:56 +0000158 MI->getParent()->getInstList().erase(I);
159 Changed = true;
160 ++NumRaised;
161 }
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000162 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
163 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
164 EqPointers.push_back(GV);
Chris Lattner771804b2003-12-07 01:42:08 +0000165 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000166 if (CE->isCast()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000167 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
168 EqPointers.push_back(CE);
169 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000170 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000171 }
172 }
173
174 // Next, process all free calls...
175 if (FreeFunc) {
176 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
Chris Lattner771804b2003-12-07 01:42:08 +0000177 std::vector<Value*> EqPointers; // Values equal to FreeFunc
Chris Lattner8ef1b882003-09-01 03:14:56 +0000178
179 while (!Users.empty()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000180 User *U = Users.back();
181 Users.pop_back();
182
183 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner8ef1b882003-09-01 03:14:56 +0000184 CallSite CS = CallSite::get(I);
Chris Lattner771804b2003-12-07 01:42:08 +0000185 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
186 (CS.getCalledFunction() == FreeFunc ||
187 std::find(EqPointers.begin(), EqPointers.end(),
188 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000189
Chris Lattner8ef1b882003-09-01 03:14:56 +0000190 // If no prototype was provided for free, we may need to cast the
191 // source pointer. This should be really uncommon, but it's necessary
Chris Lattner0ce80cd2005-02-27 06:18:25 +0000192 // just in case we are dealing with weird code like this:
Chris Lattner8ef1b882003-09-01 03:14:56 +0000193 // free((long)ptr);
194 //
195 Value *Source = *CS.arg_begin();
196 if (!isa<PointerType>(Source->getType()))
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000197 Source = new IntToPtrInst(Source, PointerType::get(Type::SByteTy),
198 "FreePtrCast", I);
Chris Lattner8ef1b882003-09-01 03:14:56 +0000199 new FreeInst(Source, I);
Chris Lattner39f398b2003-09-16 19:42:21 +0000200
201 // If the old instruction was an invoke, add an unconditional branch
202 // before the invoke, which will become the new terminator.
203 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
204 new BranchInst(II->getNormalDest(), I);
205
206 // Delete the old call site
Chris Lattner1f0a97c2004-11-09 05:10:56 +0000207 if (I->getType() != Type::VoidTy)
208 I->replaceAllUsesWith(UndefValue::get(I->getType()));
209 I->eraseFromParent();
Chris Lattner8ef1b882003-09-01 03:14:56 +0000210 Changed = true;
211 ++NumRaised;
212 }
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000213 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
214 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
215 EqPointers.push_back(GV);
Chris Lattner771804b2003-12-07 01:42:08 +0000216 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000217 if (CE->isCast()) {
Chris Lattner771804b2003-12-07 01:42:08 +0000218 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
219 EqPointers.push_back(CE);
220 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000221 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000222 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000223 }
224
225 return Changed;
226}