Add new Pass infrastructure and some examples
[oota-llvm.git] / lib / Transforms / Utils / LowerAllocations.cpp
1 //===- 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 //
22 bool LowerAllocations::doPassInitializationVirt(Module *M) {
23   const MethodType *MallocType = 
24     MethodType::get(PointerType::get(Type::UByteTy),
25                     vector<const Type*>(1, Type::UIntTy), false);
26
27   SymbolTable *SymTab = M->getSymbolTableSure();
28   
29   // Check for a definition of malloc
30   if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
31     MallocMeth = cast<Method>(V);      // Yup, got it
32   } else {                             // Nope, add one
33     M->getMethodList().push_back(MallocMeth = new Method(MallocType, "malloc"));
34   }
35
36   const MethodType *FreeType = 
37     MethodType::get(Type::VoidTy,
38                     vector<const Type*>(1, PointerType::get(Type::UByteTy)),
39                     false);
40
41   // Check for a definition of free
42   if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
43     FreeMeth = cast<Method>(V);      // Yup, got it
44   } else {                             // Nope, add one
45     M->getMethodList().push_back(FreeMeth = new Method(FreeType, "free"));
46   }
47
48   return false;  // Always successful
49 }
50
51 // doPerMethodWork - This method does the actual work of converting
52 // instructions over, assuming that the pass has already been initialized.
53 //
54 bool LowerAllocations::doPerMethodWorkVirt(Method *M) {
55   assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
56
57   // Loop over all of the instructions, looking for malloc or free instructions
58   for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
59     BasicBlock *BB = *BBI;
60     for (unsigned i = 0; i < BB->size(); ++i) {
61       BasicBlock::InstListType &BBIL = BB->getInstList();
62       if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
63         BBIL.remove(BBIL.begin()+i);   // remove the malloc instr...
64         
65         const Type *AllocTy = cast<PointerType>(MI->getType())->getValueType();
66
67         // If the user is allocating an unsized array with a dynamic size arg,
68         // start by getting the size of one element.
69         //
70         if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) 
71           if (ATy->isUnsized()) AllocTy = ATy->getElementType();
72
73         // Get the number of bytes to be allocated for one element of the
74         // requested type...
75         unsigned Size = DataLayout.getTypeSize(AllocTy);
76
77         // malloc(type) becomes sbyte *malloc(constint)
78         Value *MallocArg = ConstPoolUInt::get(Type::UIntTy, Size);
79         if (MI->getNumOperands() && Size == 1) {
80           MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
81         } else if (MI->getNumOperands()) {
82           // Multiply it by the array size if neccesary...
83           MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
84                                              MallocArg);
85           BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
86         }
87
88         // Create the call to Malloc...
89         CallInst *MCall = new CallInst(MallocMeth,
90                                        vector<Value*>(1, MallocArg));
91         BBIL.insert(BBIL.begin()+i, MCall);
92
93         // Create a cast instruction to convert to the right type...
94         CastInst *MCast = new CastInst(MCall, MI->getType());
95         BBIL.insert(BBIL.begin()+i+1, MCast);
96
97         // Replace all uses of the old malloc inst with the cast inst
98         MI->replaceAllUsesWith(MCast);
99         delete MI;                          // Delete the malloc inst
100       } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
101         BBIL.remove(BB->getInstList().begin()+i);
102
103         // Cast the argument to free into a ubyte*...
104         CastInst *MCast = new CastInst(FI->getOperand(0), 
105                                        PointerType::get(Type::UByteTy));
106         BBIL.insert(BBIL.begin()+i, MCast);
107
108         // Insert a call to the free function...
109         CallInst *FCall = new CallInst(FreeMeth,
110                                        vector<Value*>(1, MCast));
111         BBIL.insert(BBIL.begin()+i+1, FCall);
112
113         // Delete the old free instruction
114         delete FI;
115       }
116     }
117   }
118
119   return false;   // Always successful
120 }
121