2002-05-07 19:02:48 +00:00
|
|
|
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
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.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
#define DEBUG_TYPE "lowerallocs"
|
2002-07-23 22:04:17 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2006-05-09 04:13:41 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.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"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.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"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-12-13 20:00:02 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
STATISTIC(NumLowered, "Number of allocations lowered");
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
namespace {
|
2002-09-25 23:47:47 +00:00
|
|
|
/// LowerAllocations - Turn malloc and free instructions into %malloc and
|
|
|
|
/// %free calls.
|
|
|
|
///
|
2006-06-28 23:17:24 +00:00
|
|
|
class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
|
2007-01-07 08:12:01 +00:00
|
|
|
Constant *MallocFunc; // Functions in the module we are processing
|
|
|
|
Constant *FreeFunc; // Initialized by doInitialization
|
2005-03-03 01:03:43 +00:00
|
|
|
bool LowerMallocArgToInteger;
|
2002-09-25 23:47:47 +00:00
|
|
|
public:
|
2007-05-02 21:39:20 +00:00
|
|
|
static const char ID; // Pass ID, replacement for typeid
|
2005-03-03 01:03:43 +00:00
|
|
|
LowerAllocations(bool LowerToInt = false)
|
2007-05-01 21:15:47 +00:00
|
|
|
: BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0),
|
|
|
|
LowerMallocArgToInteger(LowerToInt) {}
|
2002-09-25 23:47:47 +00:00
|
|
|
|
2004-12-13 20:00:02 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
AU.setPreservesCFG();
|
2006-05-09 04:13:41 +00:00
|
|
|
|
2007-04-16 18:10:23 +00:00
|
|
|
// This is a cluster of orthogonal Transforms:
|
2006-05-09 04:13:41 +00:00
|
|
|
AU.addPreserved<UnifyFunctionExitNodes>();
|
|
|
|
AU.addPreservedID(PromoteMemoryToRegisterID);
|
|
|
|
AU.addPreservedID(LowerSelectID);
|
|
|
|
AU.addPreservedID(LowerSwitchID);
|
2006-05-17 21:05:27 +00:00
|
|
|
AU.addPreservedID(LowerInvokePassID);
|
2004-12-13 20:00:02 +00:00
|
|
|
}
|
|
|
|
|
2002-09-25 23:47:47 +00: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 08:11:36 +00:00
|
|
|
|
2004-12-13 20:00:02 +00:00
|
|
|
virtual bool doInitialization(Function &F) {
|
|
|
|
return BasicBlockPass::doInitialization(F);
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-25 23:47:47 +00:00
|
|
|
/// runOnBasicBlock - This method does the actual work of converting
|
|
|
|
/// instructions over, assuming that the pass has already been initialized.
|
|
|
|
///
|
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
|
|
|
};
|
|
|
|
|
2007-05-02 21:39:20 +00:00
|
|
|
const char LowerAllocations::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<LowerAllocations>
|
2002-09-25 23:47:47 +00:00
|
|
|
X("lowerallocs", "Lower allocations from instructions to calls");
|
2002-05-07 19:02:48 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2006-05-02 04:24:36 +00:00
|
|
|
// Publically exposed interface to pass...
|
|
|
|
const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
|
2002-05-07 19:02:48 +00:00
|
|
|
// createLowerAllocationsPass - Interface to this file...
|
2007-01-25 23:23:25 +00:00
|
|
|
Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
|
2005-03-03 01:03:43 +00:00
|
|
|
return new LowerAllocations(LowerMallocArgToInteger);
|
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) {
|
2007-01-07 08:12:01 +00:00
|
|
|
const Type *BPTy = PointerType::get(Type::Int8Ty);
|
|
|
|
// 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-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();
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2005-03-03 01:03:43 +00:00
|
|
|
const TargetData &TD = getAnalysis<TargetData>();
|
|
|
|
const Type *IntPtrTy = TD.getIntPtrType();
|
2004-12-13 20:00:02 +00:00
|
|
|
|
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();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-07-15 01:08:08 +00:00
|
|
|
// malloc(type) becomes sbyte *malloc(size)
|
2005-03-03 01:03:43 +00:00
|
|
|
Value *MallocArg;
|
|
|
|
if (LowerMallocArgToInteger)
|
2006-12-31 05:48:39 +00:00
|
|
|
MallocArg = ConstantInt::get(Type::Int64Ty, TD.getTypeSize(AllocTy));
|
2005-03-03 01:03:43 +00:00
|
|
|
else
|
|
|
|
MallocArg = ConstantExpr::getSizeOf(AllocTy);
|
2006-12-12 09:17:08 +00:00
|
|
|
MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
|
|
|
|
IntPtrTy);
|
2005-03-03 01:03:43 +00:00
|
|
|
|
2004-07-15 01:08:08 +00:00
|
|
|
if (MI->isArrayAllocation()) {
|
2004-12-13 20:00:02 +00:00
|
|
|
if (isa<ConstantInt>(MallocArg) &&
|
2007-03-02 23:03:17 +00:00
|
|
|
cast<ConstantInt>(MallocArg)->isOne()) {
|
2004-07-15 01:08:08 +00:00
|
|
|
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
|
|
|
|
} else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
|
2006-12-12 09:17:08 +00:00
|
|
|
CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
|
2004-07-15 01:08:08 +00:00
|
|
|
MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
|
|
|
|
} else {
|
2004-12-13 20:00:02 +00:00
|
|
|
Value *Scale = MI->getOperand(0);
|
|
|
|
if (Scale->getType() != IntPtrTy)
|
2006-12-13 00:50:17 +00:00
|
|
|
Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
|
|
|
|
"", I);
|
2004-12-13 20:00:02 +00:00
|
|
|
|
2004-07-15 01:08:08 +00:00
|
|
|
// Multiply it by the array size if necessary...
|
2004-12-13 20:00:02 +00:00
|
|
|
MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
|
2004-07-15 01:08:08 +00:00
|
|
|
MallocArg, "", I);
|
|
|
|
}
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
2004-02-28 18:51:45 +00:00
|
|
|
|
2007-01-07 08:12:01 +00:00
|
|
|
// Create the call to Malloc.
|
|
|
|
CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
|
2005-05-06 06:48:21 +00:00
|
|
|
MCall->setTailCall();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
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)
|
2006-12-13 00:50:17 +00:00
|
|
|
MCast = new BitCastInst(MCall, MI->getType(), "", I);
|
2004-02-28 18:51:45 +00:00
|
|
|
else
|
|
|
|
MCast = Constant::getNullValue(MI->getType());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
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)) {
|
2007-01-07 08:12:01 +00:00
|
|
|
Value *PtrCast = new BitCastInst(FI->getOperand(0),
|
|
|
|
PointerType::get(Type::Int8Ty), "", I);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-01-21 23:34:02 +00:00
|
|
|
// Insert a call to the free function...
|
2007-01-07 08:12:01 +00:00
|
|
|
(new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
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
|
|
|
|