Loop invariant code motion now depends on the LoopPreheader pass. Dead code
[oota-llvm.git] / lib / Transforms / IPO / RaiseAllocations.cpp
1 //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
2 //
3 // This file defines the RaiseAllocations pass which convert malloc and free
4 // calls to malloc and free instructions.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Transforms/Scalar.h"
9 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
10 #include "llvm/Module.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Pass.h"
15 #include "Support/StatisticReporter.h"
16
17 static Statistic<> NumRaised("raiseallocs\t- Number of allocations raised");
18
19 namespace {
20
21 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
22 // instruction.
23 //
24 class RaiseAllocations : public BasicBlockPass {
25   Function *MallocFunc;   // Functions in the module we are processing
26   Function *FreeFunc;     // Initialized by doPassInitializationVirt
27 public:
28   RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
29
30   // doPassInitialization - For the raise allocations pass, this finds a
31   // declaration for malloc and free if they exist.
32   //
33   bool doInitialization(Module &M);
34
35   // runOnBasicBlock - This method does the actual work of converting
36   // instructions over, assuming that the pass has already been initialized.
37   //
38   bool runOnBasicBlock(BasicBlock &BB);
39 };
40
41   RegisterOpt<RaiseAllocations>
42   X("raiseallocs", "Raise allocations from calls to instructions");
43 }  // end anonymous namespace
44
45
46 // createRaiseAllocationsPass - The interface to this file...
47 Pass *createRaiseAllocationsPass() {
48   return new RaiseAllocations();
49 }
50
51
52 bool RaiseAllocations::doInitialization(Module &M) {
53   // If the module has a symbol table, they might be referring to the malloc
54   // and free functions.  If this is the case, grab the method pointers that 
55   // the module is using.
56   //
57   // Lookup %malloc and %free in the symbol table, for later use.  If they
58   // don't exist, or are not external, we do not worry about converting calls
59   // to that function into the appropriate instruction.
60   //
61   const FunctionType *MallocType =   // Get the type for malloc
62     FunctionType::get(PointerType::get(Type::SByteTy),
63                     std::vector<const Type*>(1, Type::ULongTy), false);
64
65   const FunctionType *FreeType =     // Get the type for free
66     FunctionType::get(Type::VoidTy,
67                    std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
68                       false);
69
70   // Get Malloc and free prototypes if they exist!
71   MallocFunc = M.getFunction("malloc", MallocType);
72   FreeFunc   = M.getFunction("free"  , FreeType);
73
74   // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
75   // This handles the common declaration of: 'void *malloc(unsigned);'
76   if (MallocFunc == 0) {
77     MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
78                             std::vector<const Type*>(1, Type::UIntTy), false);
79     MallocFunc = M.getFunction("malloc", MallocType);
80   }
81
82   // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
83   // This handles the common declaration of: 'void *malloc();'
84   if (MallocFunc == 0) {
85     MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
86                                    std::vector<const Type*>(), true);
87     MallocFunc = M.getFunction("malloc", MallocType);
88   }
89
90   // Check to see if the prototype was forgotten, giving us void (...) * free
91   // This handles the common forward declaration of: 'void free();'
92   if (FreeFunc == 0) {
93     FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
94     FreeFunc = M.getFunction("free", FreeType);
95   }
96
97
98   // Don't mess with locally defined versions of these functions...
99   if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
100   if (FreeFunc && !FreeFunc->isExternal())     FreeFunc = 0;
101   return false;
102 }
103
104 // runOnBasicBlock - Process a basic block, fixing it up...
105 //
106 bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
107   bool Changed = false;
108   BasicBlock::InstListType &BIL = BB.getInstList();
109
110   for (BasicBlock::iterator BI = BB.begin(); BI != BB.end(); ++BI) {
111     Instruction *I = BI;
112
113     if (CallInst *CI = dyn_cast<CallInst>(I)) {
114       if (CI->getCalledValue() == MallocFunc) {      // Replace call to malloc?
115         Value *Source = CI->getOperand(1);
116         
117         // If no prototype was provided for malloc, we may need to cast the
118         // source size.
119         if (Source->getType() != Type::UIntTy)
120           Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", BI);
121
122         std::string Name(CI->getName()); CI->setName("");
123         BI = new MallocInst(Type::SByteTy, Source, Name, BI);
124         CI->replaceAllUsesWith(BI);
125         BIL.erase(I);
126         Changed = true;
127         ++NumRaised;
128       } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
129         // If no prototype was provided for free, we may need to cast the
130         // source pointer.  This should be really uncommon, but it's neccesary
131         // just in case we are dealing with wierd code like this:
132         //   free((long)ptr);
133         //
134         Value *Source = CI->getOperand(1);
135         if (!isa<PointerType>(Source->getType()))
136           Source = new CastInst(Source, PointerType::get(Type::SByteTy),
137                                 "FreePtrCast", BI);
138         BI = new FreeInst(Source, BI);
139         BIL.erase(I);
140         Changed = true;
141         ++NumRaised;
142       }
143     }
144   }
145
146   return Changed;
147 }