blob: abed41edc2adcba8a7f106cf57634a00dbffed03 [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
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 }
Chris Lattner3cab9f02002-09-25 23:47:47 +000052
53 /// 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 Lattner77f791d2002-05-07 19:02:48 +000063// createLowerAllocationsPass - Interface to this file...
Chris Lattneref1e9892005-03-03 01:03:43 +000064FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
65 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000066}
Chris Lattner37104aa2002-04-29 14:57:45 +000067
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000068
Chris Lattner0686e432002-01-21 07:31:50 +000069// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000070// module contains a declaration for a malloc and a free function.
71//
72// This function is always successful.
73//
Chris Lattner113f4f42002-06-25 16:13:24 +000074bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000075 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000076 MallocFunc = M.getNamedFunction("malloc");
77 FreeFunc = M.getNamedFunction("free");
78
Chris Lattner8f430a32004-12-13 20:00:02 +000079 if (MallocFunc == 0) {
80 // Prototype malloc as "void* malloc(...)", because we don't know in
81 // doInitialization whether size_t is int or long.
82 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
83 MallocFunc = M.getOrInsertFunction("malloc", FT);
84 }
Chris Lattner772eafa2004-02-28 18:51:45 +000085 if (FreeFunc == 0)
Chris Lattner8f430a32004-12-13 20:00:02 +000086 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000087
Chris Lattner77f791d2002-05-07 19:02:48 +000088 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000089}
90
Chris Lattnerd07471d2002-01-21 23:34:02 +000091// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000092// instructions over, assuming that the pass has already been initialized.
93//
Chris Lattner113f4f42002-06-25 16:13:24 +000094bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000095 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000096 assert(MallocFunc && FreeFunc && "Pass not initialized!");
97
98 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000099
Chris Lattneref1e9892005-03-03 01:03:43 +0000100 const TargetData &TD = getAnalysis<TargetData>();
101 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000102
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000103 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000104 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000105 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +0000107
Chris Lattnerba7aef392004-07-15 01:08:08 +0000108 // malloc(type) becomes sbyte *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000109 Value *MallocArg;
110 if (LowerMallocArgToInteger)
111 MallocArg = ConstantUInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
112 else
113 MallocArg = ConstantExpr::getSizeOf(AllocTy);
114 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
115
Chris Lattnerba7aef392004-07-15 01:08:08 +0000116 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000117 if (isa<ConstantInt>(MallocArg) &&
118 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000119 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
120 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000121 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattnerba7aef392004-07-15 01:08:08 +0000122 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
123 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000124 Value *Scale = MI->getOperand(0);
125 if (Scale->getType() != IntPtrTy)
126 Scale = new CastInst(Scale, IntPtrTy, "", I);
127
Chris Lattnerba7aef392004-07-15 01:08:08 +0000128 // Multiply it by the array size if necessary...
Chris Lattner8f430a32004-12-13 20:00:02 +0000129 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000130 MallocArg, "", I);
131 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000132 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000133
134 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
135 std::vector<Value*> MallocArgs;
Chris Lattnerd07471d2002-01-21 23:34:02 +0000136
Chris Lattner772eafa2004-02-28 18:51:45 +0000137 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000138 if (MallocFTy->isVarArg()) {
139 if (MallocArg->getType() != IntPtrTy)
140 MallocArg = new CastInst(MallocArg, IntPtrTy, "", I);
141 } else if (MallocFTy->getNumParams() > 0 &&
142 MallocFTy->getParamType(0) != Type::UIntTy)
Chris Lattner772eafa2004-02-28 18:51:45 +0000143 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
144 MallocArgs.push_back(MallocArg);
145 }
146
147 // If malloc is prototyped to take extra arguments, pass nulls.
148 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
149 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
150
Chris Lattnerd07471d2002-01-21 23:34:02 +0000151 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000152 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000153
154 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000155 Value *MCast;
156 if (MCall->getType() != Type::VoidTy)
157 MCast = new CastInst(MCall, MI->getType(), "", I);
158 else
159 MCast = Constant::getNullValue(MI->getType());
Chris Lattnerd07471d2002-01-21 23:34:02 +0000160
161 // Replace all uses of the old malloc inst with the cast inst
162 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000163 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000164 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000165 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000166 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000167 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
168 std::vector<Value*> FreeArgs;
169
170 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
171 Value *MCast = FI->getOperand(0);
172 if (FreeFTy->getNumParams() > 0 &&
173 FreeFTy->getParamType(0) != MCast->getType())
174 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
175 FreeArgs.push_back(MCast);
176 }
177
178 // If malloc is prototyped to take extra arguments, pass nulls.
179 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
180 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Chris Lattnerd07471d2002-01-21 23:34:02 +0000181
182 // Insert a call to the free function...
Chris Lattner772eafa2004-02-28 18:51:45 +0000183 new CallInst(FreeFunc, FreeArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000184
185 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000186 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000187 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000188 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000189 }
190 }
191
Chris Lattner6fea0322001-10-18 05:27:33 +0000192 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000193}
Brian Gaeke960707c2003-11-11 22:41:34 +0000194