2002-10-28 01:16:38 +00:00
|
|
|
//===-- MachineFunction.cpp -----------------------------------------------===//
|
2002-02-03 07:54:50 +00:00
|
|
|
//
|
2002-10-28 01:16:38 +00:00
|
|
|
// Collect native machine code information for a function. This allows
|
|
|
|
// target-specific information about the generated code to be stored with each
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-03 07:54:50 +00:00
|
|
|
|
2002-10-28 01:16:38 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-12-25 05:03:22 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-10-28 01:12:41 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
2002-12-25 05:03:22 +00:00
|
|
|
#include "llvm/CodeGen/SSARegMap.h"
|
2002-02-03 07:54:50 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MachineFrameInfo.h"
|
|
|
|
#include "llvm/Target/MachineCacheInfo.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-02-03 07:54:50 +00:00
|
|
|
#include "llvm/iOther.h"
|
2002-10-28 01:12:41 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-02-03 07:54:50 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
|
|
|
|
|
2002-10-29 23:18:43 +00:00
|
|
|
static AnnotationID MF_AID(
|
2002-04-07 20:49:59 +00:00
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
|
2002-02-03 07:54:50 +00:00
|
|
|
|
2002-10-28 01:12:41 +00:00
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Code generation/destruction passes
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ConstructMachineFunction : public FunctionPass {
|
|
|
|
TargetMachine &Target;
|
|
|
|
public:
|
|
|
|
ConstructMachineFunction(TargetMachine &T) : Target(T) {}
|
|
|
|
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "ConstructMachineFunction";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) {
|
2002-10-29 23:40:03 +00:00
|
|
|
MachineFunction::construct(&F, Target).CalculateArgSize();
|
2002-10-28 01:12:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DestroyMachineFunction : public FunctionPass {
|
|
|
|
const char *getPassName() const { return "FreeMachineFunction"; }
|
|
|
|
|
|
|
|
static void freeMachineCode(Instruction &I) {
|
|
|
|
MachineCodeForInstruction::destroy(&I);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
|
|
|
|
for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
|
|
|
|
MachineCodeForInstruction::get(I).dropAllReferences();
|
|
|
|
|
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
|
|
|
|
for_each(FI->begin(), FI->end(), freeMachineCode);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2002-10-30 00:48:05 +00:00
|
|
|
|
|
|
|
struct Printer : public FunctionPass {
|
|
|
|
const char *getPassName() const { return "MachineFunction Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
MachineFunction::get(&F).dump();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2002-10-28 01:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
|
|
|
|
return new ConstructMachineFunction(Target);
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createMachineCodeDestructionPass() {
|
|
|
|
return new DestroyMachineFunction();
|
|
|
|
}
|
|
|
|
|
2002-10-30 00:48:05 +00:00
|
|
|
Pass *createMachineFunctionPrinterPass() {
|
|
|
|
return new Printer();
|
|
|
|
}
|
|
|
|
|
2002-10-28 01:12:41 +00:00
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// MachineFunction implementation
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2002-10-30 00:48:05 +00:00
|
|
|
MachineFunction::MachineFunction(const Function *F,
|
|
|
|
const TargetMachine& target)
|
2002-12-25 05:03:22 +00:00
|
|
|
: Annotation(MF_AID), Fn(F), Target(target) {
|
2002-11-20 18:55:27 +00:00
|
|
|
SSARegMapping = new SSARegMap();
|
2002-12-25 05:03:22 +00:00
|
|
|
|
|
|
|
// FIXME: move state into another class
|
|
|
|
staticStackSize = automaticVarsSize = regSpillsSize = 0;
|
|
|
|
maxOptionalArgsSize = maxOptionalNumArgs = currentTmpValuesSize = 0;
|
|
|
|
maxTmpValuesSize = 0;
|
|
|
|
compiledAsLeaf = spillsAreaFrozen = automaticVarsAreaFrozen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineFunction::~MachineFunction() {
|
|
|
|
delete SSARegMapping;
|
2002-10-30 00:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::dump() const { print(std::cerr); }
|
|
|
|
|
|
|
|
void MachineFunction::print(std::ostream &OS) const {
|
|
|
|
OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
|
|
|
|
|
|
|
|
for (const_iterator BB = begin(); BB != end(); ++BB) {
|
|
|
|
BasicBlock *LBB = BB->getBasicBlock();
|
2002-12-15 20:35:25 +00:00
|
|
|
OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
|
2002-10-30 00:48:05 +00:00
|
|
|
for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
|
|
|
|
OS << "\t";
|
|
|
|
(*I)->print(OS, Target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
// The next two methods are used to construct and to retrieve
|
2002-04-07 20:49:59 +00:00
|
|
|
// the MachineCodeForFunction object for the given function.
|
|
|
|
// construct() -- Allocates and initializes for a given function and target
|
2002-02-03 07:54:50 +00:00
|
|
|
// get() -- Returns a handle to the object.
|
|
|
|
// This should not be called before "construct()"
|
2002-04-07 20:49:59 +00:00
|
|
|
// for a given Function.
|
2002-02-03 07:54:50 +00:00
|
|
|
//
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction&
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2002-10-29 23:18:43 +00:00
|
|
|
assert(Fn->getAnnotation(MF_AID) == 0 &&
|
2002-04-07 20:49:59 +00:00
|
|
|
"Object already exists for this function!");
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
|
|
|
|
Fn->addAnnotation(mcInfo);
|
2002-02-03 07:54:50 +00:00
|
|
|
return *mcInfo;
|
|
|
|
}
|
|
|
|
|
2002-03-18 03:36:30 +00:00
|
|
|
void
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction::destruct(const Function *Fn)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2002-10-29 23:18:43 +00:00
|
|
|
bool Deleted = Fn->deleteAnnotation(MF_AID);
|
2002-04-07 20:49:59 +00:00
|
|
|
assert(Deleted && "Machine code did not exist for function!");
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction& MachineFunction::get(const Function *F)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2002-10-29 23:18:43 +00:00
|
|
|
MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
|
2002-02-03 07:54:50 +00:00
|
|
|
assert(mc && "Call construct() method first to allocate the object");
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
|
2002-12-25 05:03:22 +00:00
|
|
|
void MachineFunction::clearSSARegMap() {
|
|
|
|
delete SSARegMapping;
|
|
|
|
SSARegMapping = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
static unsigned
|
2002-04-25 04:30:43 +00:00
|
|
|
ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
|
|
|
|
unsigned &maxOptionalNumArgs)
|
2002-02-03 07:54:50 +00:00
|
|
|
{
|
|
|
|
const MachineFrameInfo& frameInfo = target.getFrameInfo();
|
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
unsigned maxSize = 0;
|
2002-02-03 07:54:50 +00:00
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
|
|
|
|
{
|
|
|
|
unsigned numOperands = callInst->getNumOperands() - 1;
|
|
|
|
int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
|
|
|
|
if (numExtra <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned int sizeForThisCall;
|
|
|
|
if (frameInfo.argsOnStackHaveFixedSize())
|
|
|
|
{
|
|
|
|
int argSize = frameInfo.getSizeOfEachArgOnStack();
|
|
|
|
sizeForThisCall = numExtra * (unsigned) argSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0 && "UNTESTED CODE: Size per stack argument is not "
|
|
|
|
"fixed on this architecture: use actual arg sizes to "
|
|
|
|
"compute MaxOptionalArgsSize");
|
|
|
|
sizeForThisCall = 0;
|
|
|
|
for (unsigned i = 0; i < numOperands; ++i)
|
2002-10-11 16:10:53 +00:00
|
|
|
sizeForThisCall += target.DataLayout.getTypeSize(callInst->
|
2002-06-25 16:13:21 +00:00
|
|
|
getOperand(i)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxSize < sizeForThisCall)
|
|
|
|
maxSize = sizeForThisCall;
|
|
|
|
|
|
|
|
if ((int)maxOptionalNumArgs < numExtra)
|
|
|
|
maxOptionalNumArgs = (unsigned) numExtra;
|
|
|
|
}
|
2002-03-18 03:36:30 +00:00
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align data larger than one L1 cache line on L1 cache line boundaries.
|
2002-09-16 15:18:16 +00:00
|
|
|
// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
|
|
|
|
// but not higher than the alignment of the largest type we support
|
|
|
|
// (currently a double word). -- see class TargetData).
|
2002-02-03 07:54:50 +00:00
|
|
|
//
|
2002-09-16 15:18:16 +00:00
|
|
|
// This function is similar to the corresponding function in EmitAssembly.cpp
|
|
|
|
// but they are unrelated. This one does not align at more than a
|
|
|
|
// double-word boundary whereas that one might.
|
2002-02-03 07:54:50 +00:00
|
|
|
//
|
|
|
|
inline unsigned int
|
|
|
|
SizeToAlignment(unsigned int size, const TargetMachine& target)
|
|
|
|
{
|
|
|
|
unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
|
|
|
|
if (size > (unsigned) cacheLineSize / 2)
|
|
|
|
return cacheLineSize;
|
|
|
|
else
|
|
|
|
for (unsigned sz=1; /*no condition*/; sz *= 2)
|
2002-09-16 15:18:16 +00:00
|
|
|
if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
|
2002-02-03 07:54:50 +00:00
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-29 23:40:03 +00:00
|
|
|
void MachineFunction::CalculateArgSize() {
|
|
|
|
maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
|
2002-04-25 04:30:43 +00:00
|
|
|
maxOptionalNumArgs);
|
2002-03-18 03:36:30 +00:00
|
|
|
staticStackSize = maxOptionalArgsSize
|
2002-10-29 23:40:03 +00:00
|
|
|
+ Target.getFrameInfo().getMinStackFrameSize();
|
2002-03-18 03:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
|
2002-03-18 03:36:30 +00:00
|
|
|
const Value* val,
|
2002-03-24 03:39:26 +00:00
|
|
|
unsigned int& getPaddedSize,
|
2002-07-25 06:17:51 +00:00
|
|
|
unsigned int sizeToUse)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2002-03-24 03:39:26 +00:00
|
|
|
if (sizeToUse == 0)
|
2002-09-16 15:18:16 +00:00
|
|
|
sizeToUse = target.findOptimalStorageSize(val->getType());
|
|
|
|
unsigned int align = SizeToAlignment(sizeToUse, target);
|
|
|
|
|
|
|
|
bool growUp;
|
|
|
|
int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
|
|
|
|
growUp);
|
|
|
|
int offset = growUp? firstOffset + getAutomaticVarsSize()
|
|
|
|
: firstOffset - (getAutomaticVarsSize() + sizeToUse);
|
|
|
|
|
|
|
|
int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
|
|
|
|
getPaddedSize = sizeToUse + abs(aligned - offset);
|
|
|
|
|
|
|
|
return aligned;
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::allocateLocalVar(const TargetMachine& target,
|
2002-02-03 07:54:50 +00:00
|
|
|
const Value* val,
|
2002-07-25 06:17:51 +00:00
|
|
|
unsigned int sizeToUse)
|
2002-02-03 07:54:50 +00:00
|
|
|
{
|
2002-04-25 04:30:43 +00:00
|
|
|
assert(! automaticVarsAreaFrozen &&
|
|
|
|
"Size of auto vars area has been used to compute an offset so "
|
|
|
|
"no more automatic vars should be allocated!");
|
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
// Check if we've allocated a stack slot for this value already
|
|
|
|
//
|
|
|
|
int offset = getOffset(val);
|
|
|
|
if (offset == INVALID_FRAME_OFFSET)
|
|
|
|
{
|
2002-03-24 03:39:26 +00:00
|
|
|
unsigned int getPaddedSize;
|
2002-10-28 01:16:38 +00:00
|
|
|
offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
|
2002-02-03 07:54:50 +00:00
|
|
|
offsets[val] = offset;
|
2002-03-24 03:39:26 +00:00
|
|
|
incrementAutomaticVarsSize(getPaddedSize);
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2002-09-16 15:18:16 +00:00
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::allocateSpilledValue(const TargetMachine& target,
|
2002-02-03 07:54:50 +00:00
|
|
|
const Type* type)
|
|
|
|
{
|
2002-04-25 04:30:43 +00:00
|
|
|
assert(! spillsAreaFrozen &&
|
|
|
|
"Size of reg spills area has been used to compute an offset so "
|
|
|
|
"no more register spill slots should be allocated!");
|
|
|
|
|
2002-10-11 16:10:53 +00:00
|
|
|
unsigned int size = target.DataLayout.getTypeSize(type);
|
2002-02-03 07:54:50 +00:00
|
|
|
unsigned char align = target.DataLayout.getTypeAlignment(type);
|
|
|
|
|
|
|
|
bool growUp;
|
|
|
|
int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
|
|
|
|
|
2002-09-16 15:18:16 +00:00
|
|
|
int offset = growUp? firstOffset + getRegSpillsSize()
|
|
|
|
: firstOffset - (getRegSpillsSize() + size);
|
|
|
|
|
|
|
|
int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
|
|
|
|
size += abs(aligned - offset); // include alignment padding in size
|
2002-02-03 07:54:50 +00:00
|
|
|
|
2002-09-16 15:18:16 +00:00
|
|
|
incrementRegSpillsSize(size); // update size of reg. spills area
|
|
|
|
|
|
|
|
return aligned;
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::pushTempValue(const TargetMachine& target,
|
2002-02-03 07:54:50 +00:00
|
|
|
unsigned int size)
|
|
|
|
{
|
2002-09-16 15:18:16 +00:00
|
|
|
unsigned int align = SizeToAlignment(size, target);
|
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
bool growUp;
|
2002-09-16 15:18:16 +00:00
|
|
|
int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
|
|
|
|
|
|
|
|
int offset = growUp? firstOffset + currentTmpValuesSize
|
|
|
|
: firstOffset - (currentTmpValuesSize + size);
|
|
|
|
|
|
|
|
int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
|
|
|
|
size += abs(aligned - offset); // include alignment padding in size
|
|
|
|
|
|
|
|
incrementTmpAreaSize(size); // update "current" size of tmp area
|
|
|
|
|
|
|
|
return aligned;
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::popAllTempValues(const TargetMachine& target)
|
2002-02-03 07:54:50 +00:00
|
|
|
{
|
2002-09-16 15:18:16 +00:00
|
|
|
resetTmpAreaSize(); // clear tmp area to reuse
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction::getOffset(const Value* val) const
|
2002-02-03 07:54:50 +00:00
|
|
|
{
|
2002-07-24 21:21:32 +00:00
|
|
|
hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
|
2002-10-28 01:16:38 +00:00
|
|
|
return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|