LLVM API Documentation
00001 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // The LowerAllocations transformation is a target-dependent tranformation 00011 // because it depends on the size of data types and alignment constraints. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "lowerallocs" 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/DerivedTypes.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/Constants.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/ADT/Statistic.h" 00024 #include "llvm/Target/TargetData.h" 00025 #include "llvm/Support/Compiler.h" 00026 using namespace llvm; 00027 00028 STATISTIC(NumLowered, "Number of allocations lowered"); 00029 00030 namespace { 00031 /// LowerAllocations - Turn malloc and free instructions into %malloc and 00032 /// %free calls. 00033 /// 00034 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass { 00035 Constant *MallocFunc; // Functions in the module we are processing 00036 Constant *FreeFunc; // Initialized by doInitialization 00037 bool LowerMallocArgToInteger; 00038 public: 00039 static char ID; // Pass ID, replacement for typeid 00040 explicit LowerAllocations(bool LowerToInt = false) 00041 : BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0), 00042 LowerMallocArgToInteger(LowerToInt) {} 00043 00044 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00045 AU.addRequired<TargetData>(); 00046 AU.setPreservesCFG(); 00047 00048 // This is a cluster of orthogonal Transforms: 00049 AU.addPreserved<UnifyFunctionExitNodes>(); 00050 AU.addPreservedID(PromoteMemoryToRegisterID); 00051 AU.addPreservedID(LowerSwitchID); 00052 AU.addPreservedID(LowerInvokePassID); 00053 } 00054 00055 /// doPassInitialization - For the lower allocations pass, this ensures that 00056 /// a module contains a declaration for a malloc and a free function. 00057 /// 00058 bool doInitialization(Module &M); 00059 00060 virtual bool doInitialization(Function &F) { 00061 return BasicBlockPass::doInitialization(F); 00062 } 00063 00064 /// runOnBasicBlock - This method does the actual work of converting 00065 /// instructions over, assuming that the pass has already been initialized. 00066 /// 00067 bool runOnBasicBlock(BasicBlock &BB); 00068 }; 00069 } 00070 00071 char LowerAllocations::ID = 0; 00072 static RegisterPass<LowerAllocations> 00073 X("lowerallocs", "Lower allocations from instructions to calls"); 00074 00075 // Publically exposed interface to pass... 00076 const PassInfo *const llvm::LowerAllocationsID = &X; 00077 // createLowerAllocationsPass - Interface to this file... 00078 Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) { 00079 return new LowerAllocations(LowerMallocArgToInteger); 00080 } 00081 00082 00083 // doInitialization - For the lower allocations pass, this ensures that a 00084 // module contains a declaration for a malloc and a free function. 00085 // 00086 // This function is always successful. 00087 // 00088 bool LowerAllocations::doInitialization(Module &M) { 00089 const Type *BPTy = PointerType::getUnqual(Type::Int8Ty); 00090 // Prototype malloc as "char* malloc(...)", because we don't know in 00091 // doInitialization whether size_t is int or long. 00092 FunctionType *FT = FunctionType::get(BPTy, std::vector<const Type*>(), true); 00093 MallocFunc = M.getOrInsertFunction("malloc", FT); 00094 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0); 00095 return true; 00096 } 00097 00098 // runOnBasicBlock - This method does the actual work of converting 00099 // instructions over, assuming that the pass has already been initialized. 00100 // 00101 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { 00102 bool Changed = false; 00103 assert(MallocFunc && FreeFunc && "Pass not initialized!"); 00104 00105 BasicBlock::InstListType &BBIL = BB.getInstList(); 00106 00107 const TargetData &TD = getAnalysis<TargetData>(); 00108 const Type *IntPtrTy = TD.getIntPtrType(); 00109 00110 // Loop over all of the instructions, looking for malloc or free instructions 00111 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { 00112 if (MallocInst *MI = dyn_cast<MallocInst>(I)) { 00113 const Type *AllocTy = MI->getType()->getElementType(); 00114 00115 // malloc(type) becomes sbyte *malloc(size) 00116 Value *MallocArg; 00117 if (LowerMallocArgToInteger) 00118 MallocArg = ConstantInt::get(Type::Int64Ty, TD.getABITypeSize(AllocTy)); 00119 else 00120 MallocArg = ConstantExpr::getSizeOf(AllocTy); 00121 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg), 00122 IntPtrTy); 00123 00124 if (MI->isArrayAllocation()) { 00125 if (isa<ConstantInt>(MallocArg) && 00126 cast<ConstantInt>(MallocArg)->isOne()) { 00127 MallocArg = MI->getOperand(0); // Operand * 1 = Operand 00128 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) { 00129 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/); 00130 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg)); 00131 } else { 00132 Value *Scale = MI->getOperand(0); 00133 if (Scale->getType() != IntPtrTy) 00134 Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/, 00135 "", I); 00136 00137 // Multiply it by the array size if necessary... 00138 MallocArg = BinaryOperator::Create(Instruction::Mul, Scale, 00139 MallocArg, "", I); 00140 } 00141 } 00142 00143 // Create the call to Malloc. 00144 CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I); 00145 MCall->setTailCall(); 00146 00147 // Create a cast instruction to convert to the right type... 00148 Value *MCast; 00149 if (MCall->getType() != Type::VoidTy) 00150 MCast = new BitCastInst(MCall, MI->getType(), "", I); 00151 else 00152 MCast = Constant::getNullValue(MI->getType()); 00153 00154 // Replace all uses of the old malloc inst with the cast inst 00155 MI->replaceAllUsesWith(MCast); 00156 I = --BBIL.erase(I); // remove and delete the malloc instr... 00157 Changed = true; 00158 ++NumLowered; 00159 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) { 00160 Value *PtrCast = 00161 new BitCastInst(FI->getOperand(0), 00162 PointerType::getUnqual(Type::Int8Ty), "", I); 00163 00164 // Insert a call to the free function... 00165 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall(); 00166 00167 // Delete the old free instruction 00168 I = --BBIL.erase(I); 00169 Changed = true; 00170 ++NumLowered; 00171 } 00172 } 00173 00174 return Changed; 00175 } 00176