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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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 {
|
2002-09-26 07:47:47 +08:00
|
|
|
Function *MallocFunc; // Functions in the module we are processing
|
|
|
|
Function *FreeFunc; // Initialized by doInitialization
|
2005-03-03 09:03:43 +08:00
|
|
|
bool LowerMallocArgToInteger;
|
2002-09-26 07:47:47 +08:00
|
|
|
public:
|
2005-03-03 09:03:43 +08:00
|
|
|
LowerAllocations(bool LowerToInt = false)
|
|
|
|
: MallocFunc(0), FreeFunc(0), 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
|
|
|
|
2006-05-18 05:05:27 +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(LowerSelectID);
|
|
|
|
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) {
|
|
|
|
return BasicBlockPass::doInitialization(F);
|
|
|
|
}
|
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);
|
|
|
|
};
|
|
|
|
|
2006-08-28 06:42:52 +08:00
|
|
|
RegisterPass<LowerAllocations>
|
2002-09-26 07:47:47 +08:00
|
|
|
X("lowerallocs", "Lower allocations from instructions to calls");
|
2002-05-08 03:02:48 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2006-05-02 12:24:36 +08:00
|
|
|
// Publically exposed interface to pass...
|
|
|
|
const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
|
2002-05-08 03:02:48 +08:00
|
|
|
// createLowerAllocationsPass - Interface to this file...
|
2005-03-03 09:03:43 +08:00
|
|
|
FunctionPass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
|
|
|
|
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) {
|
2006-12-31 13:48:39 +08:00
|
|
|
const Type *SBPTy = PointerType::get(Type::Int8Ty);
|
2004-02-29 02:51:45 +08:00
|
|
|
MallocFunc = M.getNamedFunction("malloc");
|
|
|
|
FreeFunc = M.getNamedFunction("free");
|
|
|
|
|
2004-12-14 04:00:02 +08:00
|
|
|
if (MallocFunc == 0) {
|
|
|
|
// Prototype malloc as "void* malloc(...)", because we don't know in
|
|
|
|
// doInitialization whether size_t is int or long.
|
|
|
|
FunctionType *FT = FunctionType::get(SBPTy,std::vector<const Type*>(),true);
|
|
|
|
MallocFunc = M.getOrInsertFunction("malloc", FT);
|
|
|
|
}
|
2004-02-29 02:51:45 +08:00
|
|
|
if (FreeFunc == 0)
|
2005-10-23 12:37:20 +08:00
|
|
|
FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, (Type *)0);
|
2001-10-16 01:31:51 +08:00
|
|
|
|
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)
|
2006-12-31 13:48:39 +08:00
|
|
|
MallocArg = ConstantInt::get(Type::Int64Ty, TD.getTypeSize(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) &&
|
2006-10-20 15:07:24 +08:00
|
|
|
cast<ConstantInt>(MallocArg)->getZExtValue() == 1) {
|
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)
|
2006-12-13 08:50:17 +08:00
|
|
|
Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
|
|
|
|
"", 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...
|
2004-12-14 04:00:02 +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
|
|
|
|
|
|
|
const FunctionType *MallocFTy = MallocFunc->getFunctionType();
|
|
|
|
std::vector<Value*> MallocArgs;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-02-29 02:51:45 +08:00
|
|
|
if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
|
2004-12-14 04:00:02 +08:00
|
|
|
if (MallocFTy->isVarArg()) {
|
|
|
|
if (MallocArg->getType() != IntPtrTy)
|
2006-12-13 08:50:17 +08:00
|
|
|
MallocArg = CastInst::createIntegerCast(MallocArg, IntPtrTy,
|
|
|
|
false /*ZExt*/, "", I);
|
2004-12-14 04:00:02 +08:00
|
|
|
} else if (MallocFTy->getNumParams() > 0 &&
|
2006-12-31 13:48:39 +08:00
|
|
|
MallocFTy->getParamType(0) != Type::Int32Ty)
|
2006-12-13 08:50:17 +08:00
|
|
|
MallocArg = CastInst::createIntegerCast(
|
|
|
|
MallocArg, MallocFTy->getParamType(0), false/*ZExt*/, "",I);
|
2004-02-29 02:51:45 +08:00
|
|
|
MallocArgs.push_back(MallocArg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If malloc is prototyped to take extra arguments, pass nulls.
|
|
|
|
for (unsigned i = 1; i < MallocFTy->getNumParams(); ++i)
|
|
|
|
MallocArgs.push_back(Constant::getNullValue(MallocFTy->getParamType(i)));
|
|
|
|
|
2002-01-22 07:34:02 +08:00
|
|
|
// Create the call to Malloc...
|
2004-02-29 02:51:45 +08:00
|
|
|
CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", 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)) {
|
2004-02-29 02:51:45 +08:00
|
|
|
const FunctionType *FreeFTy = FreeFunc->getFunctionType();
|
|
|
|
std::vector<Value*> FreeArgs;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-02-29 02:51:45 +08:00
|
|
|
if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
|
|
|
|
Value *MCast = FI->getOperand(0);
|
|
|
|
if (FreeFTy->getNumParams() > 0 &&
|
|
|
|
FreeFTy->getParamType(0) != MCast->getType())
|
2006-12-13 08:50:17 +08:00
|
|
|
MCast = new BitCastInst(MCast, FreeFTy->getParamType(0), "", I);
|
2004-02-29 02:51:45 +08:00
|
|
|
FreeArgs.push_back(MCast);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If malloc is prototyped to take extra arguments, pass nulls.
|
|
|
|
for (unsigned i = 1; i < FreeFTy->getNumParams(); ++i)
|
|
|
|
FreeArgs.push_back(Constant::getNullValue(FreeFTy->getParamType(i)));
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-01-22 07:34:02 +08:00
|
|
|
// Insert a call to the free function...
|
2005-05-06 14:48:21 +08:00
|
|
|
(new CallInst(FreeFunc, FreeArgs, "", 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
|
|
|
|