blob: bb2cdb67fa49f4b5ffeb23985bffce4cb2b32231 [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 Lattner1bffea02001-10-15 17:31:51 +000017
18// doPassInitialization - For the lower allocations pass, this ensures that a
19// module contains a declaration for a malloc and a free function.
20//
21// This function is always successful.
22//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000023bool LowerAllocations::doPassInitialization(Module *M) {
24 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000025 const MethodType *MallocType =
Chris Lattner7ef65592001-10-31 06:36:23 +000026 MethodType::get(PointerType::get(Type::SByteTy),
Chris Lattner1bffea02001-10-15 17:31:51 +000027 vector<const Type*>(1, Type::UIntTy), false);
28
29 SymbolTable *SymTab = M->getSymbolTableSure();
30
31 // Check for a definition of malloc
32 if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
33 MallocMeth = cast<Method>(V); // Yup, got it
34 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000035 M->getMethodList().push_back(MallocMeth = new Method(MallocType, false,
36 "malloc"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000037 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000038 }
39
40 const MethodType *FreeType =
41 MethodType::get(Type::VoidTy,
Chris Lattner7ef65592001-10-31 06:36:23 +000042 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bffea02001-10-15 17:31:51 +000043 false);
44
45 // Check for a definition of free
46 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
47 FreeMeth = cast<Method>(V); // Yup, got it
48 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000049 M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000050 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000051 }
52
Chris Lattner42c9c2c2001-10-18 05:27:33 +000053 return Changed; // Always successful
Chris Lattner1bffea02001-10-15 17:31:51 +000054}
55
56// doPerMethodWork - This method does the actual work of converting
57// instructions over, assuming that the pass has already been initialized.
58//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000059bool LowerAllocations::doPerMethodWork(Method *M) {
60 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000061 assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
62
63 // Loop over all of the instructions, looking for malloc or free instructions
64 for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
65 BasicBlock *BB = *BBI;
66 for (unsigned i = 0; i < BB->size(); ++i) {
67 BasicBlock::InstListType &BBIL = BB->getInstList();
68 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
69 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
70
Chris Lattner7a176752001-12-04 00:03:30 +000071 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
Chris Lattner1bffea02001-10-15 17:31:51 +000072
73 // If the user is allocating an unsized array with a dynamic size arg,
74 // start by getting the size of one element.
75 //
76 if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy))
77 if (ATy->isUnsized()) AllocTy = ATy->getElementType();
78
79 // Get the number of bytes to be allocated for one element of the
80 // requested type...
81 unsigned Size = DataLayout.getTypeSize(AllocTy);
82
83 // malloc(type) becomes sbyte *malloc(constint)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000084 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
Chris Lattner1bffea02001-10-15 17:31:51 +000085 if (MI->getNumOperands() && Size == 1) {
86 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
87 } else if (MI->getNumOperands()) {
88 // Multiply it by the array size if neccesary...
89 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
90 MallocArg);
91 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
92 }
93
94 // Create the call to Malloc...
95 CallInst *MCall = new CallInst(MallocMeth,
96 vector<Value*>(1, MallocArg));
97 BBIL.insert(BBIL.begin()+i, MCall);
98
99 // Create a cast instruction to convert to the right type...
100 CastInst *MCast = new CastInst(MCall, MI->getType());
101 BBIL.insert(BBIL.begin()+i+1, MCast);
102
103 // Replace all uses of the old malloc inst with the cast inst
104 MI->replaceAllUsesWith(MCast);
105 delete MI; // Delete the malloc inst
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000106 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000107 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
108 BBIL.remove(BB->getInstList().begin()+i);
109
110 // Cast the argument to free into a ubyte*...
111 CastInst *MCast = new CastInst(FI->getOperand(0),
112 PointerType::get(Type::UByteTy));
113 BBIL.insert(BBIL.begin()+i, MCast);
114
115 // Insert a call to the free function...
116 CallInst *FCall = new CallInst(FreeMeth,
117 vector<Value*>(1, MCast));
118 BBIL.insert(BBIL.begin()+i+1, FCall);
119
120 // Delete the old free instruction
121 delete FI;
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000122 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000123 }
124 }
125 }
126
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000127 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000128}
129