blob: 7e0960dbf72e024ba2c3d7035357bf5f20eb81ff [file] [log] [blame]
Chris Lattnerade686e2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Chris Lattner1bffea02001-10-15 17:31:51 +00002//
Chris Lattnerade686e2002-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 Lattner1bffea02001-10-15 17:31:51 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattnere9ee3e52002-07-23 22:04:17 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattner793c6b82002-01-31 00:45:11 +00009#include "llvm/Module.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000010#include "llvm/Function.h"
Chris Lattnerade686e2002-05-07 19:02:48 +000011#include "llvm/BasicBlock.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000012#include "llvm/DerivedTypes.h"
13#include "llvm/iMemory.h"
14#include "llvm/iOther.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000015#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000016#include "llvm/Pass.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000017#include "llvm/Target/TargetData.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000018#include "Support/StatisticReporter.h"
19
20static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
22
Chris Lattnerbd0ef772002-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 Lattner79df7c02002-03-26 18:01:55 +000029 Function *MallocFunc; // Functions in the module we are processing
30 Function *FreeFunc; // Initialized by doInitialization
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031
32 const TargetData &DataLayout;
33public:
Chris Lattneraf41a122002-07-23 18:06:30 +000034 LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner79df7c02002-03-26 18:01:55 +000035 MallocFunc = FreeFunc = 0;
Chris Lattnerbd0ef772002-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 Lattner7e708292002-06-25 16:13:24 +000041 bool doInitialization(Module &M);
Chris Lattnerbd0ef772002-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 Lattner7e708292002-06-25 16:13:24 +000046 bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000047};
Chris Lattnerade686e2002-05-07 19:02:48 +000048}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000049
Chris Lattnerade686e2002-05-07 19:02:48 +000050// createLowerAllocationsPass - Interface to this file...
51Pass *createLowerAllocationsPass(const TargetData &TD) {
52 return new LowerAllocations(TD);
53}
Chris Lattner96c466b2002-04-29 14:57:45 +000054
Chris Lattnera6275cc2002-07-26 21:12:46 +000055static RegisterOpt<LowerAllocations>
Chris Lattneraf41a122002-07-23 18:06:30 +000056X("lowerallocs", "Lower allocations from instructions to calls (TD)",
57 createLowerAllocationsPass);
58
Chris Lattner1bffea02001-10-15 17:31:51 +000059
Chris Lattnerf4de63f2002-01-21 07:31:50 +000060// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-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 Lattner7e708292002-06-25 16:13:24 +000065bool LowerAllocations::doInitialization(Module &M) {
Chris Lattnerbe591a72002-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 Lattner1bffea02001-10-15 17:31:51 +000073
Chris Lattner7e708292002-06-25 16:13:24 +000074 MallocFunc = M.getOrInsertFunction("malloc", MallocType);
75 FreeFunc = M.getOrInsertFunction("free" , FreeType);
Chris Lattner1bffea02001-10-15 17:31:51 +000076
Chris Lattnerade686e2002-05-07 19:02:48 +000077 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000078}
79
Chris Lattner84453722002-01-21 23:34:02 +000080// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000081// instructions over, assuming that the pass has already been initialized.
82//
Chris Lattner7e708292002-06-25 16:13:24 +000083bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000084 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000085 assert(MallocFunc && FreeFunc && "Pass not initialized!");
86
87 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +000088
89 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-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 Lattner84453722002-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 Lattner7e708292002-06-25 16:13:24 +0000108 I = ++BBIL.insert(I, cast<Instruction>(MallocArg));
Chris Lattner1bffea02001-10-15 17:31:51 +0000109 }
Chris Lattner84453722002-01-21 23:34:02 +0000110
111 // Create the call to Malloc...
Chris Lattner79df7c02002-03-26 18:01:55 +0000112 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattner84453722002-01-21 23:34:02 +0000113 vector<Value*>(1, MallocArg));
Chris Lattner7e708292002-06-25 16:13:24 +0000114 I = BBIL.insert(I, MCall);
Chris Lattner84453722002-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 Lattner7e708292002-06-25 16:13:24 +0000118 I = BBIL.insert(++I, MCast);
Chris Lattner84453722002-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 Lattner3dec1f22002-05-10 15:38:35 +0000124 ++NumLowered;
Chris Lattner7e708292002-06-25 16:13:24 +0000125 } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
126 BBIL.remove(I);
Chris Lattner84453722002-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 Lattner7e708292002-06-25 16:13:24 +0000131 I = ++BBIL.insert(I, MCast);
Chris Lattner84453722002-01-21 23:34:02 +0000132
133 // Insert a call to the free function...
Chris Lattner7e708292002-06-25 16:13:24 +0000134 CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast));
135 I = BBIL.insert(I, FCall);
Chris Lattner84453722002-01-21 23:34:02 +0000136
137 // Delete the old free instruction
138 delete FI;
139 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000140 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000141 }
142 }
143
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000144 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000145}