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