blob: d826a91d8d751cd651c32bb253ccd73f8f8b8fb8 [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
34 public:
35 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000036
Chris Lattner8f430a32004-12-13 20:00:02 +000037 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.addRequired<TargetData>();
39 AU.setPreservesCFG();
40 }
41
Chris Lattner3cab9f02002-09-25 23:47:47 +000042 /// doPassInitialization - For the lower allocations pass, this ensures that
43 /// a module contains a declaration for a malloc and a free function.
44 ///
45 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000046
Chris Lattner8f430a32004-12-13 20:00:02 +000047 virtual bool doInitialization(Function &F) {
48 return BasicBlockPass::doInitialization(F);
49 }
Chris Lattner3cab9f02002-09-25 23:47:47 +000050
51 /// runOnBasicBlock - This method does the actual work of converting
52 /// instructions over, assuming that the pass has already been initialized.
53 ///
54 bool runOnBasicBlock(BasicBlock &BB);
55 };
Chris Lattner04805fa2002-02-26 21:46:54 +000056
Chris Lattner3cab9f02002-09-25 23:47:47 +000057 RegisterOpt<LowerAllocations>
58 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000059}
Chris Lattner04805fa2002-02-26 21:46:54 +000060
Chris Lattner77f791d2002-05-07 19:02:48 +000061// createLowerAllocationsPass - Interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000062FunctionPass *llvm::createLowerAllocationsPass() {
Chris Lattner3cab9f02002-09-25 23:47:47 +000063 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000064}
Chris Lattner37104aa2002-04-29 14:57:45 +000065
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000066
Chris Lattner0686e432002-01-21 07:31:50 +000067// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000068// module contains a declaration for a malloc and a free function.
69//
70// This function is always successful.
71//
Chris Lattner113f4f42002-06-25 16:13:24 +000072bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000073 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000074 MallocFunc = M.getNamedFunction("malloc");
75 FreeFunc = M.getNamedFunction("free");
76
Chris Lattner8f430a32004-12-13 20:00:02 +000077 if (MallocFunc == 0) {
78 // Prototype malloc as "void* malloc(...)", because we don't know in
79 // doInitialization whether size_t is int or long.
80 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
81 MallocFunc = M.getOrInsertFunction("malloc", FT);
82 }
Chris Lattner772eafa2004-02-28 18:51:45 +000083 if (FreeFunc == 0)
Chris Lattner8f430a32004-12-13 20:00:02 +000084 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000085
Chris Lattner77f791d2002-05-07 19:02:48 +000086 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000087}
88
Chris Lattnerd07471d2002-01-21 23:34:02 +000089// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000090// instructions over, assuming that the pass has already been initialized.
91//
Chris Lattner113f4f42002-06-25 16:13:24 +000092bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000093 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000094 assert(MallocFunc && FreeFunc && "Pass not initialized!");
95
96 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000097
Chris Lattner8f430a32004-12-13 20:00:02 +000098 const Type *IntPtrTy = getAnalysis<TargetData>().getIntPtrType();
99
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000100 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000101 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000102 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000103 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +0000104
Chris Lattnerba7aef392004-07-15 01:08:08 +0000105 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner8f430a32004-12-13 20:00:02 +0000106 Value *MallocArg = ConstantExpr::getCast(ConstantExpr::getSizeOf(AllocTy),
107 IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000108 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000109 if (isa<ConstantInt>(MallocArg) &&
110 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000111 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
112 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000113 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000114 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
115 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000116 Value *Scale = MI->getOperand(0);
117 if (Scale->getType() != IntPtrTy)
118 Scale = new CastInst(Scale, IntPtrTy, "", I);
119
Chris Lattnerba7aef392004-07-15 01:08:08 +0000120 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000121 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000122 MallocArg, "", I);
123 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000124 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000125
126 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
127 std::vector<Value*> MallocArgs;
Chris Lattnerd07471d2002-01-21 23:34:02 +0000128
Chris Lattner772eafa2004-02-28 18:51:45 +0000129 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000130 if (MallocFTy->isVarArg()) {
131 if (MallocArg->getType() != IntPtrTy)
132 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
133 } else if (MallocFTy->getNumParams() > 0 &&
134 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000135 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
136 MallocArgs.push_back(MallocArg);
137 }
138
139 // If malloc is prototyped to take extra arguments, pass nulls.
140 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
141 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
142
Chris Lattnerd07471d2002-01-21 23:34:02 +0000143 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000144 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000145
146 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000147 Value *MCast;
148 if (MCall->getType() != Type::VoidTy)
149 MCast = new CastInst(MCall, MI->getType(), "", I);
150 else
151 MCast = Constant::getNullValue(MI->getType());
Chris Lattnerd07471d2002-01-21 23:34:02 +0000152
153 // Replace all uses of the old malloc inst with the cast inst
154 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000155 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000156 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000157 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000158 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000159 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
160 std::vector<Value*> FreeArgs;
161
162 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
163 Value *MCast = FI->getOperand(0);
164 if (FreeFTy->getNumParams() > 0 &&
165 FreeFTy->getParamType(0) != MCast->getType())
166 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
167 FreeArgs.push_back(MCast);
168 }
169
170 // If malloc is prototyped to take extra arguments, pass nulls.
171 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
172 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Chris Lattnerd07471d2002-01-21 23:34:02 +0000173
174 // Insert a call to the free function...
Chris Lattner772eafa2004-02-28 18:51:45 +0000175 new CallInst(FreeFunc, FreeArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000176
177 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000178 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000179 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000180 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000181 }
182 }
183
Chris Lattner6fea0322001-10-18 05:27:33 +0000184 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000185}
Brian Gaeke960707c2003-11-11 22:41:34 +0000186