1 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
3 // The LowerAllocations transformation is a target dependant tranformation
4 // because it depends on the size of data types and alignment constraints.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/ChangeAllocations.h"
9 #include "llvm/Module.h"
10 #include "llvm/Function.h"
11 #include "llvm/BasicBlock.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Target/TargetData.h"
22 // LowerAllocations - Turn malloc and free instructions into %malloc and %free
25 class LowerAllocations : public BasicBlockPass {
26 Function *MallocFunc; // Functions in the module we are processing
27 Function *FreeFunc; // Initialized by doInitialization
29 const TargetData &DataLayout;
31 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
32 MallocFunc = FreeFunc = 0;
35 const char *getPassName() const { return "Lower Allocations"; }
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);
50 // createLowerAllocationsPass - Interface to this file...
51 Pass *createLowerAllocationsPass(const TargetData &TD) {
52 return new LowerAllocations(TD);
56 // doInitialization - For the lower allocations pass, this ensures that a
57 // module contains a declaration for a malloc and a free function.
59 // This function is always successful.
61 bool LowerAllocations::doInitialization(Module *M) {
62 const FunctionType *MallocType =
63 FunctionType::get(PointerType::get(Type::SByteTy),
64 vector<const Type*>(1, Type::UIntTy), false);
65 const FunctionType *FreeType =
66 FunctionType::get(Type::VoidTy,
67 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
70 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
71 FreeFunc = M->getOrInsertFunction("free" , FreeType);
76 // runOnBasicBlock - This method does the actual work of converting
77 // instructions over, assuming that the pass has already been initialized.
79 bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
81 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
83 // Loop over all of the instructions, looking for malloc or free instructions
84 for (unsigned i = 0; i < BB->size(); ++i) {
85 BasicBlock::InstListType &BBIL = BB->getInstList();
86 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
87 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
89 const Type *AllocTy = cast<PointerType>(MI->getType())->getElementType();
91 // Get the number of bytes to be allocated for one element of the
93 unsigned Size = DataLayout.getTypeSize(AllocTy);
95 // malloc(type) becomes sbyte *malloc(constint)
96 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
97 if (MI->getNumOperands() && Size == 1) {
98 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
99 } else if (MI->getNumOperands()) {
100 // Multiply it by the array size if neccesary...
101 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
103 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
106 // Create the call to Malloc...
107 CallInst *MCall = new CallInst(MallocFunc,
108 vector<Value*>(1, MallocArg));
109 BBIL.insert(BBIL.begin()+i, MCall);
111 // Create a cast instruction to convert to the right type...
112 CastInst *MCast = new CastInst(MCall, MI->getType());
113 BBIL.insert(BBIL.begin()+i+1, MCast);
115 // Replace all uses of the old malloc inst with the cast inst
116 MI->replaceAllUsesWith(MCast);
117 delete MI; // Delete the malloc inst
119 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
120 BBIL.remove(BB->getInstList().begin()+i);
122 // Cast the argument to free into a ubyte*...
123 CastInst *MCast = new CastInst(FI->getOperand(0),
124 PointerType::get(Type::UByteTy));
125 BBIL.insert(BBIL.begin()+i, MCast);
127 // Insert a call to the free function...
128 CallInst *FCall = new CallInst(FreeFunc,
129 vector<Value*>(1, MCast));
130 BBIL.insert(BBIL.begin()+i+1, FCall);
132 // Delete the old free instruction