1 //===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
3 // 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.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/ChangeAllocations.h"
11 #include "llvm/Target/TargetData.h"
12 #include "llvm/Module.h"
13 #include "llvm/Function.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/iMemory.h"
16 #include "llvm/iOther.h"
17 #include "llvm/ConstantVals.h"
18 #include "llvm/Pass.h"
19 #include "TransformInternals.h"
24 // LowerAllocations - Turn malloc and free instructions into %malloc and %free
27 class LowerAllocations : public BasicBlockPass {
28 Function *MallocFunc; // Functions in the module we are processing
29 Function *FreeFunc; // Initialized by doInitialization
31 const TargetData &DataLayout;
33 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
34 MallocFunc = FreeFunc = 0;
37 // doPassInitialization - For the lower allocations pass, this ensures that a
38 // module contains a declaration for a malloc and a free function.
40 bool doInitialization(Module *M);
42 // runOnBasicBlock - This method does the actual work of converting
43 // instructions over, assuming that the pass has already been initialized.
45 bool runOnBasicBlock(BasicBlock *BB);
48 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
51 class RaiseAllocations : public BasicBlockPass {
52 Function *MallocFunc; // Functions in the module we are processing
53 Function *FreeFunc; // Initialized by doPassInitializationVirt
55 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
57 // doPassInitialization - For the raise allocations pass, this finds a
58 // declaration for malloc and free if they exist.
60 bool doInitialization(Module *M);
62 // runOnBasicBlock - This method does the actual work of converting
63 // instructions over, assuming that the pass has already been initialized.
65 bool runOnBasicBlock(BasicBlock *BB);
68 } // end anonymous namespace
70 // doInitialization - For the lower allocations pass, this ensures that a
71 // module contains a declaration for a malloc and a free function.
73 // This function is always successful.
75 bool LowerAllocations::doInitialization(Module *M) {
76 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)),
84 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
85 FreeFunc = M->getOrInsertFunction("free" , FreeType);
90 // runOnBasicBlock - This method does the actual work of converting
91 // instructions over, assuming that the pass has already been initialized.
93 bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
95 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
97 // Loop over all of the instructions, looking for malloc or free instructions
98 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...
103 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
105 // Get the number of bytes to be allocated for one element of the
107 unsigned Size = DataLayout.getTypeSize(AllocTy);
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),
117 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
120 // Create the call to Malloc...
121 CallInst *MCall = new CallInst(MallocFunc,
122 vector<Value*>(1, MallocArg));
123 BBIL.insert(BBIL.begin()+i, MCall);
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);
129 // Replace all uses of the old malloc inst with the cast inst
130 MI->replaceAllUsesWith(MCast);
131 delete MI; // Delete the malloc inst
133 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
134 BBIL.remove(BB->getInstList().begin()+i);
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);
141 // Insert a call to the free function...
142 CallInst *FCall = new CallInst(FreeFunc,
143 vector<Value*>(1, MCast));
144 BBIL.insert(BBIL.begin()+i+1, FCall);
146 // Delete the old free instruction
155 bool RaiseAllocations::doInitialization(Module *M) {
156 // 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.
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.
164 const FunctionType *MallocType = // Get the type for malloc
165 FunctionType::get(PointerType::get(Type::SByteTy),
166 vector<const Type*>(1, Type::UIntTy), false);
168 const FunctionType *FreeType = // Get the type for free
169 FunctionType::get(Type::VoidTy,
170 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
173 MallocFunc = M->getFunction("malloc", MallocType);
174 FreeFunc = M->getFunction("free" , FreeType);
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;
182 // doOneCleanupPass - Do one pass over the input method, fixing stuff up.
184 bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
185 bool Changed = false;
186 BasicBlock::InstListType &BIL = BB->getInstList();
188 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
189 Instruction *I = *BI;
191 if (CallInst *CI = dyn_cast<CallInst>(I)) {
192 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
193 const Type *PtrSByte = PointerType::get(Type::SByteTy);
194 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
197 ReplaceInstWithInst(BIL, BI, MallocI);
199 continue; // Skip the ++BI
200 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
201 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
203 continue; // Skip the ++BI
213 Pass *createLowerAllocationsPass(const TargetData &TD) {
214 return new LowerAllocations(TD);
216 Pass *createRaiseAllocationsPass() {
217 return new RaiseAllocations();