blob: d214d2edfe691f7e8e874706beaa004b12a7245a [file] [log] [blame]
Chris Lattner65e96e52002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
Chris Lattnerade686e2002-05-07 19:02:48 +00002//
Chris Lattner65e96e52002-05-07 19:04:39 +00003// This file defines the RaiseAllocations pass which convert malloc and free
4// calls to malloc and free instructions.
Chris Lattnerade686e2002-05-07 19:02:48 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner2dbfa032003-09-01 03:14:56 +00008#include "llvm/Transforms/IPO.h"
Chris Lattnerade686e2002-05-07 19:02:48 +00009#include "llvm/Module.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000010#include "llvm/DerivedTypes.h"
11#include "llvm/iMemory.h"
Chris Lattnercc838342003-09-16 19:42:21 +000012#include "llvm/iTerminators.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000013#include "llvm/iOther.h"
14#include "llvm/Pass.h"
Chris Lattner2dbfa032003-09-01 03:14:56 +000015#include "llvm/Support/CallSite.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000017
18namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000019 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
Chris Lattnerade686e2002-05-07 19:02:48 +000020
Chris Lattnera92f6962002-10-01 22:38:41 +000021 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
22 // instruction.
Chris Lattnerade686e2002-05-07 19:02:48 +000023 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000024 class RaiseAllocations : public Pass {
Chris Lattnera92f6962002-10-01 22:38:41 +000025 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doPassInitializationVirt
27 public:
28 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
29
30 // doPassInitialization - For the raise allocations pass, this finds a
31 // declaration for malloc and free if they exist.
32 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000033 void doInitialization(Module &M);
Chris Lattnera92f6962002-10-01 22:38:41 +000034
Chris Lattner2dbfa032003-09-01 03:14:56 +000035 // run - This method does the actual work of converting instructions over.
Chris Lattnera92f6962002-10-01 22:38:41 +000036 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000037 bool run(Module &M);
Chris Lattnera92f6962002-10-01 22:38:41 +000038 };
39
Chris Lattnera6275cc2002-07-26 21:12:46 +000040 RegisterOpt<RaiseAllocations>
41 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattnerade686e2002-05-07 19:02:48 +000042} // end anonymous namespace
43
44
45// createRaiseAllocationsPass - The interface to this file...
46Pass *createRaiseAllocationsPass() {
47 return new RaiseAllocations();
48}
49
50
Chris Lattner2dbfa032003-09-01 03:14:56 +000051// If the module has a symbol table, they might be referring to the malloc and
52// free functions. If this is the case, grab the method pointers that the
53// module is using.
54//
55// Lookup %malloc and %free in the symbol table, for later use. If they don't
56// exist, or are not external, we do not worry about converting calls to that
57// function into the appropriate instruction.
58//
59void RaiseAllocations::doInitialization(Module &M) {
Chris Lattnerade686e2002-05-07 19:02:48 +000060 const FunctionType *MallocType = // Get the type for malloc
61 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattner0b5909e2002-07-18 00:18:01 +000062 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattnerade686e2002-05-07 19:02:48 +000063
64 const FunctionType *FreeType = // Get the type for free
65 FunctionType::get(Type::VoidTy,
66 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
67 false);
68
Chris Lattner0b5909e2002-07-18 00:18:01 +000069 // Get Malloc and free prototypes if they exist!
Chris Lattner7e708292002-06-25 16:13:24 +000070 MallocFunc = M.getFunction("malloc", MallocType);
71 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattnerade686e2002-05-07 19:02:48 +000072
Chris Lattner0b5909e2002-07-18 00:18:01 +000073 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
74 // This handles the common declaration of: 'void *malloc(unsigned);'
75 if (MallocFunc == 0) {
76 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
77 std::vector<const Type*>(1, Type::UIntTy), false);
78 MallocFunc = M.getFunction("malloc", MallocType);
79 }
80
Chris Lattner47e0f3a2002-05-24 20:29:18 +000081 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattner0b5909e2002-07-18 00:18:01 +000082 // This handles the common declaration of: 'void *malloc();'
Chris Lattner47e0f3a2002-05-24 20:29:18 +000083 if (MallocFunc == 0) {
84 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
85 std::vector<const Type*>(), true);
Chris Lattner7e708292002-06-25 16:13:24 +000086 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattner47e0f3a2002-05-24 20:29:18 +000087 }
88
89 // Check to see if the prototype was forgotten, giving us void (...) * free
90 // This handles the common forward declaration of: 'void free();'
91 if (FreeFunc == 0) {
92 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner7e708292002-06-25 16:13:24 +000093 FreeFunc = M.getFunction("free", FreeType);
Chris Lattner47e0f3a2002-05-24 20:29:18 +000094 }
95
Chris Lattner1f28e8c2003-08-11 15:05:08 +000096 // One last try, check to see if we can find free as 'int (...)* free'. This
97 // handles the case where NOTHING was declared.
98 if (FreeFunc == 0) {
99 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
100 FreeFunc = M.getFunction("free", FreeType);
101 }
102
Chris Lattnerade686e2002-05-07 19:02:48 +0000103 // Don't mess with locally defined versions of these functions...
104 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
105 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattnerade686e2002-05-07 19:02:48 +0000106}
107
Chris Lattner2dbfa032003-09-01 03:14:56 +0000108// run - Transform calls into instructions...
Chris Lattnerade686e2002-05-07 19:02:48 +0000109//
Chris Lattner2dbfa032003-09-01 03:14:56 +0000110bool RaiseAllocations::run(Module &M) {
111 // Find the malloc/free prototypes...
112 doInitialization(M);
113
Chris Lattnerade686e2002-05-07 19:02:48 +0000114 bool Changed = false;
Chris Lattnerade686e2002-05-07 19:02:48 +0000115
Chris Lattner2dbfa032003-09-01 03:14:56 +0000116 // First, process all of the malloc calls...
117 if (MallocFunc) {
118 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
119 while (!Users.empty()) {
120 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
121 CallSite CS = CallSite::get(I);
122 if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
123 CS.arg_begin() != CS.arg_end()) {
124 Value *Source = *CS.arg_begin();
125
126 // If no prototype was provided for malloc, we may need to cast the
127 // source size.
128 if (Source->getType() != Type::UIntTy)
129 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
130
131 std::string Name(I->getName()); I->setName("");
132 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
133 I->replaceAllUsesWith(MI);
Chris Lattnercc838342003-09-16 19:42:21 +0000134
135 // If the old instruction was an invoke, add an unconditional branch
136 // before the invoke, which will become the new terminator.
137 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
138 new BranchInst(II->getNormalDest(), I);
139
140 // Delete the old call site
Chris Lattner2dbfa032003-09-01 03:14:56 +0000141 MI->getParent()->getInstList().erase(I);
142 Changed = true;
143 ++NumRaised;
144 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000145 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000146
147 Users.pop_back();
148 }
149 }
150
151 // Next, process all free calls...
152 if (FreeFunc) {
153 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
154
155 while (!Users.empty()) {
156 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
157 CallSite CS = CallSite::get(I);
158 if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
159 CS.arg_begin() != CS.arg_end()) {
160
161 // If no prototype was provided for free, we may need to cast the
162 // source pointer. This should be really uncommon, but it's necessary
163 // just in case we are dealing with wierd code like this:
164 // free((long)ptr);
165 //
166 Value *Source = *CS.arg_begin();
167 if (!isa<PointerType>(Source->getType()))
168 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
169 "FreePtrCast", I);
170 new FreeInst(Source, I);
Chris Lattnercc838342003-09-16 19:42:21 +0000171
172 // If the old instruction was an invoke, add an unconditional branch
173 // before the invoke, which will become the new terminator.
174 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
175 new BranchInst(II->getNormalDest(), I);
176
177 // Delete the old call site
Chris Lattner2dbfa032003-09-01 03:14:56 +0000178 I->getParent()->getInstList().erase(I);
179 Changed = true;
180 ++NumRaised;
181 }
182 }
183
184 Users.pop_back();
Chris Lattnerade686e2002-05-07 19:02:48 +0000185 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000186 }
187
188 return Changed;
189}