blob: b7e4040145d9345431e9c19ec86c69f4680e75d3 [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 Lattnere9ee3e52002-07-23 22:04:17 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000016#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000017#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000018#include "llvm/DerivedTypes.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000020#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/Statistic.h"
Chris Lattnerde97b572004-12-13 20:00:02 +000023#include "llvm/Target/TargetData.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000028 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029
Chris Lattner3b2106f2002-09-25 23:47:47 +000030 /// LowerAllocations - Turn malloc and free instructions into %malloc and
31 /// %free calls.
32 ///
Chris Lattner95255282006-06-28 23:17:24 +000033 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Chris Lattner3b2106f2002-09-25 23:47:47 +000034 Function *MallocFunc; // Functions in the module we are processing
35 Function *FreeFunc; // Initialized by doInitialization
Chris Lattner5f792c22005-03-03 01:03:43 +000036 bool LowerMallocArgToInteger;
Chris Lattner3b2106f2002-09-25 23:47:47 +000037 public:
Chris Lattner5f792c22005-03-03 01:03:43 +000038 LowerAllocations(bool LowerToInt = false)
39 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000040
Chris Lattnerde97b572004-12-13 20:00:02 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<TargetData>();
43 AU.setPreservesCFG();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000044
Chris Lattnered96fe82006-05-17 21:05:27 +000045 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000046 AU.addPreserved<UnifyFunctionExitNodes>();
47 AU.addPreservedID(PromoteMemoryToRegisterID);
48 AU.addPreservedID(LowerSelectID);
49 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000050 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerde97b572004-12-13 20:00:02 +000051 }
52
Chris Lattner3b2106f2002-09-25 23:47:47 +000053 /// doPassInitialization - For the lower allocations pass, this ensures that
54 /// a module contains a declaration for a malloc and a free function.
55 ///
56 bool doInitialization(Module &M);
Reid Spencer791a9592004-12-07 08:11:36 +000057
Chris Lattnerde97b572004-12-13 20:00:02 +000058 virtual bool doInitialization(Function &F) {
59 return BasicBlockPass::doInitialization(F);
60 }
Misha Brukmanfd939082005-04-21 23:48:37 +000061
Chris Lattner3b2106f2002-09-25 23:47:47 +000062 /// runOnBasicBlock - This method does the actual work of converting
63 /// instructions over, assuming that the pass has already been initialized.
64 ///
65 bool runOnBasicBlock(BasicBlock &BB);
66 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067
Chris Lattner7f8897f2006-08-27 22:42:52 +000068 RegisterPass<LowerAllocations>
Chris Lattner3b2106f2002-09-25 23:47:47 +000069 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000070}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000071
Chris Lattnerb3674e42006-05-02 04:24:36 +000072// Publically exposed interface to pass...
73const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattnerade686e2002-05-07 19:02:48 +000074// createLowerAllocationsPass - Interface to this file...
Chris Lattner5f792c22005-03-03 01:03:43 +000075FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
76 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattnerade686e2002-05-07 19:02:48 +000077}
Chris Lattner96c466b2002-04-29 14:57:45 +000078
Chris Lattner1bffea02001-10-15 17:31:51 +000079
Chris Lattnerf4de63f2002-01-21 07:31:50 +000080// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000081// module contains a declaration for a malloc and a free function.
82//
83// This function is always successful.
84//
Chris Lattner7e708292002-06-25 16:13:24 +000085bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner7d8a86a2003-08-31 00:22:27 +000086 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattnerb99df4f2004-02-28 18:51:45 +000087 MallocFunc = M.getNamedFunction("malloc");
88 FreeFunc = M.getNamedFunction("free");
89
Chris Lattnerde97b572004-12-13 20:00:02 +000090 if (MallocFunc == 0) {
91 // Prototype malloc as "void* malloc(...)", because we don't know in
92 // doInitialization whether size_t is int or long.
93 FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
94 MallocFunc = M.getOrInsertFunction("malloc", FT);
95 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +000096 if (FreeFunc == 0)
Jeff Cohen66c5fd62005-10-23 04:37:20 +000097 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bffea02001-10-15 17:31:51 +000098
Chris Lattnerade686e2002-05-07 19:02:48 +000099 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000100}
101
Chris Lattner84453722002-01-21 23:34:02 +0000102// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +0000103// instructions over, assuming that the pass has already been initialized.
104//
Chris Lattner7e708292002-06-25 16:13:24 +0000105bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000106 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000107 assert(MallocFunc && FreeFunc && "Pass not initialized!");
108
109 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +0000110
Chris Lattner5f792c22005-03-03 01:03:43 +0000111 const TargetData &TD = getAnalysis<TargetData>();
112 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattnerde97b572004-12-13 20:00:02 +0000113
Chris Lattner1bffea02001-10-15 17:31:51 +0000114 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000115 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +0000116 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000117 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000118
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000119 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner5f792c22005-03-03 01:03:43 +0000120 Value *MallocArg;
121 if (LowerMallocArgToInteger)
Reid Spencerb83eb642006-10-20 07:07:24 +0000122 MallocArg = ConstantInt::get(Type::ULongTy, TD.getTypeSize(AllocTy));
Chris Lattner5f792c22005-03-03 01:03:43 +0000123 else
124 MallocArg = ConstantExpr::getSizeOf(AllocTy);
125 MallocArg = ConstantExpr::getCast(cast<Constant>(MallocArg), IntPtrTy);
126
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000127 if (MI->isArrayAllocation()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000128 if (isa<ConstantInt>(MallocArg) &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000129 cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000130 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
131 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000132 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000133 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
134 } else {
Chris Lattnerde97b572004-12-13 20:00:02 +0000135 Value *Scale = MI->getOperand(0);
136 if (Scale->getType() != IntPtrTy)
Reid Spencer3da59db2006-11-27 01:05:10 +0000137 Scale = CastInst::createInferredCast(Scale, IntPtrTy, "", I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000138
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000139 // Multiply it by the array size if necessary...
Chris Lattnerde97b572004-12-13 20:00:02 +0000140 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000141 MallocArg, "", I);
142 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000143 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000144
145 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
146 std::vector<Value*> MallocArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000147
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000148 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000149 if (MallocFTy->isVarArg()) {
150 if (MallocArg->getType() != IntPtrTy)
Reid Spencer3da59db2006-11-27 01:05:10 +0000151 MallocArg = CastInst::createInferredCast(MallocArg, IntPtrTy, "",
152 I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000153 } else if (MallocFTy->getNumParams() > 0 &&
154 MallocFTy->getParamType(0) != Type::UIntTy)
Reid Spencer3da59db2006-11-27 01:05:10 +0000155 MallocArg =
156 CastInst::createInferredCast(MallocArg, MallocFTy->getParamType(0),
157 "",I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000158 MallocArgs.push_back(MallocArg);
159 }
160
161 // If malloc is prototyped to take extra arguments, pass nulls.
162 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
163 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
164
Chris Lattner84453722002-01-21 23:34:02 +0000165 // Create the call to Malloc...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000166 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnera9e92112005-05-06 06:48:21 +0000167 MCall->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000168
Chris Lattner84453722002-01-21 23:34:02 +0000169 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000170 Value *MCast;
171 if (MCall->getType() != Type::VoidTy)
Reid Spencer3da59db2006-11-27 01:05:10 +0000172 MCast = CastInst::createInferredCast(MCall, MI->getType(), "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000173 else
174 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000175
Chris Lattner84453722002-01-21 23:34:02 +0000176 // Replace all uses of the old malloc inst with the cast inst
177 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000178 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000179 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000180 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000181 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000182 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
183 std::vector<Value*> FreeArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000184
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000185 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
186 Value *MCast = FI->getOperand(0);
187 if (FreeFTy->getNumParams() > 0 &&
188 FreeFTy->getParamType(0) != MCast->getType())
Reid Spencer3da59db2006-11-27 01:05:10 +0000189 MCast = CastInst::createInferredCast(MCast, FreeFTy->getParamType(0),
190 "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000191 FreeArgs.push_back(MCast);
192 }
193
194 // If malloc is prototyped to take extra arguments, pass nulls.
195 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
196 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanfd939082005-04-21 23:48:37 +0000197
Chris Lattner84453722002-01-21 23:34:02 +0000198 // Insert a call to the free function...
Chris Lattnera9e92112005-05-06 06:48:21 +0000199 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000200
Chris Lattner84453722002-01-21 23:34:02 +0000201 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000202 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000203 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000204 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000205 }
206 }
207
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000208 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000209}
Brian Gaeked0fde302003-11-11 22:41:34 +0000210