mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-14 13:07:31 +00:00
514ab348fd
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
177 lines
6.6 KiB
C++
177 lines
6.6 KiB
C++
//===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
|
|
//
|
|
// 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.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// The LowerAllocations transformation is a target-dependent tranformation
|
|
// because it depends on the size of data types and alignment constraints.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "lowerallocs"
|
|
#include "llvm/Transforms/Scalar.h"
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
|
#include "llvm/Module.h"
|
|
#include "llvm/DerivedTypes.h"
|
|
#include "llvm/Instructions.h"
|
|
#include "llvm/Constants.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/Target/TargetData.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
using namespace llvm;
|
|
|
|
STATISTIC(NumLowered, "Number of allocations lowered");
|
|
|
|
namespace {
|
|
/// LowerAllocations - Turn malloc and free instructions into %malloc and
|
|
/// %free calls.
|
|
///
|
|
class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
|
|
Constant *MallocFunc; // Functions in the module we are processing
|
|
Constant *FreeFunc; // Initialized by doInitialization
|
|
bool LowerMallocArgToInteger;
|
|
public:
|
|
static char ID; // Pass ID, replacement for typeid
|
|
explicit LowerAllocations(bool LowerToInt = false)
|
|
: BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0),
|
|
LowerMallocArgToInteger(LowerToInt) {}
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
AU.addRequired<TargetData>();
|
|
AU.setPreservesCFG();
|
|
|
|
// This is a cluster of orthogonal Transforms:
|
|
AU.addPreserved<UnifyFunctionExitNodes>();
|
|
AU.addPreservedID(PromoteMemoryToRegisterID);
|
|
AU.addPreservedID(LowerSelectID);
|
|
AU.addPreservedID(LowerSwitchID);
|
|
AU.addPreservedID(LowerInvokePassID);
|
|
}
|
|
|
|
/// 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);
|
|
|
|
virtual bool doInitialization(Function &F) {
|
|
return BasicBlockPass::doInitialization(F);
|
|
}
|
|
|
|
/// runOnBasicBlock - This method does the actual work of converting
|
|
/// instructions over, assuming that the pass has already been initialized.
|
|
///
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
|
};
|
|
|
|
char LowerAllocations::ID = 0;
|
|
RegisterPass<LowerAllocations>
|
|
X("lowerallocs", "Lower allocations from instructions to calls");
|
|
}
|
|
|
|
// Publically exposed interface to pass...
|
|
const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
|
|
// createLowerAllocationsPass - Interface to this file...
|
|
Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
|
|
return new LowerAllocations(LowerMallocArgToInteger);
|
|
}
|
|
|
|
|
|
// doInitialization - For the lower allocations pass, this ensures that a
|
|
// module contains a declaration for a malloc and a free function.
|
|
//
|
|
// This function is always successful.
|
|
//
|
|
bool LowerAllocations::doInitialization(Module &M) {
|
|
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);
|
|
return true;
|
|
}
|
|
|
|
// runOnBasicBlock - This method does the actual work of converting
|
|
// instructions over, assuming that the pass has already been initialized.
|
|
//
|
|
bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
|
|
bool Changed = false;
|
|
assert(MallocFunc && FreeFunc && "Pass not initialized!");
|
|
|
|
BasicBlock::InstListType &BBIL = BB.getInstList();
|
|
|
|
const TargetData &TD = getAnalysis<TargetData>();
|
|
const Type *IntPtrTy = TD.getIntPtrType();
|
|
|
|
// Loop over all of the instructions, looking for malloc or free instructions
|
|
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();
|
|
|
|
// malloc(type) becomes sbyte *malloc(size)
|
|
Value *MallocArg;
|
|
if (LowerMallocArgToInteger)
|
|
MallocArg = ConstantInt::get(Type::Int64Ty, TD.getABITypeSize(AllocTy));
|
|
else
|
|
MallocArg = ConstantExpr::getSizeOf(AllocTy);
|
|
MallocArg = ConstantExpr::getTruncOrBitCast(cast<Constant>(MallocArg),
|
|
IntPtrTy);
|
|
|
|
if (MI->isArrayAllocation()) {
|
|
if (isa<ConstantInt>(MallocArg) &&
|
|
cast<ConstantInt>(MallocArg)->isOne()) {
|
|
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
|
|
} else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
|
|
CO = ConstantExpr::getIntegerCast(CO, IntPtrTy, false /*ZExt*/);
|
|
MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
|
|
} else {
|
|
Value *Scale = MI->getOperand(0);
|
|
if (Scale->getType() != IntPtrTy)
|
|
Scale = CastInst::createIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
|
|
"", I);
|
|
|
|
// Multiply it by the array size if necessary...
|
|
MallocArg = BinaryOperator::create(Instruction::Mul, Scale,
|
|
MallocArg, "", I);
|
|
}
|
|
}
|
|
|
|
// Create the call to Malloc.
|
|
CallInst *MCall = new CallInst(MallocFunc, MallocArg, "", I);
|
|
MCall->setTailCall();
|
|
|
|
// Create a cast instruction to convert to the right type...
|
|
Value *MCast;
|
|
if (MCall->getType() != Type::VoidTy)
|
|
MCast = new BitCastInst(MCall, MI->getType(), "", I);
|
|
else
|
|
MCast = Constant::getNullValue(MI->getType());
|
|
|
|
// Replace all uses of the old malloc inst with the cast inst
|
|
MI->replaceAllUsesWith(MCast);
|
|
I = --BBIL.erase(I); // remove and delete the malloc instr...
|
|
Changed = true;
|
|
++NumLowered;
|
|
} else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
|
|
Value *PtrCast = new BitCastInst(FI->getOperand(0),
|
|
PointerType::get(Type::Int8Ty), "", I);
|
|
|
|
// Insert a call to the free function...
|
|
(new CallInst(FreeFunc, PtrCast, "", I))->setTailCall();
|
|
|
|
// Delete the old free instruction
|
|
I = --BBIL.erase(I);
|
|
Changed = true;
|
|
++NumLowered;
|
|
}
|
|
}
|
|
|
|
return Changed;
|
|
}
|
|
|