2002-04-25 06:30:43 +02:00
|
|
|
//===-- MachineCodeForMethod.cpp -------------------------------------------=//
|
2002-02-03 08:54:50 +01:00
|
|
|
//
|
|
|
|
// Purpose:
|
2002-04-07 22:49:59 +02:00
|
|
|
// Collect native machine code information for a function.
|
2002-02-03 08:54:50 +01:00
|
|
|
// This allows target-specific information about the generated code
|
2002-04-07 22:49:59 +02:00
|
|
|
// to be stored with each function.
|
2002-04-25 06:30:43 +02:00
|
|
|
//===---------------------------------------------------------------------===//
|
2002-02-03 08:54:50 +01:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineCodeForMethod.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h" // For debug output
|
2002-07-09 01:03:54 +02:00
|
|
|
#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
|
2002-02-03 08:54:50 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/MachineFrameInfo.h"
|
|
|
|
#include "llvm/Target/MachineCacheInfo.h"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2002-02-12 22:07:25 +01:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-02-03 08:54:50 +01:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include <limits.h>
|
2002-02-25 00:01:56 +01:00
|
|
|
#include <iostream>
|
2002-02-03 08:54:50 +01:00
|
|
|
|
|
|
|
const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
|
|
|
|
|
|
|
|
static AnnotationID MCFM_AID(
|
2002-04-07 22:49:59 +02:00
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
|
2002-02-03 08:54:50 +01:00
|
|
|
|
|
|
|
// The next two methods are used to construct and to retrieve
|
2002-04-07 22:49:59 +02:00
|
|
|
// the MachineCodeForFunction object for the given function.
|
|
|
|
// construct() -- Allocates and initializes for a given function and target
|
2002-02-03 08:54:50 +01:00
|
|
|
// get() -- Returns a handle to the object.
|
|
|
|
// This should not be called before "construct()"
|
2002-04-07 22:49:59 +02:00
|
|
|
// for a given Function.
|
2002-02-03 08:54:50 +01:00
|
|
|
//
|
2002-03-18 04:36:30 +01:00
|
|
|
MachineCodeForMethod&
|
2002-04-07 22:49:59 +02:00
|
|
|
MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-02-03 08:54:50 +01:00
|
|
|
assert(M->getAnnotation(MCFM_AID) == 0 &&
|
2002-04-07 22:49:59 +02:00
|
|
|
"Object already exists for this function!");
|
2002-02-03 08:54:50 +01:00
|
|
|
MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
|
|
|
|
M->addAnnotation(mcInfo);
|
|
|
|
return *mcInfo;
|
|
|
|
}
|
|
|
|
|
2002-03-18 04:36:30 +01:00
|
|
|
void
|
2002-04-07 22:49:59 +02:00
|
|
|
MachineCodeForMethod::destruct(const Function *M)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-02-03 08:54:50 +01:00
|
|
|
bool Deleted = M->deleteAnnotation(MCFM_AID);
|
2002-04-07 22:49:59 +02:00
|
|
|
assert(Deleted && "Machine code did not exist for function!");
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
2002-03-18 04:36:30 +01:00
|
|
|
MachineCodeForMethod&
|
2002-04-07 22:49:59 +02:00
|
|
|
MachineCodeForMethod::get(const Function *F)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-04-07 22:49:59 +02:00
|
|
|
MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID);
|
2002-02-03 08:54:50 +01:00
|
|
|
assert(mc && "Call construct() method first to allocate the object");
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2002-04-25 06:30:43 +02:00
|
|
|
ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
|
|
|
|
unsigned &maxOptionalNumArgs)
|
2002-02-03 08:54:50 +01:00
|
|
|
{
|
|
|
|
const MachineFrameInfo& frameInfo = target.getFrameInfo();
|
|
|
|
|
2002-06-25 18:13:21 +02:00
|
|
|
unsigned maxSize = 0;
|
2002-02-03 08:54:50 +01:00
|
|
|
|
2002-06-25 18:13:21 +02: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 18:10:53 +02:00
|
|
|
sizeForThisCall += target.DataLayout.getTypeSize(callInst->
|
2002-06-25 18:13:21 +02:00
|
|
|
getOperand(i)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxSize < sizeForThisCall)
|
|
|
|
maxSize = sizeForThisCall;
|
|
|
|
|
|
|
|
if ((int)maxOptionalNumArgs < numExtra)
|
|
|
|
maxOptionalNumArgs = (unsigned) numExtra;
|
|
|
|
}
|
2002-03-18 04:36:30 +01:00
|
|
|
|
2002-02-03 08:54:50 +01:00
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align data larger than one L1 cache line on L1 cache line boundaries.
|
2002-09-16 17:18:16 +02: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 08:54:50 +01:00
|
|
|
//
|
2002-09-16 17:18:16 +02: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 08:54:50 +01: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 17:18:16 +02:00
|
|
|
if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
|
2002-02-03 08:54:50 +01:00
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*ctor*/
|
2002-04-07 22:49:59 +02:00
|
|
|
MachineCodeForMethod::MachineCodeForMethod(const Function *F,
|
2002-02-03 08:54:50 +01:00
|
|
|
const TargetMachine& target)
|
|
|
|
: Annotation(MCFM_AID),
|
2002-04-25 06:30:43 +02:00
|
|
|
method(F), staticStackSize(0),
|
2002-02-03 08:54:50 +01:00
|
|
|
automaticVarsSize(0), regSpillsSize(0),
|
2002-04-25 06:30:43 +02:00
|
|
|
maxOptionalArgsSize(0), maxOptionalNumArgs(0),
|
|
|
|
currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
|
|
|
|
spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
|
2002-02-03 08:54:50 +01:00
|
|
|
{
|
2002-04-25 06:30:43 +02:00
|
|
|
maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
|
|
|
|
maxOptionalNumArgs);
|
2002-03-18 04:36:30 +01:00
|
|
|
staticStackSize = maxOptionalArgsSize
|
|
|
|
+ target.getFrameInfo().getMinStackFrameSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
|
|
|
|
const Value* val,
|
2002-03-24 04:39:26 +01:00
|
|
|
unsigned int& getPaddedSize,
|
2002-07-25 08:17:51 +02:00
|
|
|
unsigned int sizeToUse)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-03-24 04:39:26 +01:00
|
|
|
if (sizeToUse == 0)
|
2002-09-16 17:18:16 +02: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 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
|
|
|
|
const Value* val,
|
2002-07-25 08:17:51 +02:00
|
|
|
unsigned int sizeToUse)
|
2002-02-03 08:54:50 +01:00
|
|
|
{
|
2002-04-25 06:30:43 +02: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 08:54:50 +01: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 04:39:26 +01:00
|
|
|
unsigned int getPaddedSize;
|
|
|
|
offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
|
|
|
|
sizeToUse);
|
2002-02-03 08:54:50 +01:00
|
|
|
offsets[val] = offset;
|
2002-03-24 04:39:26 +01:00
|
|
|
incrementAutomaticVarsSize(getPaddedSize);
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2002-09-16 17:18:16 +02:00
|
|
|
|
2002-02-03 08:54:50 +01:00
|
|
|
int
|
|
|
|
MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
|
|
|
|
const Type* type)
|
|
|
|
{
|
2002-04-25 06:30:43 +02: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 18:10:53 +02:00
|
|
|
unsigned int size = target.DataLayout.getTypeSize(type);
|
2002-02-03 08:54:50 +01:00
|
|
|
unsigned char align = target.DataLayout.getTypeAlignment(type);
|
|
|
|
|
|
|
|
bool growUp;
|
|
|
|
int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
|
|
|
|
|
2002-09-16 17:18:16 +02: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 08:54:50 +01:00
|
|
|
|
2002-09-16 17:18:16 +02:00
|
|
|
incrementRegSpillsSize(size); // update size of reg. spills area
|
|
|
|
|
|
|
|
return aligned;
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::pushTempValue(const TargetMachine& target,
|
|
|
|
unsigned int size)
|
|
|
|
{
|
2002-09-16 17:18:16 +02:00
|
|
|
unsigned int align = SizeToAlignment(size, target);
|
|
|
|
|
2002-02-03 08:54:50 +01:00
|
|
|
bool growUp;
|
2002-09-16 17:18:16 +02: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 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
|
|
|
|
{
|
2002-09-16 17:18:16 +02:00
|
|
|
resetTmpAreaSize(); // clear tmp area to reuse
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MachineCodeForMethod::getOffset(const Value* val) const
|
|
|
|
{
|
2002-07-24 23:21:32 +02:00
|
|
|
hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
|
2002-02-03 08:54:50 +01:00
|
|
|
return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MachineCodeForMethod::dump() const
|
|
|
|
{
|
2002-02-25 00:01:56 +01:00
|
|
|
std::cerr << "\n" << method->getReturnType()
|
|
|
|
<< " \"" << method->getName() << "\"\n";
|
2002-02-03 08:54:50 +01:00
|
|
|
|
2002-06-25 18:13:21 +02:00
|
|
|
for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
|
2002-02-03 08:54:50 +01:00
|
|
|
{
|
2002-07-11 02:17:17 +02:00
|
|
|
std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
|
2002-07-09 01:03:54 +02:00
|
|
|
MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
|
2002-02-03 08:54:50 +01:00
|
|
|
for (unsigned i=0; i < mvec.size(); i++)
|
2002-02-25 00:01:56 +01:00
|
|
|
std::cerr << "\t" << *mvec[i];
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
2002-04-07 22:49:59 +02:00
|
|
|
std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|