2002-05-08 03:02:48 +08:00
|
|
|
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
2001-10-16 01:31:51 +08:00
|
|
|
//
|
2002-05-08 03:02:48 +08:00
|
|
|
// The LowerAllocations transformation is a target dependant tranformation
|
|
|
|
// because it depends on the size of data types and alignment constraints.
|
2001-10-16 01:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-24 06:04:17 +08:00
|
|
|
#include "llvm/Transforms/Scalar.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"
|
|
|
|
#include "llvm/iMemory.h"
|
|
|
|
#include "llvm/iOther.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"
|
2002-05-08 02:12:18 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-05-10 23:38:35 +08:00
|
|
|
#include "Support/StatisticReporter.h"
|
|
|
|
|
|
|
|
static Statistic<> NumLowered("lowerallocs\t- Number of allocations lowered");
|
2002-01-21 06:54:45 +08:00
|
|
|
using std::vector;
|
|
|
|
|
2002-02-27 05:46:54 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// LowerAllocations - Turn malloc and free instructions into %malloc and %free
|
|
|
|
// calls.
|
|
|
|
//
|
|
|
|
class LowerAllocations : public BasicBlockPass {
|
2002-03-27 02:01:55 +08:00
|
|
|
Function *MallocFunc; // Functions in the module we are processing
|
|
|
|
Function *FreeFunc; // Initialized by doInitialization
|
2002-02-27 05:46:54 +08:00
|
|
|
|
|
|
|
const TargetData &DataLayout;
|
|
|
|
public:
|
2002-07-24 02:06:30 +08:00
|
|
|
LowerAllocations(const TargetData &TD) : DataLayout(TD) {
|
2002-03-27 02:01:55 +08:00
|
|
|
MallocFunc = FreeFunc = 0;
|
2002-02-27 05:46:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// doPassInitialization - For the lower allocations pass, this ensures that a
|
|
|
|
// module contains a declaration for a malloc and a free function.
|
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool doInitialization(Module &M);
|
2002-02-27 05:46:54 +08:00
|
|
|
|
|
|
|
// runOnBasicBlock - This method does the actual work of converting
|
|
|
|
// instructions over, assuming that the pass has already been initialized.
|
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
2002-02-27 05:46:54 +08:00
|
|
|
};
|
2002-05-08 03:02:48 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2002-05-08 03:02:48 +08:00
|
|
|
// createLowerAllocationsPass - Interface to this file...
|
|
|
|
Pass *createLowerAllocationsPass(const TargetData &TD) {
|
|
|
|
return new LowerAllocations(TD);
|
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2002-07-27 05:12:46 +08:00
|
|
|
static RegisterOpt<LowerAllocations>
|
2002-07-24 02:06:30 +08:00
|
|
|
X("lowerallocs", "Lower allocations from instructions to calls (TD)",
|
|
|
|
createLowerAllocationsPass);
|
|
|
|
|
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) {
|
2002-03-29 11:38:05 +08:00
|
|
|
const FunctionType *MallocType =
|
|
|
|
FunctionType::get(PointerType::get(Type::SByteTy),
|
|
|
|
vector<const Type*>(1, Type::UIntTy), false);
|
|
|
|
const FunctionType *FreeType =
|
|
|
|
FunctionType::get(Type::VoidTy,
|
|
|
|
vector<const Type*>(1, PointerType::get(Type::SByteTy)),
|
|
|
|
false);
|
2001-10-16 01:31:51 +08:00
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
MallocFunc = M.getOrInsertFunction("malloc", MallocType);
|
|
|
|
FreeFunc = M.getOrInsertFunction("free" , FreeType);
|
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
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
|
|
|
|
BBIL.remove(I); // remove the malloc instr...
|
|
|
|
|
|
|
|
const Type *AllocTy = MI->getType()->getElementType();
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Get the number of bytes to be allocated for one element of the
|
|
|
|
// requested type...
|
|
|
|
unsigned Size = DataLayout.getTypeSize(AllocTy);
|
|
|
|
|
|
|
|
// malloc(type) becomes sbyte *malloc(constint)
|
|
|
|
Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
|
|
|
|
if (MI->getNumOperands() && Size == 1) {
|
|
|
|
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
|
|
|
|
} else if (MI->getNumOperands()) {
|
|
|
|
// Multiply it by the array size if neccesary...
|
|
|
|
MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
|
|
|
|
MallocArg);
|
2002-06-26 00:13:24 +08:00
|
|
|
I = ++BBIL.insert(I, cast<Instruction>(MallocArg));
|
2001-10-16 01:31:51 +08:00
|
|
|
}
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Create the call to Malloc...
|
2002-03-27 02:01:55 +08:00
|
|
|
CallInst *MCall = new CallInst(MallocFunc,
|
2002-01-22 07:34:02 +08:00
|
|
|
vector<Value*>(1, MallocArg));
|
2002-06-26 00:13:24 +08:00
|
|
|
I = BBIL.insert(I, MCall);
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Create a cast instruction to convert to the right type...
|
|
|
|
CastInst *MCast = new CastInst(MCall, MI->getType());
|
2002-06-26 00:13:24 +08:00
|
|
|
I = BBIL.insert(++I, MCast);
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Replace all uses of the old malloc inst with the cast inst
|
|
|
|
MI->replaceAllUsesWith(MCast);
|
|
|
|
delete MI; // Delete the malloc inst
|
|
|
|
Changed = true;
|
2002-05-10 23:38:35 +08:00
|
|
|
++NumLowered;
|
2002-06-26 00:13:24 +08:00
|
|
|
} else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
|
|
|
|
BBIL.remove(I);
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Cast the argument to free into a ubyte*...
|
|
|
|
CastInst *MCast = new CastInst(FI->getOperand(0),
|
|
|
|
PointerType::get(Type::UByteTy));
|
2002-06-26 00:13:24 +08:00
|
|
|
I = ++BBIL.insert(I, MCast);
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Insert a call to the free function...
|
2002-06-26 00:13:24 +08:00
|
|
|
CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast));
|
|
|
|
I = BBIL.insert(I, FCall);
|
2002-01-22 07:34:02 +08:00
|
|
|
|
|
|
|
// Delete the old free instruction
|
|
|
|
delete FI;
|
|
|
|
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
|
|
|
}
|