2002-05-07 19:02:48 +00:00
|
|
|
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-05-07 19:02:48 +00:00
|
|
|
// The LowerAllocations transformation is a target dependant tranformation
|
|
|
|
// 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-05-07 18:12:18 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-10 15:38:35 +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) {}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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...
|
2002-09-25 23:47:47 +00:00
|
|
|
Pass *createLowerAllocationsPass() {
|
|
|
|
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) {
|
2002-03-29 03:38:05 +00:00
|
|
|
const FunctionType *MallocType =
|
|
|
|
FunctionType::get(PointerType::get(Type::SByteTy),
|
2003-04-23 16:18:14 +00:00
|
|
|
std::vector<const Type*>(1, Type::UIntTy), false);
|
2002-03-29 03:38:05 +00:00
|
|
|
const FunctionType *FreeType =
|
|
|
|
FunctionType::get(Type::VoidTy,
|
2003-04-23 16:18:14 +00:00
|
|
|
std::vector<const Type*>(1,
|
|
|
|
PointerType::get(Type::SByteTy)),
|
2002-03-29 03:38:05 +00:00
|
|
|
false);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
MallocFunc = M.getOrInsertFunction("malloc", MallocType);
|
|
|
|
FreeFunc = M.getOrInsertFunction("free" , FreeType);
|
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
|
|
|
}
|
|
|
|
|
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();
|
2002-09-25 23:47:47 +00:00
|
|
|
TargetData &DataLayout = getAnalysis<TargetData>();
|
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) {
|
|
|
|
if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) {
|
|
|
|
const Type *AllocTy = MI->getType()->getElementType();
|
2002-01-21 23:34:02 +00: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...
|
2002-09-10 22:38:47 +00:00
|
|
|
MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
|
|
|
|
MallocArg, "", I);
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
2002-01-21 23:34:02 +00:00
|
|
|
|
|
|
|
// Create the call to Malloc...
|
2002-03-26 18:01:55 +00:00
|
|
|
CallInst *MCall = new CallInst(MallocFunc,
|
2003-04-23 16:18:14 +00:00
|
|
|
std::vector<Value*>(1, MallocArg), "", I);
|
2002-01-21 23:34:02 +00:00
|
|
|
|
|
|
|
// Create a cast instruction to convert to the right type...
|
2002-09-10 22:38:47 +00:00
|
|
|
CastInst *MCast = new CastInst(MCall, MI->getType(), "", I);
|
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;
|
2002-06-25 16:13:24 +00:00
|
|
|
} else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) {
|
2002-01-21 23:34:02 +00:00
|
|
|
// Cast the argument to free into a ubyte*...
|
|
|
|
CastInst *MCast = new CastInst(FI->getOperand(0),
|
2002-09-10 22:38:47 +00:00
|
|
|
PointerType::get(Type::UByteTy), "", I);
|
2002-01-21 23:34:02 +00:00
|
|
|
|
|
|
|
// Insert a call to the free function...
|
2003-04-23 16:18:14 +00:00
|
|
|
CallInst *FCall = new CallInst(FreeFunc, std::vector<Value*>(1, MCast),
|
|
|
|
"", 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
|
|
|
}
|