blob: eea800ad54759f94390b9e0d6faf4129418332c8 [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00009//
Misha Brukmanbe372b92003-08-21 22:14:26 +000010// The LowerAllocations transformation is a target dependent tranformation
Chris Lattner77f791d2002-05-07 19:02:48 +000011// because it depends on the size of data types and alignment constraints.
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner42706e42002-07-23 22:04:17 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +000016#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iMemory.h"
19#include "llvm/iOther.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000020#include "llvm/Constants.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000022#include "llvm/Target/TargetData.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000023#include "Support/Statistic.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000024
Brian Gaeke960707c2003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner04805fa2002-02-26 21:46:54 +000027namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000028 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000029
Chris Lattner3cab9f02002-09-25 23:47:47 +000030 /// LowerAllocations - Turn malloc and free instructions into %malloc and
31 /// %free calls.
32 ///
33 class LowerAllocations : public BasicBlockPass {
34 Function *MallocFunc; // Functions in the module we are processing
35 Function *FreeFunc; // Initialized by doInitialization
36 public:
37 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000038
Chris Lattner3cab9f02002-09-25 23:47:47 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 }
Chris Lattner04805fa2002-02-26 21:46:54 +000042
Chris Lattner3cab9f02002-09-25 23:47:47 +000043 /// doPassInitialization - For the lower allocations pass, this ensures that
44 /// a module contains a declaration for a malloc and a free function.
45 ///
46 bool doInitialization(Module &M);
47
48 /// runOnBasicBlock - This method does the actual work of converting
49 /// instructions over, assuming that the pass has already been initialized.
50 ///
51 bool runOnBasicBlock(BasicBlock &BB);
52 };
Chris Lattner04805fa2002-02-26 21:46:54 +000053
Chris Lattner3cab9f02002-09-25 23:47:47 +000054 RegisterOpt<LowerAllocations>
55 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattner77f791d2002-05-07 19:02:48 +000056}
Chris Lattner04805fa2002-02-26 21:46:54 +000057
Chris Lattner77f791d2002-05-07 19:02:48 +000058// createLowerAllocationsPass - Interface to this file...
Brian Gaeke8c14ba92003-08-14 06:09:32 +000059FunctionPass *createLowerAllocationsPass() {
Chris Lattner3cab9f02002-09-25 23:47:47 +000060 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000061}
Chris Lattner37104aa2002-04-29 14:57:45 +000062
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000063
Chris Lattner0686e432002-01-21 07:31:50 +000064// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000065// module contains a declaration for a malloc and a free function.
66//
67// This function is always successful.
68//
Chris Lattner113f4f42002-06-25 16:13:24 +000069bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner28873282003-08-31 00:22:27 +000070 const Type *SBPTy = PointerType::get(Type::SByteTy);
71 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
72 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000073
Chris Lattner77f791d2002-05-07 19:02:48 +000074 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000075}
76
Chris Lattnerd07471d2002-01-21 23:34:02 +000077// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000078// instructions over, assuming that the pass has already been initialized.
79//
Chris Lattner113f4f42002-06-25 16:13:24 +000080bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000081 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +000082 assert(MallocFunc && FreeFunc && "Pass not initialized!");
83
84 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner3cab9f02002-09-25 23:47:47 +000085 TargetData &DataLayout = getAnalysis<TargetData>();
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) {
Chris Lattner889f6202003-04-23 16:37:45 +000089 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()) {
Misha Brukman7eb05a12003-08-18 14:43:39 +0000101 // Multiply it by the array size if necessary...
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 Lattner1f45f4e2003-04-23 16:18:14 +0000108 std::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 Lattner889f6202003-04-23 16:37:45 +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 Lattner652e7bf2003-05-15 18:25:13 +0000121 PointerType::get(Type::SByteTy), "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000122
123 // Insert a call to the free function...
Chris Lattner1f45f4e2003-04-23 16:18:14 +0000124 CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
125 "", I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000126
127 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000128 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000129 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000130 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000131 }
132 }
133
Chris Lattner6fea0322001-10-18 05:27:33 +0000134 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000135}
Brian Gaeke960707c2003-11-11 22:41:34 +0000136
137} // End llvm namespace