blob: d50a3c93eb95cd1eebeb4a2db98dfd440f444113 [file] [log] [blame]
Victor Hernandez37f513d2009-10-17 01:18:07 +00001//===- LowerAllocations.cpp - Reduce free insts to calls ------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
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 {
Victor Hernandez37f513d2009-10-17 01:18:07 +000032 /// LowerAllocations - Turn free instructions into @free calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 ///
Nick Lewycky492d06e2009-10-25 06:33:48 +000034 class LowerAllocations : public BasicBlockPass {
Evan Chenge5406462009-09-10 04:36:43 +000035 Constant *FreeFunc; // Functions in the module we are processing
36 // Initialized by doInitialization
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 public:
38 static char ID; // Pass ID, replacement for typeid
Victor Hernandez37f513d2009-10-17 01:18:07 +000039 explicit LowerAllocations()
40 : BasicBlockPass(&ID), FreeFunc(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetData>();
44 AU.setPreservesCFG();
45
46 // This is a cluster of orthogonal Transforms:
47 AU.addPreserved<UnifyFunctionExitNodes>();
48 AU.addPreservedID(PromoteMemoryToRegisterID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 AU.addPreservedID(LowerSwitchID);
50 AU.addPreservedID(LowerInvokePassID);
51 }
52
53 /// doPassInitialization - For the lower allocations pass, this ensures that
Victor Hernandez37f513d2009-10-17 01:18:07 +000054 /// a module contains a declaration for a free function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 ///
56 bool doInitialization(Module &M);
57
58 virtual bool doInitialization(Function &F) {
Devang Patel35d69b02008-11-18 18:43:07 +000059 return doInitialization(*F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61
62 /// runOnBasicBlock - This method does the actual work of converting
63 /// instructions over, assuming that the pass has already been initialized.
64 ///
65 bool runOnBasicBlock(BasicBlock &BB);
66 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067}
68
Dan Gohman089efff2008-05-13 00:00:25 +000069char LowerAllocations::ID = 0;
70static RegisterPass<LowerAllocations>
71X("lowerallocs", "Lower allocations from instructions to calls");
72
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073// Publically exposed interface to pass...
Dan Gohman66a636e2008-05-13 02:05:11 +000074const PassInfo *const llvm::LowerAllocationsID = &X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075// createLowerAllocationsPass - Interface to this file...
Victor Hernandez37f513d2009-10-17 01:18:07 +000076Pass *llvm::createLowerAllocationsPass() {
77 return new LowerAllocations();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078}
79
80
81// doInitialization - For the lower allocations pass, this ensures that a
Victor Hernandez37f513d2009-10-17 01:18:07 +000082// module contains a declaration for a free function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083//
84// This function is always successful.
85//
86bool LowerAllocations::doInitialization(Module &M) {
Duncan Sandsf2519d62009-10-06 15:40:36 +000087 const Type *BPTy = Type::getInt8PtrTy(M.getContext());
Owen Anderson35b47072009-08-13 21:58:54 +000088 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
89 BPTy, (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 return true;
91}
92
93// runOnBasicBlock - This method does the actual work of converting
94// instructions over, assuming that the pass has already been initialized.
95//
96bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
97 bool Changed = false;
Evan Chenge5406462009-09-10 04:36:43 +000098 assert(FreeFunc && "Pass not initialized!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 BasicBlock::InstListType &BBIL = BB.getInstList();
101
Victor Hernandez37f513d2009-10-17 01:18:07 +0000102 // Loop over all of the instructions, looking for free instructions
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Victor Hernandez37f513d2009-10-17 01:18:07 +0000104 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // Insert a call to the free function...
Victor Hernandez93946082009-10-24 04:23:03 +0000106 CallInst::CreateFree(FI->getOperand(0), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 // Delete the old free instruction
109 I = --BBIL.erase(I);
110 Changed = true;
111 ++NumLowered;
112 }
113 }
114
115 return Changed;
116}
117