blob: 42524c8be86c2dfbef2421351e23e16ae71ab69e [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 Lattner0ac54292002-04-09 19:08:28 +000013#include "llvm/Function.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000014#include "llvm/DerivedTypes.h"
15#include "llvm/iMemory.h"
16#include "llvm/iOther.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000017#include "llvm/ConstantVals.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattner5048c3b2002-01-22 00:13:51 +000019#include "TransformInternals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::vector;
21
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022namespace {
23
24// LowerAllocations - Turn malloc and free instructions into %malloc and %free
25// calls.
26//
27class LowerAllocations : public BasicBlockPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000028 Function *MallocFunc; // Functions in the module we are processing
29 Function *FreeFunc; // Initialized by doInitialization
Chris Lattnerbd0ef772002-02-26 21:46:54 +000030
31 const TargetData &DataLayout;
32public:
33 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner79df7c02002-03-26 18:01:55 +000034 MallocFunc = FreeFunc = 0;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000035 }
36
37 // doPassInitialization - For the lower allocations pass, this ensures that a
38 // module contains a declaration for a malloc and a free function.
39 //
40 bool doInitialization(Module *M);
41
42 // runOnBasicBlock - This method does the actual work of converting
43 // instructions over, assuming that the pass has already been initialized.
44 //
45 bool runOnBasicBlock(BasicBlock *BB);
46};
47
48// RaiseAllocations - Turn %malloc and %free calls into the appropriate
49// instruction.
50//
51class RaiseAllocations : public BasicBlockPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000052 Function *MallocFunc; // Functions in the module we are processing
53 Function *FreeFunc; // Initialized by doPassInitializationVirt
Chris Lattnerbd0ef772002-02-26 21:46:54 +000054public:
Chris Lattner79df7c02002-03-26 18:01:55 +000055 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000056
57 // doPassInitialization - For the raise allocations pass, this finds a
58 // declaration for malloc and free if they exist.
59 //
60 bool doInitialization(Module *M);
61
62 // runOnBasicBlock - This method does the actual work of converting
63 // instructions over, assuming that the pass has already been initialized.
64 //
65 bool runOnBasicBlock(BasicBlock *BB);
66};
67
68} // end anonymous namespace
Chris Lattner1bffea02001-10-15 17:31:51 +000069
Chris Lattnerf4de63f2002-01-21 07:31:50 +000070// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000071// module contains a declaration for a malloc and a free function.
72//
73// This function is always successful.
74//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000075bool LowerAllocations::doInitialization(Module *M) {
Chris Lattnerbe591a72002-03-29 03:38:05 +000076 const FunctionType *MallocType =
77 FunctionType::get(PointerType::get(Type::SByteTy),
78 vector<const Type*>(1, Type::UIntTy), false);
79 const FunctionType *FreeType =
80 FunctionType::get(Type::VoidTy,
81 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
82 false);
Chris Lattner1bffea02001-10-15 17:31:51 +000083
Chris Lattnerbe591a72002-03-29 03:38:05 +000084 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
85 FreeFunc = M->getOrInsertFunction("free" , FreeType);
Chris Lattner1bffea02001-10-15 17:31:51 +000086
Chris Lattnerbe591a72002-03-29 03:38:05 +000087 return false;
Chris Lattner1bffea02001-10-15 17:31:51 +000088}
89
Chris Lattner84453722002-01-21 23:34:02 +000090// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000091// instructions over, assuming that the pass has already been initialized.
92//
Chris Lattner84453722002-01-21 23:34:02 +000093bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000094 bool Changed = false;
Chris Lattner79df7c02002-03-26 18:01:55 +000095 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
Chris Lattner1bffea02001-10-15 17:31:51 +000096
97 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner84453722002-01-21 23:34:02 +000098 for (unsigned i = 0; i < BB->size(); ++i) {
99 BasicBlock::InstListType &BBIL = BB->getInstList();
100 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
101 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
Chris Lattner1bffea02001-10-15 17:31:51 +0000102
Chris Lattner84453722002-01-21 23:34:02 +0000103 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
104
105 // Get the number of bytes to be allocated for one element of the
106 // requested type...
107 unsigned Size = DataLayout.getTypeSize(AllocTy);
108
109 // malloc(type) becomes sbyte *malloc(constint)
110 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
111 if (MI->getNumOperands() && Size == 1) {
112 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
113 } else if (MI->getNumOperands()) {
114 // Multiply it by the array size if neccesary...
115 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
116 MallocArg);
117 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
Chris Lattner1bffea02001-10-15 17:31:51 +0000118 }
Chris Lattner84453722002-01-21 23:34:02 +0000119
120 // Create the call to Malloc...
Chris Lattner79df7c02002-03-26 18:01:55 +0000121 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattner84453722002-01-21 23:34:02 +0000122 vector<Value*>(1, MallocArg));
123 BBIL.insert(BBIL.begin()+i, MCall);
124
125 // Create a cast instruction to convert to the right type...
126 CastInst *MCast = new CastInst(MCall, MI->getType());
127 BBIL.insert(BBIL.begin()+i+1, MCast);
128
129 // Replace all uses of the old malloc inst with the cast inst
130 MI->replaceAllUsesWith(MCast);
131 delete MI; // Delete the malloc inst
132 Changed = true;
133 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
134 BBIL.remove(BB->getInstList().begin()+i);
135
136 // Cast the argument to free into a ubyte*...
137 CastInst *MCast = new CastInst(FI->getOperand(0),
138 PointerType::get(Type::UByteTy));
139 BBIL.insert(BBIL.begin()+i, MCast);
140
141 // Insert a call to the free function...
Chris Lattner79df7c02002-03-26 18:01:55 +0000142 CallInst *FCall = new CallInst(FreeFunc,
Chris Lattner84453722002-01-21 23:34:02 +0000143 vector<Value*>(1, MCast));
144 BBIL.insert(BBIL.begin()+i+1, FCall);
145
146 // Delete the old free instruction
147 delete FI;
148 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000149 }
150 }
151
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000152 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000153}
154
Chris Lattner5048c3b2002-01-22 00:13:51 +0000155bool RaiseAllocations::doInitialization(Module *M) {
Chris Lattner5048c3b2002-01-22 00:13:51 +0000156 // If the module has a symbol table, they might be referring to the malloc
157 // and free functions. If this is the case, grab the method pointers that
158 // the module is using.
159 //
160 // Lookup %malloc and %free in the symbol table, for later use. If they
161 // don't exist, or are not external, we do not worry about converting calls
162 // to that function into the appropriate instruction.
163 //
Chris Lattnerbe591a72002-03-29 03:38:05 +0000164 const FunctionType *MallocType = // Get the type for malloc
165 FunctionType::get(PointerType::get(Type::SByteTy),
166 vector<const Type*>(1, Type::UIntTy), false);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000167
Chris Lattnerbe591a72002-03-29 03:38:05 +0000168 const FunctionType *FreeType = // Get the type for free
169 FunctionType::get(Type::VoidTy,
170 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
171 false);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000172
Chris Lattnerbe591a72002-03-29 03:38:05 +0000173 MallocFunc = M->getFunction("malloc", MallocType);
174 FreeFunc = M->getFunction("free" , FreeType);
175
176 // Don't mess with locally defined versions of these functions...
177 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
178 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
Chris Lattner5048c3b2002-01-22 00:13:51 +0000179 return false;
180}
181
182// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
183//
184bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
185 bool Changed = false;
186 BasicBlock::InstListType &BIL = BB->getInstList();
187
188 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
189 Instruction *I = *BI;
190
191 if (CallInst *CI = dyn_cast<CallInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000192 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
Chris Lattner5048c3b2002-01-22 00:13:51 +0000193 const Type *PtrSByte = PointerType::get(Type::SByteTy);
194 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
195 CI->getName());
196 CI->setName("");
Chris Lattner31b80d92002-01-22 03:30:06 +0000197 ReplaceInstWithInst(BIL, BI, MallocI);
Chris Lattner5048c3b2002-01-22 00:13:51 +0000198 Changed = true;
199 continue; // Skip the ++BI
Chris Lattner79df7c02002-03-26 18:01:55 +0000200 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
Chris Lattner5048c3b2002-01-22 00:13:51 +0000201 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
202 Changed = true;
203 continue; // Skip the ++BI
204 }
205 }
206
207 ++BI;
208 }
209
210 return Changed;
211}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000212
213Pass *createLowerAllocationsPass(const TargetData &TD) {
214 return new LowerAllocations(TD);
215}
216Pass *createRaiseAllocationsPass() {
217 return new RaiseAllocations();
218}
219
220