blob: 0a6dc09147e8c6d2fcf8d20bd2c86d737d9f8978 [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 Lattnerd5d56782002-01-31 00:45:11 +000016#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000017#include "llvm/DerivedTypes.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000019#include "llvm/Constants.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000020#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
Chris Lattner8f430a32004-12-13 20:00:02 +000022#include "llvm/Target/TargetData.h"
Chris Lattner49525f82004-01-09 06:02:20 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner04805fa2002-02-26 21:46:54 +000025namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000026 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000027
Chris Lattner3cab9f02002-09-25 23:47:47 +000028 /// LowerAllocations - Turn malloc and free instructions into %malloc and
29 /// %free calls.
30 ///
31 class LowerAllocations : public BasicBlockPass {
32 Function *MallocFunc; // Functions in the module we are processing
33 Function *FreeFunc; // Initialized by doInitialization
Chris Lattneref1e9892005-03-03 01:03:43 +000034 bool LowerMallocArgToInteger;
Chris Lattner3cab9f02002-09-25 23:47:47 +000035 public:
Chris Lattneref1e9892005-03-03 01:03:43 +000036 LowerAllocations(bool LowerToInt = false)
37 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000038
Chris Lattner8f430a32004-12-13 20:00:02 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 AU.setPreservesCFG();
42 }
43
Chris Lattner3cab9f02002-09-25 23:47:47 +000044 /// doPassInitialization - For the lower allocations pass, this ensures that
45 /// a module contains a declaration for a malloc and a free function.
46 ///
47 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000048
Chris Lattner8f430a32004-12-13 20:00:02 +000049 virtual bool doInitialization(Function &F) {
50 return BasicBlockPass::doInitialization(F);
51 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000052
Chris Lattner3cab9f02002-09-25 23:47:47 +000053 /// runOnBasicBlock - This method does the actual work of converting
54 /// instructions over, assuming that the pass has already been initialized.
55 ///
56 bool runOnBasicBlock(BasicBlock &BB);
57 };
Chris Lattner04805fa2002-02-26 21:46:54 +000058
Chris Lattner3cab9f02002-09-25 23:47:47 +000059 RegisterOpt<LowerAllocations>
60 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000061}
Chris Lattner04805fa2002-02-26 21:46:54 +000062
Chris Lattner2d3a0272006-05-02 04:24:36 +000063// Publically exposed interface to pass...
64const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattner77f791d2002-05-07 19:02:48 +000065// createLowerAllocationsPass - Interface to this file...
Chris Lattneref1e9892005-03-03 01:03:43 +000066FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
67 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000068}
Chris Lattner37104aa2002-04-29 14:57:45 +000069
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000070
Chris Lattner0686e432002-01-21 07:31:50 +000071// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000072// module contains a declaration for a malloc and a free function.
73//
74// This function is always successful.
75//
Chris Lattner113f4f42002-06-25 16:13:24 +000076bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000077 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000078 MallocFunc = M.getNamedFunction("malloc");
79 FreeFunc = M.getNamedFunction("free");
80
Chris Lattner8f430a32004-12-13 20:00:02 +000081 if (MallocFunc == 0) {
82 // Prototype malloc as "void* malloc(...)", because we don't know in
83 // doInitialization whether size_t is int or long.
84 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
85 MallocFunc = M.getOrInsertFunction("malloc", FT);
86 }
Chris Lattner772eafa2004-02-28 18:51:45 +000087 if (FreeFunc == 0)
Jeff Cohen11e26b52005-10-23 04:37:20 +000088 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000089
Chris Lattner77f791d2002-05-07 19:02:48 +000090 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000091}
92
Chris Lattnerd07471d2002-01-21 23:34:02 +000093// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000094// instructions over, assuming that the pass has already been initialized.
95//
Chris Lattner113f4f42002-06-25 16:13:24 +000096bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000097 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000098 assert(MallocFunc && FreeFunc && "Pass not initialized!");
99
100 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000101
Chris Lattneref1e9892005-03-03 01:03:43 +0000102 const TargetData &TD = getAnalysis<TargetData>();
103 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000104
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000105 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000107 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000109
Chris Lattnerba7aef392004-07-15 01:08:08 +0000110 // malloc(type) becomes sbyte *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000111 Value *MallocArg;
112 if (LowerMallocArgToInteger)
113 MallocArg = ConstantUInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
114 else
115 MallocArg = ConstantExpr::getSizeOf(AllocTy);
116 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
117
Chris Lattnerba7aef392004-07-15 01:08:08 +0000118 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000119 if (isa<ConstantInt>(MallocArg) &&
120 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000121 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
122 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000123 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000124 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
125 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000126 Value *Scale = MI->getOperand(0);
127 if (Scale->getType() != IntPtrTy)
128 Scale = new CastInst(Scale, IntPtrTy, "", I);
129
Chris Lattnerba7aef392004-07-15 01:08:08 +0000130 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000131 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000132 MallocArg, "", I);
133 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000134 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000135
136 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
137 std::vector<Value*> MallocArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000138
Chris Lattner772eafa2004-02-28 18:51:45 +0000139 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000140 if (MallocFTy->isVarArg()) {
141 if (MallocArg->getType() != IntPtrTy)
142 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
143 } else if (MallocFTy->getNumParams() > 0 &&
144 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000145 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
146 MallocArgs.push_back(MallocArg);
147 }
148
149 // If malloc is prototyped to take extra arguments, pass nulls.
150 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
151 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
152
Chris Lattnerd07471d2002-01-21 23:34:02 +0000153 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000154 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000155 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156
Chris Lattnerd07471d2002-01-21 23:34:02 +0000157 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000158 Value *MCast;
159 if (MCall->getType() != Type::VoidTy)
160 MCast = new CastInst(MCall, MI->getType(), "", I);
161 else
162 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000163
Chris Lattnerd07471d2002-01-21 23:34:02 +0000164 // Replace all uses of the old malloc inst with the cast inst
165 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000166 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000167 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000168 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000169 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000170 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
171 std::vector<Value*> FreeArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000172
Chris Lattner772eafa2004-02-28 18:51:45 +0000173 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
174 Value *MCast = FI->getOperand(0);
175 if (FreeFTy->getNumParams() > 0 &&
176 FreeFTy->getParamType(0) != MCast->getType())
177 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
178 FreeArgs.push_back(MCast);
179 }
180
181 // If malloc is prototyped to take extra arguments, pass nulls.
182 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
183 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000184
Chris Lattnerd07471d2002-01-21 23:34:02 +0000185 // Insert a call to the free function...
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000186 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187
Chris Lattnerd07471d2002-01-21 23:34:02 +0000188 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000189 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000190 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000191 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000192 }
193 }
194
Chris Lattner6fea0322001-10-18 05:27:33 +0000195 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000196}
Brian Gaeke960707c2003-11-11 22:41:34 +0000197