blob: cc1f6ea5e50af4d3ed4f5ada0995d9e1e9cfabc0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// The LowerAllocations transformation is a target-dependent tranformation
11// because it depends on the size of data types and alignment constraints.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "lowerallocs"
16#include "llvm/Transforms/Scalar.h"
17#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
18#include "llvm/Module.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
21#include "llvm/Constants.h"
Owen Andersona09d2342009-07-05 22:41:43 +000022#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Pass.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Support/Compiler.h"
27using namespace llvm;
28
29STATISTIC(NumLowered, "Number of allocations lowered");
30
31namespace {
Dan Gohmandd35c042009-08-17 18:45:31 +000032 /// LowerAllocations - Turn malloc and free instructions into @malloc and
33 /// @free calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 ///
35 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Evan Chenge5406462009-09-10 04:36:43 +000036 Constant *FreeFunc; // Functions in the module we are processing
37 // Initialized by doInitialization
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 bool LowerMallocArgToInteger;
39 public:
40 static char ID; // Pass ID, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000041 explicit LowerAllocations(bool LowerToInt = false)
Evan Chenge5406462009-09-10 04:36:43 +000042 : BasicBlockPass(&ID), FreeFunc(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 LowerMallocArgToInteger(LowerToInt) {}
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequired<TargetData>();
47 AU.setPreservesCFG();
48
49 // This is a cluster of orthogonal Transforms:
50 AU.addPreserved<UnifyFunctionExitNodes>();
51 AU.addPreservedID(PromoteMemoryToRegisterID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 AU.addPreservedID(LowerSwitchID);
53 AU.addPreservedID(LowerInvokePassID);
54 }
55
56 /// 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);
60
61 virtual bool doInitialization(Function &F) {
Devang Patel35d69b02008-11-18 18:43:07 +000062 return doInitialization(*F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 }
64
65 /// 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 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070}
71
Dan Gohman089efff2008-05-13 00:00:25 +000072char LowerAllocations::ID = 0;
73static RegisterPass<LowerAllocations>
74X("lowerallocs", "Lower allocations from instructions to calls");
75
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076// Publically exposed interface to pass...
Dan Gohman66a636e2008-05-13 02:05:11 +000077const PassInfo *const llvm::LowerAllocationsID = &X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078// createLowerAllocationsPass - Interface to this file...
79Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
80 return new LowerAllocations(LowerMallocArgToInteger);
81}
82
83
84// doInitialization - For the lower allocations pass, this ensures that a
85// module contains a declaration for a malloc and a free function.
86//
87// This function is always successful.
88//
89bool LowerAllocations::doInitialization(Module &M) {
Owen Anderson35b47072009-08-13 21:58:54 +000090 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext()));
Owen Anderson35b47072009-08-13 21:58:54 +000091 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
92 BPTy, (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 return true;
94}
95
96// runOnBasicBlock - This method does the actual work of converting
97// instructions over, assuming that the pass has already been initialized.
98//
99bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
100 bool Changed = false;
Evan Chenge5406462009-09-10 04:36:43 +0000101 assert(FreeFunc && "Pass not initialized!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 BasicBlock::InstListType &BBIL = BB.getInstList();
104
105 const TargetData &TD = getAnalysis<TargetData>();
Owen Anderson35b47072009-08-13 21:58:54 +0000106 const Type *IntPtrTy = TD.getIntPtrType(BB.getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 // Loop over all of the instructions, looking for malloc or free instructions
109 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
110 if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
Evan Chenge5406462009-09-10 04:36:43 +0000111 Value *MCast = CallInst::CreateMalloc(I, MI->getType(), IntPtrTy,
112 MI->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
114 // Replace all uses of the old malloc inst with the cast inst
115 MI->replaceAllUsesWith(MCast);
116 I = --BBIL.erase(I); // remove and delete the malloc instr...
117 Changed = true;
118 ++NumLowered;
119 } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Christopher Lambbb2f2222007-12-17 01:12:55 +0000120 Value *PtrCast =
121 new BitCastInst(FI->getOperand(0),
Owen Anderson35b47072009-08-13 21:58:54 +0000122 PointerType::getUnqual(Type::getInt8Ty(BB.getContext())), "", I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
124 // Insert a call to the free function...
Gabor Greifd6da1d02008-04-06 20:25:17 +0000125 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
127 // Delete the old free instruction
128 I = --BBIL.erase(I);
129 Changed = true;
130 ++NumLowered;
131 }
132 }
133
134 return Changed;
135}
136