blob: a50cd8a5d11db2263b5272dfece787c213a9fe9a [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00002//
Chris Lattner77f791d2002-05-07 19:02:48 +00003// The LowerAllocations transformation is a target dependant tranformation
4// because it depends on the size of data types and alignment constraints.
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner3787ee62002-01-22 01:04:08 +00008#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +00009#include "llvm/Module.h"
Chris Lattner06be1802002-04-09 19:08:28 +000010#include "llvm/Function.h"
Chris Lattner77f791d2002-05-07 19:02:48 +000011#include "llvm/BasicBlock.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000012#include "llvm/DerivedTypes.h"
13#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000015#include "llvm/Constants.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000016#include "llvm/Pass.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000017#include "llvm/Target/TargetData.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000018#include "Support/StatisticReporter.h"
19
20static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
Chris Lattner7f74a562002-01-20 22:54:45 +000021using std::vector;
22
Chris Lattner04805fa2002-02-26 21:46:54 +000023namespace {
24
25// LowerAllocations - Turn malloc and free instructions into %malloc and %free
26// calls.
27//
28class LowerAllocations : public BasicBlockPass {
Chris Lattner57698e22002-03-26 18:01:55 +000029 Function *MallocFunc; // Functions in the module we are processing
30 Function *FreeFunc; // Initialized by doInitialization
Chris Lattner04805fa2002-02-26 21:46:54 +000031
32 const TargetData &DataLayout;
33public:
Chris Lattner6788f252002-07-23 18:06:30 +000034 LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner57698e22002-03-26 18:01:55 +000035 MallocFunc = FreeFunc = 0;
Chris Lattner04805fa2002-02-26 21:46:54 +000036 }
37
38 // doPassInitialization - For the lower allocations pass, this ensures that a
39 // module contains a declaration for a malloc and a free function.
40 //
Chris Lattner113f4f42002-06-25 16:13:24 +000041 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000042
43 // runOnBasicBlock - This method does the actual work of converting
44 // instructions over, assuming that the pass has already been initialized.
45 //
Chris Lattner113f4f42002-06-25 16:13:24 +000046 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner04805fa2002-02-26 21:46:54 +000047};
Chris Lattner77f791d2002-05-07 19:02:48 +000048}
Chris Lattner04805fa2002-02-26 21:46:54 +000049
Chris Lattner77f791d2002-05-07 19:02:48 +000050// createLowerAllocationsPass - Interface to this file...
51Pass *createLowerAllocationsPass(const TargetData &TD) {
52 return new LowerAllocations(TD);
53}
Chris Lattner37104aa2002-04-29 14:57:45 +000054
Chris Lattner6788f252002-07-23 18:06:30 +000055static RegisterPass<LowerAllocations>
56X("lowerallocs", "Lower allocations from instructions to calls (TD)",
57 createLowerAllocationsPass);
58
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000059
Chris Lattner0686e432002-01-21 07:31:50 +000060// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000061// module contains a declaration for a malloc and a free function.
62//
63// This function is always successful.
64//
Chris Lattner113f4f42002-06-25 16:13:24 +000065bool LowerAllocations::doInitialization(Module &M) {
Chris Lattnerc13563d2002-03-29 03:38:05 +000066 const FunctionType *MallocType =
67 FunctionType::get(PointerType::get(Type::SByteTy),
68 vector<const Type*>(1, Type::UIntTy), false);
69 const FunctionType *FreeType =
70 FunctionType::get(Type::VoidTy,
71 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
72 false);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000073
Chris Lattner113f4f42002-06-25 16:13:24 +000074 MallocFunc = M.getOrInsertFunction("malloc", MallocType);
75 FreeFunc = M.getOrInsertFunction("free" , FreeType);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000076
Chris Lattner77f791d2002-05-07 19:02:48 +000077 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000078}
79
Chris Lattnerd07471d2002-01-21 23:34:02 +000080// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000081// instructions over, assuming that the pass has already been initialized.
82//
Chris Lattner113f4f42002-06-25 16:13:24 +000083bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000084 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000085 assert(MallocFunc && FreeFunc && "Pass not initialized!");
86
87 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000088
89 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +000090 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
91 if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
92 BBIL.remove(I); // remove the malloc instr...
93
94 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +000095
96 // Get the number of bytes to be allocated for one element of the
97 // requested type...
98 unsigned Size = DataLayout.getTypeSize(AllocTy);
99
100 // malloc(type) becomes sbyte *malloc(constint)
101 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
102 if (MI->getNumOperands() && Size == 1) {
103 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
104 } else if (MI->getNumOperands()) {
105 // Multiply it by the array size if neccesary...
106 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
107 MallocArg);
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 I = ++BBIL.insert(I, cast<Instruction>(MallocArg));
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000109 }
Chris Lattnerd07471d2002-01-21 23:34:02 +0000110
111 // Create the call to Malloc...
Chris Lattner57698e22002-03-26 18:01:55 +0000112 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattnerd07471d2002-01-21 23:34:02 +0000113 vector<Value*>(1, MallocArg));
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 I = BBIL.insert(I, MCall);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000115
116 // Create a cast instruction to convert to the right type...
117 CastInst *MCast = new CastInst(MCall, MI->getType());
Chris Lattner113f4f42002-06-25 16:13:24 +0000118 I = BBIL.insert(++I, MCast);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000119
120 // Replace all uses of the old malloc inst with the cast inst
121 MI->replaceAllUsesWith(MCast);
122 delete MI; // Delete the malloc inst
123 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000124 ++NumLowered;
Chris Lattner113f4f42002-06-25 16:13:24 +0000125 } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
126 BBIL.remove(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000127
128 // Cast the argument to free into a ubyte*...
129 CastInst *MCast = new CastInst(FI->getOperand(0),
130 PointerType::get(Type::UByteTy));
Chris Lattner113f4f42002-06-25 16:13:24 +0000131 I = ++BBIL.insert(I, MCast);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000132
133 // Insert a call to the free function...
Chris Lattner113f4f42002-06-25 16:13:24 +0000134 CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast));
135 I = BBIL.insert(I, FCall);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000136
137 // Delete the old free instruction
138 delete FI;
139 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000140 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000141 }
142 }
143
Chris Lattner6fea0322001-10-18 05:27:33 +0000144 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000145}