blob: 91f45d8206957138883b5ae1353f9b3735b463a5 [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"
16
17// doPassInitialization - For the lower allocations pass, this ensures that a
18// module contains a declaration for a malloc and a free function.
19//
20// This function is always successful.
21//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000022bool LowerAllocations::doPassInitialization(Module *M) {
23 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000024 const MethodType *MallocType =
Chris Lattner7ef65592001-10-31 06:36:23 +000025 MethodType::get(PointerType::get(Type::SByteTy),
Chris Lattner1bffea02001-10-15 17:31:51 +000026 vector<const Type*>(1, Type::UIntTy), false);
27
28 SymbolTable *SymTab = M->getSymbolTableSure();
29
30 // Check for a definition of malloc
31 if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
32 MallocMeth = cast<Method>(V); // Yup, got it
33 } else { // Nope, add one
34 M->getMethodList().push_back(MallocMeth = new Method(MallocType, "malloc"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000035 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000036 }
37
38 const MethodType *FreeType =
39 MethodType::get(Type::VoidTy,
Chris Lattner7ef65592001-10-31 06:36:23 +000040 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bffea02001-10-15 17:31:51 +000041 false);
42
43 // Check for a definition of free
44 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
45 FreeMeth = cast<Method>(V); // Yup, got it
46 } else { // Nope, add one
47 M->getMethodList().push_back(FreeMeth = new Method(FreeType, "free"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000048 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000049 }
50
Chris Lattner42c9c2c2001-10-18 05:27:33 +000051 return Changed; // Always successful
Chris Lattner1bffea02001-10-15 17:31:51 +000052}
53
54// doPerMethodWork - This method does the actual work of converting
55// instructions over, assuming that the pass has already been initialized.
56//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000057bool LowerAllocations::doPerMethodWork(Method *M) {
58 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000059 assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
60
61 // Loop over all of the instructions, looking for malloc or free instructions
62 for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
63 BasicBlock *BB = *BBI;
64 for (unsigned i = 0; i < BB->size(); ++i) {
65 BasicBlock::InstListType &BBIL = BB->getInstList();
66 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
67 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
68
69 const Type *AllocTy = cast<PointerType>(MI->getType())->getValueType();
70
71 // If the user is allocating an unsized array with a dynamic size arg,
72 // start by getting the size of one element.
73 //
74 if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy))
75 if (ATy->isUnsized()) AllocTy = ATy->getElementType();
76
77 // Get the number of bytes to be allocated for one element of the
78 // requested type...
79 unsigned Size = DataLayout.getTypeSize(AllocTy);
80
81 // malloc(type) becomes sbyte *malloc(constint)
82 Value *MallocArg = ConstPoolUInt::get(Type::UIntTy, Size);
83 if (MI->getNumOperands() && Size == 1) {
84 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
85 } else if (MI->getNumOperands()) {
86 // Multiply it by the array size if neccesary...
87 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
88 MallocArg);
89 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
90 }
91
92 // Create the call to Malloc...
93 CallInst *MCall = new CallInst(MallocMeth,
94 vector<Value*>(1, MallocArg));
95 BBIL.insert(BBIL.begin()+i, MCall);
96
97 // Create a cast instruction to convert to the right type...
98 CastInst *MCast = new CastInst(MCall, MI->getType());
99 BBIL.insert(BBIL.begin()+i+1, MCast);
100
101 // Replace all uses of the old malloc inst with the cast inst
102 MI->replaceAllUsesWith(MCast);
103 delete MI; // Delete the malloc inst
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000104 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000105 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
106 BBIL.remove(BB->getInstList().begin()+i);
107
108 // Cast the argument to free into a ubyte*...
109 CastInst *MCast = new CastInst(FI->getOperand(0),
110 PointerType::get(Type::UByteTy));
111 BBIL.insert(BBIL.begin()+i, MCast);
112
113 // Insert a call to the free function...
114 CallInst *FCall = new CallInst(FreeMeth,
115 vector<Value*>(1, MCast));
116 BBIL.insert(BBIL.begin()+i+1, FCall);
117
118 // Delete the old free instruction
119 delete FI;
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000120 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000121 }
122 }
123 }
124
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000125 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000126}
127