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-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit 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-03 01:11:54 +00:00
|
|
|
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)
|
Executive summary: getTypeSize -> getTypeStoreSize / getABITypeSize.
The meaning of getTypeSize was not clear - clarifying it is important
now that we have x86 long double and arbitrary precision integers.
The issue with long double is that it requires 80 bits, and this is
not a multiple of its alignment. This gives a primitive type for
which getTypeSize differed from getABITypeSize. For arbitrary precision
integers it is even worse: there is the minimum number of bits needed to
hold the type (eg: 36 for an i36), the maximum number of bits that will
be overwriten when storing the type (40 bits for i36) and the ABI size
(i.e. the storage size rounded up to a multiple of the alignment; 64 bits
for i36).
This patch removes getTypeSize (not really - it is still there but
deprecated to allow for a gradual transition). Instead there is:
(1) getTypeSizeInBits - a number of bits that suffices to hold all
values of the type. For a primitive type, this is the minimum number
of bits. For an i36 this is 36 bits. For x86 long double it is 80.
This corresponds to gcc's TYPE_PRECISION.
(2) getTypeStoreSizeInBits - the maximum number of bits that is
written when storing the type (or read when reading it). For an
i36 this is 40 bits, for an x86 long double it is 80 bits. This
is the size alias analysis is interested in (getTypeStoreSize
returns the number of bytes). There doesn't seem to be anything
corresponding to this in gcc.
(3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded
up to a multiple of the alignment. For an i36 this is 64, for an
x86 long double this is 96 or 128 depending on the OS. This is the
spacing between consecutive elements when you form an array out of
this type (getABITypeSize returns the number of bytes). This is
TYPE_SIZE in gcc.
Since successive elements in a SequentialType (arrays, pointers
and vectors) need to be aligned, the spacing between them will be
given by getABITypeSize. This means that the size of an array
is the length times the getABITypeSize. It also means that GEP
computations need to use getABITypeSize when computing offsets.
Furthermore, if an alloca allocates several elements at once then
these too need to be aligned, so the size of the alloca has to be
the number of elements multiplied by getABITypeSize. Logically
speaking this doesn't have to be the case when allocating just
one element, but it is simpler to also use getABITypeSize in this
case. So alloca's and mallocs should use getABITypeSize. Finally,
since gcc's only notion of size is that given by getABITypeSize, if
you want to output assembler etc the same as gcc then getABITypeSize
is the size you want.
Since a store will overwrite no more than getTypeStoreSize bytes,
and a read will read no more than that many bytes, this is the
notion of size appropriate for alias analysis calculations.
In this patch I have corrected all type size uses except some of
those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard
cases). I will get around to auditing these too at some point,
but I could do with some help.
Finally, I made one change which I think wise but others might
consider pointless and suboptimal: in an unpacked struct the
amount of space allocated for a field is now given by the ABI
size rather than getTypeStoreSize. I did this because every
other place that reserves memory for a type (eg: alloca) now
uses getABITypeSize, and I didn't want to make an exception
for unpacked structs, i.e. I did it to make things more uniform.
This only effects structs containing long doubles and arbitrary
precision integers. If someone wants to pack these types more
tightly they can always use a packed struct.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
2007-11-01 20:53:16 +00:00
|
|
|
MallocArg = ConstantInt::get(Type::Int64Ty, TD.getABITypeSize(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
|
|
|
|