blob: a43ec4b3573a730d222a2cdf837145ed62f676fe [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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:
Devang Patel19974732007-05-03 01:11:54 +000039 static char ID; // Pass ID, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000040 explicit LowerAllocations(bool LowerToInt = false)
Devang Patel794fd752007-05-01 21:15:47 +000041 : BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0),
42 LowerMallocArgToInteger(LowerToInt) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043
Chris Lattnerde97b572004-12-13 20:00:02 +000044 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.addRequired<TargetData>();
46 AU.setPreservesCFG();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000047
Anton Korobeynikovbed29462007-04-16 18:10:23 +000048 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000049 AU.addPreserved<UnifyFunctionExitNodes>();
50 AU.addPreservedID(PromoteMemoryToRegisterID);
51 AU.addPreservedID(LowerSelectID);
52 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000053 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerde97b572004-12-13 20:00:02 +000054 }
55
Chris Lattner3b2106f2002-09-25 23:47:47 +000056 /// doPassInitialization - For the lower allocations pass, this ensures that
57 /// a module contains a declaration for a malloc and a free function.
58 ///
59 bool doInitialization(Module &M);
Reid Spencer791a9592004-12-07 08:11:36 +000060
Chris Lattnerde97b572004-12-13 20:00:02 +000061 virtual bool doInitialization(Function &F) {
62 return BasicBlockPass::doInitialization(F);
63 }
Misha Brukmanfd939082005-04-21 23:48:37 +000064
Chris Lattner3b2106f2002-09-25 23:47:47 +000065 /// runOnBasicBlock - This method does the actual work of converting
66 /// instructions over, assuming that the pass has already been initialized.
67 ///
68 bool runOnBasicBlock(BasicBlock &BB);
69 };
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070
Devang Patel19974732007-05-03 01:11:54 +000071 char LowerAllocations::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000072 RegisterPass<LowerAllocations>
Chris Lattner3b2106f2002-09-25 23:47:47 +000073 X("lowerallocs", "Lower allocations from instructions to calls");
Chris Lattnerade686e2002-05-07 19:02:48 +000074}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000075
Chris Lattnerb3674e42006-05-02 04:24:36 +000076// Publically exposed interface to pass...
77const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
Chris Lattnerade686e2002-05-07 19:02:48 +000078// createLowerAllocationsPass - Interface to this file...
Devang Patel4d447f52007-01-25 23:23:25 +000079Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
Chris Lattner5f792c22005-03-03 01:03:43 +000080 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattnerade686e2002-05-07 19:02:48 +000081}
Chris Lattner96c466b2002-04-29 14:57:45 +000082
Chris Lattner1bffea02001-10-15 17:31:51 +000083
Chris Lattnerf4de63f2002-01-21 07:31:50 +000084// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bffea02001-10-15 17:31:51 +000085// module contains a declaration for a malloc and a free function.
86//
87// This function is always successful.
88//
Chris Lattner7e708292002-06-25 16:13:24 +000089bool LowerAllocations::doInitialization(Module &M) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +000090 const Type *BPTy = PointerType::getUnqual(Type::Int8Ty);
Chris Lattnerb76efb72007-01-07 08:12:01 +000091 // Prototype malloc as "char* malloc(...)", because we don't know in
92 // doInitialization whether size_t is int or long.
93 FunctionType *FT = FunctionType::get(BPTy, std::vector<const Type*>(), true);
94 MallocFunc = M.getOrInsertFunction("malloc", FT);
95 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0);
Chris Lattnerade686e2002-05-07 19:02:48 +000096 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000097}
98
Chris Lattner84453722002-01-21 23:34:02 +000099// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +0000100// instructions over, assuming that the pass has already been initialized.
101//
Chris Lattner7e708292002-06-25 16:13:24 +0000102bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000103 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000104 assert(MallocFunc && FreeFunc && "Pass not initialized!");
105
106 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +0000107
Chris Lattner5f792c22005-03-03 01:03:43 +0000108 const TargetData &TD = getAnalysis<TargetData>();
109 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattnerde97b572004-12-13 20:00:02 +0000110
Chris Lattner1bffea02001-10-15 17:31:51 +0000111 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000112 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattnere408e252003-04-23 16:37:45 +0000113 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000114 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanfd939082005-04-21 23:48:37 +0000115
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000116 // malloc(type) becomes sbyte *malloc(size)
Chris Lattner5f792c22005-03-03 01:03:43 +0000117 Value *MallocArg;
118 if (LowerMallocArgToInteger)
Duncan Sands514ab342007-11-01 20:53:16 +0000119 MallocArg = ConstantInt::get(Type::Int64Ty, TD.getABITypeSize(AllocTy));
Chris Lattner5f792c22005-03-03 01:03:43 +0000120 else
121 MallocArg = ConstantExpr::getSizeOf(AllocTy);
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000122 MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
123 IntPtrTy);
Chris Lattner5f792c22005-03-03 01:03:43 +0000124
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000125 if (MI->isArrayAllocation()) {
Chris Lattnerde97b572004-12-13 20:00:02 +0000126 if (isa<ConstantInt>(MallocArg) &&
Reid Spencer4cf735b2007-03-02 23:03:17 +0000127 cast<ConstantInt>(MallocArg)->isOne()) {
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000128 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
129 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Reid Spencerdaa8e3c2006-12-12 09:17:08 +0000130 CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000131 MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
132 } else {
Chris Lattnerde97b572004-12-13 20:00:02 +0000133 Value *Scale = MI->getOperand(0);
134 if (Scale->getType() != IntPtrTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000135 Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
136 "", I);
Chris Lattnerde97b572004-12-13 20:00:02 +0000137
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000138 // Multiply it by the array size if necessary...
Chris Lattnerde97b572004-12-13 20:00:02 +0000139 MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
Chris Lattner8bfc2f12004-07-15 01:08:08 +0000140 MallocArg, "", I);
141 }
Chris Lattner1bffea02001-10-15 17:31:51 +0000142 }
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000143
Chris Lattnerb76efb72007-01-07 08:12:01 +0000144 // Create the call to Malloc.
145 CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
Chris Lattnera9e92112005-05-06 06:48:21 +0000146 MCall->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000147
Chris Lattner84453722002-01-21 23:34:02 +0000148 // Create a cast instruction to convert to the right type...
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000149 Value *MCast;
150 if (MCall->getType() != Type::VoidTy)
Reid Spencer7b06bd52006-12-13 00:50:17 +0000151 MCast = new BitCastInst(MCall, MI->getType(), "", I);
Chris Lattnerb99df4f2004-02-28 18:51:45 +0000152 else
153 MCast = Constant::getNullValue(MI->getType());
Misha Brukmanfd939082005-04-21 23:48:37 +0000154
Chris Lattner84453722002-01-21 23:34:02 +0000155 // Replace all uses of the old malloc inst with the cast inst
156 MI->replaceAllUsesWith(MCast);
Chris Lattner1d608ab2002-09-10 22:38:47 +0000157 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattner84453722002-01-21 23:34:02 +0000158 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000159 ++NumLowered;
Chris Lattnere408e252003-04-23 16:37:45 +0000160 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000161 Value *PtrCast =
162 new BitCastInst(FI->getOperand(0),
163 PointerType::getUnqual(Type::Int8Ty), "", I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000164
Chris Lattner84453722002-01-21 23:34:02 +0000165 // Insert a call to the free function...
Chris Lattnerb76efb72007-01-07 08:12:01 +0000166 (new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
Misha Brukmanfd939082005-04-21 23:48:37 +0000167
Chris Lattner84453722002-01-21 23:34:02 +0000168 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000169 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000170 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000171 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000172 }
173 }
174
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000175 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000176}
Brian Gaeked0fde302003-11-11 22:41:34 +0000177