1 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The LowerAllocations transformation is a target-dependent tranformation
11 // because it depends on the size of data types and alignment constraints.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/iMemory.h"
19 #include "llvm/iOther.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Target/TargetData.h"
23 #include "Support/Statistic.h"
27 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
29 /// LowerAllocations - Turn malloc and free instructions into %malloc and
32 class LowerAllocations : public BasicBlockPass {
33 Function *MallocFunc; // Functions in the module we are processing
34 Function *FreeFunc; // Initialized by doInitialization
36 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired<TargetData>();
42 /// doPassInitialization - For the lower allocations pass, this ensures that
43 /// a module contains a declaration for a malloc and a free function.
45 bool doInitialization(Module &M);
47 /// runOnBasicBlock - This method does the actual work of converting
48 /// instructions over, assuming that the pass has already been initialized.
50 bool runOnBasicBlock(BasicBlock &BB);
53 RegisterOpt<LowerAllocations>
54 X("lowerallocs", "Lower allocations from instructions to calls");
57 // createLowerAllocationsPass - Interface to this file...
58 FunctionPass *llvm::createLowerAllocationsPass() {
59 return new LowerAllocations();
63 // doInitialization - For the lower allocations pass, this ensures that a
64 // module contains a declaration for a malloc and a free function.
66 // This function is always successful.
68 bool LowerAllocations::doInitialization(Module &M) {
69 const Type *SBPTy = PointerType::get(Type::SByteTy);
70 MallocFunc = M.getNamedFunction("malloc");
71 FreeFunc = M.getNamedFunction("free");
74 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
76 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
81 // runOnBasicBlock - This method does the actual work of converting
82 // instructions over, assuming that the pass has already been initialized.
84 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
86 assert(MallocFunc && FreeFunc && "Pass not initialized!");
88 BasicBlock::InstListType &BBIL = BB.getInstList();
89 TargetData &DataLayout = getAnalysis<TargetData>();
91 // Loop over all of the instructions, looking for malloc or free instructions
92 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
93 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
94 const Type *AllocTy = MI->getType()->getElementType();
96 // Get the number of bytes to be allocated for one element of the
98 unsigned Size = DataLayout.getTypeSize(AllocTy);
100 // malloc(type) becomes sbyte *malloc(constint)
101 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
102 if (MI->getNumOperands() && Size == 1) {
103 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
104 } else if (MI->isArrayAllocation()) {
105 // Multiply it by the array size if necessary...
106 MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
110 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
111 std::vector<Value*> MallocArgs;
113 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
114 if (MallocFTy->getNumParams() > 0 &&
115 MallocFTy->getParamType(0) != Type::UIntTy)
116 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
117 MallocArgs.push_back(MallocArg);
120 // If malloc is prototyped to take extra arguments, pass nulls.
121 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
122 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
124 // Create the call to Malloc...
125 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
127 // Create a cast instruction to convert to the right type...
129 if (MCall->getType() != Type::VoidTy)
130 MCast = new CastInst(MCall, MI->getType(), "", I);
132 MCast = Constant::getNullValue(MI->getType());
134 // Replace all uses of the old malloc inst with the cast inst
135 MI->replaceAllUsesWith(MCast);
136 I = --BBIL.erase(I); // remove and delete the malloc instr...
139 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
140 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
141 std::vector<Value*> FreeArgs;
143 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
144 Value *MCast = FI->getOperand(0);
145 if (FreeFTy->getNumParams() > 0 &&
146 FreeFTy->getParamType(0) != MCast->getType())
147 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
148 FreeArgs.push_back(MCast);
151 // If malloc is prototyped to take extra arguments, pass nulls.
152 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
153 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
155 // Insert a call to the free function...
156 new CallInst(FreeFunc, FreeArgs, "", I);
158 // Delete the old free instruction