Add support for printing out statistics information when -stats is added to
[oota-llvm.git] / lib / Transforms / Utils / LowerAllocations.cpp
1 //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
2 //
3 // The LowerAllocations transformation is a target dependant tranformation
4 // because it depends on the size of data types and alignment constraints.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Transforms/ChangeAllocations.h"
9 #include "llvm/Module.h"
10 #include "llvm/Function.h"
11 #include "llvm/BasicBlock.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Target/TargetData.h"
18 #include "Support/StatisticReporter.h"
19
20 static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
21 using std::vector;
22
23 namespace {
24
25 // LowerAllocations - Turn malloc and free instructions into %malloc and %free
26 // calls.
27 //
28 class LowerAllocations : public BasicBlockPass {
29   Function *MallocFunc;   // Functions in the module we are processing
30   Function *FreeFunc;     // Initialized by doInitialization
31
32   const TargetData &DataLayout;
33 public:
34   inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
35     MallocFunc = FreeFunc = 0;
36   }
37
38   const char *getPassName() const { return "Lower Allocations"; }
39
40   // doPassInitialization - For the lower allocations pass, this ensures that a
41   // module contains a declaration for a malloc and a free function.
42   //
43   bool doInitialization(Module *M);
44
45   // runOnBasicBlock - This method does the actual work of converting
46   // instructions over, assuming that the pass has already been initialized.
47   //
48   bool runOnBasicBlock(BasicBlock *BB);
49 };
50
51 }
52
53 // createLowerAllocationsPass - Interface to this file...
54 Pass *createLowerAllocationsPass(const TargetData &TD) {
55   return new LowerAllocations(TD);
56 }
57
58
59 // doInitialization - For the lower allocations pass, this ensures that a
60 // module contains a declaration for a malloc and a free function.
61 //
62 // This function is always successful.
63 //
64 bool LowerAllocations::doInitialization(Module *M) {
65   const FunctionType *MallocType = 
66     FunctionType::get(PointerType::get(Type::SByteTy),
67                       vector<const Type*>(1, Type::UIntTy), false);
68   const FunctionType *FreeType = 
69     FunctionType::get(Type::VoidTy,
70                       vector<const Type*>(1, PointerType::get(Type::SByteTy)),
71                       false);
72
73   MallocFunc = M->getOrInsertFunction("malloc", MallocType);
74   FreeFunc   = M->getOrInsertFunction("free"  , FreeType);
75
76   return true;
77 }
78
79 // runOnBasicBlock - This method does the actual work of converting
80 // instructions over, assuming that the pass has already been initialized.
81 //
82 bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
83   bool Changed = false;
84   assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
85
86   // Loop over all of the instructions, looking for malloc or free instructions
87   for (unsigned i = 0; i != BB->size(); ++i) {
88     BasicBlock::InstListType &BBIL = BB->getInstList();
89     if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
90       BBIL.remove(BBIL.begin()+i);   // remove the malloc instr...
91         
92       const Type *AllocTy = cast<PointerType>(MI->getType())->getElementType();
93       
94       // Get the number of bytes to be allocated for one element of the
95       // requested type...
96       unsigned Size = DataLayout.getTypeSize(AllocTy);
97       
98       // malloc(type) becomes sbyte *malloc(constint)
99       Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
100       if (MI->getNumOperands() && Size == 1) {
101         MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
102       } else if (MI->getNumOperands()) {
103         // Multiply it by the array size if neccesary...
104         MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
105                                            MallocArg);
106         BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
107       }
108       
109       // Create the call to Malloc...
110       CallInst *MCall = new CallInst(MallocFunc,
111                                      vector<Value*>(1, MallocArg));
112       BBIL.insert(BBIL.begin()+i, MCall);
113       
114       // Create a cast instruction to convert to the right type...
115       CastInst *MCast = new CastInst(MCall, MI->getType());
116       BBIL.insert(BBIL.begin()+i+1, MCast);
117       
118       // Replace all uses of the old malloc inst with the cast inst
119       MI->replaceAllUsesWith(MCast);
120       delete MI;                          // Delete the malloc inst
121       Changed = true;
122       ++NumLowered;
123     } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
124       BBIL.remove(BB->getInstList().begin()+i);
125       
126       // Cast the argument to free into a ubyte*...
127       CastInst *MCast = new CastInst(FI->getOperand(0), 
128                                      PointerType::get(Type::UByteTy));
129       BBIL.insert(BBIL.begin()+i, MCast);
130       
131       // Insert a call to the free function...
132       CallInst *FCall = new CallInst(FreeFunc,
133                                      vector<Value*>(1, MCast));
134       BBIL.insert(BBIL.begin()+i+1, FCall);
135       
136       // Delete the old free instruction
137       delete FI;
138       Changed = true;
139       ++NumLowered;
140     }
141   }
142
143   return Changed;
144 }