2002-05-07 19:02:48 +00:00
|
|
|
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2004-01-28 20:43:01 +00:00
|
|
|
// The LowerAllocations transformation is a target-dependent tranformation
|
2002-05-07 19:02:48 +00:00
|
|
|
// because it depends on the size of data types and alignment constraints.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
#define DEBUG_TYPE "lowerallocs"
|
2002-07-23 22:04:17 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2006-05-09 04:13:41 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2002-01-31 00:45:11 +00:00
|
|
|
#include "llvm/Module.h"
|
2001-10-15 17:31:51 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2002-05-07 18:12:18 +00:00
|
|
|
#include "llvm/Constants.h"
|
2009-07-05 22:41:43 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-12-13 20:00:02 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
STATISTIC(NumLowered, "Number of allocations lowered");
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
namespace {
|
2009-08-17 18:45:31 +00:00
|
|
|
/// LowerAllocations - Turn malloc and free instructions into @malloc and
|
|
|
|
/// @free calls.
|
2002-09-25 23:47:47 +00:00
|
|
|
///
|
2006-06-28 23:17:24 +00:00
|
|
|
class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
|
2009-09-10 04:36:43 +00:00
|
|
|
Constant *FreeFunc; // Functions in the module we are processing
|
|
|
|
// Initialized by doInitialization
|
2005-03-03 01:03:43 +00:00
|
|
|
bool LowerMallocArgToInteger;
|
2002-09-25 23:47:47 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit LowerAllocations(bool LowerToInt = false)
|
2009-09-10 04:36:43 +00:00
|
|
|
: BasicBlockPass(&ID), FreeFunc(0),
|
2007-05-01 21:15:47 +00:00
|
|
|
LowerMallocArgToInteger(LowerToInt) {}
|
2002-09-25 23:47:47 +00:00
|
|
|
|
2004-12-13 20:00:02 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
AU.setPreservesCFG();
|
2006-05-09 04:13:41 +00:00
|
|
|
|
2007-04-16 18:10:23 +00:00
|
|
|
// This is a cluster of orthogonal Transforms:
|
2006-05-09 04:13:41 +00:00
|
|
|
AU.addPreserved<UnifyFunctionExitNodes>();
|
|
|
|
AU.addPreservedID(PromoteMemoryToRegisterID);
|
|
|
|
AU.addPreservedID(LowerSwitchID);
|
2006-05-17 21:05:27 +00:00
|
|
|
AU.addPreservedID(LowerInvokePassID);
|
2004-12-13 20:00:02 +00:00
|
|
|
}
|
|
|
|
|
2002-09-25 23:47:47 +00:00
|
|
|
/// doPassInitialization - For the lower allocations pass, this ensures that
|
|
|
|
/// a module contains a declaration for a malloc and a free function.
|
|
|
|
///
|
|
|
|
bool doInitialization(Module &M);
|
2004-12-07 08:11:36 +00:00
|
|
|
|
2004-12-13 20:00:02 +00:00
|
|
|
virtual bool doInitialization(Function &F) {
|
2008-11-18 18:43:07 +00:00
|
|
|
return doInitialization(*F.getParent());
|
2004-12-13 20:00:02 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-25 23:47:47 +00:00
|
|
|
/// runOnBasicBlock - This method does the actual work of converting
|
|
|
|
/// instructions over, assuming that the pass has already been initialized.
|
|
|
|
///
|
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
|
|
|
};
|
2002-05-07 19:02:48 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LowerAllocations::ID = 0;
|
|
|
|
static RegisterPass<LowerAllocations>
|
|
|
|
X("lowerallocs", "Lower allocations from instructions to calls");
|
|
|
|
|
2006-05-02 04:24:36 +00:00
|
|
|
// Publically exposed interface to pass...
|
2008-05-13 02:05:11 +00:00
|
|
|
const PassInfo *const llvm::LowerAllocationsID = &X;
|
2002-05-07 19:02:48 +00:00
|
|
|
// createLowerAllocationsPass - Interface to this file...
|
2007-01-25 23:23:25 +00:00
|
|
|
Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
|
2005-03-03 01:03:43 +00:00
|
|
|
return new LowerAllocations(LowerMallocArgToInteger);
|
2002-05-07 19:02:48 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
// doInitialization - For the lower allocations pass, this ensures that a
|
2001-10-15 17:31:51 +00:00
|
|
|
// module contains a declaration for a malloc and a free function.
|
|
|
|
//
|
|
|
|
// This function is always successful.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool LowerAllocations::doInitialization(Module &M) {
|
2009-08-13 21:58:54 +00:00
|
|
|
const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(M.getContext()));
|
|
|
|
FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
|
|
|
|
BPTy, (Type *)0);
|
2002-05-07 19:02:48 +00:00
|
|
|
return true;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 23:34:02 +00:00
|
|
|
// runOnBasicBlock - This method does the actual work of converting
|
2001-10-15 17:31:51 +00:00
|
|
|
// instructions over, assuming that the pass has already been initialized.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
|
2001-10-18 05:27:33 +00:00
|
|
|
bool Changed = false;
|
2009-09-10 04:36:43 +00:00
|
|
|
assert(FreeFunc && "Pass not initialized!");
|
2002-06-25 16:13:24 +00:00
|
|
|
|
|
|
|
BasicBlock::InstListType &BBIL = BB.getInstList();
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2005-03-03 01:03:43 +00:00
|
|
|
const TargetData &TD = getAnalysis<TargetData>();
|
2009-08-13 21:58:54 +00:00
|
|
|
const Type *IntPtrTy = TD.getIntPtrType(BB.getContext());
|
2004-12-13 20:00:02 +00:00
|
|
|
|
2001-10-15 17:31:51 +00:00
|
|
|
// Loop over all of the instructions, looking for malloc or free instructions
|
2002-06-25 16:13:24 +00:00
|
|
|
for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
|
2003-04-23 16:37:45 +00:00
|
|
|
if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
|
2009-09-18 19:20:02 +00:00
|
|
|
Value *ArraySize = MI->getOperand(0);
|
|
|
|
if (ArraySize->getType() != IntPtrTy)
|
|
|
|
ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy,
|
|
|
|
false /*ZExt*/, "", I);
|
|
|
|
Value *MCast = CallInst::CreateMalloc(I, IntPtrTy,
|
|
|
|
MI->getAllocatedType(), ArraySize);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-01-21 23:34:02 +00:00
|
|
|
// Replace all uses of the old malloc inst with the cast inst
|
|
|
|
MI->replaceAllUsesWith(MCast);
|
2002-09-10 22:38:47 +00:00
|
|
|
I = --BBIL.erase(I); // remove and delete the malloc instr...
|
2002-01-21 23:34:02 +00:00
|
|
|
Changed = true;
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumLowered;
|
2003-04-23 16:37:45 +00:00
|
|
|
} else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
|
2007-12-17 01:12:55 +00:00
|
|
|
Value *PtrCast =
|
|
|
|
new BitCastInst(FI->getOperand(0),
|
2009-08-13 21:58:54 +00:00
|
|
|
PointerType::getUnqual(Type::getInt8Ty(BB.getContext())), "", I);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-01-21 23:34:02 +00:00
|
|
|
// Insert a call to the free function...
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-01-21 23:34:02 +00:00
|
|
|
// Delete the old free instruction
|
2002-09-10 22:38:47 +00:00
|
|
|
I = --BBIL.erase(I);
|
2002-01-21 23:34:02 +00:00
|
|
|
Changed = true;
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumLowered;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-18 05:27:33 +00:00
|
|
|
return Changed;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|