blob: 2f4a1df842eaebfe2486a26ed74497ba52e82f41 [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 Lattner42706e42002-07-23 22:04:17 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattner77f791d2002-05-07 19:02:48 +00009#include "llvm/Transforms/Utils/BasicBlockUtils.h"
10#include "llvm/Module.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000011#include "llvm/DerivedTypes.h"
12#include "llvm/iMemory.h"
13#include "llvm/iOther.h"
14#include "llvm/Pass.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000015#include "Support/StatisticReporter.h"
16
17static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000018
19namespace {
20
21// RaiseAllocations - Turn %malloc and %free calls into the appropriate
22// instruction.
23//
24class RaiseAllocations : public BasicBlockPass {
25 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doPassInitializationVirt
27public:
Chris Lattner6788f252002-07-23 18:06:30 +000028 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattner77f791d2002-05-07 19:02:48 +000029
30 // doPassInitialization - For the raise allocations pass, this finds a
31 // declaration for malloc and free if they exist.
32 //
Chris Lattner113f4f42002-06-25 16:13:24 +000033 bool doInitialization(Module &M);
Chris Lattner77f791d2002-05-07 19:02:48 +000034
35 // runOnBasicBlock - This method does the actual work of converting
36 // instructions over, assuming that the pass has already been initialized.
37 //
Chris Lattner113f4f42002-06-25 16:13:24 +000038 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner77f791d2002-05-07 19:02:48 +000039};
40
Chris Lattnerc8b70922002-07-26 21:12:46 +000041 RegisterOpt<RaiseAllocations>
42 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattner77f791d2002-05-07 19:02:48 +000043} // end anonymous namespace
44
45
46// createRaiseAllocationsPass - The interface to this file...
47Pass *createRaiseAllocationsPass() {
48 return new RaiseAllocations();
49}
50
51
Chris Lattner113f4f42002-06-25 16:13:24 +000052bool RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000053 // If the module has a symbol table, they might be referring to the malloc
54 // and free functions. If this is the case, grab the method pointers that
55 // the module is using.
56 //
57 // Lookup %malloc and %free in the symbol table, for later use. If they
58 // don't exist, or are not external, we do not worry about converting calls
59 // to that function into the appropriate instruction.
60 //
61 const FunctionType *MallocType = // Get the type for malloc
62 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattnerdfe04182002-07-18 00:18:01 +000063 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattner77f791d2002-05-07 19:02:48 +000064
65 const FunctionType *FreeType = // Get the type for free
66 FunctionType::get(Type::VoidTy,
67 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
68 false);
69
Chris Lattnerdfe04182002-07-18 00:18:01 +000070 // Get Malloc and free prototypes if they exist!
Chris Lattner113f4f42002-06-25 16:13:24 +000071 MallocFunc = M.getFunction("malloc", MallocType);
72 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattner77f791d2002-05-07 19:02:48 +000073
Chris Lattnerdfe04182002-07-18 00:18:01 +000074 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
75 // This handles the common declaration of: 'void *malloc(unsigned);'
76 if (MallocFunc == 0) {
77 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
78 std::vector<const Type*>(1, Type::UIntTy), false);
79 MallocFunc = M.getFunction("malloc", MallocType);
80 }
81
Chris Lattnere3da2982002-05-24 20:29:18 +000082 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattnerdfe04182002-07-18 00:18:01 +000083 // This handles the common declaration of: 'void *malloc();'
Chris Lattnere3da2982002-05-24 20:29:18 +000084 if (MallocFunc == 0) {
85 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
86 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000087 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000088 }
89
90 // Check to see if the prototype was forgotten, giving us void (...) * free
91 // This handles the common forward declaration of: 'void free();'
92 if (FreeFunc == 0) {
93 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +000094 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +000095 }
96
97
Chris Lattner77f791d2002-05-07 19:02:48 +000098 // Don't mess with locally defined versions of these functions...
99 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
100 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
101 return false;
102}
103
Chris Lattner03453a02002-05-07 19:04:39 +0000104// runOnBasicBlock - Process a basic block, fixing it up...
Chris Lattner77f791d2002-05-07 19:02:48 +0000105//
Chris Lattner113f4f42002-06-25 16:13:24 +0000106bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner77f791d2002-05-07 19:02:48 +0000107 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 BasicBlock::InstListType &BIL = BB.getInstList();
Chris Lattner77f791d2002-05-07 19:02:48 +0000109
Chris Lattnera239e682002-09-10 22:38:47 +0000110 for (BasicBlock::iterator BI = BB.begin(); BI != BB.end(); ++BI) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000111 Instruction *I = BI;
Chris Lattner77f791d2002-05-07 19:02:48 +0000112
113 if (CallInst *CI = dyn_cast<CallInst>(I)) {
114 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
Chris Lattnere3da2982002-05-24 20:29:18 +0000115 Value *Source = CI->getOperand(1);
116
117 // If no prototype was provided for malloc, we may need to cast the
118 // source size.
Chris Lattnera239e682002-09-10 22:38:47 +0000119 if (Source->getType() != Type::UIntTy)
120 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", BI);
Chris Lattnere3da2982002-05-24 20:29:18 +0000121
Chris Lattnera239e682002-09-10 22:38:47 +0000122 std::string Name(CI->getName()); CI->setName("");
Chris Lattner322bf4f2002-09-13 22:28:45 +0000123 BI = new MallocInst(Type::SByteTy, Source, Name, BI);
Chris Lattner9674b862002-09-10 23:31:12 +0000124 CI->replaceAllUsesWith(BI);
Chris Lattnera239e682002-09-10 22:38:47 +0000125 BIL.erase(I);
Chris Lattner77f791d2002-05-07 19:02:48 +0000126 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000127 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000128 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
Chris Lattnere3da2982002-05-24 20:29:18 +0000129 // If no prototype was provided for free, we may need to cast the
130 // source pointer. This should be really uncommon, but it's neccesary
131 // just in case we are dealing with wierd code like this:
132 // free((long)ptr);
133 //
134 Value *Source = CI->getOperand(1);
Chris Lattnera239e682002-09-10 22:38:47 +0000135 if (!isa<PointerType>(Source->getType()))
136 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
137 "FreePtrCast", BI);
138 BI = new FreeInst(Source, BI);
139 BIL.erase(I);
Chris Lattner77f791d2002-05-07 19:02:48 +0000140 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000141 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000142 }
143 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000144 }
145
146 return Changed;
147}