blob: 8f61d88089b2b52f6ebea01e5cab634268b8ca75 [file] [log] [blame]
Chris Lattner77f791d2002-05-07 19:02:48 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner1bb5f8e2001-10-15 17:31:51 +00009//
Misha Brukmanbf437872004-01-28 20:43:01 +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 Lattner45f966d2006-12-19 22:17:40 +000015#define DEBUG_TYPE "lowerallocs"
Chris Lattner42706e42002-07-23 22:04:17 +000016#include "llvm/Transforms/Scalar.h"
Chris Lattner4fe87d62006-05-09 04:13:41 +000017#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattnerd5d56782002-01-31 00:45:11 +000018#include "llvm/Module.h"
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000019#include "llvm/DerivedTypes.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000021#include "llvm/Constants.h"
Owen Andersone70b6372009-07-05 22:41:43 +000022#include "llvm/LLVMContext.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000023#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chris Lattner8f430a32004-12-13 20:00:02 +000025#include "llvm/Target/TargetData.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattner49525f82004-01-09 06:02:20 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner45f966d2006-12-19 22:17:40 +000029STATISTIC(NumLowered, "Number of allocations lowered");
Chris Lattner04805fa2002-02-26 21:46:54 +000030
Chris Lattner45f966d2006-12-19 22:17:40 +000031namespace {
Chris Lattner3cab9f02002-09-25 23:47:47 +000032 /// LowerAllocations - Turn malloc and free instructions into %malloc and
33 /// %free calls.
34 ///
Chris Lattner996795b2006-06-28 23:17:24 +000035 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Chris Lattner34acba42007-01-07 08:12:01 +000036 Constant *MallocFunc; // Functions in the module we are processing
37 Constant *FreeFunc; // Initialized by doInitialization
Chris Lattneref1e9892005-03-03 01:03:43 +000038 bool LowerMallocArgToInteger;
Chris Lattner3cab9f02002-09-25 23:47:47 +000039 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000040 static char ID; // Pass ID, replacement for typeid
Dan Gohman34d442f2007-08-01 15:32:29 +000041 explicit LowerAllocations(bool LowerToInt = false)
Dan Gohmana79db302008-09-04 17:05:41 +000042 : BasicBlockPass(&ID), MallocFunc(0), FreeFunc(0),
Devang Patel09f162c2007-05-01 21:15:47 +000043 LowerMallocArgToInteger(LowerToInt) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000044
Chris Lattner8f430a32004-12-13 20:00:02 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<TargetData>();
47 AU.setPreservesCFG();
Chris Lattner4fe87d62006-05-09 04:13:41 +000048
Anton Korobeynikovfb801512007-04-16 18:10:23 +000049 // This is a cluster of orthogonal Transforms:
Chris Lattner4fe87d62006-05-09 04:13:41 +000050 AU.addPreserved<UnifyFunctionExitNodes>();
51 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner4fe87d62006-05-09 04:13:41 +000052 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000053 AU.addPreservedID(LowerInvokePassID);
Chris Lattner8f430a32004-12-13 20:00:02 +000054 }
55
Chris Lattner3cab9f02002-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 Spencer9273d482004-12-07 08:11:36 +000060
Chris Lattner8f430a32004-12-13 20:00:02 +000061 virtual bool doInitialization(Function &F) {
Devang Patela13f1f32008-11-18 18:43:07 +000062 return doInitialization(*F.getParent());
Chris Lattner8f430a32004-12-13 20:00:02 +000063 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000064
Chris Lattner3cab9f02002-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 Lattner77f791d2002-05-07 19:02:48 +000070}
Chris Lattner04805fa2002-02-26 21:46:54 +000071
Dan Gohmand78c4002008-05-13 00:00:25 +000072char LowerAllocations::ID = 0;
73static RegisterPass<LowerAllocations>
74X("lowerallocs", "Lower allocations from instructions to calls");
75
Chris Lattner2d3a0272006-05-02 04:24:36 +000076// Publically exposed interface to pass...
Dan Gohman0479aa52008-05-13 02:05:11 +000077const PassInfo *const llvm::LowerAllocationsID = &X;
Chris Lattner77f791d2002-05-07 19:02:48 +000078// createLowerAllocationsPass - Interface to this file...
Devang Patel5292e652007-01-25 23:23:25 +000079Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
Chris Lattneref1e9892005-03-03 01:03:43 +000080 return new LowerAllocations(LowerMallocArgToInteger);
Chris Lattner77f791d2002-05-07 19:02:48 +000081}
Chris Lattner37104aa2002-04-29 14:57:45 +000082
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000083
Chris Lattner0686e432002-01-21 07:31:50 +000084// doInitialization - For the lower allocations pass, this ensures that a
Chris Lattner1bb5f8e2001-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 Lattner113f4f42002-06-25 16:13:24 +000089bool LowerAllocations::doInitialization(Module &M) {
Owen Anderson47db9412009-07-22 00:24:57 +000090 const Type *BPTy = M.getContext().getPointerTypeUnqual(Type::Int8Ty);
Chris Lattner34acba42007-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.
Owen Anderson47db9412009-07-22 00:24:57 +000093 FunctionType *FT = M.getContext().getFunctionType(BPTy, true);
Chris Lattner34acba42007-01-07 08:12:01 +000094 MallocFunc = M.getOrInsertFunction("malloc", FT);
95 FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, BPTy, (Type *)0);
Chris Lattner77f791d2002-05-07 19:02:48 +000096 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000097}
98
Chris Lattnerd07471d2002-01-21 23:34:02 +000099// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000100// instructions over, assuming that the pass has already been initialized.
101//
Chris Lattner113f4f42002-06-25 16:13:24 +0000102bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +0000103 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000104 assert(MallocFunc && FreeFunc && "Pass not initialized!");
105
Owen Anderson47db9412009-07-22 00:24:57 +0000106 LLVMContext &Context = BB.getContext();
107
Chris Lattner113f4f42002-06-25 16:13:24 +0000108 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000109
Chris Lattneref1e9892005-03-03 01:03:43 +0000110 const TargetData &TD = getAnalysis<TargetData>();
111 const Type *IntPtrTy = TD.getIntPtrType();
Chris Lattner8f430a32004-12-13 20:00:02 +0000112
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000113 // Loop over all of the instructions, looking for malloc or free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Chris Lattner889f6202003-04-23 16:37:45 +0000115 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 const Type *AllocTy = MI->getType()->getElementType();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000117
Dan Gohman4fe64de2009-06-14 23:30:43 +0000118 // malloc(type) becomes i8 *malloc(size)
Chris Lattneref1e9892005-03-03 01:03:43 +0000119 Value *MallocArg;
120 if (LowerMallocArgToInteger)
Owen Anderson47db9412009-07-22 00:24:57 +0000121 MallocArg = Context.getConstantInt(Type::Int64Ty,
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000122 TD.getTypeAllocSize(AllocTy));
Chris Lattneref1e9892005-03-03 01:03:43 +0000123 else
Owen Anderson47db9412009-07-22 00:24:57 +0000124 MallocArg = Context.getConstantExprSizeOf(AllocTy);
Owen Andersone70b6372009-07-05 22:41:43 +0000125 MallocArg =
Owen Anderson47db9412009-07-22 00:24:57 +0000126 Context.getConstantExprTruncOrBitCast(cast<Constant>(MallocArg),
Reid Spencer41cb2692006-12-12 09:17:08 +0000127 IntPtrTy);
Chris Lattneref1e9892005-03-03 01:03:43 +0000128
Chris Lattnerba7aef392004-07-15 01:08:08 +0000129 if (MI->isArrayAllocation()) {
Chris Lattner8f430a32004-12-13 20:00:02 +0000130 if (isa<ConstantInt>(MallocArg) &&
Reid Spencer43376a72007-03-02 23:03:17 +0000131 cast<ConstantInt>(MallocArg)->isOne()) {
Chris Lattnerba7aef392004-07-15 01:08:08 +0000132 MallocArg = MI->getOperand(0); // Operand * 1 = Operand
133 } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
Owen Andersone70b6372009-07-05 22:41:43 +0000134 CO =
Owen Anderson47db9412009-07-22 00:24:57 +0000135 Context.getConstantExprIntegerCast(CO, IntPtrTy, false /*ZExt*/);
136 MallocArg = Context.getConstantExprMul(CO,
Owen Andersone70b6372009-07-05 22:41:43 +0000137 cast<Constant>(MallocArg));
Chris Lattnerba7aef392004-07-15 01:08:08 +0000138 } else {
Chris Lattner8f430a32004-12-13 20:00:02 +0000139 Value *Scale = MI->getOperand(0);
140 if (Scale->getType() != IntPtrTy)
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000141 Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000142 "", I);
Chris Lattner8f430a32004-12-13 20:00:02 +0000143
Chris Lattnerba7aef392004-07-15 01:08:08 +0000144 // Multiply it by the array size if necessary...
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000145 MallocArg = BinaryOperator::Create(Instruction::Mul, Scale,
Chris Lattnerba7aef392004-07-15 01:08:08 +0000146 MallocArg, "", I);
147 }
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000148 }
Chris Lattner772eafa2004-02-28 18:51:45 +0000149
Chris Lattner34acba42007-01-07 08:12:01 +0000150 // Create the call to Malloc.
Gabor Greife9ecc682008-04-06 20:25:17 +0000151 CallInst *MCall = CallInst::Create(MallocFunc, MallocArg, "", I);
Chris Lattner6aacb0f2005-05-06 06:48:21 +0000152 MCall->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153
Chris Lattnerd07471d2002-01-21 23:34:02 +0000154 // Create a cast instruction to convert to the right type...
Chris Lattner772eafa2004-02-28 18:51:45 +0000155 Value *MCast;
156 if (MCall->getType() != Type::VoidTy)
Reid Spencerbfe26ff2006-12-13 00:50:17 +0000157 MCast = new BitCastInst(MCall, MI->getType(), "", I);
Chris Lattner772eafa2004-02-28 18:51:45 +0000158 else
Owen Anderson47db9412009-07-22 00:24:57 +0000159 MCast = Context.getNullValue(MI->getType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000160
Chris Lattnerd07471d2002-01-21 23:34:02 +0000161 // Replace all uses of the old malloc inst with the cast inst
162 MI->replaceAllUsesWith(MCast);
Chris Lattnera239e682002-09-10 22:38:47 +0000163 I = --BBIL.erase(I); // remove and delete the malloc instr...
Chris Lattnerd07471d2002-01-21 23:34:02 +0000164 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000165 ++NumLowered;
Chris Lattner889f6202003-04-23 16:37:45 +0000166 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Christopher Lambedf07882007-12-17 01:12:55 +0000167 Value *PtrCast =
168 new BitCastInst(FI->getOperand(0),
Owen Anderson47db9412009-07-22 00:24:57 +0000169 Context.getPointerTypeUnqual(Type::Int8Ty), "", I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000170
Chris Lattnerd07471d2002-01-21 23:34:02 +0000171 // Insert a call to the free function...
Gabor Greife9ecc682008-04-06 20:25:17 +0000172 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000173
Chris Lattnerd07471d2002-01-21 23:34:02 +0000174 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000175 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000176 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000177 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000178 }
179 }
180
Chris Lattner6fea0322001-10-18 05:27:33 +0000181 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000182}
Brian Gaeke960707c2003-11-11 22:41:34 +0000183