1 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
3 // The LowerAllocations transformation is a target dependent 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/Statistic.h"
19 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
21 /// LowerAllocations - Turn malloc and free instructions into %malloc and
24 class LowerAllocations : public BasicBlockPass {
25 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doInitialization
28 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
30 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired<TargetData>();
34 /// doPassInitialization - For the lower allocations pass, this ensures that
35 /// a module contains a declaration for a malloc and a free function.
37 bool doInitialization(Module &M);
39 /// runOnBasicBlock - This method does the actual work of converting
40 /// instructions over, assuming that the pass has already been initialized.
42 bool runOnBasicBlock(BasicBlock &BB);
45 RegisterOpt<LowerAllocations>
46 X("lowerallocs", "Lower allocations from instructions to calls");
49 // createLowerAllocationsPass - Interface to this file...
50 FunctionPass *createLowerAllocationsPass() {
51 return new LowerAllocations();
55 // doInitialization - For the lower allocations pass, this ensures that a
56 // module contains a declaration for a malloc and a free function.
58 // This function is always successful.
60 bool LowerAllocations::doInitialization(Module &M) {
61 const Type *SBPTy = PointerType::get(Type::SByteTy);
62 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
63 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
68 // runOnBasicBlock - This method does the actual work of converting
69 // instructions over, assuming that the pass has already been initialized.
71 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
73 assert(MallocFunc && FreeFunc && "Pass not initialized!");
75 BasicBlock::InstListType &BBIL = BB.getInstList();
76 TargetData &DataLayout = getAnalysis<TargetData>();
78 // Loop over all of the instructions, looking for malloc or free instructions
79 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
80 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
81 const Type *AllocTy = MI->getType()->getElementType();
83 // Get the number of bytes to be allocated for one element of the
85 unsigned Size = DataLayout.getTypeSize(AllocTy);
87 // malloc(type) becomes sbyte *malloc(constint)
88 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
89 if (MI->getNumOperands() && Size == 1) {
90 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
91 } else if (MI->getNumOperands()) {
92 // Multiply it by the array size if necessary...
93 MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
97 // Create the call to Malloc...
98 CallInst *MCall = new CallInst(MallocFunc,
99 std::vector<Value*>(1, MallocArg), "", I);
101 // Create a cast instruction to convert to the right type...
102 CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
104 // Replace all uses of the old malloc inst with the cast inst
105 MI->replaceAllUsesWith(MCast);
106 I = --BBIL.erase(I); // remove and delete the malloc instr...
109 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
110 // Cast the argument to free into a ubyte*...
111 CastInst *MCast = new CastInst(FI->getOperand(0),
112 PointerType::get(Type::SByteTy), "", I);
114 // Insert a call to the free function...
115 CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
118 // Delete the old free instruction