blob: 5f7bf4c76675a60ceb3cc7876ea2022fc51a6dc6 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
28STATISTIC(NumLowered, "Number of allocations lowered");
29
30namespace {
Victor Hernandez37f513d2009-10-17 01:18:07 +000031 /// LowerAllocations - Turn free instructions into @free calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 ///
Nick Lewycky492d06e2009-10-25 06:33:48 +000033 class LowerAllocations : public BasicBlockPass {
Evan Chenge5406462009-09-10 04:36:43 +000034 Constant *FreeFunc; // Functions in the module we are processing
35 // Initialized by doInitialization
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 public:
37 static char ID; // Pass ID, replacement for typeid
Victor Hernandez37f513d2009-10-17 01:18:07 +000038 explicit LowerAllocations()
39 : BasicBlockPass(&ID), FreeFunc(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<TargetData>();
43 AU.setPreservesCFG();
44
45 // This is a cluster of orthogonal Transforms:
46 AU.addPreserved<UnifyFunctionExitNodes>();
47 AU.addPreservedID(PromoteMemoryToRegisterID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 AU.addPreservedID(LowerSwitchID);
49 AU.addPreservedID(LowerInvokePassID);
50 }
51
52 /// doPassInitialization - For the lower allocations pass, this ensures that
Victor Hernandez37f513d2009-10-17 01:18:07 +000053 /// a module contains a declaration for a free function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 ///
55 bool doInitialization(Module &M);
56
57 virtual bool doInitialization(Function &F) {
Devang Patel35d69b02008-11-18 18:43:07 +000058 return doInitialization(*F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 }
60
61 /// runOnBasicBlock - This method does the actual work of converting
62 /// instructions over, assuming that the pass has already been initialized.
63 ///
64 bool runOnBasicBlock(BasicBlock &BB);
65 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066}
67
Dan Gohman089efff2008-05-13 00:00:25 +000068char LowerAllocations::ID = 0;
69static RegisterPass<LowerAllocations>
70X("lowerallocs", "Lower allocations from instructions to calls");
71
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072// Publically exposed interface to pass...
Dan Gohman66a636e2008-05-13 02:05:11 +000073const PassInfo *const llvm::LowerAllocationsID = &X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074// createLowerAllocationsPass - Interface to this file...
Victor Hernandez37f513d2009-10-17 01:18:07 +000075Pass *llvm::createLowerAllocationsPass() {
76 return new LowerAllocations();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077}
78
79
80// doInitialization - For the lower allocations pass, this ensures that a
Victor Hernandez37f513d2009-10-17 01:18:07 +000081// module contains a declaration for a free function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082//
83// This function is always successful.
84//
85bool LowerAllocations::doInitialization(Module &M) {
Duncan Sandsf2519d62009-10-06 15:40:36 +000086 const Type *BPTy = Type::getInt8PtrTy(M.getContext());
Owen Anderson35b47072009-08-13 21:58:54 +000087 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
88 BPTy, (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 return true;
90}
91
92// runOnBasicBlock - This method does the actual work of converting
93// instructions over, assuming that the pass has already been initialized.
94//
95bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
96 bool Changed = false;
Evan Chenge5406462009-09-10 04:36:43 +000097 assert(FreeFunc && "Pass not initialized!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
99 BasicBlock::InstListType &BBIL = BB.getInstList();
100
Victor Hernandez37f513d2009-10-17 01:18:07 +0000101 // Loop over all of the instructions, looking for free instructions
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Victor Hernandez37f513d2009-10-17 01:18:07 +0000103 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 // Insert a call to the free function...
Victor Hernandez93946082009-10-24 04:23:03 +0000105 CallInst::CreateFree(FI->getOperand(0), I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 // Delete the old free instruction
108 I = --BBIL.erase(I);
109 Changed = true;
110 ++NumLowered;
111 }
112 }
113
114 return Changed;
115}
116