blob: 9c9113daa93a4a202a7814e957f5be23af35f483 [file] [log] [blame]
Victor Hernandeza3aaf852009-10-17 01:18:07 +00001//===- LowerAllocations.cpp - Reduce 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 {
Victor Hernandeza3aaf852009-10-17 01:18:07 +000032 /// LowerAllocations - Turn free instructions into @free calls.
Chris Lattner3cab9f02002-09-25 23:47:47 +000033 ///
Chris Lattner996795b2006-06-28 23:17:24 +000034 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000035 Constant *FreeFunc; // Functions in the module we are processing
36 // Initialized by doInitialization
Chris Lattner3cab9f02002-09-25 23:47:47 +000037 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000038 static char ID; // Pass ID, replacement for typeid
Victor Hernandeza3aaf852009-10-17 01:18:07 +000039 explicit LowerAllocations()
40 : BasicBlockPass(&ID), FreeFunc(0) {}
Chris Lattner04805fa2002-02-26 21:46:54 +000041
Chris Lattner8f430a32004-12-13 20:00:02 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetData>();
44 AU.setPreservesCFG();
Chris Lattner4fe87d62006-05-09 04:13:41 +000045
Anton Korobeynikovfb801512007-04-16 18:10:23 +000046 // This is a cluster of orthogonal Transforms:
Chris Lattner4fe87d62006-05-09 04:13:41 +000047 AU.addPreserved<UnifyFunctionExitNodes>();
48 AU.addPreservedID(PromoteMemoryToRegisterID);
Chris Lattner4fe87d62006-05-09 04:13:41 +000049 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000050 AU.addPreservedID(LowerInvokePassID);
Chris Lattner8f430a32004-12-13 20:00:02 +000051 }
52
Chris Lattner3cab9f02002-09-25 23:47:47 +000053 /// doPassInitialization - For the lower allocations pass, this ensures that
Victor Hernandeza3aaf852009-10-17 01:18:07 +000054 /// a module contains a declaration for a free function.
Chris Lattner3cab9f02002-09-25 23:47:47 +000055 ///
56 bool doInitialization(Module &M);
Reid Spencer9273d482004-12-07 08:11:36 +000057
Chris Lattner8f430a32004-12-13 20:00:02 +000058 virtual bool doInitialization(Function &F) {
Devang Patela13f1f32008-11-18 18:43:07 +000059 return doInitialization(*F.getParent());
Chris Lattner8f430a32004-12-13 20:00:02 +000060 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000061
Chris Lattner3cab9f02002-09-25 23:47:47 +000062 /// 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 };
Chris Lattner77f791d2002-05-07 19:02:48 +000067}
Chris Lattner04805fa2002-02-26 21:46:54 +000068
Dan Gohmand78c4002008-05-13 00:00:25 +000069char LowerAllocations::ID = 0;
70static RegisterPass<LowerAllocations>
71X("lowerallocs", "Lower allocations from instructions to calls");
72
Chris Lattner2d3a0272006-05-02 04:24:36 +000073// Publically exposed interface to pass...
Dan Gohman0479aa52008-05-13 02:05:11 +000074const PassInfo *const llvm::LowerAllocationsID = &X;
Chris Lattner77f791d2002-05-07 19:02:48 +000075// createLowerAllocationsPass - Interface to this file...
Victor Hernandeza3aaf852009-10-17 01:18:07 +000076Pass *llvm::createLowerAllocationsPass() {
77 return new LowerAllocations();
Chris Lattner77f791d2002-05-07 19:02:48 +000078}
Chris Lattner37104aa2002-04-29 14:57:45 +000079
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000080
Chris Lattner0686e432002-01-21 07:31:50 +000081// doInitialization - For the lower allocations pass, this ensures that a
Victor Hernandeza3aaf852009-10-17 01:18:07 +000082// module contains a declaration for a free function.
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000083//
84// This function is always successful.
85//
Chris Lattner113f4f42002-06-25 16:13:24 +000086bool LowerAllocations::doInitialization(Module &M) {
Duncan Sands9ed7b162009-10-06 15:40:36 +000087 const Type *BPTy = Type::getInt8PtrTy(M.getContext());
Owen Anderson55f1c092009-08-13 21:58:54 +000088 FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
89 BPTy, (Type *)0);
Chris Lattner77f791d2002-05-07 19:02:48 +000090 return true;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000091}
92
Chris Lattnerd07471d2002-01-21 23:34:02 +000093// runOnBasicBlock - This method does the actual work of converting
Chris Lattner1bb5f8e2001-10-15 17:31:51 +000094// instructions over, assuming that the pass has already been initialized.
95//
Chris Lattner113f4f42002-06-25 16:13:24 +000096bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
Chris Lattner6fea0322001-10-18 05:27:33 +000097 bool Changed = false;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000098 assert(FreeFunc && "Pass not initialized!");
Chris Lattner113f4f42002-06-25 16:13:24 +000099
100 BasicBlock::InstListType &BBIL = BB.getInstList();
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000101
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000102 // Loop over all of the instructions, looking for free instructions
Chris Lattner113f4f42002-06-25 16:13:24 +0000103 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000104 if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
Christopher Lambedf07882007-12-17 01:12:55 +0000105 Value *PtrCast =
106 new BitCastInst(FI->getOperand(0),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000107 Type::getInt8PtrTy(BB.getContext()), "", I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000108
Chris Lattnerd07471d2002-01-21 23:34:02 +0000109 // Insert a call to the free function...
Gabor Greife9ecc682008-04-06 20:25:17 +0000110 CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111
Chris Lattnerd07471d2002-01-21 23:34:02 +0000112 // Delete the old free instruction
Chris Lattnera239e682002-09-10 22:38:47 +0000113 I = --BBIL.erase(I);
Chris Lattnerd07471d2002-01-21 23:34:02 +0000114 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000115 ++NumLowered;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000116 }
117 }
118
Chris Lattner6fea0322001-10-18 05:27:33 +0000119 return Changed;
Chris Lattner1bb5f8e2001-10-15 17:31:51 +0000120}
Brian Gaeke960707c2003-11-11 22:41:34 +0000121