blob: e5bf88fc00caa70bd65abe4956739d57836b2b3a [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 Lattner7f74a562002-01-20 22:54:45 +000018using std::vector;
19
Chris Lattner04805fa2002-02-26 21:46:54 +000020namespace {
21
22// LowerAllocations - Turn malloc and free instructions into %malloc and %free
23// calls.
24//
25class LowerAllocations : public BasicBlockPass {
Chris Lattner57698e22002-03-26 18:01:55 +000026 Function *MallocFunc; // Functions in the module we are processing
27 Function *FreeFunc; // Initialized by doInitialization
Chris Lattner04805fa2002-02-26 21:46:54 +000028
29 const TargetData &DataLayout;
30public:
31 inline LowerAllocations(const TargetData &TD) : DataLayout(TD) {
Chris Lattner57698e22002-03-26 18:01:55 +000032 MallocFunc = FreeFunc = 0;
Chris Lattner04805fa2002-02-26 21:46:54 +000033 }
34
Chris Lattner37104aa2002-04-29 14:57:45 +000035 const char *getPassName() const { return "Lower Allocations"; }
36
Chris Lattner04805fa2002-02-26 21:46:54 +000037 // doPassInitialization - For the lower allocations pass, this ensures that a
38 // module contains a declaration for a malloc and a free function.
39 //
40 bool doInitialization(Module *M);
41
42 // runOnBasicBlock - This method does the actual work of converting
43 // instructions over, assuming that the pass has already been initialized.
44 //
45 bool runOnBasicBlock(BasicBlock *BB);
46};
47
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 Lattner1bb5f8e2001-10-15 17:31:51 +000055
Chris Lattner0686e432002-01-21 07:31:50 +000056// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000057// module contains a declaration for a malloc and a free function.
58//
59// This function is always successful.
60//
Chris Lattner0686e432002-01-21 07:31:50 +000061bool LowerAllocations::doInitialization(Module *M) {
Chris Lattnerc13563d2002-03-29 03:38:05 +000062 const FunctionType *MallocType =
63 FunctionType::get(PointerType::get(Type::SByteTy),
64 vector<const Type*>(1, Type::UIntTy), false);
65 const FunctionType *FreeType =
66 FunctionType::get(Type::VoidTy,
67 vector<const Type*>(1, PointerType::get(Type::SByteTy)),
68 false);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000069
Chris Lattnerc13563d2002-03-29 03:38:05 +000070 MallocFunc = M->getOrInsertFunction("malloc", MallocType);
71 FreeFunc = M->getOrInsertFunction("free" , FreeType);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000072
Chris Lattner77f791d2002-05-07 19:02:48 +000073 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000074}
75
Chris Lattnerd07471d2002-01-21 23:34:02 +000076// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000077// instructions over, assuming that the pass has already been initialized.
78//
Chris Lattnerd07471d2002-01-21 23:34:02 +000079bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000080 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +000081 assert(MallocFunc && FreeFunc && BB && "Pass not initialized!");
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000082
83 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattnerd07471d2002-01-21 23:34:02 +000084 for (unsigned i = 0; i < BB->size(); ++i) {
85 BasicBlock::InstListType &BBIL = BB->getInstList();
86 if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
87 BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000088
Chris Lattner9b55e5a2002-05-07 18:12:18 +000089 const Type *AllocTy = cast<PointerType>(MI->getType())->getElementType();
Chris Lattnerd07471d2002-01-21 23:34:02 +000090
91 // Get the number of bytes to be allocated for one element of the
92 // requested type...
93 unsigned Size = DataLayout.getTypeSize(AllocTy);
94
95 // malloc(type) becomes sbyte *malloc(constint)
96 Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
97 if (MI->getNumOperands() && Size == 1) {
98 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
99 } else if (MI->getNumOperands()) {
100 // Multiply it by the array size if neccesary...
101 MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
102 MallocArg);
103 BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
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 Lattnerd07471d2002-01-21 23:34:02 +0000108 vector<Value*>(1, MallocArg));
109 BBIL.insert(BBIL.begin()+i, MCall);
110
111 // Create a cast instruction to convert to the right type...
112 CastInst *MCast = new CastInst(MCall, MI->getType());
113 BBIL.insert(BBIL.begin()+i+1, MCast);
114
115 // Replace all uses of the old malloc inst with the cast inst
116 MI->replaceAllUsesWith(MCast);
117 delete MI; // Delete the malloc inst
118 Changed = true;
119 } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
120 BBIL.remove(BB->getInstList().begin()+i);
121
122 // Cast the argument to free into a ubyte*...
123 CastInst *MCast = new CastInst(FI->getOperand(0),
124 PointerType::get(Type::UByteTy));
125 BBIL.insert(BBIL.begin()+i, MCast);
126
127 // Insert a call to the free function...
Chris Lattner57698e22002-03-26 18:01:55 +0000128 CallInst *FCall = new CallInst(FreeFunc,
Chris Lattnerd07471d2002-01-21 23:34:02 +0000129 vector<Value*>(1, MCast));
130 BBIL.insert(BBIL.begin()+i+1, FCall);
131
132 // Delete the old free instruction
133 delete FI;
134 Changed = true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000135 }
136 }
137
Chris Lattner6fea0322001-10-18 05:27:33 +0000138 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000139}