blob: d08235cd9d79f4918a78e38042287c8271af6e21 [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 Lattnerbf3a0992002-10-01 22:38:41 +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);
125 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
126
Chris Lattnerba7aef392004-07-15 01:08:08 +0000127 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000128 if (isa<ConstantInt>(MallocArg) &&
Reid Spencere0fc4df2006-10-20 07:07:24 +0000129 cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000130 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
131 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000132 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000133 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
134 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000135 Value *Scale = MI->getOperand(0);
136 if (Scale->getType() != IntPtrTy)
137 Scale = new CastInst(Scale, IntPtrTy, "", I);
138
Chris Lattnerba7aef392004-07-15 01:08:08 +0000139 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000140 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000141 MallocArg, "", I);
142 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000143 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000144
145 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
146 std::vector<Value*> MallocArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147
Chris Lattner772eafa2004-02-28 18:51:45 +0000148 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000149 if (MallocFTy->isVarArg()) {
150 if (MallocArg->getType() != IntPtrTy)
151 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
152 } else if (MallocFTy->getNumParams() > 0 &&
153 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000154 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
155 MallocArgs.push_back(MallocArg);
156 }
157
158 // If malloc is prototyped to take extra arguments, pass nulls.
159 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
160 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
161
Chris Lattnerd07471d2002-01-21 23:34:02 +0000162 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000163 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000164 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000165
Chris Lattnerd07471d2002-01-21 23:34:02 +0000166 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000167 Value *MCast;
168 if (MCall->getType() != Type::VoidTy)
169 MCast = new CastInst(MCall, MI->getType(), "", I);
170 else
171 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000172
Chris Lattnerd07471d2002-01-21 23:34:02 +0000173 // Replace all uses of the old malloc inst with the cast inst
174 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000175 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000176 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000177 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000178 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000179 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
180 std::vector<Value*> FreeArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000181
Chris Lattner772eafa2004-02-28 18:51:45 +0000182 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
183 Value *MCast = FI->getOperand(0);
184 if (FreeFTy->getNumParams() > 0 &&
185 FreeFTy->getParamType(0) != MCast->getType())
186 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
187 FreeArgs.push_back(MCast);
188 }
189
190 // If malloc is prototyped to take extra arguments, pass nulls.
191 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
192 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000193
Chris Lattnerd07471d2002-01-21 23:34:02 +0000194 // Insert a call to the free function...
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000195 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000196
Chris Lattnerd07471d2002-01-21 23:34:02 +0000197 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000198 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000199 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000200 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000201 }
202 }
203
Chris Lattner6fea0322001-10-18 05:27:33 +0000204 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000205}
Brian Gaeke960707c2003-11-11 22:41:34 +0000206