2002-04-25 12:30:43 +08:00
|
|
|
//===-- MachineCodeForMethod.cpp -------------------------------------------=//
|
2002-02-03 15:54:50 +08:00
|
|
|
//
|
|
|
|
// Purpose:
|
2002-04-08 04:49:59 +08:00
|
|
|
// Collect native machine code information for a function.
|
2002-02-03 15:54:50 +08:00
|
|
|
// This allows target-specific information about the generated code
|
2002-04-08 04:49:59 +08:00
|
|
|
// to be stored with each function.
|
2002-04-25 12:30:43 +08:00
|
|
|
//===---------------------------------------------------------------------===//
|
2002-02-03 15:54:50 +08:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineCodeForMethod.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h" // For debug output
|
2002-07-09 07:03:54 +08:00
|
|
|
#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
|
2002-02-03 15:54:50 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MachineFrameInfo.h"
|
|
|
|
#include "llvm/Target/MachineCacheInfo.h"
|
2002-04-08 04:49:59 +08:00
|
|
|
#include "llvm/Function.h"
|
2002-02-13 05:07:25 +08:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-02-03 15:54:50 +08:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include <limits.h>
|
2002-02-25 07:01:56 +08:00
|
|
|
#include <iostream>
|
2002-02-03 15:54:50 +08:00
|
|
|
|
|
|
|
const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
|
|
|
|
|
|
|
|
static AnnotationID MCFM_AID(
|
2002-04-08 04:49:59 +08:00
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
|
2002-02-03 15:54:50 +08:00
|
|
|
|
|
|
|
// The next two methods are used to construct and to retrieve
|
2002-04-08 04:49:59 +08:00
|
|
|
// the MachineCodeForFunction object for the given function.
|
|
|
|
// construct() -- Allocates and initializes for a given function and target
|
2002-02-03 15:54:50 +08:00
|
|
|
// get() -- Returns a handle to the object.
|
|
|
|
// This should not be called before "construct()"
|
2002-04-08 04:49:59 +08:00
|
|
|
// for a given Function.
|
2002-02-03 15:54:50 +08:00
|
|
|
//
|
2002-03-18 11:36:30 +08:00
|
|
|
MachineCodeForMethod&
|
2002-04-08 04:49:59 +08:00
|
|
|
MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar)
|
2002-03-18 11:36:30 +08:00
|
|
|
{
|
2002-02-03 15:54:50 +08:00
|
|
|
assert(M->getAnnotation(MCFM_AID) == 0 &&
|
2002-04-08 04:49:59 +08:00
|
|
|
"Object already exists for this function!");
|
2002-02-03 15:54:50 +08:00
|
|
|
MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
|
|
|
|
M->addAnnotation(mcInfo);
|
|
|
|
return *mcInfo;
|
|
|
|
}
|
|
|
|
|
2002-03-18 11:36:30 +08:00
|
|
|
void
|
2002-04-08 04:49:59 +08:00
|
|
|
MachineCodeForMethod::destruct(const Function *M)
|
2002-03-18 11:36:30 +08:00
|
|
|
{
|
2002-02-03 15:54:50 +08:00
|
|
|
bool Deleted = M->deleteAnnotation(MCFM_AID);
|
2002-04-08 04:49:59 +08:00
|
|
|
assert(Deleted && "Machine code did not exist for function!");
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|
|
|
|
|
2002-03-18 11:36:30 +08:00
|
|
|
MachineCodeForMethod&
|
2002-04-08 04:49:59 +08:00
|
|
|
MachineCodeForMethod::get(const Function *F)
|
2002-03-18 11:36:30 +08:00
|
|
|
{
|
2002-04-08 04:49:59 +08:00
|
|
|
MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID);
|
2002-02-03 15:54:50 +08:00
|
|
|
assert(mc && "Call construct() method first to allocate the object");
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2002-04-25 12:30:43 +08:00
|
|
|
ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
|
|
|
|
unsigned &maxOptionalNumArgs)
|
2002-02-03 15:54:50 +08:00
|
|
|
{
|
|
|
|
const MachineFrameInfo& frameInfo = target.getFrameInfo();
|
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
unsigned maxSize = 0;
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2002-06-26 00:13:21 +08: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)
|
|
|
|
sizeForThisCall += target.findOptimalStorageSize(callInst->
|
|
|
|
getOperand(i)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxSize < sizeForThisCall)
|
|
|
|
maxSize = sizeForThisCall;
|
|
|
|
|
|
|
|
if ((int)maxOptionalNumArgs < numExtra)
|
|
|
|
maxOptionalNumArgs = (unsigned) numExtra;
|
|
|
|
}
|
2002-03-18 11:36:30 +08:00
|
|
|
|
2002-02-03 15:54:50 +08:00
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align data larger than one L1 cache line on L1 cache line boundaries.
|
|
|
|
// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
|
|
|
|
//
|
|
|
|
// THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
|
|
|
|
// SHOULD BE USED DIRECTLY THERE
|
|
|
|
//
|
|
|
|
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)
|
|
|
|
if (sz >= size)
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*ctor*/
|
2002-04-08 04:49:59 +08:00
|
|
|
MachineCodeForMethod::MachineCodeForMethod(const Function *F,
|
2002-02-03 15:54:50 +08:00
|
|
|
const TargetMachine& target)
|
|
|
|
: Annotation(MCFM_AID),
|
2002-04-25 12:30:43 +08:00
|
|
|
method(F), staticStackSize(0),
|
2002-02-03 15:54:50 +08:00
|
|
|
automaticVarsSize(0), regSpillsSize(0),
|
2002-04-25 12:30:43 +08:00
|
|
|
maxOptionalArgsSize(0), maxOptionalNumArgs(0),
|
|
|
|
currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
|
|
|
|
spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
|
2002-02-03 15:54:50 +08:00
|
|
|
{
|
2002-04-25 12:30:43 +08:00
|
|
|
maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
|
|
|
|
maxOptionalNumArgs);
|
2002-03-18 11:36:30 +08:00
|
|
|
staticStackSize = maxOptionalArgsSize
|
|
|
|
+ target.getFrameInfo().getMinStackFrameSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
|
|
|
|
const Value* val,
|
2002-03-24 11:39:26 +08:00
|
|
|
unsigned int& getPaddedSize,
|
|
|
|
unsigned int sizeToUse = 0)
|
2002-03-18 11:36:30 +08:00
|
|
|
{
|
|
|
|
bool growUp;
|
|
|
|
int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
|
|
|
|
growUp);
|
|
|
|
unsigned char align;
|
2002-03-24 11:39:26 +08:00
|
|
|
if (sizeToUse == 0)
|
2002-03-18 11:36:30 +08:00
|
|
|
{
|
2002-03-24 11:39:26 +08:00
|
|
|
sizeToUse = target.findOptimalStorageSize(val->getType());
|
2002-03-18 11:36:30 +08:00
|
|
|
// align = target.DataLayout.getTypeAlignment(val->getType());
|
|
|
|
}
|
2002-03-24 11:39:26 +08:00
|
|
|
|
|
|
|
align = SizeToAlignment(sizeToUse, target);
|
2002-03-18 11:36:30 +08:00
|
|
|
|
|
|
|
int offset = getAutomaticVarsSize();
|
|
|
|
if (! growUp)
|
2002-03-24 11:39:26 +08:00
|
|
|
offset += sizeToUse;
|
2002-03-18 11:36:30 +08:00
|
|
|
|
|
|
|
if (unsigned int mod = offset % align)
|
|
|
|
{
|
2002-03-24 11:39:26 +08:00
|
|
|
offset += align - mod;
|
|
|
|
getPaddedSize = sizeToUse + align - mod;
|
2002-03-18 11:36:30 +08:00
|
|
|
}
|
2002-03-24 11:39:26 +08:00
|
|
|
else
|
|
|
|
getPaddedSize = sizeToUse;
|
|
|
|
|
2002-03-18 11:36:30 +08:00
|
|
|
offset = growUp? firstOffset + offset
|
|
|
|
: firstOffset - offset;
|
2002-03-24 11:39:26 +08:00
|
|
|
|
2002-03-18 11:36:30 +08:00
|
|
|
return offset;
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
|
|
|
|
const Value* val,
|
2002-03-24 11:39:26 +08:00
|
|
|
unsigned int sizeToUse = 0)
|
2002-02-03 15:54:50 +08:00
|
|
|
{
|
2002-04-25 12:30:43 +08: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 15:54:50 +08: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 11:39:26 +08:00
|
|
|
unsigned int getPaddedSize;
|
|
|
|
offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
|
|
|
|
sizeToUse);
|
2002-02-03 15:54:50 +08:00
|
|
|
offsets[val] = offset;
|
2002-03-24 11:39:26 +08:00
|
|
|
incrementAutomaticVarsSize(getPaddedSize);
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2002-03-18 11:36:30 +08:00
|
|
|
|
2002-02-03 15:54:50 +08:00
|
|
|
int
|
|
|
|
MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
|
|
|
|
const Type* type)
|
|
|
|
{
|
2002-04-25 12:30:43 +08: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-02-03 15:54:50 +08:00
|
|
|
unsigned int size = target.findOptimalStorageSize(type);
|
|
|
|
unsigned char align = target.DataLayout.getTypeAlignment(type);
|
|
|
|
|
|
|
|
bool growUp;
|
|
|
|
int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
|
|
|
|
|
|
|
|
int offset = getRegSpillsSize();
|
|
|
|
if (! growUp)
|
|
|
|
offset += size;
|
|
|
|
|
|
|
|
if (unsigned int mod = offset % align)
|
|
|
|
{
|
|
|
|
offset += align - mod;
|
|
|
|
size += align - mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = growUp? firstOffset + offset
|
|
|
|
: firstOffset - offset;
|
|
|
|
|
|
|
|
incrementRegSpillsSize(size);
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::pushTempValue(const TargetMachine& target,
|
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
// Compute a power-of-2 alignment according to the possible sizes,
|
|
|
|
// but not greater than the alignment of the largest type we support
|
|
|
|
// (currently a double word -- see class TargetData).
|
|
|
|
unsigned char align = 1;
|
|
|
|
for (; align < size && align < target.DataLayout.getDoubleAlignment();
|
|
|
|
align = 2*align)
|
|
|
|
;
|
|
|
|
|
|
|
|
bool growUp;
|
|
|
|
int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
|
|
|
|
|
|
|
|
int offset = currentTmpValuesSize;
|
|
|
|
if (! growUp)
|
|
|
|
offset += size;
|
|
|
|
|
|
|
|
if (unsigned int mod = offset % align)
|
|
|
|
{
|
|
|
|
offset += align - mod;
|
|
|
|
size += align - mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
|
|
|
|
|
2002-04-01 02:57:49 +08:00
|
|
|
incrementTmpAreaSize(size);
|
2002-02-03 15:54:50 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
|
|
|
|
{
|
2002-04-01 02:57:49 +08:00
|
|
|
resetTmpAreaSize();
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::getOffset(const Value* val) const
|
|
|
|
{
|
|
|
|
std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
|
|
|
|
return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineCodeForMethod::dump() const
|
|
|
|
{
|
2002-02-25 07:01:56 +08:00
|
|
|
std::cerr << "\n" << method->getReturnType()
|
|
|
|
<< " \"" << method->getName() << "\"\n";
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2002-06-26 00:13:21 +08:00
|
|
|
for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
|
2002-02-03 15:54:50 +08:00
|
|
|
{
|
2002-07-11 08:17:17 +08:00
|
|
|
std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
|
2002-07-09 07:03:54 +08:00
|
|
|
MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
|
2002-02-03 15:54:50 +08:00
|
|
|
for (unsigned i=0; i < mvec.size(); i++)
|
2002-02-25 07:01:56 +08:00
|
|
|
std::cerr << "\t" << *mvec[i];
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|
2002-04-08 04:49:59 +08:00
|
|
|
std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
|
2002-02-03 15:54:50 +08:00
|
|
|
}
|