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