blob: ff7a3674ec1d2fa1cb237f4260b822e54aa08cb7 [file] [log] [blame]
Chris Lattner4f0f0972002-01-22 00:13:51 +00001//===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00002//
Chris Lattner4f0f0972002-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 Lattner1bb5f8e2001-10-15 17:31:51 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattner3787ee62002-01-22 01:04:08 +000010#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000011#include "llvm/Target/TargetData.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +000012#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/iMemory.h"
15#include "llvm/iOther.h"
16#include "llvm/SymbolTable.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000017#include "llvm/ConstantVals.h"
Chris Lattner4f0f0972002-01-22 00:13:51 +000018#include "TransformInternals.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000019using std::vector;
20
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000021
Chris Lattner0686e432002-01-21 07:31:50 +000022// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000023// module contains a declaration for a malloc and a free function.
24//
25// This function is always successful.
26//
Chris Lattner0686e432002-01-21 07:31:50 +000027bool LowerAllocations::doInitialization(Module *M) {
Chris Lattner6fea0322001-10-18 05:27:33 +000028 bool Changed = false;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000029 const MethodType *MallocType =
Chris Lattner7e6a0d82001-10-31 06:36:23 +000030 MethodType::get(PointerType::get(Type::SByteTy),
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000031 vector<const Type*>(1, Type::UIntTy), false);
32
33 SymbolTable *SymTab = M->getSymbolTableSure();
34
35 // Check for a definition of malloc
36 if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
37 MallocMeth = cast<Method>(V); // Yup, got it
38 } else { // Nope, add one
Chris Lattner25b151d2001-12-04 18:01:49 +000039 M->getMethodList().push_back(MallocMeth = new Method(MallocType, false,
40 "malloc"));
Chris Lattner6fea0322001-10-18 05:27:33 +000041 Changed = true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000042 }
43
44 const MethodType *FreeType =
45 MethodType::get(Type::VoidTy,
Chris Lattner7e6a0d82001-10-31 06:36:23 +000046 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000047 false);
48
49 // Check for a definition of free
50 if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
51 FreeMeth = cast<Method>(V); // Yup, got it
52 } else { // Nope, add one
Chris Lattner25b151d2001-12-04 18:01:49 +000053 M->getMethodList().push_back(FreeMeth = new Method(FreeType, false,"free"));
Chris Lattner6fea0322001-10-18 05:27:33 +000054 Changed = true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000055 }
56
Chris Lattnerd07471d2002-01-21 23:34:02 +000057 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000058}
59
Chris Lattnerd07471d2002-01-21 23:34:02 +000060// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000061// instructions over, assuming that the pass has already been initialized.
62//
Chris Lattnerd07471d2002-01-21 23:34:02 +000063bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000064 bool Changed = false;
Chris Lattnerd07471d2002-01-21 23:34:02 +000065 assert(MallocMeth && FreeMeth && BB && "Pass not initialized!");
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000066
67 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattnerd07471d2002-01-21 23:34:02 +000068 for (unsigned i = 0; i < BB->size(); ++i) {
69 BasicBlock::InstListType &BBIL = BB->getInstList();
70 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
71 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000072
Chris Lattnerd07471d2002-01-21 23:34:02 +000073 const Type *AllocTy =cast<PointerType>(MI->getType())->getElementType();
74
75 // Get the number of bytes to be allocated for one element of the
76 // requested type...
77 unsigned Size = DataLayout.getTypeSize(AllocTy);
78
79 // malloc(type) becomes sbyte *malloc(constint)
80 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
81 if (MI->getNumOperands() && Size == 1) {
82 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
83 } else if (MI->getNumOperands()) {
84 // Multiply it by the array size if neccesary...
85 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
86 MallocArg);
87 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000088 }
Chris Lattnerd07471d2002-01-21 23:34:02 +000089
90 // Create the call to Malloc...
91 CallInst *MCall = new CallInst(MallocMeth,
92 vector<Value*>(1, MallocArg));
93 BBIL.insert(BBIL.begin()+i, MCall);
94
95 // Create a cast instruction to convert to the right type...
96 CastInst *MCast = new CastInst(MCall, MI->getType());
97 BBIL.insert(BBIL.begin()+i+1, MCast);
98
99 // Replace all uses of the old malloc inst with the cast inst
100 MI->replaceAllUsesWith(MCast);
101 delete MI; // Delete the malloc inst
102 Changed = true;
103 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
104 BBIL.remove(BB->getInstList().begin()+i);
105
106 // Cast the argument to free into a ubyte*...
107 CastInst *MCast = new CastInst(FI->getOperand(0),
108 PointerType::get(Type::UByteTy));
109 BBIL.insert(BBIL.begin()+i, MCast);
110
111 // Insert a call to the free function...
112 CallInst *FCall = new CallInst(FreeMeth,
113 vector<Value*>(1, MCast));
114 BBIL.insert(BBIL.begin()+i+1, FCall);
115
116 // Delete the old free instruction
117 delete FI;
118 Changed = true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000119 }
120 }
121
Chris Lattner6fea0322001-10-18 05:27:33 +0000122 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000123}
124
Chris Lattner4f0f0972002-01-22 00:13:51 +0000125bool RaiseAllocations::doInitialization(Module *M) {
126 SymbolTable *ST = M->getSymbolTable();
127 if (!ST) return false;
128
129 // If the module has a symbol table, they might be referring to the malloc
130 // and free functions. If this is the case, grab the method pointers that
131 // the module is using.
132 //
133 // Lookup %malloc and %free in the symbol table, for later use. If they
134 // don't exist, or are not external, we do not worry about converting calls
135 // to that function into the appropriate instruction.
136 //
137 const PointerType *MallocType = // Get the type for malloc
138 PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
139 vector<const Type*>(1, Type::UIntTy), false));
140 MallocMeth = cast_or_null<Method>(ST->lookup(MallocType, "malloc"));
141 if (MallocMeth && !MallocMeth->isExternal())
142 MallocMeth = 0; // Don't mess with locally defined versions of the fn
143
144 const PointerType *FreeType = // Get the type for free
145 PointerType::get(MethodType::get(Type::VoidTy,
146 vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
147 FreeMeth = cast_or_null<Method>(ST->lookup(FreeType, "free"));
148 if (FreeMeth && !FreeMeth->isExternal())
149 FreeMeth = 0; // Don't mess with locally defined versions of the fn
150
151 return false;
152}
153
154// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
155//
156bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
157 bool Changed = false;
158 BasicBlock::InstListType &BIL = BB->getInstList();
159
160 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
161 Instruction *I = *BI;
162
163 if (CallInst *CI = dyn_cast<CallInst>(I)) {
164 if (CI->getCalledValue() == MallocMeth) { // Replace call to malloc?
165 const Type *PtrSByte = PointerType::get(Type::SByteTy);
166 MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
167 CI->getName());
168 CI->setName("");
Chris Lattner70090072002-01-22 03:30:06 +0000169 ReplaceInstWithInst(BIL, BI, MallocI);
Chris Lattner4f0f0972002-01-22 00:13:51 +0000170 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}