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/DerivedTypes.h"
14 #include "llvm/iMemory.h"
15 #include "llvm/iOther.h"
16 #include "llvm/ConstantVals.h"
17 #include "llvm/Pass.h"
18 #include "TransformInternals.h"
23 // LowerAllocations - Turn malloc and free instructions into %malloc and %free
26 class LowerAllocations : public BasicBlockPass {
27 Function *MallocFunc; // Functions in the module we are processing
28 Function *FreeFunc; // Initialized by doInitialization
30 const TargetData &DataLayout;
32 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
33 MallocFunc = FreeFunc = 0;
36 // doPassInitialization - For the lower allocations pass, this ensures that a
37 // module contains a declaration for a malloc and a free function.
39 bool doInitialization(Module *M);
41 // runOnBasicBlock - This method does the actual work of converting
42 // instructions over, assuming that the pass has already been initialized.
44 bool runOnBasicBlock(BasicBlock *BB);
47 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
50 class RaiseAllocations : public BasicBlockPass {
51 Function *MallocFunc; // Functions in the module we are processing
52 Function *FreeFunc; // Initialized by doPassInitializationVirt
54 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
56 // doPassInitialization - For the raise allocations pass, this finds a
57 // declaration for malloc and free if they exist.
59 bool doInitialization(Module *M);
61 // runOnBasicBlock - This method does the actual work of converting
62 // instructions over, assuming that the pass has already been initialized.
64 bool runOnBasicBlock(BasicBlock *BB);
67 } // end anonymous namespace
69 // doInitialization - For the lower allocations pass, this ensures that a
70 // module contains a declaration for a malloc and a free function.
72 // This function is always successful.
74 bool LowerAllocations::doInitialization(Module *M) {
75 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)),
83 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
84 FreeFunc = M->getOrInsertFunction("free" , FreeType);
89 // runOnBasicBlock - This method does the actual work of converting
90 // instructions over, assuming that the pass has already been initialized.
92 bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
94 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
96 // Loop over all of the instructions, looking for malloc or free instructions
97 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...
102 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
104 // Get the number of bytes to be allocated for one element of the
106 unsigned Size = DataLayout.getTypeSize(AllocTy);
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),
116 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
119 // Create the call to Malloc...
120 CallInst *MCall = new CallInst(MallocFunc,
121 vector<Value*>(1, MallocArg));
122 BBIL.insert(BBIL.begin()+i, MCall);
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);
128 // Replace all uses of the old malloc inst with the cast inst
129 MI->replaceAllUsesWith(MCast);
130 delete MI; // Delete the malloc inst
132 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
133 BBIL.remove(BB->getInstList().begin()+i);
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);
140 // Insert a call to the free function...
141 CallInst *FCall = new CallInst(FreeFunc,
142 vector<Value*>(1, MCast));
143 BBIL.insert(BBIL.begin()+i+1, FCall);
145 // Delete the old free instruction
154 bool RaiseAllocations::doInitialization(Module *M) {
155 // 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.
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.
163 const FunctionType *MallocType = // Get the type for malloc
164 FunctionType::get(PointerType::get(Type::SByteTy),
165 vector<const Type*>(1, Type::UIntTy), false);
167 const FunctionType *FreeType = // Get the type for free
168 FunctionType::get(Type::VoidTy,
169 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
172 MallocFunc = M->getFunction("malloc", MallocType);
173 FreeFunc = M->getFunction("free" , FreeType);
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;
181 // doOneCleanupPass - Do one pass over the input method, fixing stuff up.
183 bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
184 bool Changed = false;
185 BasicBlock::InstListType &BIL = BB->getInstList();
187 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
188 Instruction *I = *BI;
190 if (CallInst *CI = dyn_cast<CallInst>(I)) {
191 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
192 const Type *PtrSByte = PointerType::get(Type::SByteTy);
193 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
196 ReplaceInstWithInst(BIL, BI, MallocI);
198 continue; // Skip the ++BI
199 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
200 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
202 continue; // Skip the ++BI
212 Pass *createLowerAllocationsPass(const TargetData &TD) {
213 return new LowerAllocations(TD);
215 Pass *createRaiseAllocationsPass() {
216 return new RaiseAllocations();