blob: a0719e12bf9b5cf2dd85bd30b2805c3189f7b059 [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
2//
3// This file defines two passes that convert malloc and free instructions to
4// calls to and from %malloc & %free function calls. The LowerAllocations
5// transformation is a target dependant tranformation because it depends on the
6// size of data types and alignment constraints.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Transforms/ChangeAllocations.h"
11#include "llvm/Transforms/Utils/BasicBlockUtils.h"
12#include "llvm/Module.h"
13#include "llvm/Function.h"
14#include "llvm/DerivedTypes.h"
15#include "llvm/iMemory.h"
16#include "llvm/iOther.h"
17#include "llvm/Pass.h"
18
19namespace {
20
21// RaiseAllocations - Turn %malloc and %free calls into the appropriate
22// instruction.
23//
24class RaiseAllocations : public BasicBlockPass {
25 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doPassInitializationVirt
27public:
28 inline RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
29
30 const char *getPassName() const { return "Raise Allocations"; }
31
32 // doPassInitialization - For the raise allocations pass, this finds a
33 // declaration for malloc and free if they exist.
34 //
35 bool doInitialization(Module *M);
36
37 // runOnBasicBlock - This method does the actual work of converting
38 // instructions over, assuming that the pass has already been initialized.
39 //
40 bool runOnBasicBlock(BasicBlock *BB);
41};
42
43} // end anonymous namespace
44
45
46// createRaiseAllocationsPass - The interface to this file...
47Pass *createRaiseAllocationsPass() {
48 return new RaiseAllocations();
49}
50
51
52bool 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::UIntTy), 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 MallocFunc = M->getFunction("malloc", MallocType);
71 FreeFunc = M->getFunction("free" , FreeType);
72
73 // Don't mess with locally defined versions of these functions...
74 if (MallocFunc && !MallocFunc->isExternal()) MallocFunc = 0;
75 if (FreeFunc && !FreeFunc->isExternal()) FreeFunc = 0;
76 return false;
77}
78
79// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
80//
81bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
82 bool Changed = false;
83 BasicBlock::InstListType &BIL = BB->getInstList();
84
85 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
86 Instruction *I = *BI;
87
88 if (CallInst *CI = dyn_cast<CallInst>(I)) {
89 if (CI->getCalledValue() == MallocFunc) { // Replace call to malloc?
90 const Type *PtrSByte = PointerType::get(Type::SByteTy);
91 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
92 CI->getName());
93 CI->setName("");
94 ReplaceInstWithInst(BIL, BI, MallocI);
95 Changed = true;
96 continue; // Skip the ++BI
97 } else if (CI->getCalledValue() == FreeFunc) { // Replace call to free?
98 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
99 Changed = true;
100 continue; // Skip the ++BI
101 }
102 }
103
104 ++BI;
105 }
106
107 return Changed;
108}