blob: 42e1a28af7a5369944634c9ea66d10df063a8634 [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
8#include "llvm/Transforms/ChangeAllocations.h"
9#include "llvm/Transforms/Utils/BasicBlockUtils.h"
10#include "llvm/Module.h"
11#include "llvm/Function.h"
12#include "llvm/DerivedTypes.h"
13#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
15#include "llvm/Pass.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000016#include "Support/StatisticReporter.h"
17
18static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000019
20namespace {
21
22// RaiseAllocations - Turn %malloc and %free calls into the appropriate
23// instruction.
24//
25class RaiseAllocations : public BasicBlockPass {
26 Function *MallocFunc; // Functions in the module we are processing
27 Function *FreeFunc; // Initialized by doPassInitializationVirt
28public:
29 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
30
31 const char *getPassName() const { return "Raise Allocations"; }
32
33 // doPassInitialization - For the raise allocations pass, this finds a
34 // declaration for malloc and free if they exist.
35 //
Chris Lattner113f4f42002-06-25 16:13:24 +000036 bool doInitialization(Module &M);
Chris Lattner77f791d2002-05-07 19:02:48 +000037
38 // runOnBasicBlock - This method does the actual work of converting
39 // instructions over, assuming that the pass has already been initialized.
40 //
Chris Lattner113f4f42002-06-25 16:13:24 +000041 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner77f791d2002-05-07 19:02:48 +000042};
43
44} // end anonymous namespace
45
46
47// createRaiseAllocationsPass - The interface to this file...
48Pass *createRaiseAllocationsPass() {
49 return new RaiseAllocations();
50}
51
52
Chris Lattner113f4f42002-06-25 16:13:24 +000053bool RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000054 // If the module has a symbol table, they might be referring to the malloc
55 // and free functions. If this is the case, grab the method pointers that
56 // the module is using.
57 //
58 // Lookup %malloc and %free in the symbol table, for later use. If they
59 // don't exist, or are not external, we do not worry about converting calls
60 // to that function into the appropriate instruction.
61 //
62 const FunctionType *MallocType = // Get the type for malloc
63 FunctionType::get(PointerType::get(Type::SByteTy),
64 std::vector<const Type*>(1, Type::UIntTy), false);
65
66 const FunctionType *FreeType = // Get the type for free
67 FunctionType::get(Type::VoidTy,
68 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
69 false);
70
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 Lattnere3da2982002-05-24 20:29:18 +000074 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
75 // This handles the common declaration of: 'char *malloc();'
76 if (MallocFunc == 0) {
77 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
78 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000079 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000080 }
81
82 // Check to see if the prototype was forgotten, giving us void (...) * free
83 // This handles the common forward declaration of: 'void free();'
84 if (FreeFunc == 0) {
85 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +000086 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +000087 }
88
89
Chris Lattner77f791d2002-05-07 19:02:48 +000090 // Don't mess with locally defined versions of these functions...
91 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
92 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
93 return false;
94}
95
Chris Lattner03453a02002-05-07 19:04:39 +000096// runOnBasicBlock - Process a basic block, fixing it up...
Chris Lattner77f791d2002-05-07 19:02:48 +000097//
Chris Lattner113f4f42002-06-25 16:13:24 +000098bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner77f791d2002-05-07 19:02:48 +000099 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000100 BasicBlock::InstListType &BIL = BB.getInstList();
Chris Lattner77f791d2002-05-07 19:02:48 +0000101
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 for (BasicBlock::iterator BI = BB.begin(); BI != BB.end();) {
103 Instruction *I = BI;
Chris Lattner77f791d2002-05-07 19:02:48 +0000104
105 if (CallInst *CI = dyn_cast<CallInst>(I)) {
106 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
107 const Type *PtrSByte = PointerType::get(Type::SByteTy);
Chris Lattnere3da2982002-05-24 20:29:18 +0000108 Value *Source = CI->getOperand(1);
109
110 // If no prototype was provided for malloc, we may need to cast the
111 // source size.
112 if (Source->getType() != Type::UIntTy) {
113 CastInst *New = new CastInst(Source, Type::UIntTy, "MallocAmtCast");
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 BI = ++BIL.insert(BI, New);
Chris Lattnere3da2982002-05-24 20:29:18 +0000115 Source = New;
116 }
117
118 MallocInst *MallocI = new MallocInst(PtrSByte, Source, CI->getName());
119
Chris Lattner77f791d2002-05-07 19:02:48 +0000120 CI->setName("");
121 ReplaceInstWithInst(BIL, BI, MallocI);
122 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000123 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000124 continue; // Skip the ++BI
125 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
Chris Lattnere3da2982002-05-24 20:29:18 +0000126 // If no prototype was provided for free, we may need to cast the
127 // source pointer. This should be really uncommon, but it's neccesary
128 // just in case we are dealing with wierd code like this:
129 // free((long)ptr);
130 //
131 Value *Source = CI->getOperand(1);
132 if (!isa<PointerType>(Source->getType())) {
133 CastInst *New = new CastInst(Source, PointerType::get(Type::SByteTy),
134 "FreePtrCast");
Chris Lattner113f4f42002-06-25 16:13:24 +0000135 BI = ++BIL.insert(BI, New);
Chris Lattnere3da2982002-05-24 20:29:18 +0000136 Source = New;
137 }
138
139 ReplaceInstWithInst(BIL, BI, new FreeInst(Source));
Chris Lattner77f791d2002-05-07 19:02:48 +0000140 Changed = true;
141 continue; // Skip the ++BI
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000142 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000143 }
144 }
145
146 ++BI;
147 }
148
149 return Changed;
150}