blob: a842139714e10d89a8eb2fb2db95b371954c0566 [file] [log] [blame]
Chris Lattnerade686e2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1bffea02001-10-15 17:31:51 +00009//
Misha Brukman7c791ed2004-01-28 20:43:01 +000010// The LowerAllocations transformation is a target-dependent tranformation
Chris Lattnerade686e2002-05-07 19:02:48 +000011// because it depends on the size of data types and alignment constraints.
Chris Lattner1bffea02001-10-15 17:31:51 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerd216e8b2006-12-19 22:17:40 +000015#define DEBUG_TYPE "lowerallocs"
Chris Lattnere9ee3e52002-07-23 22:04:17 +000016#include "llvm/Transforms/Scalar.h"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000018#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000019#include "llvm/DerivedTypes.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000021#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattnerde97b572004-12-13 20:00:02 +000024#include "llvm/Target/TargetData.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerd216e8b2006-12-19 22:17:40 +000028STATISTIC(NumLowered, "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029
Chris Lattnerd216e8b2006-12-19 22:17:40 +000030namespace {
Chris Lattner3b2106f2002-09-25 23:47:47 +000031 /// LowerAllocations - Turn malloc and free instructions into %malloc and
32 /// %free calls.
33 ///
Chris Lattner95255282006-06-28 23:17:24 +000034 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Chris Lattner3b2106f2002-09-25 23:47:47 +000035 Function *MallocFunc; // Functions in the module we are processing
36 Function *FreeFunc; // Initialized by doInitialization
Chris Lattner5f792c22005-03-03 01:03:43 +000037 bool LowerMallocArgToInteger;
Chris Lattner3b2106f2002-09-25 23:47:47 +000038 public:
Chris Lattner5f792c22005-03-03 01:03:43 +000039 LowerAllocations(bool LowerToInt = false)
40 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000041
Chris Lattnerde97b572004-12-13 20:00:02 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetData>();
44 AU.setPreservesCFG();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000045
Chris Lattnered96fe82006-05-17 21:05:27 +000046 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000047 AU.addPreserved<UnifyFunctionExitNodes>();
48 AU.addPreservedID(PromoteMemoryToRegisterID);
49 AU.addPreservedID(LowerSelectID);
50 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000051 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerde97b572004-12-13 20:00:02 +000052 }
53
Chris Lattner3b2106f2002-09-25 23:47:47 +000054 /// doPassInitialization - For the lower allocations pass, this ensures that
55 /// a module contains a declaration for a malloc and a free function.
56 ///
57 bool doInitialization(Module &M);
Reid Spencer791a9592004-12-07 08:11:36 +000058
Chris Lattnerde97b572004-12-13 20:00:02 +000059 virtual bool doInitialization(Function &F) {
60 return BasicBlockPass::doInitialization(F);
61 }
Misha Brukmanfd939082005-04-21 23:48:37 +000062
Chris Lattner3b2106f2002-09-25 23:47:47 +000063 /// runOnBasicBlock - This method does the actual work of converting
64 /// instructions over, assuming that the pass has already been initialized.
65 ///
66 bool runOnBasicBlock(BasicBlock &BB);
67 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000068
Chris Lattner7f8897f2006-08-27 22:42:52 +000069 RegisterPass<LowerAllocations>
Chris Lattner3b2106f2002-09-25 23:47:47 +000070 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000071}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000072
Chris Lattnerb3674e42006-05-02 04:24:36 +000073// Publically exposed interface to pass...
74const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattnerade686e2002-05-07 19:02:48 +000075// createLowerAllocationsPass - Interface to this file...
Chris Lattner5f792c22005-03-03 01:03:43 +000076FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
77 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattnerade686e2002-05-07 19:02:48 +000078}
Chris Lattner96c466b2002-04-29 14:57:45 +000079
Chris Lattner1bffea02001-10-15 17:31:51 +000080
Chris Lattnerf4de63f2002-01-21 07:31:50 +000081// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000082// module contains a declaration for a malloc and a free function.
83//
84// This function is always successful.
85//
Chris Lattner7e708292002-06-25 16:13:24 +000086bool LowerAllocations::doInitialization(Module &M) {
Reid Spencerc5b206b2006-12-31 05:48:39 +000087 const Type *SBPTy = PointerType::get(Type::Int8Ty);
Chris Lattnerb99df4f2004-02-28 18:51:45 +000088 MallocFunc = M.getNamedFunction("malloc");
89 FreeFunc = M.getNamedFunction("free");
90
Chris Lattnerde97b572004-12-13 20:00:02 +000091 if (MallocFunc == 0) {
92 // Prototype malloc as "void* malloc(...)", because we don't know in
93 // doInitialization whether size_t is int or long.
94 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
95 MallocFunc = M.getOrInsertFunction("malloc", FT);
96 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +000097 if (FreeFunc == 0)
Jeff Cohen66c5fd62005-10-23 04:37:20 +000098 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bffea02001-10-15 17:31:51 +000099
Chris Lattnerade686e2002-05-07 19:02:48 +0000100 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000101}
102
Chris Lattner84453722002-01-21 23:34:02 +0000103// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +0000104// instructions over, assuming that the pass has already been initialized.
105//
Chris Lattner7e708292002-06-25 16:13:24 +0000106bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000107 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000108 assert(MallocFunc && FreeFunc && "Pass not initialized!");
109
110 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +0000111
Chris Lattner5f792c22005-03-03 01:03:43 +0000112 const TargetData &TD = getAnalysis<TargetData>();
113 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattnerde97b572004-12-13 20:00:02 +0000114
Chris Lattner1bffea02001-10-15 17:31:51 +0000115 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000116 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +0000117 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000118 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000119
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000120 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner5f792c22005-03-03 01:03:43 +0000121 Value *MallocArg;
122 if (LowerMallocArgToInteger)
Reid Spencerc5b206b2006-12-31 05:48:39 +0000123 MallocArg = ConstantInt::get(Type::Int64Ty, TD.getTypeSize(AllocTy));
Chris Lattner5f792c22005-03-03 01:03:43 +0000124 else
125 MallocArg = ConstantExpr::getSizeOf(AllocTy);
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000126 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
127 IntPtrTy);
Chris Lattner5f792c22005-03-03 01:03:43 +0000128
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000129 if (MI->isArrayAllocation()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000130 if (isa<ConstantInt>(MallocArg) &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000131 cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000132 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
133 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000134 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000135 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
136 } else {
Chris Lattnerde97b572004-12-13 20:00:02 +0000137 Value *Scale = MI->getOperand(0);
138 if (Scale->getType() != IntPtrTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000139 Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
140 "", I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000141
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000142 // Multiply it by the array size if necessary...
Chris Lattnerde97b572004-12-13 20:00:02 +0000143 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000144 MallocArg, "", I);
145 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000146 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000147
148 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
149 std::vector<Value*> MallocArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000150
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000151 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000152 if (MallocFTy->isVarArg()) {
153 if (MallocArg->getType() != IntPtrTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000154 MallocArg = CastInst::createIntegerCast(MallocArg, IntPtrTy,
155 false /*ZExt*/, "", I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000156 } else if (MallocFTy->getNumParams() > 0 &&
Reid Spencerc5b206b2006-12-31 05:48:39 +0000157 MallocFTy->getParamType(0) != Type::Int32Ty)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000158 MallocArg = CastInst::createIntegerCast(
159 MallocArg, MallocFTy->getParamType(0), false/*ZExt*/, "",I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000160 MallocArgs.push_back(MallocArg);
161 }
162
163 // If malloc is prototyped to take extra arguments, pass nulls.
164 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
165 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
166
Chris Lattner84453722002-01-21 23:34:02 +0000167 // Create the call to Malloc...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000168 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnera9e92112005-05-06 06:48:21 +0000169 MCall->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000170
Chris Lattner84453722002-01-21 23:34:02 +0000171 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000172 Value *MCast;
173 if (MCall->getType() != Type::VoidTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000174 MCast = new BitCastInst(MCall, MI->getType(), "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000175 else
176 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000177
Chris Lattner84453722002-01-21 23:34:02 +0000178 // Replace all uses of the old malloc inst with the cast inst
179 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000180 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000181 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000182 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000183 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000184 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
185 std::vector<Value*> FreeArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000186
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000187 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
188 Value *MCast = FI->getOperand(0);
189 if (FreeFTy->getNumParams() > 0 &&
190 FreeFTy->getParamType(0) != MCast->getType())
Reid Spencer7b06bd52006-12-13 00:50:17 +0000191 MCast = new BitCastInst(MCast, FreeFTy->getParamType(0), "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000192 FreeArgs.push_back(MCast);
193 }
194
195 // If malloc is prototyped to take extra arguments, pass nulls.
196 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
197 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanfd939082005-04-21 23:48:37 +0000198
Chris Lattner84453722002-01-21 23:34:02 +0000199 // Insert a call to the free function...
Chris Lattnera9e92112005-05-06 06:48:21 +0000200 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000201
Chris Lattner84453722002-01-21 23:34:02 +0000202 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000203 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000204 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000205 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000206 }
207 }
208
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000209 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000210}
Brian Gaeked0fde302003-11-11 22:41:34 +0000211