blob: 5f7bf4c76675a60ceb3cc7876ea2022fc51a6dc6 [file] [log] [blame]
Victor Hernandeza276c602009-10-17 01:18:07 +00001//===- LowerAllocations.cpp - Reduce 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"
Owen Anderson0a205a42009-07-05 22:41:43 +000022#include "llvm/LLVMContext.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000023#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Chris Lattnerde97b572004-12-13 20:00:02 +000025#include "llvm/Target/TargetData.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 {
Victor Hernandeza276c602009-10-17 01:18:07 +000031 /// LowerAllocations - Turn free instructions into @free calls.
Chris Lattner3b2106f2002-09-25 23:47:47 +000032 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +000033 class LowerAllocations : public BasicBlockPass {
Evan Chengfabcb912009-09-10 04:36:43 +000034 Constant *FreeFunc; // Functions in the module we are processing
35 // Initialized by doInitialization
Chris Lattner3b2106f2002-09-25 23:47:47 +000036 public:
Devang Patel19974732007-05-03 01:11:54 +000037 static char ID; // Pass ID, replacement for typeid
Victor Hernandeza276c602009-10-17 01:18:07 +000038 explicit LowerAllocations()
39 : BasicBlockPass(&ID), FreeFunc(0) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000040
Chris Lattnerde97b572004-12-13 20:00:02 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<TargetData>();
43 AU.setPreservesCFG();
Chris Lattner8d89e7b2006-05-09 04:13:41 +000044
Anton Korobeynikovbed29462007-04-16 18:10:23 +000045 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000046 AU.addPreserved<UnifyFunctionExitNodes>();
47 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner8d89e7b2006-05-09 04:13:41 +000048 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000049 AU.addPreservedID(LowerInvokePassID);
Chris Lattnerde97b572004-12-13 20:00:02 +000050 }
51
Chris Lattner3b2106f2002-09-25 23:47:47 +000052 /// doPassInitialization - For the lower allocations pass, this ensures that
Victor Hernandeza276c602009-10-17 01:18:07 +000053 /// a module contains a declaration for a free function.
Chris Lattner3b2106f2002-09-25 23:47:47 +000054 ///
55 bool doInitialization(Module &M);
Reid Spencer791a9592004-12-07 08:11:36 +000056
Chris Lattnerde97b572004-12-13 20:00:02 +000057 virtual bool doInitialization(Function &F) {
Devang Patel4f3096b2008-11-18 18:43:07 +000058 return doInitialization(*F.getParent());
Chris Lattnerde97b572004-12-13 20:00:02 +000059 }
Misha Brukmanfd939082005-04-21 23:48:37 +000060
Chris Lattner3b2106f2002-09-25 23:47:47 +000061 /// 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 };
Chris Lattnerade686e2002-05-07 19:02:48 +000066}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067
Dan Gohman844731a2008-05-13 00:00:25 +000068char LowerAllocations::ID = 0;
69static RegisterPass<LowerAllocations>
70X("lowerallocs", "Lower allocations from instructions to calls");
71
Chris Lattnerb3674e42006-05-02 04:24:36 +000072// Publically exposed interface to pass...
Dan Gohman6ddba2b2008-05-13 02:05:11 +000073const PassInfo *const llvm::LowerAllocationsID = &X;
Chris Lattnerade686e2002-05-07 19:02:48 +000074// createLowerAllocationsPass - Interface to this file...
Victor Hernandeza276c602009-10-17 01:18:07 +000075Pass *llvm::createLowerAllocationsPass() {
76 return new LowerAllocations();
Chris Lattnerade686e2002-05-07 19:02:48 +000077}
Chris Lattner96c466b2002-04-29 14:57:45 +000078
Chris Lattner1bffea02001-10-15 17:31:51 +000079
Chris Lattnerf4de63f2002-01-21 07:31:50 +000080// doInitialization - For the lower allocations pass, this ensures that a
Victor Hernandeza276c602009-10-17 01:18:07 +000081// module contains a declaration for a free function.
Chris Lattner1bffea02001-10-15 17:31:51 +000082//
83// This function is always successful.
84//
Chris Lattner7e708292002-06-25 16:13:24 +000085bool LowerAllocations::doInitialization(Module &M) {
Duncan Sandsac53a0b2009-10-06 15:40:36 +000086 const Type *BPTy = Type::getInt8PtrTy(M.getContext());
Owen Anderson1d0be152009-08-13 21:58:54 +000087 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
88 BPTy, (Type *)0);
Chris Lattnerade686e2002-05-07 19:02:48 +000089 return true;
Chris Lattner1bffea02001-10-15 17:31:51 +000090}
91
Chris Lattner84453722002-01-21 23:34:02 +000092// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bffea02001-10-15 17:31:51 +000093// instructions over, assuming that the pass has already been initialized.
94//
Chris Lattner7e708292002-06-25 16:13:24 +000095bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner42c9c2c2001-10-18 05:27:33 +000096 bool Changed = false;
Evan Chengfabcb912009-09-10 04:36:43 +000097 assert(FreeFunc && "Pass not initialized!");
Chris Lattner7e708292002-06-25 16:13:24 +000098
99 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bffea02001-10-15 17:31:51 +0000100
Victor Hernandeza276c602009-10-17 01:18:07 +0000101 // Loop over all of the instructions, looking for free instructions
Chris Lattner7e708292002-06-25 16:13:24 +0000102 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Victor Hernandeza276c602009-10-17 01:18:07 +0000103 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Chris Lattner84453722002-01-21 23:34:02 +0000104 // Insert a call to the free function...
Victor Hernandez66284e02009-10-24 04:23:03 +0000105 CallInst::CreateFree(FI->getOperand(0), I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000106
Chris Lattner84453722002-01-21 23:34:02 +0000107 // Delete the old free instruction
Chris Lattner1d608ab2002-09-10 22:38:47 +0000108 I = --BBIL.erase(I);
Chris Lattner84453722002-01-21 23:34:02 +0000109 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000110 ++NumLowered;
Chris Lattner1bffea02001-10-15 17:31:51 +0000111 }
112 }
113
Chris Lattner42c9c2c2001-10-18 05:27:33 +0000114 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +0000115}
Brian Gaeked0fde302003-11-11 22:41:34 +0000116