blob: a5197a0e01cec37ff9dcf1f86c5fac5b2a9cb3af [file] [log] [blame]
Chris Lattnerade686e2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
John Criswellb576c942003-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 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"
Chris Lattnera92f6962002-10-01 22:38:41 +000021#include "Support/Statistic.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerbd0ef772002-02-26 21:46:54 +000024namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000025 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000026
Chris Lattner3b2106f2002-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 Lattnerbd0ef772002-02-26 21:46:54 +000035
Chris Lattner3b2106f2002-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);
40
41 /// runOnBasicBlock - This method does the actual work of converting
42 /// instructions over, assuming that the pass has already been initialized.
43 ///
44 bool runOnBasicBlock(BasicBlock &BB);
45 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000046
Chris Lattner3b2106f2002-09-25 23:47:47 +000047 RegisterOpt<LowerAllocations>
48 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000049}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000050
Chris Lattnerade686e2002-05-07 19:02:48 +000051// createLowerAllocationsPass - Interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000052FunctionPass *llvm::createLowerAllocationsPass() {
Chris Lattner3b2106f2002-09-25 23:47:47 +000053 return new LowerAllocations();
Chris Lattnerade686e2002-05-07 19:02:48 +000054}
Chris Lattner96c466b2002-04-29 14:57:45 +000055
Chris Lattner1bffea02001-10-15 17:31:51 +000056
Chris Lattnerf4de63f2002-01-21 07:31:50 +000057// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000058// module contains a declaration for a malloc and a free function.
59//
60// This function is always successful.
61//
Chris Lattner7e708292002-06-25 16:13:24 +000062bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner7d8a86a2003-08-31 00:22:27 +000063 const Type *SBPTy = PointerType::get(Type::SByteTy);
Chris Lattnerb99df4f2004-02-28 18:51:45 +000064 MallocFunc = M.getNamedFunction("malloc");
65 FreeFunc = M.getNamedFunction("free");
66
67 if (MallocFunc == 0)
68 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
69 if (FreeFunc == 0)
70 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bffea02001-10-15 17:31:51 +000071
Chris Lattnerade686e2002-05-07 19:02:48 +000072 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000073}
74
Chris Lattner8bfc2f12004-07-15 01:08:08 +000075static Constant *getSizeof(const Type *Ty) {
76 Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
77 std::vector<Constant*> Idx;
78 Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
79 Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
80 return ConstantExpr::getCast(Ret, Type::UIntTy);
81}
82
Chris Lattner84453722002-01-21 23:34:02 +000083// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000084// instructions over, assuming that the pass has already been initialized.
85//
Chris Lattner7e708292002-06-25 16:13:24 +000086bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000087 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000088 assert(MallocFunc && FreeFunc && "Pass not initialized!");
89
90 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +000091
92 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +000093 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +000094 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +000095 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattner84453722002-01-21 23:34:02 +000096
Chris Lattner8bfc2f12004-07-15 01:08:08 +000097 // malloc(type) becomes sbyte *malloc(size)
98 Value *MallocArg = getSizeof(AllocTy);
99 if (MI->isArrayAllocation()) {
100 if (isa<ConstantUInt>(MallocArg) &&
101 cast<ConstantUInt>(MallocArg)->getValue() == 1) {
102 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
103 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
104 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
105 } else {
106 // Multiply it by the array size if necessary...
107 MallocArg = BinaryOperator::create(Instruction::Mul,
108 MI->getOperand(0),
109 MallocArg, "", I);
110 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000111 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000112
113 const FunctionType *MallocFTy = MallocFunc->getFunctionType();
114 std::vector<Value*> MallocArgs;
Chris Lattner84453722002-01-21 23:34:02 +0000115
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000116 if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
117 if (MallocFTy->getNumParams() > 0 &&
118 MallocFTy->getParamType(0) != Type::UIntTy)
119 MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
120 MallocArgs.push_back(MallocArg);
121 }
122
123 // If malloc is prototyped to take extra arguments, pass nulls.
124 for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
125 MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
126
Chris Lattner84453722002-01-21 23:34:02 +0000127 // Create the call to Malloc...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000128 CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000129
130 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000131 Value *MCast;
132 if (MCall->getType() != Type::VoidTy)
133 MCast = new CastInst(MCall, MI->getType(), "", I);
134 else
135 MCast = Constant::getNullValue(MI->getType());
Chris Lattner84453722002-01-21 23:34:02 +0000136
137 // Replace all uses of the old malloc inst with the cast inst
138 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000139 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000140 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000141 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000142 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000143 const FunctionType *FreeFTy = FreeFunc->getFunctionType();
144 std::vector<Value*> FreeArgs;
145
146 if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
147 Value *MCast = FI->getOperand(0);
148 if (FreeFTy->getNumParams() > 0 &&
149 FreeFTy->getParamType(0) != MCast->getType())
150 MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
151 FreeArgs.push_back(MCast);
152 }
153
154 // If malloc is prototyped to take extra arguments, pass nulls.
155 for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
156 FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
Chris Lattner84453722002-01-21 23:34:02 +0000157
158 // Insert a call to the free function...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000159 new CallInst(FreeFunc, FreeArgs, "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000160
161 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000162 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000163 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000164 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000165 }
166 }
167
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000168 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000169}
Brian Gaeked0fde302003-11-11 22:41:34 +0000170