blob: 62355be87805782a3507a0317b8700e876422569 [file] [log] [blame]
Chris Lattner03453a02002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
Chris Lattner77f791d2002-05-07 19:02:48 +00002//
Chris Lattner03453a02002-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 Lattner77f791d2002-05-07 19:02:48 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner8ef1b882003-09-01 03:14:56 +00008#include "llvm/Transforms/IPO.h"
Chris Lattner77f791d2002-05-07 19:02:48 +00009#include "llvm/Module.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000010#include "llvm/DerivedTypes.h"
11#include "llvm/iMemory.h"
12#include "llvm/iOther.h"
13#include "llvm/Pass.h"
Chris Lattner8ef1b882003-09-01 03:14:56 +000014#include "llvm/Support/CallSite.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000015#include "Support/Statistic.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000016
17namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000018 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000019
Chris Lattnerbf3a0992002-10-01 22:38:41 +000020 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
21 // instruction.
Chris Lattner77f791d2002-05-07 19:02:48 +000022 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000023 class RaiseAllocations : public Pass {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000024 Function *MallocFunc; // Functions in the module we are processing
25 Function *FreeFunc; // Initialized by doPassInitializationVirt
26 public:
27 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
28
29 // doPassInitialization - For the raise allocations pass, this finds a
30 // declaration for malloc and free if they exist.
31 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000032 void doInitialization(Module &M);
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033
Chris Lattner8ef1b882003-09-01 03:14:56 +000034 // run - This method does the actual work of converting instructions over.
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035 //
Chris Lattner8ef1b882003-09-01 03:14:56 +000036 bool run(Module &M);
Chris Lattnerbf3a0992002-10-01 22:38:41 +000037 };
38
Chris Lattnerc8b70922002-07-26 21:12:46 +000039 RegisterOpt<RaiseAllocations>
40 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattner77f791d2002-05-07 19:02:48 +000041} // end anonymous namespace
42
43
44// createRaiseAllocationsPass - The interface to this file...
45Pass *createRaiseAllocationsPass() {
46 return new RaiseAllocations();
47}
48
49
Chris Lattner8ef1b882003-09-01 03:14:56 +000050// If the module has a symbol table, they might be referring to the malloc and
51// free functions. If this is the case, grab the method pointers that the
52// module is using.
53//
54// Lookup %malloc and %free in the symbol table, for later use. If they don't
55// exist, or are not external, we do not worry about converting calls to that
56// function into the appropriate instruction.
57//
58void RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000059 const FunctionType *MallocType = // Get the type for malloc
60 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattnerdfe04182002-07-18 00:18:01 +000061 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattner77f791d2002-05-07 19:02:48 +000062
63 const FunctionType *FreeType = // Get the type for free
64 FunctionType::get(Type::VoidTy,
65 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
66 false);
67
Chris Lattnerdfe04182002-07-18 00:18:01 +000068 // Get Malloc and free prototypes if they exist!
Chris Lattner113f4f42002-06-25 16:13:24 +000069 MallocFunc = M.getFunction("malloc", MallocType);
70 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattner77f791d2002-05-07 19:02:48 +000071
Chris Lattnerdfe04182002-07-18 00:18:01 +000072 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
73 // This handles the common declaration of: 'void *malloc(unsigned);'
74 if (MallocFunc == 0) {
75 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
76 std::vector<const Type*>(1, Type::UIntTy), false);
77 MallocFunc = M.getFunction("malloc", MallocType);
78 }
79
Chris Lattnere3da2982002-05-24 20:29:18 +000080 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattnerdfe04182002-07-18 00:18:01 +000081 // This handles the common declaration of: 'void *malloc();'
Chris Lattnere3da2982002-05-24 20:29:18 +000082 if (MallocFunc == 0) {
83 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
84 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000085 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000086 }
87
88 // Check to see if the prototype was forgotten, giving us void (...) * free
89 // This handles the common forward declaration of: 'void free();'
90 if (FreeFunc == 0) {
91 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +000092 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +000093 }
94
Chris Lattner603e0072003-08-11 15:05:08 +000095 // One last try, check to see if we can find free as 'int (...)* free'. This
96 // handles the case where NOTHING was declared.
97 if (FreeFunc == 0) {
98 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
99 FreeFunc = M.getFunction("free", FreeType);
100 }
101
Chris Lattner77f791d2002-05-07 19:02:48 +0000102 // Don't mess with locally defined versions of these functions...
103 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
104 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattner77f791d2002-05-07 19:02:48 +0000105}
106
Chris Lattner8ef1b882003-09-01 03:14:56 +0000107// run - Transform calls into instructions...
Chris Lattner77f791d2002-05-07 19:02:48 +0000108//
Chris Lattner8ef1b882003-09-01 03:14:56 +0000109bool RaiseAllocations::run(Module &M) {
110 // Find the malloc/free prototypes...
111 doInitialization(M);
112
Chris Lattner77f791d2002-05-07 19:02:48 +0000113 bool Changed = false;
Chris Lattner77f791d2002-05-07 19:02:48 +0000114
Chris Lattner8ef1b882003-09-01 03:14:56 +0000115 // First, process all of the malloc calls...
116 if (MallocFunc) {
117 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
118 while (!Users.empty()) {
119 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
120 CallSite CS = CallSite::get(I);
121 if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
122 CS.arg_begin() != CS.arg_end()) {
123 Value *Source = *CS.arg_begin();
124
125 // If no prototype was provided for malloc, we may need to cast the
126 // source size.
127 if (Source->getType() != Type::UIntTy)
128 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
129
130 std::string Name(I->getName()); I->setName("");
131 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
132 I->replaceAllUsesWith(MI);
133 MI->getParent()->getInstList().erase(I);
134 Changed = true;
135 ++NumRaised;
136 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000137 }
Chris Lattner8ef1b882003-09-01 03:14:56 +0000138
139 Users.pop_back();
140 }
141 }
142
143 // Next, process all free calls...
144 if (FreeFunc) {
145 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
146
147 while (!Users.empty()) {
148 if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
149 CallSite CS = CallSite::get(I);
150 if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
151 CS.arg_begin() != CS.arg_end()) {
152
153 // If no prototype was provided for free, we may need to cast the
154 // source pointer. This should be really uncommon, but it's necessary
155 // just in case we are dealing with wierd code like this:
156 // free((long)ptr);
157 //
158 Value *Source = *CS.arg_begin();
159 if (!isa<PointerType>(Source->getType()))
160 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
161 "FreePtrCast", I);
162 new FreeInst(Source, I);
163 I->getParent()->getInstList().erase(I);
164 Changed = true;
165 ++NumRaised;
166 }
167 }
168
169 Users.pop_back();
Chris Lattner77f791d2002-05-07 19:02:48 +0000170 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000171 }
172
173 return Changed;
174}