blob: 001028eb21ae60918389219d301d4eac3126bcde [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 Lattner42706e42002-07-23 22:04:17 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +00009#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000010#include "llvm/DerivedTypes.h"
11#include "llvm/iMemory.h"
12#include "llvm/iOther.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000013#include "llvm/Constants.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000014#include "llvm/Pass.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000015#include "llvm/Target/TargetData.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000016#include "Support/StatisticReporter.h"
17
18static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
Chris Lattner7f74a562002-01-20 22:54:45 +000019using std::vector;
20
Chris Lattner04805fa2002-02-26 21:46:54 +000021namespace {
22
23// LowerAllocations - Turn malloc and free instructions into %malloc and %free
24// calls.
25//
26class LowerAllocations : public BasicBlockPass {
Chris Lattner57698e22002-03-26 18:01:55 +000027 Function *MallocFunc; // Functions in the module we are processing
28 Function *FreeFunc; // Initialized by doInitialization
Chris Lattner04805fa2002-02-26 21:46:54 +000029
30 const TargetData &DataLayout;
31public:
Chris Lattner6788f252002-07-23 18:06:30 +000032 LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner57698e22002-03-26 18:01:55 +000033 MallocFunc = FreeFunc = 0;
Chris Lattner04805fa2002-02-26 21:46:54 +000034 }
35
36 // doPassInitialization - For the lower allocations pass, this ensures that a
37 // module contains a declaration for a malloc and a free function.
38 //
Chris Lattner113f4f42002-06-25 16:13:24 +000039 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000040
41 // runOnBasicBlock - This method does the actual work of converting
42 // instructions over, assuming that the pass has already been initialized.
43 //
Chris Lattner113f4f42002-06-25 16:13:24 +000044 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattner04805fa2002-02-26 21:46:54 +000045};
Chris Lattner77f791d2002-05-07 19:02:48 +000046}
Chris Lattner04805fa2002-02-26 21:46:54 +000047
Chris Lattner77f791d2002-05-07 19:02:48 +000048// createLowerAllocationsPass - Interface to this file...
49Pass *createLowerAllocationsPass(const TargetData &TD) {
50 return new LowerAllocations(TD);
51}
Chris Lattner37104aa2002-04-29 14:57:45 +000052
Chris Lattnerc8b70922002-07-26 21:12:46 +000053static RegisterOpt<LowerAllocations>
Chris Lattner6788f252002-07-23 18:06:30 +000054X("lowerallocs", "Lower allocations from instructions to calls (TD)",
55 createLowerAllocationsPass);
56
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000057
Chris Lattner0686e432002-01-21 07:31:50 +000058// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000059// module contains a declaration for a malloc and a free function.
60//
61// This function is always successful.
62//
Chris Lattner113f4f42002-06-25 16:13:24 +000063bool LowerAllocations::doInitialization(Module &M) {
Chris Lattnerc13563d2002-03-29 03:38:05 +000064 const FunctionType *MallocType =
65 FunctionType::get(PointerType::get(Type::SByteTy),
66 vector<const Type*>(1, Type::UIntTy), false);
67 const FunctionType *FreeType =
68 FunctionType::get(Type::VoidTy,
69 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
70 false);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000071
Chris Lattner113f4f42002-06-25 16:13:24 +000072 MallocFunc = M.getOrInsertFunction("malloc", MallocType);
73 FreeFunc = M.getOrInsertFunction("free" , FreeType);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000074
Chris Lattner77f791d2002-05-07 19:02:48 +000075 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000076}
77
Chris Lattnerd07471d2002-01-21 23:34:02 +000078// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000079// instructions over, assuming that the pass has already been initialized.
80//
Chris Lattner113f4f42002-06-25 16:13:24 +000081bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000082 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000083 assert(MallocFunc && FreeFunc && "Pass not initialized!");
84
85 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000086
87 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +000088 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
89 if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +000090 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +000091
92 // Get the number of bytes to be allocated for one element of the
93 // requested type...
94 unsigned Size = DataLayout.getTypeSize(AllocTy);
95
96 // malloc(type) becomes sbyte *malloc(constint)
97 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
98 if (MI->getNumOperands() && Size == 1) {
99 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
100 } else if (MI->getNumOperands()) {
101 // Multiply it by the array size if neccesary...
Chris Lattnera239e682002-09-10 22:38:47 +0000102 MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
103 MallocArg, "", I);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000104 }
Chris Lattnerd07471d2002-01-21 23:34:02 +0000105
106 // Create the call to Malloc...
Chris Lattner57698e22002-03-26 18:01:55 +0000107 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattnera239e682002-09-10 22:38:47 +0000108 vector<Value*>(1, MallocArg), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000109
110 // Create a cast instruction to convert to the right type...
Chris Lattnera239e682002-09-10 22:38:47 +0000111 CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000112
113 // Replace all uses of the old malloc inst with the cast inst
114 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000115 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000116 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000117 ++NumLowered;
Chris Lattner113f4f42002-06-25 16:13:24 +0000118 } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
Chris Lattnerd07471d2002-01-21 23:34:02 +0000119 // Cast the argument to free into a ubyte*...
120 CastInst *MCast = new CastInst(FI->getOperand(0),
Chris Lattnera239e682002-09-10 22:38:47 +0000121 PointerType::get(Type::UByteTy), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000122
123 // Insert a call to the free function...
Chris Lattnera239e682002-09-10 22:38:47 +0000124 CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000125
126 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000127 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000128 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000129 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000130 }
131 }
132
Chris Lattner6fea0322001-10-18 05:27:33 +0000133 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000134}