blob: 0213daf38c7162fd45f9eee1427670759dae2e7a [file] [log] [blame]
Chris Lattnerade686e2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattnerd216e8b2006-12-19 22:17:40 +000015#define DEBUG_TYPE "lowerallocs"
Chris Lattnere9ee3e52002-07-23 22:04:17 +000016#include "llvm/Transforms/Scalar.h"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000018#include "llvm/Module.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000019#include "llvm/DerivedTypes.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000021#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattnerde97b572004-12-13 20:00:02 +000024#include "llvm/Target/TargetData.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnerd216e8b2006-12-19 22:17:40 +000028STATISTIC(NumLowered, "Number of allocations lowered");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029
Chris Lattnerd216e8b2006-12-19 22:17:40 +000030namespace {
Chris Lattner3b2106f2002-09-25 23:47:47 +000031 /// LowerAllocations - Turn malloc and free instructions into %malloc and
32 /// %free calls.
33 ///
Chris Lattner95255282006-06-28 23:17:24 +000034 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Chris Lattnerb76efb72007-01-07 08:12:01 +000035 Constant *MallocFunc; // Functions in the module we are processing
36 Constant *FreeFunc; // Initialized by doInitialization
Chris Lattner5f792c22005-03-03 01:03:43 +000037 bool LowerMallocArgToInteger;
Chris Lattner3b2106f2002-09-25 23:47:47 +000038 public:
Chris Lattner5f792c22005-03-03 01:03:43 +000039 LowerAllocations(bool LowerToInt = false)
40 : MallocFunc(0), FreeFunc(0), LowerMallocArgToInteger(LowerToInt) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000041
Chris Lattnerde97b572004-12-13 20:00:02 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetData>();
44 AU.setPreservesCFG();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000045
Chris Lattnered96fe82006-05-17 21:05:27 +000046 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000047 AU.addPreserved<UnifyFunctionExitNodes>();
48 AU.addPreservedID(PromoteMemoryToRegisterID);
49 AU.addPreservedID(LowerSelectID);
50 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000051 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerde97b572004-12-13 20:00:02 +000052 }
53
Chris Lattner3b2106f2002-09-25 23:47:47 +000054 /// doPassInitialization - For the lower allocations pass, this ensures that
55 /// a module contains a declaration for a malloc and a free function.
56 ///
57 bool doInitialization(Module &M);
Reid Spencer791a9592004-12-07 08:11:36 +000058
Chris Lattnerde97b572004-12-13 20:00:02 +000059 virtual bool doInitialization(Function &F) {
60 return BasicBlockPass::doInitialization(F);
61 }
Misha Brukmanfd939082005-04-21 23:48:37 +000062
Chris Lattner3b2106f2002-09-25 23:47:47 +000063 /// runOnBasicBlock - This method does the actual work of converting
64 /// instructions over, assuming that the pass has already been initialized.
65 ///
66 bool runOnBasicBlock(BasicBlock &BB);
67 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000068
Chris Lattner7f8897f2006-08-27 22:42:52 +000069 RegisterPass<LowerAllocations>
Chris Lattner3b2106f2002-09-25 23:47:47 +000070 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000071}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000072
Chris Lattnerb3674e42006-05-02 04:24:36 +000073// Publically exposed interface to pass...
74const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattnerade686e2002-05-07 19:02:48 +000075// createLowerAllocationsPass - Interface to this file...
Devang Patel4d447f52007-01-25 23:23:25 +000076Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
Chris Lattner5f792c22005-03-03 01:03:43 +000077 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattnerade686e2002-05-07 19:02:48 +000078}
Chris Lattner96c466b2002-04-29 14:57:45 +000079
Chris Lattner1bffea02001-10-15 17:31:51 +000080
Chris Lattnerf4de63f2002-01-21 07:31:50 +000081// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000082// module contains a declaration for a malloc and a free function.
83//
84// This function is always successful.
85//
Chris Lattner7e708292002-06-25 16:13:24 +000086bool LowerAllocations::doInitialization(Module &M) {
Chris Lattnerb76efb72007-01-07 08:12:01 +000087 const Type *BPTy = PointerType::get(Type::Int8Ty);
88 // Prototype malloc as "char* malloc(...)", because we don't know in
89 // doInitialization whether size_t is int or long.
90 FunctionType *FT = FunctionType::get(BPTy, std::vector<const Type*>(), true);
91 MallocFunc = M.getOrInsertFunction("malloc", FT);
92 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0);
Chris Lattnerade686e2002-05-07 19:02:48 +000093 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000094}
95
Chris Lattner84453722002-01-21 23:34:02 +000096// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000097// instructions over, assuming that the pass has already been initialized.
98//
Chris Lattner7e708292002-06-25 16:13:24 +000099bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000100 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000101 assert(MallocFunc && FreeFunc && "Pass not initialized!");
102
103 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +0000104
Chris Lattner5f792c22005-03-03 01:03:43 +0000105 const TargetData &TD = getAnalysis<TargetData>();
106 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattnerde97b572004-12-13 20:00:02 +0000107
Chris Lattner1bffea02001-10-15 17:31:51 +0000108 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000109 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +0000110 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000111 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000112
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000113 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner5f792c22005-03-03 01:03:43 +0000114 Value *MallocArg;
115 if (LowerMallocArgToInteger)
Reid Spencerc5b206b2006-12-31 05:48:39 +0000116 MallocArg = ConstantInt::get(Type::Int64Ty, TD.getTypeSize(AllocTy));
Chris Lattner5f792c22005-03-03 01:03:43 +0000117 else
118 MallocArg = ConstantExpr::getSizeOf(AllocTy);
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000119 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
120 IntPtrTy);
Chris Lattner5f792c22005-03-03 01:03:43 +0000121
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000122 if (MI->isArrayAllocation()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000123 if (isa<ConstantInt>(MallocArg) &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000124 cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000125 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
126 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000127 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000128 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
129 } else {
Chris Lattnerde97b572004-12-13 20:00:02 +0000130 Value *Scale = MI->getOperand(0);
131 if (Scale->getType() != IntPtrTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000132 Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
133 "", I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000134
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000135 // Multiply it by the array size if necessary...
Chris Lattnerde97b572004-12-13 20:00:02 +0000136 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000137 MallocArg, "", I);
138 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000139 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000140
Chris Lattnerb76efb72007-01-07 08:12:01 +0000141 // Create the call to Malloc.
142 CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
Chris Lattnera9e92112005-05-06 06:48:21 +0000143 MCall->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000144
Chris Lattner84453722002-01-21 23:34:02 +0000145 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000146 Value *MCast;
147 if (MCall->getType() != Type::VoidTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000148 MCast = new BitCastInst(MCall, MI->getType(), "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000149 else
150 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000151
Chris Lattner84453722002-01-21 23:34:02 +0000152 // Replace all uses of the old malloc inst with the cast inst
153 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000154 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000155 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000156 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000157 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000158 Value *PtrCast = new BitCastInst(FI->getOperand(0),
159 PointerType::get(Type::Int8Ty), "", I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000160
Chris Lattner84453722002-01-21 23:34:02 +0000161 // Insert a call to the free function...
Chris Lattnerb76efb72007-01-07 08:12:01 +0000162 (new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000163
Chris Lattner84453722002-01-21 23:34:02 +0000164 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000165 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000166 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000167 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000168 }
169 }
170
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000171 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000172}
Brian Gaeked0fde302003-11-11 22:41:34 +0000173