blob: dab8084c4c8a45deb4b02d745df362accb067b0f [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 Lattner49525f82004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner04805fa2002-02-26 21:46:54 +000026namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000027 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000028
Chris Lattner3cab9f02002-09-25 23:47:47 +000029 /// LowerAllocations - Turn malloc and free instructions into %malloc and
30 /// %free calls.
31 ///
32 class LowerAllocations : public BasicBlockPass {
33 Function *MallocFunc; // Functions in the module we are processing
34 Function *FreeFunc; // Initialized by doInitialization
Chris Lattneref1e9892005-03-03 01:03:43 +000035 bool LowerMallocArgToInteger;
Chris Lattner3cab9f02002-09-25 23:47:47 +000036 public:
Chris Lattneref1e9892005-03-03 01:03:43 +000037 LowerAllocations(bool LowerToInt = false)
38 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000039
Chris Lattner8f430a32004-12-13 20:00:02 +000040 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.addRequired<TargetData>();
42 AU.setPreservesCFG();
Chris Lattner4fe87d62006-05-09 04:13:41 +000043
Chris Lattnere4cb4762006-05-17 21:05:27 +000044 // This is a cluster of orthogonal Transforms:
Chris Lattner4fe87d62006-05-09 04:13:41 +000045 AU.addPreserved<UnifyFunctionExitNodes>();
46 AU.addPreservedID(PromoteMemoryToRegisterID);
47 AU.addPreservedID(LowerSelectID);
48 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000049 AU.addPreservedID(LowerInvokePassID);
Chris Lattner8f430a32004-12-13 20:00:02 +000050 }
51
Chris Lattner3cab9f02002-09-25 23:47:47 +000052 /// doPassInitialization - For the lower allocations pass, this ensures that
53 /// a module contains a declaration for a malloc and a free function.
54 ///
55 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000056
Chris Lattner8f430a32004-12-13 20:00:02 +000057 virtual bool doInitialization(Function &F) {
58 return BasicBlockPass::doInitialization(F);
59 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000060
Chris Lattner3cab9f02002-09-25 23:47:47 +000061 /// runOnBasicBlock - This method does the actual work of converting
62 /// instructions over, assuming that the pass has already been initialized.
63 ///
64 bool runOnBasicBlock(BasicBlock &BB);
65 };
Chris Lattner04805fa2002-02-26 21:46:54 +000066
Chris Lattner3cab9f02002-09-25 23:47:47 +000067 RegisterOpt<LowerAllocations>
68 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000069}
Chris Lattner04805fa2002-02-26 21:46:54 +000070
Chris Lattner2d3a0272006-05-02 04:24:36 +000071// Publically exposed interface to pass...
72const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattner77f791d2002-05-07 19:02:48 +000073// createLowerAllocationsPass - Interface to this file...
Chris Lattneref1e9892005-03-03 01:03:43 +000074FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
75 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000076}
Chris Lattner37104aa2002-04-29 14:57:45 +000077
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000078
Chris Lattner0686e432002-01-21 07:31:50 +000079// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000080// module contains a declaration for a malloc and a free function.
81//
82// This function is always successful.
83//
Chris Lattner113f4f42002-06-25 16:13:24 +000084bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000085 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000086 MallocFunc = M.getNamedFunction("malloc");
87 FreeFunc = M.getNamedFunction("free");
88
Chris Lattner8f430a32004-12-13 20:00:02 +000089 if (MallocFunc == 0) {
90 // Prototype malloc as "void* malloc(...)", because we don't know in
91 // doInitialization whether size_t is int or long.
92 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
93 MallocFunc = M.getOrInsertFunction("malloc", FT);
94 }
Chris Lattner772eafa2004-02-28 18:51:45 +000095 if (FreeFunc == 0)
Jeff Cohen11e26b52005-10-23 04:37:20 +000096 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000097
Chris Lattner77f791d2002-05-07 19:02:48 +000098 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000099}
100
Chris Lattnerd07471d2002-01-21 23:34:02 +0000101// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000102// instructions over, assuming that the pass has already been initialized.
103//
Chris Lattner113f4f42002-06-25 16:13:24 +0000104bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +0000105 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 assert(MallocFunc && FreeFunc && "Pass not initialized!");
107
108 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000109
Chris Lattneref1e9892005-03-03 01:03:43 +0000110 const TargetData &TD = getAnalysis<TargetData>();
111 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000112
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000113 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000115 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000117
Chris Lattnerba7aef392004-07-15 01:08:08 +0000118 // malloc(type) becomes sbyte *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000119 Value *MallocArg;
120 if (LowerMallocArgToInteger)
121 MallocArg = ConstantUInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
122 else
123 MallocArg = ConstantExpr::getSizeOf(AllocTy);
124 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
125
Chris Lattnerba7aef392004-07-15 01:08:08 +0000126 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000127 if (isa<ConstantInt>(MallocArg) &&
128 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000129 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
130 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000131 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000132 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
133 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000134 Value *Scale = MI->getOperand(0);
135 if (Scale->getType() != IntPtrTy)
136 Scale = new CastInst(Scale, IntPtrTy, "", I);
137
Chris Lattnerba7aef392004-07-15 01:08:08 +0000138 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000139 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000140 MallocArg, "", I);
141 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000142 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000143
144 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
145 std::vector<Value*> MallocArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000146
Chris Lattner772eafa2004-02-28 18:51:45 +0000147 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000148 if (MallocFTy->isVarArg()) {
149 if (MallocArg->getType() != IntPtrTy)
150 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
151 } else if (MallocFTy->getNumParams() > 0 &&
152 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000153 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
154 MallocArgs.push_back(MallocArg);
155 }
156
157 // If malloc is prototyped to take extra arguments, pass nulls.
158 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
159 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
160
Chris Lattnerd07471d2002-01-21 23:34:02 +0000161 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000162 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000163 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000164
Chris Lattnerd07471d2002-01-21 23:34:02 +0000165 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000166 Value *MCast;
167 if (MCall->getType() != Type::VoidTy)
168 MCast = new CastInst(MCall, MI->getType(), "", I);
169 else
170 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171
Chris Lattnerd07471d2002-01-21 23:34:02 +0000172 // Replace all uses of the old malloc inst with the cast inst
173 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000174 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000175 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000176 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000177 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000178 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
179 std::vector<Value*> FreeArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000180
Chris Lattner772eafa2004-02-28 18:51:45 +0000181 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
182 Value *MCast = FI->getOperand(0);
183 if (FreeFTy->getNumParams() > 0 &&
184 FreeFTy->getParamType(0) != MCast->getType())
185 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
186 FreeArgs.push_back(MCast);
187 }
188
189 // If malloc is prototyped to take extra arguments, pass nulls.
190 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
191 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000192
Chris Lattnerd07471d2002-01-21 23:34:02 +0000193 // Insert a call to the free function...
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000194 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195
Chris Lattnerd07471d2002-01-21 23:34:02 +0000196 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000197 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000198 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000199 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000200 }
201 }
202
Chris Lattner6fea0322001-10-18 05:27:33 +0000203 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000204}
Brian Gaeke960707c2003-11-11 22:41:34 +0000205