blob: 3bbec3dfd96a510e3cfaaff01a689b46a8fcaae4 [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 Lattner49525f82004-01-09 06:02:20 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner04805fa2002-02-26 21:46:54 +000024namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000025 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000026
Chris Lattner3cab9f02002-09-25 23:47:47 +000027 /// LowerAllocations - Turn malloc and free instructions into %malloc and
28 /// %free calls.
29 ///
30 class LowerAllocations : public BasicBlockPass {
31 Function *MallocFunc; // Functions in the module we are processing
32 Function *FreeFunc; // Initialized by doInitialization
33 public:
34 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000035
Chris Lattner3cab9f02002-09-25 23:47:47 +000036 /// doPassInitialization - For the lower allocations pass, this ensures that
37 /// a module contains a declaration for a malloc and a free function.
38 ///
39 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000040
41 virtual bool doInitialization(Function&f)
42 { return BasicBlockPass::doInitialization(f); }
Chris Lattner3cab9f02002-09-25 23:47:47 +000043
44 /// runOnBasicBlock - This method does the actual work of converting
45 /// instructions over, assuming that the pass has already been initialized.
46 ///
47 bool runOnBasicBlock(BasicBlock &BB);
48 };
Chris Lattner04805fa2002-02-26 21:46:54 +000049
Chris Lattner3cab9f02002-09-25 23:47:47 +000050 RegisterOpt<LowerAllocations>
51 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000052}
Chris Lattner04805fa2002-02-26 21:46:54 +000053
Chris Lattner77f791d2002-05-07 19:02:48 +000054// createLowerAllocationsPass - Interface to this file...
Chris Lattner49525f82004-01-09 06:02:20 +000055FunctionPass *llvm::createLowerAllocationsPass() {
Chris Lattner3cab9f02002-09-25 23:47:47 +000056 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000057}
Chris Lattner37104aa2002-04-29 14:57:45 +000058
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000059
Chris Lattner0686e432002-01-21 07:31:50 +000060// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000061// module contains a declaration for a malloc and a free function.
62//
63// This function is always successful.
64//
Chris Lattner113f4f42002-06-25 16:13:24 +000065bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000066 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattner772eafa2004-02-28 18:51:45 +000067 MallocFunc = M.getNamedFunction("malloc");
68 FreeFunc = M.getNamedFunction("free");
69
70 if (MallocFunc == 0)
71 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
72 if (FreeFunc == 0)
73 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000074
Chris Lattner77f791d2002-05-07 19:02:48 +000075 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000076}
77
Chris Lattnerba7aef392004-07-15 01:08:08 +000078static Constant *getSizeof(const Type *Ty) {
79 Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
80 std::vector<Constant*> Idx;
81 Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
82 Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
83 return ConstantExpr::getCast(Ret, Type::UIntTy);
84}
85
Chris Lattnerd07471d2002-01-21 23:34:02 +000086// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000087// instructions over, assuming that the pass has already been initialized.
88//
Chris Lattner113f4f42002-06-25 16:13:24 +000089bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000090 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000091 assert(MallocFunc && FreeFunc && "Pass not initialized!");
92
93 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000094
95 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +000096 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +000097 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +000098 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +000099
Chris Lattnerba7aef392004-07-15 01:08:08 +0000100 // malloc(type) becomes sbyte *malloc(size)
101 Value *MallocArg = getSizeof(AllocTy);
102 if (MI->isArrayAllocation()) {
103 if (isa<ConstantUInt>(MallocArg) &&
104 cast<ConstantUInt>(MallocArg)->getValue() == 1) {
105 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
106 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
107 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
108 } else {
109 // Multiply it by the array size if necessary...
110 MallocArg = BinaryOperator::create(Instruction::Mul,
111 MI->getOperand(0),
112 MallocArg, "", I);
113 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000114 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000115
116 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
117 std::vector<Value*> MallocArgs;
Chris Lattnerd07471d2002-01-21 23:34:02 +0000118
Chris Lattner772eafa2004-02-28 18:51:45 +0000119 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
120 if (MallocFTy->getNumParams() > 0 &&
121 MallocFTy->getParamType(0) != Type::UIntTy)
122 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
123 MallocArgs.push_back(MallocArg);
124 }
125
126 // If malloc is prototyped to take extra arguments, pass nulls.
127 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
128 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
129
Chris Lattnerd07471d2002-01-21 23:34:02 +0000130 // Create the call to Malloc...
Chris Lattner772eafa2004-02-28 18:51:45 +0000131 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000132
133 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000134 Value *MCast;
135 if (MCall->getType() != Type::VoidTy)
136 MCast = new CastInst(MCall, MI->getType(), "", I);
137 else
138 MCast = Constant::getNullValue(MI->getType());
Chris Lattnerd07471d2002-01-21 23:34:02 +0000139
140 // Replace all uses of the old malloc inst with the cast inst
141 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000142 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000143 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000144 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000145 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner772eafa2004-02-28 18:51:45 +0000146 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
147 std::vector<Value*> FreeArgs;
148
149 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
150 Value *MCast = FI->getOperand(0);
151 if (FreeFTy->getNumParams() > 0 &&
152 FreeFTy->getParamType(0) != MCast->getType())
153 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
154 FreeArgs.push_back(MCast);
155 }
156
157 // If malloc is prototyped to take extra arguments, pass nulls.
158 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
159 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Chris Lattnerd07471d2002-01-21 23:34:02 +0000160
161 // Insert a call to the free function...
Chris Lattner772eafa2004-02-28 18:51:45 +0000162 new CallInst(FreeFunc, FreeArgs, "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000163
164 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000165 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000166 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000167 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000168 }
169 }
170
Chris Lattner6fea0322001-10-18 05:27:33 +0000171 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000172}
Brian Gaeke960707c2003-11-11 22:41:34 +0000173