blob: fd5b7fb1f13c3f25f6a8dcd743f0030bfab31f74 [file] [log] [blame]
Chris Lattner03453a02002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner8ef1b882003-09-01 03:14:56 +000015#include "llvm/Transforms/IPO.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000016#include "llvm/Module.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iMemory.h"
Chris Lattner39f398b2003-09-16 19:42:21 +000019#include "llvm/iTerminators.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000020#include "llvm/iOther.h"
21#include "llvm/Pass.h"
Chris Lattner8ef1b882003-09-01 03:14:56 +000022#include "llvm/Support/CallSite.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000023#include "Support/Statistic.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000024
Brian Gaeke960707c2003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner77f791d2002-05-07 19:02:48 +000027namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000028 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000029
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 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000033 class RaiseAllocations : public Pass {
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) {}
38
39 // 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);
Chris Lattnerbf3a0992002-10-01 22:38:41 +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 Lattner8ef1b882003-09-01 03:14:56 +000046 bool run(Module &M);
Chris Lattnerbf3a0992002-10-01 22:38:41 +000047 };
48
Chris Lattnerc8b70922002-07-26 21:12:46 +000049 RegisterOpt<RaiseAllocations>
50 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...
55Pass *createRaiseAllocationsPass() {
56 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 const FunctionType *MallocType = // Get the type for malloc
70 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattnerdfe04182002-07-18 00:18:01 +000071 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattner77f791d2002-05-07 19:02:48 +000072
73 const FunctionType *FreeType = // Get the type for free
74 FunctionType::get(Type::VoidTy,
75 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
76 false);
77
Chris Lattnerdfe04182002-07-18 00:18:01 +000078 // Get Malloc and free prototypes if they exist!
Chris Lattner113f4f42002-06-25 16:13:24 +000079 MallocFunc = M.getFunction("malloc", MallocType);
80 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattner77f791d2002-05-07 19:02:48 +000081
Chris Lattnerdfe04182002-07-18 00:18:01 +000082 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
83 // This handles the common declaration of: 'void *malloc(unsigned);'
84 if (MallocFunc == 0) {
85 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
86 std::vector<const Type*>(1, Type::UIntTy), false);
87 MallocFunc = M.getFunction("malloc", MallocType);
88 }
89
Chris Lattnere3da2982002-05-24 20:29:18 +000090 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattnerdfe04182002-07-18 00:18:01 +000091 // This handles the common declaration of: 'void *malloc();'
Chris Lattnere3da2982002-05-24 20:29:18 +000092 if (MallocFunc == 0) {
93 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
94 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000095 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000096 }
97
98 // Check to see if the prototype was forgotten, giving us void (...) * free
99 // This handles the common forward declaration of: 'void free();'
100 if (FreeFunc == 0) {
101 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +0000103 }
104
Chris Lattner603e0072003-08-11 15:05:08 +0000105 // One last try, check to see if we can find free as 'int (...)* free'. This
106 // handles the case where NOTHING was declared.
107 if (FreeFunc == 0) {
108 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
109 FreeFunc = M.getFunction("free", FreeType);
110 }
111
Chris Lattner77f791d2002-05-07 19:02:48 +0000112 // Don't mess with locally defined versions of these functions...
113 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
114 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattner77f791d2002-05-07 19:02:48 +0000115}
116
Chris Lattner8ef1b882003-09-01 03:14:56 +0000117// run - Transform calls into instructions...
Chris Lattner77f791d2002-05-07 19:02:48 +0000118//
Chris Lattner8ef1b882003-09-01 03:14:56 +0000119bool RaiseAllocations::run(Module &M) {
120 // Find the malloc/free prototypes...
121 doInitialization(M);
122
Chris Lattner77f791d2002-05-07 19:02:48 +0000123 bool Changed = false;
Chris Lattner77f791d2002-05-07 19:02:48 +0000124
Chris Lattner8ef1b882003-09-01 03:14:56 +0000125 // First, process all of the malloc calls...
126 if (MallocFunc) {
127 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
128 while (!Users.empty()) {
129 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
130 CallSite CS = CallSite::get(I);
131 if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
132 CS.arg_begin() != CS.arg_end()) {
133 Value *Source = *CS.arg_begin();
134
135 // If no prototype was provided for malloc, we may need to cast the
136 // source size.
137 if (Source->getType() != Type::UIntTy)
138 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
139
140 std::string Name(I->getName()); I->setName("");
141 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
142 I->replaceAllUsesWith(MI);
Chris Lattner39f398b2003-09-16 19:42:21 +0000143
144 // If the old instruction was an invoke, add an unconditional branch
145 // before the invoke, which will become the new terminator.
146 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
147 new BranchInst(II->getNormalDest(), I);
148
149 // Delete the old call site
Chris Lattner8ef1b882003-09-01 03:14:56 +0000150 MI->getParent()->getInstList().erase(I);
151 Changed = true;
152 ++NumRaised;
153 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000154 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000155
156 Users.pop_back();
157 }
158 }
159
160 // Next, process all free calls...
161 if (FreeFunc) {
162 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
163
164 while (!Users.empty()) {
165 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
166 CallSite CS = CallSite::get(I);
167 if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
168 CS.arg_begin() != CS.arg_end()) {
169
170 // If no prototype was provided for free, we may need to cast the
171 // source pointer. This should be really uncommon, but it's necessary
172 // just in case we are dealing with wierd code like this:
173 // free((long)ptr);
174 //
175 Value *Source = *CS.arg_begin();
176 if (!isa<PointerType>(Source->getType()))
177 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
178 "FreePtrCast", I);
179 new FreeInst(Source, I);
Chris Lattner39f398b2003-09-16 19:42:21 +0000180
181 // If the old instruction was an invoke, add an unconditional branch
182 // before the invoke, which will become the new terminator.
183 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
184 new BranchInst(II->getNormalDest(), I);
185
186 // Delete the old call site
Chris Lattner8ef1b882003-09-01 03:14:56 +0000187 I->getParent()->getInstList().erase(I);
188 Changed = true;
189 ++NumRaised;
190 }
191 }
192
193 Users.pop_back();
Chris Lattner77f791d2002-05-07 19:02:48 +0000194 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000195 }
196
197 return Changed;
198}
Brian Gaeke960707c2003-11-11 22:41:34 +0000199
200} // End llvm namespace