blob: 53584d9d3e3723eaae0cb0ca5db3bc4c0b7742a7 [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 Lattnerbf3a0992002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000017
Chris Lattner04805fa2002-02-26 21:46:54 +000018namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000019 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000020
Chris Lattner3cab9f02002-09-25 23:47:47 +000021 /// LowerAllocations - Turn malloc and free instructions into %malloc and
22 /// %free calls.
23 ///
24 class LowerAllocations : public BasicBlockPass {
25 Function *MallocFunc; // Functions in the module we are processing
26 Function *FreeFunc; // Initialized by doInitialization
27 public:
28 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000029
Chris Lattner3cab9f02002-09-25 23:47:47 +000030 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired<TargetData>();
32 }
Chris Lattner04805fa2002-02-26 21:46:54 +000033
Chris Lattner3cab9f02002-09-25 23:47:47 +000034 /// doPassInitialization - For the lower allocations pass, this ensures that
35 /// a module contains a declaration for a malloc and a free function.
36 ///
37 bool doInitialization(Module &M);
38
39 /// runOnBasicBlock - This method does the actual work of converting
40 /// instructions over, assuming that the pass has already been initialized.
41 ///
42 bool runOnBasicBlock(BasicBlock &BB);
43 };
Chris Lattner04805fa2002-02-26 21:46:54 +000044
Chris Lattner3cab9f02002-09-25 23:47:47 +000045 RegisterOpt<LowerAllocations>
46 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000047}
Chris Lattner04805fa2002-02-26 21:46:54 +000048
Chris Lattner77f791d2002-05-07 19:02:48 +000049// createLowerAllocationsPass - Interface to this file...
Brian Gaeke8c14ba92003-08-14 06:09:32 +000050FunctionPass *createLowerAllocationsPass() {
Chris Lattner3cab9f02002-09-25 23:47:47 +000051 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000052}
Chris Lattner37104aa2002-04-29 14:57:45 +000053
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000054
Chris Lattner0686e432002-01-21 07:31:50 +000055// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000056// module contains a declaration for a malloc and a free function.
57//
58// This function is always successful.
59//
Chris Lattner113f4f42002-06-25 16:13:24 +000060bool LowerAllocations::doInitialization(Module &M) {
Chris Lattnerc13563d2002-03-29 03:38:05 +000061 const FunctionType *MallocType =
62 FunctionType::get(PointerType::get(Type::SByteTy),
Chris Lattner1f45f4e2003-04-23 16:18:14 +000063 std::vector<const Type*>(1, Type::UIntTy), false);
Chris Lattnerc13563d2002-03-29 03:38:05 +000064 const FunctionType *FreeType =
65 FunctionType::get(Type::VoidTy,
Chris Lattner1f45f4e2003-04-23 16:18:14 +000066 std::vector<const Type*>(1,
67 PointerType::get(Type::SByteTy)),
Chris Lattnerc13563d2002-03-29 03:38:05 +000068 false);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000069
Chris Lattner113f4f42002-06-25 16:13:24 +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 Lattner113f4f42002-06-25 16:13:24 +000079bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000080 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000081 assert(MallocFunc && FreeFunc && "Pass not initialized!");
82
83 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner3cab9f02002-09-25 23:47:47 +000084 TargetData &DataLayout = getAnalysis<TargetData>();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000085
86 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +000087 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +000088 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +000089 const Type *AllocTy = 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...
Chris Lattnera239e682002-09-10 22:38:47 +0000101 MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
102 MallocArg, "", I);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000103 }
Chris Lattnerd07471d2002-01-21 23:34:02 +0000104
105 // Create the call to Malloc...
Chris Lattner57698e22002-03-26 18:01:55 +0000106 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattner1f45f4e2003-04-23 16:18:14 +0000107 std::vector<Value*>(1, MallocArg), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000108
109 // Create a cast instruction to convert to the right type...
Chris Lattnera239e682002-09-10 22:38:47 +0000110 CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000111
112 // Replace all uses of the old malloc inst with the cast inst
113 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000114 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000115 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000116 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000117 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerd07471d2002-01-21 23:34:02 +0000118 // Cast the argument to free into a ubyte*...
119 CastInst *MCast = new CastInst(FI->getOperand(0),
Chris Lattner652e7bf2003-05-15 18:25:13 +0000120 PointerType::get(Type::SByteTy), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000121
122 // Insert a call to the free function...
Chris Lattner1f45f4e2003-04-23 16:18:14 +0000123 CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
124 "", 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}