blob: ca1085e3edc73569b65c99020a2740898c4a3f74 [file] [log] [blame]
Chris Lattner1bffea02001-10-15 17:31:51 +00001//===- llvm/Transforms/LowerAllocations.h - Remove Malloc & Free Insts ------=//
2//
3// This file implements a pass that lowers malloc and free instructions to
4// calls to %malloc & %free functions. This transformation is a target
5// dependant tranformation because we depend on the size of data types and
6// alignment constraints.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Transforms/LowerAllocations.h"
11#include "llvm/Target/TargetData.h"
12#include "llvm/DerivedTypes.h"
13#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
15#include "llvm/SymbolTable.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016#include "llvm/ConstantVals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000017using std::vector;
18
Chris Lattner1bffea02001-10-15 17:31:51 +000019
20// doPassInitialization - For the lower allocations pass, this ensures that a
21// module contains a declaration for a malloc and a free function.
22//
23// This function is always successful.
24//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000025bool LowerAllocations::doPassInitialization(Module *M) {
26 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000027 const MethodType *MallocType =
Chris Lattner7ef65592001-10-31 06:36:23 +000028 MethodType::get(PointerType::get(Type::SByteTy),
Chris Lattner1bffea02001-10-15 17:31:51 +000029 vector<const Type*>(1, Type::UIntTy), false);
30
31 SymbolTable *SymTab = M->getSymbolTableSure();
32
33 // Check for a definition of malloc
34 if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
35 MallocMeth = cast<Method>(V); // Yup, got it
36 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000037 M->getMethodList().push_back(MallocMeth = new Method(MallocType, false,
38 "malloc"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000039 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000040 }
41
42 const MethodType *FreeType =
43 MethodType::get(Type::VoidTy,
Chris Lattner7ef65592001-10-31 06:36:23 +000044 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bffea02001-10-15 17:31:51 +000045 false);
46
47 // Check for a definition of free
48 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
49 FreeMeth = cast<Method>(V); // Yup, got it
50 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000051 M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000052 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000053 }
54
Chris Lattner42c9c2c2001-10-18 05:27:33 +000055 return Changed; // Always successful
Chris Lattner1bffea02001-10-15 17:31:51 +000056}
57
58// doPerMethodWork - This method does the actual work of converting
59// instructions over, assuming that the pass has already been initialized.
60//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000061bool LowerAllocations::doPerMethodWork(Method *M) {
62 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000063 assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
64
65 // Loop over all of the instructions, looking for malloc or free instructions
66 for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
67 BasicBlock *BB = *BBI;
68 for (unsigned i = 0; i < BB->size(); ++i) {
69 BasicBlock::InstListType &BBIL = BB->getInstList();
70 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
71 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
72
Chris Lattner7a176752001-12-04 00:03:30 +000073 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
Chris Lattner1bffea02001-10-15 17:31:51 +000074
Chris Lattner1bffea02001-10-15 17:31:51 +000075 // Get the number of bytes to be allocated for one element of the
76 // requested type...
77 unsigned Size = DataLayout.getTypeSize(AllocTy);
78
79 // malloc(type) becomes sbyte *malloc(constint)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
Chris Lattner1bffea02001-10-15 17:31:51 +000081 if (MI->getNumOperands() && Size == 1) {
82 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
83 } else if (MI->getNumOperands()) {
84 // Multiply it by the array size if neccesary...
85 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
86 MallocArg);
87 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
88 }
89
90 // Create the call to Malloc...
91 CallInst *MCall = new CallInst(MallocMeth,
92 vector<Value*>(1, MallocArg));
93 BBIL.insert(BBIL.begin()+i, MCall);
94
95 // Create a cast instruction to convert to the right type...
96 CastInst *MCast = new CastInst(MCall, MI->getType());
97 BBIL.insert(BBIL.begin()+i+1, MCast);
98
99 // Replace all uses of the old malloc inst with the cast inst
100 MI->replaceAllUsesWith(MCast);
101 delete MI; // Delete the malloc inst
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000102 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000103 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
104 BBIL.remove(BB->getInstList().begin()+i);
105
106 // Cast the argument to free into a ubyte*...
107 CastInst *MCast = new CastInst(FI->getOperand(0),
108 PointerType::get(Type::UByteTy));
109 BBIL.insert(BBIL.begin()+i, MCast);
110
111 // Insert a call to the free function...
112 CallInst *FCall = new CallInst(FreeMeth,
113 vector<Value*>(1, MCast));
114 BBIL.insert(BBIL.begin()+i+1, FCall);
115
116 // Delete the old free instruction
117 delete FI;
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000118 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000119 }
120 }
121 }
122
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000123 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000124}
125