blob: a1ad2c2918fb39771a31359cc6165ffc15150c8c [file] [log] [blame]
Chris Lattner03453a02002-05-07 19:04:39 +00001//===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
Chris Lattner77f791d2002-05-07 19:02:48 +00002//
Chris Lattner03453a02002-05-07 19:04:39 +00003// This file defines the RaiseAllocations pass which convert malloc and free
4// calls to malloc and free instructions.
Chris Lattner77f791d2002-05-07 19:02:48 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner42706e42002-07-23 22:04:17 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattner77f791d2002-05-07 19:02:48 +00009#include "llvm/Transforms/Utils/BasicBlockUtils.h"
10#include "llvm/Module.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000011#include "llvm/DerivedTypes.h"
12#include "llvm/iMemory.h"
13#include "llvm/iOther.h"
14#include "llvm/Pass.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000015#include "Support/Statistic.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000016
17namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000018 Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
Chris Lattner77f791d2002-05-07 19:02:48 +000019
Chris Lattnerbf3a0992002-10-01 22:38:41 +000020 // RaiseAllocations - Turn %malloc and %free calls into the appropriate
21 // instruction.
Chris Lattner77f791d2002-05-07 19:02:48 +000022 //
Chris Lattnerbf3a0992002-10-01 22:38:41 +000023 class RaiseAllocations : public BasicBlockPass {
24 Function *MallocFunc; // Functions in the module we are processing
25 Function *FreeFunc; // Initialized by doPassInitializationVirt
26 public:
27 RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
28
29 // doPassInitialization - For the raise allocations pass, this finds a
30 // declaration for malloc and free if they exist.
31 //
32 bool doInitialization(Module &M);
33
34 // runOnBasicBlock - This method does the actual work of converting
35 // instructions over, assuming that the pass has already been initialized.
36 //
37 bool runOnBasicBlock(BasicBlock &BB);
38 };
39
Chris Lattnerc8b70922002-07-26 21:12:46 +000040 RegisterOpt<RaiseAllocations>
41 X("raiseallocs", "Raise allocations from calls to instructions");
Chris Lattner77f791d2002-05-07 19:02:48 +000042} // end anonymous namespace
43
44
45// createRaiseAllocationsPass - The interface to this file...
46Pass *createRaiseAllocationsPass() {
47 return new RaiseAllocations();
48}
49
50
Chris Lattner113f4f42002-06-25 16:13:24 +000051bool RaiseAllocations::doInitialization(Module &M) {
Chris Lattner77f791d2002-05-07 19:02:48 +000052 // If the module has a symbol table, they might be referring to the malloc
53 // and free functions. If this is the case, grab the method pointers that
54 // the module is using.
55 //
56 // Lookup %malloc and %free in the symbol table, for later use. If they
57 // don't exist, or are not external, we do not worry about converting calls
58 // to that function into the appropriate instruction.
59 //
60 const FunctionType *MallocType = // Get the type for malloc
61 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattnerdfe04182002-07-18 00:18:01 +000062 std::vector<const Type*>(1, Type::ULongTy), false);
Chris Lattner77f791d2002-05-07 19:02:48 +000063
64 const FunctionType *FreeType = // Get the type for free
65 FunctionType::get(Type::VoidTy,
66 std::vector<const Type*>(1, PointerType::get(Type::SByteTy)),
67 false);
68
Chris Lattnerdfe04182002-07-18 00:18:01 +000069 // Get Malloc and free prototypes if they exist!
Chris Lattner113f4f42002-06-25 16:13:24 +000070 MallocFunc = M.getFunction("malloc", MallocType);
71 FreeFunc = M.getFunction("free" , FreeType);
Chris Lattner77f791d2002-05-07 19:02:48 +000072
Chris Lattnerdfe04182002-07-18 00:18:01 +000073 // Check to see if the prototype is wrong, giving us sbyte*(uint) * malloc
74 // This handles the common declaration of: 'void *malloc(unsigned);'
75 if (MallocFunc == 0) {
76 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
77 std::vector<const Type*>(1, Type::UIntTy), false);
78 MallocFunc = M.getFunction("malloc", MallocType);
79 }
80
Chris Lattnere3da2982002-05-24 20:29:18 +000081 // Check to see if the prototype is missing, giving us sbyte*(...) * malloc
Chris Lattnerdfe04182002-07-18 00:18:01 +000082 // This handles the common declaration of: 'void *malloc();'
Chris Lattnere3da2982002-05-24 20:29:18 +000083 if (MallocFunc == 0) {
84 MallocType = FunctionType::get(PointerType::get(Type::SByteTy),
85 std::vector<const Type*>(), true);
Chris Lattner113f4f42002-06-25 16:13:24 +000086 MallocFunc = M.getFunction("malloc", MallocType);
Chris Lattnere3da2982002-05-24 20:29:18 +000087 }
88
89 // Check to see if the prototype was forgotten, giving us void (...) * free
90 // This handles the common forward declaration of: 'void free();'
91 if (FreeFunc == 0) {
92 FreeType = FunctionType::get(Type::VoidTy, std::vector<const Type*>(),true);
Chris Lattner113f4f42002-06-25 16:13:24 +000093 FreeFunc = M.getFunction("free", FreeType);
Chris Lattnere3da2982002-05-24 20:29:18 +000094 }
95
Chris Lattner603e0072003-08-11 15:05:08 +000096 // One last try, check to see if we can find free as 'int (...)* free'. This
97 // handles the case where NOTHING was declared.
98 if (FreeFunc == 0) {
99 FreeType = FunctionType::get(Type::IntTy, std::vector<const Type*>(),true);
100 FreeFunc = M.getFunction("free", FreeType);
101 }
102
Chris Lattnere3da2982002-05-24 20:29:18 +0000103
Chris Lattner77f791d2002-05-07 19:02:48 +0000104 // Don't mess with locally defined versions of these functions...
105 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
106 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
107 return false;
108}
109
Chris Lattner03453a02002-05-07 19:04:39 +0000110// runOnBasicBlock - Process a basic block, fixing it up...
Chris Lattner77f791d2002-05-07 19:02:48 +0000111//
Chris Lattner113f4f42002-06-25 16:13:24 +0000112bool RaiseAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner77f791d2002-05-07 19:02:48 +0000113 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 BasicBlock::InstListType &BIL = BB.getInstList();
Chris Lattner77f791d2002-05-07 19:02:48 +0000115
Chris Lattnera239e682002-09-10 22:38:47 +0000116 for (BasicBlock::iterator BI = BB.begin(); BI != BB.end(); ++BI) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000117 Instruction *I = BI;
Chris Lattner77f791d2002-05-07 19:02:48 +0000118
119 if (CallInst *CI = dyn_cast<CallInst>(I)) {
120 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
Chris Lattnere3da2982002-05-24 20:29:18 +0000121 Value *Source = CI->getOperand(1);
122
123 // If no prototype was provided for malloc, we may need to cast the
124 // source size.
Chris Lattnera239e682002-09-10 22:38:47 +0000125 if (Source->getType() != Type::UIntTy)
126 Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", BI);
Chris Lattnere3da2982002-05-24 20:29:18 +0000127
Chris Lattnera239e682002-09-10 22:38:47 +0000128 std::string Name(CI->getName()); CI->setName("");
Chris Lattner322bf4f2002-09-13 22:28:45 +0000129 BI = new MallocInst(Type::SByteTy, Source, Name, BI);
Chris Lattner9674b862002-09-10 23:31:12 +0000130 CI->replaceAllUsesWith(BI);
Chris Lattnera239e682002-09-10 22:38:47 +0000131 BIL.erase(I);
Chris Lattner77f791d2002-05-07 19:02:48 +0000132 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000133 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000134 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
Chris Lattnere3da2982002-05-24 20:29:18 +0000135 // If no prototype was provided for free, we may need to cast the
Misha Brukman7eb05a12003-08-18 14:43:39 +0000136 // source pointer. This should be really uncommon, but it's necessary
Chris Lattnere3da2982002-05-24 20:29:18 +0000137 // just in case we are dealing with wierd code like this:
138 // free((long)ptr);
139 //
140 Value *Source = CI->getOperand(1);
Chris Lattnera239e682002-09-10 22:38:47 +0000141 if (!isa<PointerType>(Source->getType()))
142 Source = new CastInst(Source, PointerType::get(Type::SByteTy),
143 "FreePtrCast", BI);
144 BI = new FreeInst(Source, BI);
145 BIL.erase(I);
Chris Lattner77f791d2002-05-07 19:02:48 +0000146 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000147 ++NumRaised;
Chris Lattner77f791d2002-05-07 19:02:48 +0000148 }
149 }
Chris Lattner77f791d2002-05-07 19:02:48 +0000150 }
151
152 return Changed;
153}