blob: 3345acd6f73c354907ae0e369a6fd1d23b82cebd [file] [log] [blame]
Chris Lattner5048c3b2002-01-22 00:13:51 +00001//===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
Chris Lattner1bffea02001-10-15 17:31:51 +00002//
Chris Lattner5048c3b2002-01-22 00:13:51 +00003// This file defines two passes that convert malloc and free instructions to
4// calls to and from %malloc & %free function calls. The LowerAllocations
5// transformation is a target dependant tranformation because it depends on the
6// size of data types and alignment constraints.
Chris Lattner1bffea02001-10-15 17:31:51 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattnerd7db8632002-01-22 01:04:08 +000010#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000011#include "llvm/Target/TargetData.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000012#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/iMemory.h"
15#include "llvm/iOther.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016#include "llvm/ConstantVals.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000017#include "llvm/Pass.h"
Chris Lattner5048c3b2002-01-22 00:13:51 +000018#include "TransformInternals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::vector;
20
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021namespace {
22
23// LowerAllocations - Turn malloc and free instructions into %malloc and %free
24// calls.
25//
26class LowerAllocations : public BasicBlockPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000027 Function *MallocFunc; // Functions in the module we are processing
28 Function *FreeFunc; // Initialized by doInitialization
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029
30 const TargetData &DataLayout;
31public:
32 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner79df7c02002-03-26 18:01:55 +000033 MallocFunc = FreeFunc = 0;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000034 }
35
36 // doPassInitialization - For the lower allocations pass, this ensures that a
37 // module contains a declaration for a malloc and a free function.
38 //
39 bool doInitialization(Module *M);
40
41 // runOnBasicBlock - This method does the actual work of converting
42 // instructions over, assuming that the pass has already been initialized.
43 //
44 bool runOnBasicBlock(BasicBlock *BB);
45};
46
47// RaiseAllocations - Turn %malloc and %free calls into the appropriate
48// instruction.
49//
50class RaiseAllocations : public BasicBlockPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000051 Function *MallocFunc; // Functions in the module we are processing
52 Function *FreeFunc; // Initialized by doPassInitializationVirt
Chris Lattnerbd0ef772002-02-26 21:46:54 +000053public:
Chris Lattner79df7c02002-03-26 18:01:55 +000054 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000055
56 // doPassInitialization - For the raise allocations pass, this finds a
57 // declaration for malloc and free if they exist.
58 //
59 bool doInitialization(Module *M);
60
61 // runOnBasicBlock - This method does the actual work of converting
62 // instructions over, assuming that the pass has already been initialized.
63 //
64 bool runOnBasicBlock(BasicBlock *BB);
65};
66
67} // end anonymous namespace
Chris Lattner1bffea02001-10-15 17:31:51 +000068
Chris Lattnerf4de63f2002-01-21 07:31:50 +000069// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000070// module contains a declaration for a malloc and a free function.
71//
72// This function is always successful.
73//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000074bool LowerAllocations::doInitialization(Module *M) {
Chris Lattnerbe591a72002-03-29 03:38:05 +000075 const FunctionType *MallocType =
76 FunctionType::get(PointerType::get(Type::SByteTy),
77 vector<const Type*>(1, Type::UIntTy), false);
78 const FunctionType *FreeType =
79 FunctionType::get(Type::VoidTy,
80 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
81 false);
Chris Lattner1bffea02001-10-15 17:31:51 +000082
Chris Lattnerbe591a72002-03-29 03:38:05 +000083 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
84 FreeFunc = M->getOrInsertFunction("free" , FreeType);
Chris Lattner1bffea02001-10-15 17:31:51 +000085
Chris Lattnerbe591a72002-03-29 03:38:05 +000086 return false;
Chris Lattner1bffea02001-10-15 17:31:51 +000087}
88
Chris Lattner84453722002-01-21 23:34:02 +000089// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000090// instructions over, assuming that the pass has already been initialized.
91//
Chris Lattner84453722002-01-21 23:34:02 +000092bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000093 bool Changed = false;
Chris Lattner79df7c02002-03-26 18:01:55 +000094 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
Chris Lattner1bffea02001-10-15 17:31:51 +000095
96 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner84453722002-01-21 23:34:02 +000097 for (unsigned i = 0; i < BB->size(); ++i) {
98 BasicBlock::InstListType &BBIL = BB->getInstList();
99 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
100 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
Chris Lattner1bffea02001-10-15 17:31:51 +0000101
Chris Lattner84453722002-01-21 23:34:02 +0000102 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
103
104 // Get the number of bytes to be allocated for one element of the
105 // requested type...
106 unsigned Size = DataLayout.getTypeSize(AllocTy);
107
108 // malloc(type) becomes sbyte *malloc(constint)
109 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
110 if (MI->getNumOperands() && Size == 1) {
111 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
112 } else if (MI->getNumOperands()) {
113 // Multiply it by the array size if neccesary...
114 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
115 MallocArg);
116 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
Chris Lattner1bffea02001-10-15 17:31:51 +0000117 }
Chris Lattner84453722002-01-21 23:34:02 +0000118
119 // Create the call to Malloc...
Chris Lattner79df7c02002-03-26 18:01:55 +0000120 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattner84453722002-01-21 23:34:02 +0000121 vector<Value*>(1, MallocArg));
122 BBIL.insert(BBIL.begin()+i, MCall);
123
124 // Create a cast instruction to convert to the right type...
125 CastInst *MCast = new CastInst(MCall, MI->getType());
126 BBIL.insert(BBIL.begin()+i+1, MCast);
127
128 // Replace all uses of the old malloc inst with the cast inst
129 MI->replaceAllUsesWith(MCast);
130 delete MI; // Delete the malloc inst
131 Changed = true;
132 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
133 BBIL.remove(BB->getInstList().begin()+i);
134
135 // Cast the argument to free into a ubyte*...
136 CastInst *MCast = new CastInst(FI->getOperand(0),
137 PointerType::get(Type::UByteTy));
138 BBIL.insert(BBIL.begin()+i, MCast);
139
140 // Insert a call to the free function...
Chris Lattner79df7c02002-03-26 18:01:55 +0000141 CallInst *FCall = new CallInst(FreeFunc,
Chris Lattner84453722002-01-21 23:34:02 +0000142 vector<Value*>(1, MCast));
143 BBIL.insert(BBIL.begin()+i+1, FCall);
144
145 // Delete the old free instruction
146 delete FI;
147 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000148 }
149 }
150
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000151 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000152}
153
Chris Lattner5048c3b2002-01-22 00:13:51 +0000154bool RaiseAllocations::doInitialization(Module *M) {
Chris Lattner5048c3b2002-01-22 00:13:51 +0000155 // If the module has a symbol table, they might be referring to the malloc
156 // and free functions. If this is the case, grab the method pointers that
157 // the module is using.
158 //
159 // Lookup %malloc and %free in the symbol table, for later use. If they
160 // don't exist, or are not external, we do not worry about converting calls
161 // to that function into the appropriate instruction.
162 //
Chris Lattnerbe591a72002-03-29 03:38:05 +0000163 const FunctionType *MallocType = // Get the type for malloc
164 FunctionType::get(PointerType::get(Type::SByteTy),
165 vector<const Type*>(1, Type::UIntTy), false);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000166
Chris Lattnerbe591a72002-03-29 03:38:05 +0000167 const FunctionType *FreeType = // Get the type for free
168 FunctionType::get(Type::VoidTy,
169 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
170 false);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000171
Chris Lattnerbe591a72002-03-29 03:38:05 +0000172 MallocFunc = M->getFunction("malloc", MallocType);
173 FreeFunc = M->getFunction("free" , FreeType);
174
175 // Don't mess with locally defined versions of these functions...
176 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
177 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattner5048c3b2002-01-22 00:13:51 +0000178 return false;
179}
180
181// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
182//
183bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
184 bool Changed = false;
185 BasicBlock::InstListType &BIL = BB->getInstList();
186
187 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
188 Instruction *I = *BI;
189
190 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
Chris Lattner5048c3b2002-01-22 00:13:51 +0000192 const Type *PtrSByte = PointerType::get(Type::SByteTy);
193 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
194 CI->getName());
195 CI->setName("");
Chris Lattner31b80d92002-01-22 03:30:06 +0000196 ReplaceInstWithInst(BIL, BI, MallocI);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000197 Changed = true;
198 continue; // Skip the ++BI
Chris Lattner79df7c02002-03-26 18:01:55 +0000199 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
Chris Lattner5048c3b2002-01-22 00:13:51 +0000200 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
201 Changed = true;
202 continue; // Skip the ++BI
203 }
204 }
205
206 ++BI;
207 }
208
209 return Changed;
210}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000211
212Pass *createLowerAllocationsPass(const TargetData &TD) {
213 return new LowerAllocations(TD);
214}
215Pass *createRaiseAllocationsPass() {
216 return new RaiseAllocations();
217}
218
219