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(&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 doInitialization(*F.getParent()); 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, 00119 TD.getTypePaddedSize(AllocTy)); 00120 else 00121 MallocArg = ConstantExpr::getSizeOf(AllocTy); 00122 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg), 00123 IntPtrTy); 00124 00125 if (MI->isArrayAllocation()) { 00126 if (isa<ConstantInt>(MallocArg) && 00127 cast<ConstantInt>(MallocArg)->isOne()) { 00128 MallocArg = MI->getOperand(0); // Operand * 1 = Operand 00129 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) { 00130 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/); 00131 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg)); 00132 } else { 00133 Value *Scale = MI->getOperand(0); 00134 if (Scale->getType() != IntPtrTy) 00135 Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/, 00136 "", I); 00137 00138 // Multiply it by the array size if necessary... 00139 MallocArg = BinaryOperator::Create(Instruction::Mul, Scale, 00140 MallocArg, "", I); 00141 } 00142 } 00143 00144 // Create the call to Malloc. 00145 CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I); 00146 MCall->setTailCall(); 00147 00148 // Create a cast instruction to convert to the right type... 00149 Value *MCast; 00150 if (MCall->getType() != Type::VoidTy) 00151 MCast = new BitCastInst(MCall, MI->getType(), "", I); 00152 else 00153 MCast = Constant::getNullValue(MI->getType()); 00154 00155 // Replace all uses of the old malloc inst with the cast inst 00156 MI->replaceAllUsesWith(MCast); 00157 I = --BBIL.erase(I); // remove and delete the malloc instr... 00158 Changed = true; 00159 ++NumLowered; 00160 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) { 00161 Value *PtrCast = 00162 new BitCastInst(FI->getOperand(0), 00163 PointerType::getUnqual(Type::Int8Ty), "", I); 00164 00165 // Insert a call to the free function... 00166 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall(); 00167 00168 // Delete the old free instruction 00169 I = --BBIL.erase(I); 00170 Changed = true; 00171 ++NumLowered; 00172 } 00173 } 00174 00175 return Changed; 00176 } 00177