blob: d011d7547574507181108e2052234b29d6e40d12 [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00009//
Misha Brukmanbf437872004-01-28 20:43:01 +000010// The LowerAllocations transformation is a target-dependent tranformation
Chris Lattner77f791d2002-05-07 19:02:48 +000011// because it depends on the size of data types and alignment constraints.
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner42706e42002-07-23 22:04:17 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattner4fe87d62006-05-09 04:13:41 +000016#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +000017#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000018#include "llvm/DerivedTypes.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000020#include "llvm/Constants.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner8f430a32004-12-13 20:00:02 +000023#include "llvm/Target/TargetData.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Chris Lattner49525f82004-01-09 06:02:20 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner04805fa2002-02-26 21:46:54 +000027namespace {
Chris Lattner700b8732006-12-06 17:46:33 +000028 Statistic NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000029
Chris Lattner3cab9f02002-09-25 23:47:47 +000030 /// LowerAllocations - Turn malloc and free instructions into %malloc and
31 /// %free calls.
32 ///
Chris Lattner996795b2006-06-28 23:17:24 +000033 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Chris Lattner3cab9f02002-09-25 23:47:47 +000034 Function *MallocFunc; // Functions in the module we are processing
35 Function *FreeFunc; // Initialized by doInitialization
Chris Lattneref1e9892005-03-03 01:03:43 +000036 bool LowerMallocArgToInteger;
Chris Lattner3cab9f02002-09-25 23:47:47 +000037 public:
Chris Lattneref1e9892005-03-03 01:03:43 +000038 LowerAllocations(bool LowerToInt = false)
39 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000040
Chris Lattner8f430a32004-12-13 20:00:02 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<TargetData>();
43 AU.setPreservesCFG();
Chris Lattner4fe87d62006-05-09 04:13:41 +000044
Chris Lattnere4cb4762006-05-17 21:05:27 +000045 // This is a cluster of orthogonal Transforms:
Chris Lattner4fe87d62006-05-09 04:13:41 +000046 AU.addPreserved<UnifyFunctionExitNodes>();
47 AU.addPreservedID(PromoteMemoryToRegisterID);
48 AU.addPreservedID(LowerSelectID);
49 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000050 AU.addPreservedID(LowerInvokePassID);
Chris Lattner8f430a32004-12-13 20:00:02 +000051 }
52
Chris Lattner3cab9f02002-09-25 23:47:47 +000053 /// doPassInitialization - For the lower allocations pass, this ensures that
54 /// a module contains a declaration for a malloc and a free function.
55 ///
56 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000057
Chris Lattner8f430a32004-12-13 20:00:02 +000058 virtual bool doInitialization(Function &F) {
59 return BasicBlockPass::doInitialization(F);
60 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000061
Chris Lattner3cab9f02002-09-25 23:47:47 +000062 /// runOnBasicBlock - This method does the actual work of converting
63 /// instructions over, assuming that the pass has already been initialized.
64 ///
65 bool runOnBasicBlock(BasicBlock &BB);
66 };
Chris Lattner04805fa2002-02-26 21:46:54 +000067
Chris Lattnerc2d3d312006-08-27 22:42:52 +000068 RegisterPass<LowerAllocations>
Chris Lattner3cab9f02002-09-25 23:47:47 +000069 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000070}
Chris Lattner04805fa2002-02-26 21:46:54 +000071
Chris Lattner2d3a0272006-05-02 04:24:36 +000072// Publically exposed interface to pass...
73const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattner77f791d2002-05-07 19:02:48 +000074// createLowerAllocationsPass - Interface to this file...
Chris Lattneref1e9892005-03-03 01:03:43 +000075FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
76 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000077}
Chris Lattner37104aa2002-04-29 14:57:45 +000078
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000079
Chris Lattner0686e432002-01-21 07:31:50 +000080// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000081// module contains a declaration for a malloc and a free function.
82//
83// This function is always successful.
84//
Chris Lattner113f4f42002-06-25 16:13:24 +000085bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000086 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000087 MallocFunc = M.getNamedFunction("malloc");
88 FreeFunc = M.getNamedFunction("free");
89
Chris Lattner8f430a32004-12-13 20:00:02 +000090 if (MallocFunc == 0) {
91 // Prototype malloc as "void* malloc(...)", because we don't know in
92 // doInitialization whether size_t is int or long.
93 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
94 MallocFunc = M.getOrInsertFunction("malloc", FT);
95 }
Chris Lattner772eafa2004-02-28 18:51:45 +000096 if (FreeFunc == 0)
Jeff Cohen11e26b52005-10-23 04:37:20 +000097 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000098
Chris Lattner77f791d2002-05-07 19:02:48 +000099 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000100}
101
Chris Lattnerd07471d2002-01-21 23:34:02 +0000102// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000103// instructions over, assuming that the pass has already been initialized.
104//
Chris Lattner113f4f42002-06-25 16:13:24 +0000105bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +0000106 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000107 assert(MallocFunc && FreeFunc && "Pass not initialized!");
108
109 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000110
Chris Lattneref1e9892005-03-03 01:03:43 +0000111 const TargetData &TD = getAnalysis<TargetData>();
112 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000113
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000114 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000115 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000116 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000117 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118
Chris Lattnerba7aef392004-07-15 01:08:08 +0000119 // malloc(type) becomes sbyte *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000120 Value *MallocArg;
121 if (LowerMallocArgToInteger)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000122 MallocArg = ConstantInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
Chris Lattneref1e9892005-03-03 01:03:43 +0000123 else
124 MallocArg = ConstantExpr::getSizeOf(AllocTy);
Reid Spencer41cb2692006-12-12 09:17:08 +0000125 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
126 IntPtrTy);
Chris Lattneref1e9892005-03-03 01:03:43 +0000127
Chris Lattnerba7aef392004-07-15 01:08:08 +0000128 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000129 if (isa<ConstantInt>(MallocArg) &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000130 cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000131 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
132 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Reid Spencer41cb2692006-12-12 09:17:08 +0000133 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000134 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
135 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000136 Value *Scale = MI->getOperand(0);
137 if (Scale->getType() != IntPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000138 Scale = CastInst::createInferredCast(Scale, IntPtrTy, "", I);
Chris Lattner8f430a32004-12-13 20:00:02 +0000139
Chris Lattnerba7aef392004-07-15 01:08:08 +0000140 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000141 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000142 MallocArg, "", I);
143 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000144 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000145
146 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
147 std::vector<Value*> MallocArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000148
Chris Lattner772eafa2004-02-28 18:51:45 +0000149 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000150 if (MallocFTy->isVarArg()) {
151 if (MallocArg->getType() != IntPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000152 MallocArg = CastInst::createInferredCast(MallocArg, IntPtrTy, "",
153 I);
Chris Lattner8f430a32004-12-13 20:00:02 +0000154 } else if (MallocFTy->getNumParams() > 0 &&
155 MallocFTy->getParamType(0) != Type::UIntTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000156 MallocArg =
157 CastInst::createInferredCast(MallocArg, MallocFTy->getParamType(0),
158 "",I);
Chris Lattner772eafa2004-02-28 18:51:45 +0000159 MallocArgs.push_back(MallocArg);
160 }
161
162 // If malloc is prototyped to take extra arguments, pass nulls.
163 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
164 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
165
Chris Lattnerd07471d2002-01-21 23:34:02 +0000166 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000167 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000168 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000169
Chris Lattnerd07471d2002-01-21 23:34:02 +0000170 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000171 Value *MCast;
172 if (MCall->getType() != Type::VoidTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000173 MCast = CastInst::createInferredCast(MCall, MI->getType(), "", I);
Chris Lattner772eafa2004-02-28 18:51:45 +0000174 else
175 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000176
Chris Lattnerd07471d2002-01-21 23:34:02 +0000177 // Replace all uses of the old malloc inst with the cast inst
178 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000179 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000180 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000181 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000182 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000183 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
184 std::vector<Value*> FreeArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000185
Chris Lattner772eafa2004-02-28 18:51:45 +0000186 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
187 Value *MCast = FI->getOperand(0);
188 if (FreeFTy->getNumParams() > 0 &&
189 FreeFTy->getParamType(0) != MCast->getType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000190 MCast = CastInst::createInferredCast(MCast, FreeFTy->getParamType(0),
191 "", I);
Chris Lattner772eafa2004-02-28 18:51:45 +0000192 FreeArgs.push_back(MCast);
193 }
194
195 // If malloc is prototyped to take extra arguments, pass nulls.
196 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
197 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000198
Chris Lattnerd07471d2002-01-21 23:34:02 +0000199 // Insert a call to the free function...
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000200 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000201
Chris Lattnerd07471d2002-01-21 23:34:02 +0000202 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000203 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000204 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000205 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000206 }
207 }
208
Chris Lattner6fea0322001-10-18 05:27:33 +0000209 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000210}
Brian Gaeke960707c2003-11-11 22:41:34 +0000211