blob: 2385fee7aa623b179fd32ed1f8a688a1728807a8 [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 Lattner161aa5a2001-11-07 13:49:45 +000016#include "llvm/ConstPoolVals.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
35 M->getMethodList().push_back(MallocMeth = new Method(MallocType, "malloc"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000036 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000037 }
38
39 const MethodType *FreeType =
40 MethodType::get(Type::VoidTy,
Chris Lattner7ef65592001-10-31 06:36:23 +000041 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bffea02001-10-15 17:31:51 +000042 false);
43
44 // Check for a definition of free
45 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
46 FreeMeth = cast<Method>(V); // Yup, got it
47 } else { // Nope, add one
48 M->getMethodList().push_back(FreeMeth = new Method(FreeType, "free"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000049 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000050 }
51
Chris Lattner42c9c2c2001-10-18 05:27:33 +000052 return Changed; // Always successful
Chris Lattner1bffea02001-10-15 17:31:51 +000053}
54
55// doPerMethodWork - This method does the actual work of converting
56// instructions over, assuming that the pass has already been initialized.
57//
Chris Lattner42c9c2c2001-10-18 05:27:33 +000058bool LowerAllocations::doPerMethodWork(Method *M) {
59 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000060 assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
61
62 // Loop over all of the instructions, looking for malloc or free instructions
63 for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
64 BasicBlock *BB = *BBI;
65 for (unsigned i = 0; i < BB->size(); ++i) {
66 BasicBlock::InstListType &BBIL = BB->getInstList();
67 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
68 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
69
70 const Type *AllocTy = cast<PointerType>(MI->getType())->getValueType();
71
72 // If the user is allocating an unsized array with a dynamic size arg,
73 // start by getting the size of one element.
74 //
75 if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy))
76 if (ATy->isUnsized()) AllocTy = ATy->getElementType();
77
78 // Get the number of bytes to be allocated for one element of the
79 // requested type...
80 unsigned Size = DataLayout.getTypeSize(AllocTy);
81
82 // malloc(type) becomes sbyte *malloc(constint)
83 Value *MallocArg = ConstPoolUInt::get(Type::UIntTy, Size);
84 if (MI->getNumOperands() && Size == 1) {
85 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
86 } else if (MI->getNumOperands()) {
87 // Multiply it by the array size if neccesary...
88 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
89 MallocArg);
90 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
91 }
92
93 // Create the call to Malloc...
94 CallInst *MCall = new CallInst(MallocMeth,
95 vector<Value*>(1, MallocArg));
96 BBIL.insert(BBIL.begin()+i, MCall);
97
98 // Create a cast instruction to convert to the right type...
99 CastInst *MCast = new CastInst(MCall, MI->getType());
100 BBIL.insert(BBIL.begin()+i+1, MCast);
101
102 // Replace all uses of the old malloc inst with the cast inst
103 MI->replaceAllUsesWith(MCast);
104 delete MI; // Delete the malloc inst
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000105 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000106 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
107 BBIL.remove(BB->getInstList().begin()+i);
108
109 // Cast the argument to free into a ubyte*...
110 CastInst *MCast = new CastInst(FI->getOperand(0),
111 PointerType::get(Type::UByteTy));
112 BBIL.insert(BBIL.begin()+i, MCast);
113
114 // Insert a call to the free function...
115 CallInst *FCall = new CallInst(FreeMeth,
116 vector<Value*>(1, MCast));
117 BBIL.insert(BBIL.begin()+i+1, FCall);
118
119 // Delete the old free instruction
120 delete FI;
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000121 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000122 }
123 }
124 }
125
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000126 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000127}
128