blob: 27e0f70315de84b9f7710513221100f69460133c [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"
18#include "llvm/iMemory.h"
19#include "llvm/iOther.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"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000022#include "Support/Statistic.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 Lattner3cab9f02002-09-25 23:47:47 +000037 /// doPassInitialization - For the lower allocations pass, this ensures that
38 /// a module contains a declaration for a malloc and a free function.
39 ///
40 bool doInitialization(Module &M);
41
42 /// runOnBasicBlock - This method does the actual work of converting
43 /// instructions over, assuming that the pass has already been initialized.
44 ///
45 bool runOnBasicBlock(BasicBlock &BB);
46 };
Chris Lattner04805fa2002-02-26 21:46:54 +000047
Chris Lattner3cab9f02002-09-25 23:47:47 +000048 RegisterOpt<LowerAllocations>
49 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000050}
Chris Lattner04805fa2002-02-26 21:46:54 +000051
Chris Lattner77f791d2002-05-07 19:02:48 +000052// createLowerAllocationsPass - Interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000053FunctionPass *llvm::createLowerAllocationsPass() {
Chris Lattner3cab9f02002-09-25 23:47:47 +000054 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000055}
Chris Lattner37104aa2002-04-29 14:57:45 +000056
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000057
Chris Lattner0686e432002-01-21 07:31:50 +000058// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000059// module contains a declaration for a malloc and a free function.
60//
61// This function is always successful.
62//
Chris Lattner113f4f42002-06-25 16:13:24 +000063bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000064 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000065 MallocFunc = M.getNamedFunction("malloc");
66 FreeFunc = M.getNamedFunction("free");
67
68 if (MallocFunc == 0)
69 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
70 if (FreeFunc == 0)
71 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000072
Chris Lattner77f791d2002-05-07 19:02:48 +000073 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000074}
75
Chris Lattnerba7aef392004-07-15 01:08:08 +000076static Constant *getSizeof(const Type *Ty) {
77 Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
78 std::vector<Constant*> Idx;
79 Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
80 Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
81 return ConstantExpr::getCast(Ret, Type::UIntTy);
82}
83
Chris Lattnerd07471d2002-01-21 23:34:02 +000084// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000085// instructions over, assuming that the pass has already been initialized.
86//
Chris Lattner113f4f42002-06-25 16:13:24 +000087bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000088 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000089 assert(MallocFunc && FreeFunc && "Pass not initialized!");
90
91 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000092
93 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +000094 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +000095 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +000096 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +000097
Chris Lattnerba7aef392004-07-15 01:08:08 +000098 // malloc(type) becomes sbyte *malloc(size)
99 Value *MallocArg = getSizeof(AllocTy);
100 if (MI->isArrayAllocation()) {
101 if (isa<ConstantUInt>(MallocArg) &&
102 cast<ConstantUInt>(MallocArg)->getValue() == 1) {
103 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
104 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
105 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
106 } else {
107 // Multiply it by the array size if necessary...
108 MallocArg = BinaryOperator::create(Instruction::Mul,
109 MI->getOperand(0),
110 MallocArg, "", I);
111 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000112 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000113
114 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
115 std::vector<Value*> MallocArgs;
Chris Lattnerd07471d2002-01-21 23:34:02 +0000116
Chris Lattner772eafa2004-02-28 18:51:45 +0000117 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
118 if (MallocFTy->getNumParams() > 0 &&
119 MallocFTy->getParamType(0) != Type::UIntTy)
120 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
121 MallocArgs.push_back(MallocArg);
122 }
123
124 // If malloc is prototyped to take extra arguments, pass nulls.
125 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
126 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
127
Chris Lattnerd07471d2002-01-21 23:34:02 +0000128 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000129 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000130
131 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000132 Value *MCast;
133 if (MCall->getType() != Type::VoidTy)
134 MCast = new CastInst(MCall, MI->getType(), "", I);
135 else
136 MCast = Constant::getNullValue(MI->getType());
Chris Lattnerd07471d2002-01-21 23:34:02 +0000137
138 // Replace all uses of the old malloc inst with the cast inst
139 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000140 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000141 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000142 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000143 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000144 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
145 std::vector<Value*> FreeArgs;
146
147 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
148 Value *MCast = FI->getOperand(0);
149 if (FreeFTy->getNumParams() > 0 &&
150 FreeFTy->getParamType(0) != MCast->getType())
151 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
152 FreeArgs.push_back(MCast);
153 }
154
155 // If malloc is prototyped to take extra arguments, pass nulls.
156 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
157 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Chris Lattnerd07471d2002-01-21 23:34:02 +0000158
159 // Insert a call to the free function...
Chris Lattner772eafa2004-02-28 18:51:45 +0000160 new CallInst(FreeFunc, FreeArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000161
162 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000163 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000164 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000165 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000166 }
167 }
168
Chris Lattner6fea0322001-10-18 05:27:33 +0000169 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000170}
Brian Gaeke960707c2003-11-11 22:41:34 +0000171