blob: b655a2b9e91ee6230ccd940b60a28749a2292eb3 [file] [log] [blame]
Chris Lattnerade686e2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
John Criswellb576c942003-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 Lattner1bffea02001-10-15 17:31:51 +00009//
Misha Brukman7c791ed2004-01-28 20:43:01 +000010// The LowerAllocations transformation is a target-dependent tranformation
Chris Lattnerade686e2002-05-07 19:02:48 +000011// because it depends on the size of data types and alignment constraints.
Chris Lattner1bffea02001-10-15 17:31:51 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnere9ee3e52002-07-23 22:04:17 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000016#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iMemory.h"
19#include "llvm/iOther.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000020#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000022#include "llvm/Target/TargetData.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000023#include "Support/Statistic.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerbd0ef772002-02-26 21:46:54 +000026namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000027 Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000028
Chris Lattner3b2106f2002-09-25 23:47:47 +000029 /// LowerAllocations - Turn malloc and free instructions into %malloc and
30 /// %free calls.
31 ///
32 class LowerAllocations : public BasicBlockPass {
33 Function *MallocFunc; // Functions in the module we are processing
34 Function *FreeFunc; // Initialized by doInitialization
35 public:
36 LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000037
Chris Lattner3b2106f2002-09-25 23:47:47 +000038 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired<TargetData>();
40 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000041
Chris Lattner3b2106f2002-09-25 23:47:47 +000042 /// doPassInitialization - For the lower allocations pass, this ensures that
43 /// a module contains a declaration for a malloc and a free function.
44 ///
45 bool doInitialization(Module &M);
46
47 /// runOnBasicBlock - This method does the actual work of converting
48 /// instructions over, assuming that the pass has already been initialized.
49 ///
50 bool runOnBasicBlock(BasicBlock &BB);
51 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000052
Chris Lattner3b2106f2002-09-25 23:47:47 +000053 RegisterOpt<LowerAllocations>
54 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000055}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000056
Chris Lattnerade686e2002-05-07 19:02:48 +000057// createLowerAllocationsPass - Interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +000058FunctionPass *llvm::createLowerAllocationsPass() {
Chris Lattner3b2106f2002-09-25 23:47:47 +000059 return new LowerAllocations();
Chris Lattnerade686e2002-05-07 19:02:48 +000060}
Chris Lattner96c466b2002-04-29 14:57:45 +000061
Chris Lattner1bffea02001-10-15 17:31:51 +000062
Chris Lattnerf4de63f2002-01-21 07:31:50 +000063// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000064// module contains a declaration for a malloc and a free function.
65//
66// This function is always successful.
67//
Chris Lattner7e708292002-06-25 16:13:24 +000068bool LowerAllocations::doInitialization(Module &M) {
Chris Lattner7d8a86a2003-08-31 00:22:27 +000069 const Type *SBPTy = PointerType::get(Type::SByteTy);
70 MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
71 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
Chris Lattner1bffea02001-10-15 17:31:51 +000072
Chris Lattnerade686e2002-05-07 19:02:48 +000073 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000074}
75
Chris Lattner84453722002-01-21 23:34:02 +000076// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000077// instructions over, assuming that the pass has already been initialized.
78//
Chris Lattner7e708292002-06-25 16:13:24 +000079bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000080 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000081 assert(MallocFunc && FreeFunc && "Pass not initialized!");
82
83 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner3b2106f2002-09-25 23:47:47 +000084 TargetData &DataLayout = getAnalysis<TargetData>();
Chris Lattner1bffea02001-10-15 17:31:51 +000085
86 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +000087 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +000088 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +000089 const Type *AllocTy = MI->getType()->getElementType();
Chris Lattner84453722002-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()) {
Misha Brukman5560c9d2003-08-18 14:43:39 +0000100 // Multiply it by the array size if necessary...
Chris Lattner1d608ab2002-09-10 22:38:47 +0000101 MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
102 MallocArg, "", I);
Chris Lattner1bffea02001-10-15 17:31:51 +0000103 }
Chris Lattner84453722002-01-21 23:34:02 +0000104
105 // Create the call to Malloc...
Chris Lattner79df7c02002-03-26 18:01:55 +0000106 CallInst *MCall = new CallInst(MallocFunc,
Chris Lattner350c0a82003-04-23 16:18:14 +0000107 std::vector<Value*>(1, MallocArg), "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000108
109 // Create a cast instruction to convert to the right type...
Chris Lattner1d608ab2002-09-10 22:38:47 +0000110 CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000111
112 // Replace all uses of the old malloc inst with the cast inst
113 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000114 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000115 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000116 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000117 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner84453722002-01-21 23:34:02 +0000118 // Cast the argument to free into a ubyte*...
119 CastInst *MCast = new CastInst(FI->getOperand(0),
Chris Lattner6d23d972003-05-15 18:25:13 +0000120 PointerType::get(Type::SByteTy), "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000121
122 // Insert a call to the free function...
Chris Lattner350c0a82003-04-23 16:18:14 +0000123 CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
124 "", I);
Chris Lattner84453722002-01-21 23:34:02 +0000125
126 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000127 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000128 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000129 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000130 }
131 }
132
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000133 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000134}
Brian Gaeked0fde302003-11-11 22:41:34 +0000135