blob: 041d89fab17691ad35179ae9c4bdfedbd7d3d5f9 [file] [log] [blame]
Chris Lattner5048c3b2002-01-22 00:13:51 +00001//===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
Chris Lattner1bffea02001-10-15 17:31:51 +00002//
Chris Lattner5048c3b2002-01-22 00:13:51 +00003// 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.
Chris Lattner1bffea02001-10-15 17:31:51 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattnerd7db8632002-01-22 01:04:08 +000010#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000011#include "llvm/Target/TargetData.h"
12#include "llvm/DerivedTypes.h"
13#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
15#include "llvm/SymbolTable.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000016#include "llvm/ConstantVals.h"
Chris Lattner5048c3b2002-01-22 00:13:51 +000017#include "TransformInternals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::vector;
19
Chris Lattner1bffea02001-10-15 17:31:51 +000020
Chris Lattnerf4de63f2002-01-21 07:31:50 +000021// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000022// module contains a declaration for a malloc and a free function.
23//
24// This function is always successful.
25//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000026bool LowerAllocations::doInitialization(Module *M) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000027 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000028 const MethodType *MallocType =
Chris Lattner7ef65592001-10-31 06:36:23 +000029 MethodType::get(PointerType::get(Type::SByteTy),
Chris Lattner1bffea02001-10-15 17:31:51 +000030 vector<const Type*>(1, Type::UIntTy), false);
31
32 SymbolTable *SymTab = M->getSymbolTableSure();
33
34 // Check for a definition of malloc
35 if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
36 MallocMeth = cast<Method>(V); // Yup, got it
37 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000038 M->getMethodList().push_back(MallocMeth = new Method(MallocType, false,
39 "malloc"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000040 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000041 }
42
43 const MethodType *FreeType =
44 MethodType::get(Type::VoidTy,
Chris Lattner7ef65592001-10-31 06:36:23 +000045 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bffea02001-10-15 17:31:51 +000046 false);
47
48 // Check for a definition of free
49 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
50 FreeMeth = cast<Method>(V); // Yup, got it
51 } else { // Nope, add one
Chris Lattnered4feac2001-12-04 18:01:49 +000052 M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free"));
Chris Lattner42c9c2c2001-10-18 05:27:33 +000053 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +000054 }
55
Chris Lattner84453722002-01-21 23:34:02 +000056 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +000057}
58
Chris Lattner84453722002-01-21 23:34:02 +000059// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000060// instructions over, assuming that the pass has already been initialized.
61//
Chris Lattner84453722002-01-21 23:34:02 +000062bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000063 bool Changed = false;
Chris Lattner84453722002-01-21 23:34:02 +000064 assert(MallocMeth && FreeMeth && BB && "Pass not initialized!");
Chris Lattner1bffea02001-10-15 17:31:51 +000065
66 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner84453722002-01-21 23:34:02 +000067 for (unsigned i = 0; i < BB->size(); ++i) {
68 BasicBlock::InstListType &BBIL = BB->getInstList();
69 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
70 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
Chris Lattner1bffea02001-10-15 17:31:51 +000071
Chris Lattner84453722002-01-21 23:34:02 +000072 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
73
74 // Get the number of bytes to be allocated for one element of the
75 // requested type...
76 unsigned Size = DataLayout.getTypeSize(AllocTy);
77
78 // malloc(type) becomes sbyte *malloc(constint)
79 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
80 if (MI->getNumOperands() && Size == 1) {
81 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
82 } else if (MI->getNumOperands()) {
83 // Multiply it by the array size if neccesary...
84 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
85 MallocArg);
86 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
Chris Lattner1bffea02001-10-15 17:31:51 +000087 }
Chris Lattner84453722002-01-21 23:34:02 +000088
89 // Create the call to Malloc...
90 CallInst *MCall = new CallInst(MallocMeth,
91 vector<Value*>(1, MallocArg));
92 BBIL.insert(BBIL.begin()+i, MCall);
93
94 // Create a cast instruction to convert to the right type...
95 CastInst *MCast = new CastInst(MCall, MI->getType());
96 BBIL.insert(BBIL.begin()+i+1, MCast);
97
98 // Replace all uses of the old malloc inst with the cast inst
99 MI->replaceAllUsesWith(MCast);
100 delete MI; // Delete the malloc inst
101 Changed = true;
102 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
103 BBIL.remove(BB->getInstList().begin()+i);
104
105 // Cast the argument to free into a ubyte*...
106 CastInst *MCast = new CastInst(FI->getOperand(0),
107 PointerType::get(Type::UByteTy));
108 BBIL.insert(BBIL.begin()+i, MCast);
109
110 // Insert a call to the free function...
111 CallInst *FCall = new CallInst(FreeMeth,
112 vector<Value*>(1, MCast));
113 BBIL.insert(BBIL.begin()+i+1, FCall);
114
115 // Delete the old free instruction
116 delete FI;
117 Changed = true;
Chris Lattner1bffea02001-10-15 17:31:51 +0000118 }
119 }
120
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000121 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000122}
123
Chris Lattner5048c3b2002-01-22 00:13:51 +0000124bool RaiseAllocations::doInitialization(Module *M) {
125 SymbolTable *ST = M->getSymbolTable();
126 if (!ST) return false;
127
128 // If the module has a symbol table, they might be referring to the malloc
129 // and free functions. If this is the case, grab the method pointers that
130 // the module is using.
131 //
132 // Lookup %malloc and %free in the symbol table, for later use. If they
133 // don't exist, or are not external, we do not worry about converting calls
134 // to that function into the appropriate instruction.
135 //
136 const PointerType *MallocType = // Get the type for malloc
137 PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
138 vector<const Type*>(1, Type::UIntTy), false));
139 MallocMeth = cast_or_null<Method>(ST->lookup(MallocType, "malloc"));
140 if (MallocMeth && !MallocMeth->isExternal())
141 MallocMeth = 0; // Don't mess with locally defined versions of the fn
142
143 const PointerType *FreeType = // Get the type for free
144 PointerType::get(MethodType::get(Type::VoidTy,
145 vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
146 FreeMeth = cast_or_null<Method>(ST->lookup(FreeType, "free"));
147 if (FreeMeth && !FreeMeth->isExternal())
148 FreeMeth = 0; // Don't mess with locally defined versions of the fn
149
150 return false;
151}
152
153// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
154//
155bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
156 bool Changed = false;
157 BasicBlock::InstListType &BIL = BB->getInstList();
158
159 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
160 Instruction *I = *BI;
161
162 if (CallInst *CI = dyn_cast<CallInst>(I)) {
163 if (CI->getCalledValue() == MallocMeth) { // Replace call to malloc?
164 const Type *PtrSByte = PointerType::get(Type::SByteTy);
165 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
166 CI->getName());
167 CI->setName("");
168 BI = BIL.insert(BI, MallocI)+1;
169 ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte));
170 Changed = true;
171 continue; // Skip the ++BI
172 } else if (CI->getCalledValue() == FreeMeth) { // Replace call to free?
173 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
174 Changed = true;
175 continue; // Skip the ++BI
176 }
177 }
178
179 ++BI;
180 }
181
182 return Changed;
183}