* Add support for different "PassType's"
[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/Scalar.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   LowerAllocations(const TargetData &TD) : DataLayout(TD) {
35     MallocFunc = FreeFunc = 0;
36   }
37
38   // doPassInitialization - For the lower allocations pass, this ensures that a
39   // module contains a declaration for a malloc and a free function.
40   //
41   bool doInitialization(Module &M);
42
43   // runOnBasicBlock - This method does the actual work of converting
44   // instructions over, assuming that the pass has already been initialized.
45   //
46   bool runOnBasicBlock(BasicBlock &BB);
47 };
48 }
49
50 // createLowerAllocationsPass - Interface to this file...
51 Pass *createLowerAllocationsPass(const TargetData &TD) {
52   return new LowerAllocations(TD);
53 }
54
55 static RegisterOpt<LowerAllocations>
56 X("lowerallocs", "Lower allocations from instructions to calls (TD)",
57   createLowerAllocationsPass);
58
59
60 // doInitialization - For the lower allocations pass, this ensures that a
61 // module contains a declaration for a malloc and a free function.
62 //
63 // This function is always successful.
64 //
65 bool LowerAllocations::doInitialization(Module &M) {
66   const FunctionType *MallocType = 
67     FunctionType::get(PointerType::get(Type::SByteTy),
68                       vector<const Type*>(1, Type::UIntTy), false);
69   const FunctionType *FreeType = 
70     FunctionType::get(Type::VoidTy,
71                       vector<const Type*>(1, PointerType::get(Type::SByteTy)),
72                       false);
73
74   MallocFunc = M.getOrInsertFunction("malloc", MallocType);
75   FreeFunc   = M.getOrInsertFunction("free"  , FreeType);
76
77   return true;
78 }
79
80 // runOnBasicBlock - This method does the actual work of converting
81 // instructions over, assuming that the pass has already been initialized.
82 //
83 bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
84   bool Changed = false;
85   assert(MallocFunc && FreeFunc && "Pass not initialized!");
86
87   BasicBlock::InstListType &BBIL = BB.getInstList();
88
89   // Loop over all of the instructions, looking for malloc or free instructions
90   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
91     if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
92       BBIL.remove(I);   // remove the malloc instr...
93
94       const Type *AllocTy = MI->getType()->getElementType();
95       
96       // Get the number of bytes to be allocated for one element of the
97       // requested type...
98       unsigned Size = DataLayout.getTypeSize(AllocTy);
99       
100       // malloc(type) becomes sbyte *malloc(constint)
101       Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
102       if (MI->getNumOperands() && Size == 1) {
103         MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
104       } else if (MI->getNumOperands()) {
105         // Multiply it by the array size if neccesary...
106         MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
107                                            MallocArg);
108         I = ++BBIL.insert(I, cast<Instruction>(MallocArg));
109       }
110       
111       // Create the call to Malloc...
112       CallInst *MCall = new CallInst(MallocFunc,
113                                      vector<Value*>(1, MallocArg));
114       I = BBIL.insert(I, MCall);
115       
116       // Create a cast instruction to convert to the right type...
117       CastInst *MCast = new CastInst(MCall, MI->getType());
118       I = BBIL.insert(++I, MCast);
119       
120       // Replace all uses of the old malloc inst with the cast inst
121       MI->replaceAllUsesWith(MCast);
122       delete MI;                          // Delete the malloc inst
123       Changed = true;
124       ++NumLowered;
125     } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
126       BBIL.remove(I);
127       
128       // Cast the argument to free into a ubyte*...
129       CastInst *MCast = new CastInst(FI->getOperand(0), 
130                                      PointerType::get(Type::UByteTy));
131       I = ++BBIL.insert(I, MCast);
132       
133       // Insert a call to the free function...
134       CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast));
135       I = BBIL.insert(I, FCall);
136       
137       // Delete the old free instruction
138       delete FI;
139       Changed = true;
140       ++NumLowered;
141     }
142   }
143
144   return Changed;
145 }