blob: 584a2e98b098357d22858b49be97590df04b9766 [file] [log] [blame]
Chris Lattner65e96e52002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerade686e2002-05-07 19:02:48 +00009//
Chris Lattner65e96e52002-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 Lattnerade686e2002-05-07 19:02:48 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner2dbfa032003-09-01 03:14:56 +000015#include "llvm/Transforms/IPO.h"
Chris Lattnereb12cd62003-12-07 01:42:08 +000016#include "llvm/Constants.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000017#include "llvm/DerivedTypes.h"
Chris Lattnereb12cd62003-12-07 01:42:08 +000018#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000020#include "llvm/Pass.h"
Chris Lattner2dbfa032003-09-01 03:14:56 +000021#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner1e2385b2003-11-21 21:54:22 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnerade686e2002-05-07 19:02:48 +000025namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000026 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
Chris Lattnerade686e2002-05-07 19:02:48 +000027
Chris Lattnera92f6962002-10-01 22:38:41 +000028 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
29 // instruction.
Chris Lattnerade686e2002-05-07 19:02:48 +000030 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000031 class RaiseAllocations : public ModulePass {
Chris Lattnera92f6962002-10-01 22:38:41 +000032 Function *MallocFunc; // Functions in the module we are processing
33 Function *FreeFunc; // Initialized by doPassInitializationVirt
34 public:
35 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Misha Brukmanfd939082005-04-21 23:48:37 +000036
Chris Lattnera92f6962002-10-01 22:38:41 +000037 // doPassInitialization - For the raise allocations pass, this finds a
38 // declaration for malloc and free if they exist.
39 //
Chris Lattner2dbfa032003-09-01 03:14:56 +000040 void doInitialization(Module &M);
Misha Brukmanfd939082005-04-21 23:48:37 +000041
Chris Lattner2dbfa032003-09-01 03:14:56 +000042 // run - This method does the actual work of converting instructions over.
Chris Lattnera92f6962002-10-01 22:38:41 +000043 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000044 bool runOnModule(Module &M);
Chris Lattnera92f6962002-10-01 22:38:41 +000045 };
Misha Brukmanfd939082005-04-21 23:48:37 +000046
Chris Lattner7f8897f2006-08-27 22:42:52 +000047 RegisterPass<RaiseAllocations>
Chris Lattnera6275cc2002-07-26 21:12:46 +000048 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattnerade686e2002-05-07 19:02:48 +000049} // end anonymous namespace
50
51
52// createRaiseAllocationsPass - The interface to this file...
Chris Lattnerb12914b2004-09-20 04:48:05 +000053ModulePass *llvm::createRaiseAllocationsPass() {
Chris Lattnerade686e2002-05-07 19:02:48 +000054 return new RaiseAllocations();
55}
56
57
Chris Lattner2dbfa032003-09-01 03:14:56 +000058// If the module has a symbol table, they might be referring to the malloc and
59// free functions. If this is the case, grab the method pointers that the
60// module is using.
61//
62// Lookup %malloc and %free in the symbol table, for later use. If they don't
63// exist, or are not external, we do not worry about converting calls to that
64// function into the appropriate instruction.
65//
66void RaiseAllocations::doInitialization(Module &M) {
Chris Lattnerade686e2002-05-07 19:02:48 +000067 const FunctionType *MallocType = // Get the type for malloc
68 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattner0b5909e2002-07-18 00:18:01 +000069 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattnerade686e2002-05-07 19:02:48 +000070
71 const FunctionType *FreeType = // Get the type for free
72 FunctionType::get(Type::VoidTy,
73 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
74 false);
75
Chris Lattner0b5909e2002-07-18 00:18:01 +000076 // Get Malloc and free prototypes if they exist!
Chris Lattner7e708292002-06-25 16:13:24 +000077 MallocFunc = M.getFunction("malloc", MallocType);
78 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattnerade686e2002-05-07 19:02:48 +000079
Chris Lattner0b5909e2002-07-18 00:18:01 +000080 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
81 // This handles the common declaration of: 'void *malloc(unsigned);'
82 if (MallocFunc == 0) {
83 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
84 std::vector<const Type*>(1, Type::UIntTy), false);
85 MallocFunc = M.getFunction("malloc", MallocType);
86 }
87
Chris Lattner47e0f3a2002-05-24 20:29:18 +000088 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattner0b5909e2002-07-18 00:18:01 +000089 // This handles the common declaration of: 'void *malloc();'
Chris Lattner47e0f3a2002-05-24 20:29:18 +000090 if (MallocFunc == 0) {
91 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
92 std::vector<const Type*>(), true);
Chris Lattner7e708292002-06-25 16:13:24 +000093 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattner47e0f3a2002-05-24 20:29:18 +000094 }
95
96 // Check to see if the prototype was forgotten, giving us void (...) * free
97 // This handles the common forward declaration of: 'void free();'
98 if (FreeFunc == 0) {
99 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner7e708292002-06-25 16:13:24 +0000100 FreeFunc = M.getFunction("free", FreeType);
Chris Lattner47e0f3a2002-05-24 20:29:18 +0000101 }
102
Chris Lattner1f28e8c2003-08-11 15:05:08 +0000103 // One last try, check to see if we can find free as 'int (...)* free'. This
104 // handles the case where NOTHING was declared.
105 if (FreeFunc == 0) {
106 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
107 FreeFunc = M.getFunction("free", FreeType);
108 }
109
Chris Lattnerade686e2002-05-07 19:02:48 +0000110 // Don't mess with locally defined versions of these functions...
111 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
112 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattnerade686e2002-05-07 19:02:48 +0000113}
114
Chris Lattner2dbfa032003-09-01 03:14:56 +0000115// run - Transform calls into instructions...
Chris Lattnerade686e2002-05-07 19:02:48 +0000116//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000117bool RaiseAllocations::runOnModule(Module &M) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000118 // Find the malloc/free prototypes...
119 doInitialization(M);
120
Chris Lattnerade686e2002-05-07 19:02:48 +0000121 bool Changed = false;
Chris Lattnerade686e2002-05-07 19:02:48 +0000122
Chris Lattner2dbfa032003-09-01 03:14:56 +0000123 // First, process all of the malloc calls...
124 if (MallocFunc) {
125 std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000126 std::vector<Value*> EqPointers; // Values equal to MallocFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000127 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000128 User *U = Users.back();
129 Users.pop_back();
130
131 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000132 CallSite CS = CallSite::get(I);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000133 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
134 (CS.getCalledFunction() == MallocFunc ||
135 std::find(EqPointers.begin(), EqPointers.end(),
136 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000137
Chris Lattner2dbfa032003-09-01 03:14:56 +0000138 Value *Source = *CS.arg_begin();
Misha Brukmanfd939082005-04-21 23:48:37 +0000139
Chris Lattner2dbfa032003-09-01 03:14:56 +0000140 // If no prototype was provided for malloc, we may need to cast the
141 // source size.
142 if (Source->getType() != Type::UIntTy)
Reid Spencer3da59db2006-11-27 01:05:10 +0000143 Source =
144 CastInst::createInferredCast(Source, Type::UIntTy,
145 "MallocAmtCast", I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000146
Chris Lattner2dbfa032003-09-01 03:14:56 +0000147 std::string Name(I->getName()); I->setName("");
148 MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
149 I->replaceAllUsesWith(MI);
Chris Lattnercc838342003-09-16 19:42:21 +0000150
151 // If the old instruction was an invoke, add an unconditional branch
152 // before the invoke, which will become the new terminator.
153 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
154 new BranchInst(II->getNormalDest(), I);
155
156 // Delete the old call site
Chris Lattner2dbfa032003-09-01 03:14:56 +0000157 MI->getParent()->getInstList().erase(I);
158 Changed = true;
159 ++NumRaised;
160 }
Reid Spencer518310c2004-07-18 00:44:37 +0000161 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
162 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
163 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000164 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000165 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000166 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
167 EqPointers.push_back(CE);
168 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000169 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000170 }
171 }
172
173 // Next, process all free calls...
174 if (FreeFunc) {
175 std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
Chris Lattnereb12cd62003-12-07 01:42:08 +0000176 std::vector<Value*> EqPointers; // Values equal to FreeFunc
Chris Lattner2dbfa032003-09-01 03:14:56 +0000177
178 while (!Users.empty()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000179 User *U = Users.back();
180 Users.pop_back();
181
182 if (Instruction *I = dyn_cast<Instruction>(U)) {
Chris Lattner2dbfa032003-09-01 03:14:56 +0000183 CallSite CS = CallSite::get(I);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000184 if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
185 (CS.getCalledFunction() == FreeFunc ||
186 std::find(EqPointers.begin(), EqPointers.end(),
187 CS.getCalledValue()) != EqPointers.end())) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000188
Chris Lattner2dbfa032003-09-01 03:14:56 +0000189 // If no prototype was provided for free, we may need to cast the
190 // source pointer. This should be really uncommon, but it's necessary
Chris Lattnerda895d62005-02-27 06:18:25 +0000191 // just in case we are dealing with weird code like this:
Chris Lattner2dbfa032003-09-01 03:14:56 +0000192 // free((long)ptr);
193 //
194 Value *Source = *CS.arg_begin();
195 if (!isa<PointerType>(Source->getType()))
Reid Spencer3da59db2006-11-27 01:05:10 +0000196 Source = CastInst::createInferredCast(
197 Source, PointerType::get(Type::SByteTy), "FreePtrCast", I);
Chris Lattner2dbfa032003-09-01 03:14:56 +0000198 new FreeInst(Source, I);
Chris Lattnercc838342003-09-16 19:42:21 +0000199
200 // If the old instruction was an invoke, add an unconditional branch
201 // before the invoke, which will become the new terminator.
202 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
203 new BranchInst(II->getNormalDest(), I);
204
205 // Delete the old call site
Chris Lattner52f20f82004-11-09 05:10:56 +0000206 if (I->getType() != Type::VoidTy)
207 I->replaceAllUsesWith(UndefValue::get(I->getType()));
208 I->eraseFromParent();
Chris Lattner2dbfa032003-09-01 03:14:56 +0000209 Changed = true;
210 ++NumRaised;
211 }
Reid Spencer518310c2004-07-18 00:44:37 +0000212 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
213 Users.insert(Users.end(), GV->use_begin(), GV->use_end());
214 EqPointers.push_back(GV);
Chris Lattnereb12cd62003-12-07 01:42:08 +0000215 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000216 if (CE->isCast()) {
Chris Lattnereb12cd62003-12-07 01:42:08 +0000217 Users.insert(Users.end(), CE->use_begin(), CE->use_end());
218 EqPointers.push_back(CE);
219 }
Chris Lattner2dbfa032003-09-01 03:14:56 +0000220 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000221 }
Chris Lattnerade686e2002-05-07 19:02:48 +0000222 }
223
224 return Changed;
225}