2002-05-07 19:02:48 +00:00
|
|
|
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
2003-10-20 19:43:21 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-23 22:04:17 +00:00
|
|
|
#include "llvm/Transforms/Scalar.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"
|
|
|
|
#include "llvm/iMemory.h"
|
|
|
|
#include "llvm/iOther.h"
|
2002-05-07 18:12:18 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<> NumLowered("lowerallocs", "Number of allocations lowered");
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-09-25 23:47:47 +00:00
|
|
|
/// LowerAllocations - Turn malloc and free instructions into %malloc and
|
|
|
|
/// %free calls.
|
|
|
|
///
|
|
|
|
class LowerAllocations : public BasicBlockPass {
|
|
|
|
Function *MallocFunc; // Functions in the module we are processing
|
|
|
|
Function *FreeFunc; // Initialized by doInitialization
|
|
|
|
public:
|
|
|
|
LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
/// runOnBasicBlock - This method does the actual work of converting
|
|
|
|
/// instructions over, assuming that the pass has already been initialized.
|
|
|
|
///
|
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<LowerAllocations>
|
|
|
|
X("lowerallocs", "Lower allocations from instructions to calls");
|
2002-05-07 19:02:48 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-05-07 19:02:48 +00:00
|
|
|
// createLowerAllocationsPass - Interface to this file...
|
2004-01-09 06:02:20 +00:00
|
|
|
FunctionPass *llvm::createLowerAllocationsPass() {
|
2002-09-25 23:47:47 +00:00
|
|
|
return new LowerAllocations();
|
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) {
|
2003-08-31 00:22:27 +00:00
|
|
|
const Type *SBPTy = PointerType::get(Type::SByteTy);
|
2004-02-28 18:51:45 +00:00
|
|
|
MallocFunc = M.getNamedFunction("malloc");
|
|
|
|
FreeFunc = M.getNamedFunction("free");
|
|
|
|
|
|
|
|
if (MallocFunc == 0)
|
|
|
|
MallocFunc = M.getOrInsertFunction("malloc", SBPTy, Type::UIntTy, 0);
|
|
|
|
if (FreeFunc == 0)
|
|
|
|
FreeFunc = M.getOrInsertFunction("free" , Type::VoidTy, SBPTy, 0);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-05-07 19:02:48 +00:00
|
|
|
return true;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
2004-07-15 01:08:08 +00:00
|
|
|
static Constant *getSizeof(const Type *Ty) {
|
|
|
|
Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
|
|
|
|
std::vector<Constant*> Idx;
|
|
|
|
Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
|
|
|
|
Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
|
|
|
|
return ConstantExpr::getCast(Ret, Type::UIntTy);
|
|
|
|
}
|
|
|
|
|
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;
|
2002-06-25 16:13:24 +00:00
|
|
|
assert(MallocFunc && FreeFunc && "Pass not initialized!");
|
|
|
|
|
|
|
|
BasicBlock::InstListType &BBIL = BB.getInstList();
|
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)) {
|
2002-06-25 16:13:24 +00:00
|
|
|
const Type *AllocTy = MI->getType()->getElementType();
|
2002-01-21 23:34:02 +00:00
|
|
|
|
2004-07-15 01:08:08 +00:00
|
|
|
// malloc(type) becomes sbyte *malloc(size)
|
|
|
|
Value *MallocArg = getSizeof(AllocTy);
|
|
|
|
if (MI->isArrayAllocation()) {
|
|
|
|
if (isa<ConstantUInt>(MallocArg) &&
|
|
|
|
cast<ConstantUInt>(MallocArg)->getValue() == 1) {
|
|
|
|
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
|
|
|
|
} else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
|
|
|
|
MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
|
|
|
|
} else {
|
|
|
|
// Multiply it by the array size if necessary...
|
|
|
|
MallocArg = BinaryOperator::create(Instruction::Mul,
|
|
|
|
MI->getOperand(0),
|
|
|
|
MallocArg, "", I);
|
|
|
|
}
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
2004-02-28 18:51:45 +00:00
|
|
|
|
|
|
|
const FunctionType *MallocFTy = MallocFunc->getFunctionType();
|
|
|
|
std::vector<Value*> MallocArgs;
|
2002-01-21 23:34:02 +00:00
|
|
|
|
2004-02-28 18:51:45 +00:00
|
|
|
if (MallocFTy->getNumParams() > 0 || MallocFTy->isVarArg()) {
|
|
|
|
if (MallocFTy->getNumParams() > 0 &&
|
|
|
|
MallocFTy->getParamType(0) != Type::UIntTy)
|
|
|
|
MallocArg = new CastInst(MallocArg, MallocFTy->getParamType(0), "",I);
|
|
|
|
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-21 23:34:02 +00:00
|
|
|
// Create the call to Malloc...
|
2004-02-28 18:51:45 +00:00
|
|
|
CallInst *MCall = new CallInst(MallocFunc, MallocArgs, "", I);
|
2002-01-21 23:34:02 +00:00
|
|
|
|
|
|
|
// Create a cast instruction to convert to the right type...
|
2004-02-28 18:51:45 +00:00
|
|
|
Value *MCast;
|
|
|
|
if (MCall->getType() != Type::VoidTy)
|
|
|
|
MCast = new CastInst(MCall, MI->getType(), "", I);
|
|
|
|
else
|
|
|
|
MCast = Constant::getNullValue(MI->getType());
|
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)) {
|
2004-02-28 18:51:45 +00:00
|
|
|
const FunctionType *FreeFTy = FreeFunc->getFunctionType();
|
|
|
|
std::vector<Value*> FreeArgs;
|
|
|
|
|
|
|
|
if (FreeFTy->getNumParams() > 0 || FreeFTy->isVarArg()) {
|
|
|
|
Value *MCast = FI->getOperand(0);
|
|
|
|
if (FreeFTy->getNumParams() > 0 &&
|
|
|
|
FreeFTy->getParamType(0) != MCast->getType())
|
|
|
|
MCast = new CastInst(MCast, FreeFTy->getParamType(0), "", I);
|
|
|
|
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)));
|
2002-01-21 23:34:02 +00:00
|
|
|
|
|
|
|
// Insert a call to the free function...
|
2004-02-28 18:51:45 +00:00
|
|
|
new CallInst(FreeFunc, FreeArgs, "", I);
|
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
|
|
|
|