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/Scalar.h"
9 #include "llvm/Module.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/iMemory.h"
12 #include "llvm/iOther.h"
13 #include "llvm/Constants.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Target/TargetData.h"
16 #include "Support/StatisticReporter.h"
18 static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
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 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);
48 // createLowerAllocationsPass - Interface to this file...
49 Pass *createLowerAllocationsPass(const TargetData &TD) {
50 return new LowerAllocations(TD);
53 static RegisterOpt<LowerAllocations>
54 X("lowerallocs", "Lower allocations from instructions to calls (TD)",
55 createLowerAllocationsPass);
58 // doInitialization - For the lower allocations pass, this ensures that a
59 // module contains a declaration for a malloc and a free function.
61 // This function is always successful.
63 bool LowerAllocations::doInitialization(Module &M) {
64 const FunctionType *MallocType =
65 FunctionType::get(PointerType::get(Type::SByteTy),
66 vector<const Type*>(1, Type::UIntTy), false);
67 const FunctionType *FreeType =
68 FunctionType::get(Type::VoidTy,
69 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
72 MallocFunc = M.getOrInsertFunction("malloc", MallocType);
73 FreeFunc = M.getOrInsertFunction("free" , FreeType);
78 // runOnBasicBlock - This method does the actual work of converting
79 // instructions over, assuming that the pass has already been initialized.
81 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
83 assert(MallocFunc && FreeFunc && "Pass not initialized!");
85 BasicBlock::InstListType &BBIL = BB.getInstList();
87 // Loop over all of the instructions, looking for malloc or free instructions
88 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
89 if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
90 BBIL.remove(I); // remove the malloc instr...
92 const Type *AllocTy = MI->getType()->getElementType();
94 // Get the number of bytes to be allocated for one element of the
96 unsigned Size = DataLayout.getTypeSize(AllocTy);
98 // malloc(type) becomes sbyte *malloc(constint)
99 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
100 if (MI->getNumOperands() && Size == 1) {
101 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
102 } else if (MI->getNumOperands()) {
103 // Multiply it by the array size if neccesary...
104 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
106 I = ++BBIL.insert(I, cast<Instruction>(MallocArg));
109 // Create the call to Malloc...
110 CallInst *MCall = new CallInst(MallocFunc,
111 vector<Value*>(1, MallocArg));
112 I = BBIL.insert(I, MCall);
114 // Create a cast instruction to convert to the right type...
115 CastInst *MCast = new CastInst(MCall, MI->getType());
116 I = BBIL.insert(++I, MCast);
118 // Replace all uses of the old malloc inst with the cast inst
119 MI->replaceAllUsesWith(MCast);
120 delete MI; // Delete the malloc inst
123 } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
126 // Cast the argument to free into a ubyte*...
127 CastInst *MCast = new CastInst(FI->getOperand(0),
128 PointerType::get(Type::UByteTy));
129 I = ++BBIL.insert(I, MCast);
131 // Insert a call to the free function...
132 CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast));
133 I = BBIL.insert(I, FCall);
135 // Delete the old free instruction