blob: f1721b3e5872e7dca3110831793b7988ab6b1dff [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 Lattner793c6b82002-01-31 00:45:11 +000016#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000017#include "llvm/DerivedTypes.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000019#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000020#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/Statistic.h"
Chris Lattnerde97b572004-12-13 20:00:02 +000022#include "llvm/Target/TargetData.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnerbd0ef772002-02-26 21:46:54 +000025namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000026 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027
Chris Lattner3b2106f2002-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 Lattner5f792c22005-03-03 01:03:43 +000034 bool LowerMallocArgToInteger;
Chris Lattner3b2106f2002-09-25 23:47:47 +000035 public:
Chris Lattner5f792c22005-03-03 01:03:43 +000036 LowerAllocations(bool LowerToInt = false)
37 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000038
Chris Lattnerde97b572004-12-13 20:00:02 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 AU.setPreservesCFG();
42 }
43
Chris Lattner3b2106f2002-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 Spencer791a9592004-12-07 08:11:36 +000048
Chris Lattnerde97b572004-12-13 20:00:02 +000049 virtual bool doInitialization(Function &F) {
50 return BasicBlockPass::doInitialization(F);
51 }
Misha Brukmanfd939082005-04-21 23:48:37 +000052
Chris Lattner3b2106f2002-09-25 23:47:47 +000053 /// 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 Lattnerbd0ef772002-02-26 21:46:54 +000058
Chris Lattner3b2106f2002-09-25 23:47:47 +000059 RegisterOpt<LowerAllocations>
60 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000061}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000062
Chris Lattnerade686e2002-05-07 19:02:48 +000063// createLowerAllocationsPass - Interface to this file...
Chris Lattner5f792c22005-03-03 01:03:43 +000064FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
65 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattnerade686e2002-05-07 19:02:48 +000066}
Chris Lattner96c466b2002-04-29 14:57:45 +000067
Chris Lattner1bffea02001-10-15 17:31:51 +000068
Chris Lattnerf4de63f2002-01-21 07:31:50 +000069// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-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 Lattner7e708292002-06-25 16:13:24 +000074bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner7d8a86a2003-08-31 00:22:27 +000075 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattnerb99df4f2004-02-28 18:51:45 +000076 MallocFunc = M.getNamedFunction("malloc");
77 FreeFunc = M.getNamedFunction("free");
78
Chris Lattnerde97b572004-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 Lattnerb99df4f2004-02-28 18:51:45 +000085 if (FreeFunc == 0)
Jeff Cohen66c5fd62005-10-23 04:37:20 +000086 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
Chris Lattner1bffea02001-10-15 17:31:51 +000087
Chris Lattnerade686e2002-05-07 19:02:48 +000088 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000089}
90
Chris Lattner84453722002-01-21 23:34:02 +000091// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000092// instructions over, assuming that the pass has already been initialized.
93//
Chris Lattner7e708292002-06-25 16:13:24 +000094bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000095 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000096 assert(MallocFunc && FreeFunc && "Pass not initialized!");
97
98 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +000099
Chris Lattner5f792c22005-03-03 01:03:43 +0000100 const TargetData &TD = getAnalysis<TargetData>();
101 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattnerde97b572004-12-13 20:00:02 +0000102
Chris Lattner1bffea02001-10-15 17:31:51 +0000103 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000104 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +0000105 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000106 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000107
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000108 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner5f792c22005-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 Lattner8bfc2f12004-07-15 01:08:08 +0000116 if (MI->isArrayAllocation()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000117 if (isa<ConstantInt>(MallocArg) &&
118 cast<ConstantInt>(MallocArg)->getRawValue() == 1) {
Chris Lattner8bfc2f12004-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 Lattnerde97b572004-12-13 20:00:02 +0000121 CO = ConstantExpr::getCast(CO, IntPtrTy);
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000122 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
123 } else {
Chris Lattnerde97b572004-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 Lattner8bfc2f12004-07-15 01:08:08 +0000128 // Multiply it by the array size if necessary...
Chris Lattnerde97b572004-12-13 20:00:02 +0000129 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000130 MallocArg, "", I);
131 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000132 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000133
134 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
135 std::vector<Value*> MallocArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000136
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000137 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
Chris Lattnerde97b572004-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 Lattnerb99df4f2004-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 Lattner84453722002-01-21 23:34:02 +0000151 // Create the call to Malloc...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000152 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattnera9e92112005-05-06 06:48:21 +0000153 MCall->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000154
Chris Lattner84453722002-01-21 23:34:02 +0000155 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000156 Value *MCast;
157 if (MCall->getType() != Type::VoidTy)
158 MCast = new CastInst(MCall, MI->getType(), "", I);
159 else
160 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000161
Chris Lattner84453722002-01-21 23:34:02 +0000162 // Replace all uses of the old malloc inst with the cast inst
163 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000164 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000165 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000166 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000167 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000168 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
169 std::vector<Value*> FreeArgs;
Misha Brukmanfd939082005-04-21 23:48:37 +0000170
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000171 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
172 Value *MCast = FI->getOperand(0);
173 if (FreeFTy->getNumParams() > 0 &&
174 FreeFTy->getParamType(0) != MCast->getType())
175 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
176 FreeArgs.push_back(MCast);
177 }
178
179 // If malloc is prototyped to take extra arguments, pass nulls.
180 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
181 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Misha Brukmanfd939082005-04-21 23:48:37 +0000182
Chris Lattner84453722002-01-21 23:34:02 +0000183 // Insert a call to the free function...
Chris Lattnera9e92112005-05-06 06:48:21 +0000184 (new CallInst(FreeFunc, FreeArgs, "", I))->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000185
Chris Lattner84453722002-01-21 23:34:02 +0000186 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000187 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000188 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000189 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000190 }
191 }
192
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000193 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000194}
Brian Gaeked0fde302003-11-11 22:41:34 +0000195