blob: f6e70e11f5f460ef6d9043f2c5bb40cf86d429cd [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
44 // This is a cluster of orthogonal Transforms:
45 AU.addPreserved<UnifyFunctionExitNodes>();
46 AU.addPreservedID(PromoteMemoryToRegisterID);
47 AU.addPreservedID(LowerSelectID);
48 AU.addPreservedID(LowerSwitchID);
Chris Lattner8f430a32004-12-13 20:00:02 +000049 }
50
Chris Lattner3cab9f02002-09-25 23:47:47 +000051 /// doPassInitialization - For the lower allocations pass, this ensures that
52 /// a module contains a declaration for a malloc and a free function.
53 ///
54 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000055
Chris Lattner8f430a32004-12-13 20:00:02 +000056 virtual bool doInitialization(Function &F) {
57 return BasicBlockPass::doInitialization(F);
58 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000059
Chris Lattner3cab9f02002-09-25 23:47:47 +000060 /// runOnBasicBlock - This method does the actual work of converting
61 /// instructions over, assuming that the pass has already been initialized.
62 ///
63 bool runOnBasicBlock(BasicBlock &BB);
64 };
Chris Lattner04805fa2002-02-26 21:46:54 +000065
Chris Lattner3cab9f02002-09-25 23:47:47 +000066 RegisterOpt<LowerAllocations>
67 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000068}
Chris Lattner04805fa2002-02-26 21:46:54 +000069
Chris Lattner2d3a0272006-05-02 04:24:36 +000070// Publically exposed interface to pass...
71const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattner77f791d2002-05-07 19:02:48 +000072// createLowerAllocationsPass - Interface to this file...
Chris Lattneref1e9892005-03-03 01:03:43 +000073FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
74 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000075}
Chris Lattner37104aa2002-04-29 14:57:45 +000076
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000077
Chris Lattner0686e432002-01-21 07:31:50 +000078// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000079// module contains a declaration for a malloc and a free function.
80//
81// This function is always successful.
82//
Chris Lattner113f4f42002-06-25 16:13:24 +000083bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000084 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000085 MallocFunc = M.getNamedFunction("malloc");
86 FreeFunc = M.getNamedFunction("free");
87
Chris Lattner8f430a32004-12-13 20:00:02 +000088 if (MallocFunc == 0) {
89 // Prototype malloc as "void* malloc(...)", because we don't know in
90 // doInitialization whether size_t is int or long.
91 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
92 MallocFunc = M.getOrInsertFunction("malloc", FT);
93 }
Chris Lattner772eafa2004-02-28 18:51:45 +000094 if (FreeFunc == 0)
Jeff Cohen11e26b52005-10-23 04:37:20 +000095 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000096
Chris Lattner77f791d2002-05-07 19:02:48 +000097 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000098}
99
Chris Lattnerd07471d2002-01-21 23:34:02 +0000100// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000101// instructions over, assuming that the pass has already been initialized.
102//
Chris Lattner113f4f42002-06-25 16:13:24 +0000103bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +0000104 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 assert(MallocFunc && FreeFunc && "Pass not initialized!");
106
107 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000108
Chris Lattneref1e9892005-03-03 01:03:43 +0000109 const TargetData &TD = getAnalysis<TargetData>();
110 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000111
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000112 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000113 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000114 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000115 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116
Chris Lattnerba7aef392004-07-15 01:08:08 +0000117 // malloc(type) becomes sbyte *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000118 Value *MallocArg;
119 if (LowerMallocArgToInteger)
120 MallocArg = ConstantUInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
121 else
122 MallocArg = ConstantExpr::getSizeOf(AllocTy);
123 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
124
Chris Lattnerba7aef392004-07-15 01:08:08 +0000125 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000126 if (isa<ConstantInt>(MallocArg) &&
127 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000128 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
129 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000130 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000131 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
132 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000133 Value *Scale = MI->getOperand(0);
134 if (Scale->getType() != IntPtrTy)
135 Scale = new CastInst(Scale, IntPtrTy, "", I);
136
Chris Lattnerba7aef392004-07-15 01:08:08 +0000137 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000138 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000139 MallocArg, "", I);
140 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000141 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000142
143 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
144 std::vector<Value*> MallocArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000145
Chris Lattner772eafa2004-02-28 18:51:45 +0000146 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000147 if (MallocFTy->isVarArg()) {
148 if (MallocArg->getType() != IntPtrTy)
149 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
150 } else if (MallocFTy->getNumParams() > 0 &&
151 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000152 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
153 MallocArgs.push_back(MallocArg);
154 }
155
156 // If malloc is prototyped to take extra arguments, pass nulls.
157 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
158 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
159
Chris Lattnerd07471d2002-01-21 23:34:02 +0000160 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000161 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000162 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000163
Chris Lattnerd07471d2002-01-21 23:34:02 +0000164 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000165 Value *MCast;
166 if (MCall->getType() != Type::VoidTy)
167 MCast = new CastInst(MCall, MI->getType(), "", I);
168 else
169 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000170
Chris Lattnerd07471d2002-01-21 23:34:02 +0000171 // Replace all uses of the old malloc inst with the cast inst
172 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000173 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000174 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000175 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000176 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000177 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
178 std::vector<Value*> FreeArgs;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179
Chris Lattner772eafa2004-02-28 18:51:45 +0000180 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
181 Value *MCast = FI->getOperand(0);
182 if (FreeFTy->getNumParams() > 0 &&
183 FreeFTy->getParamType(0) != MCast->getType())
184 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
185 FreeArgs.push_back(MCast);
186 }
187
188 // If malloc is prototyped to take extra arguments, pass nulls.
189 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
190 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanb1c93172005-04-21 23:48:37 +0000191
Chris Lattnerd07471d2002-01-21 23:34:02 +0000192 // Insert a call to the free function...
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000193 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000194
Chris Lattnerd07471d2002-01-21 23:34:02 +0000195 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000196 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000197 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000198 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000199 }
200 }
201
Chris Lattner6fea0322001-10-18 05:27:33 +0000202 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000203}
Brian Gaeke960707c2003-11-11 22:41:34 +0000204