#include "llvm/Support/DataTypes.h"
#include "llvm/Support/NonCopyable.h"
#include "llvm/Target/MachineInstrInfo.h"
+#include "llvm/Annotation.h"
+#include "llvm/Method.h"
#include <hash_map>
#include <hash_set>
+#include <values.h>
template<class _MI, class _V> class ValOpIterator;
+//************************** External Constants ****************************/
+
+const int INVALID_FRAME_OFFSET = MAXINT;
+
+
+//*************************** External Classes *****************************/
+
+
//---------------------------------------------------------------------------
// class MachineOperand
//
//---------------------------------------------------------------------------
-class MachineCodeForMethod: public NonCopyable {
+
+class MachineCodeForMethod: public NonCopyable, private Annotation {
+private:
+ static AnnotationID AID;
private:
- Method* method;
+ const Method* method;
bool compiledAsLeaf;
unsigned staticStackSize;
unsigned automaticVarsSize;
unsigned regSpillsSize;
- unsigned optionalOutgoingArgsSize;
+ unsigned currentOptionalArgsSize;
+ unsigned maxOptionalArgsSize;
+ unsigned currentTmpValuesSize;
hash_set<const ConstPoolVal*> constantsForConstPool;
- hash_map<const Value*, int> offsetsFromFP;
- hash_map<const Value*, int> offsetsFromSP;
-
- inline void incrementAutomaticVarsSize(int incr)
- { automaticVarsSize+= incr;
- staticStackSize += incr; }
+ hash_map<const Value*, int> offsets;
+ // hash_map<const Value*, int> offsetsFromSP;
public:
- /*ctor*/ MachineCodeForMethod(Method* _M)
- : method(_M), compiledAsLeaf(false), staticStackSize(0),
- automaticVarsSize(0), regSpillsSize(0), optionalOutgoingArgsSize(0) {}
-
- inline bool isCompiledAsLeafMethod() const { return compiledAsLeaf; }
- inline unsigned getStaticStackSize() const { return staticStackSize; }
- inline unsigned getAutomaticVarsSize() const { return automaticVarsSize; }
- inline unsigned getRegSpillsSize() const { return regSpillsSize; }
- inline unsigned getOptionalOutgoingArgsSize() const
- { return optionalOutgoingArgsSize; }
+ /*ctor*/ MachineCodeForMethod(const Method* method,
+ const TargetMachine& target);
+
+ // The next two methods are used to construct and to retrieve
+ // the MachineCodeForMethod object for the given method.
+ // construct() -- Allocates and initializes for a given method and target
+ // get() -- Returns a handle to the object.
+ // This should not be called before "construct()"
+ // for a given Method.
+ //
+ inline static MachineCodeForMethod& construct(const Method* method,
+ const TargetMachine& target)
+ {
+ assert(method->getAnnotation(MachineCodeForMethod::AID) == NULL &&
+ "Object already exists for this method!");
+ MachineCodeForMethod* mcInfo = new MachineCodeForMethod(method, target);
+ method->addAnnotation(mcInfo);
+ return *mcInfo;
+ }
+
+ inline static MachineCodeForMethod& get(const Method* method)
+ {
+ MachineCodeForMethod* mc = (MachineCodeForMethod*)
+ method->getAnnotation(MachineCodeForMethod::AID);
+ assert(mc && "Call construct() method first to allocate the object");
+ return *mc;
+ }
+
+ //
+ // Accessors for global information about generated code for a method.
+ //
+ inline bool isCompiledAsLeafMethod() const { return compiledAsLeaf; }
+ inline unsigned getStaticStackSize() const { return staticStackSize; }
+ inline unsigned getAutomaticVarsSize() const { return automaticVarsSize; }
+ inline unsigned getRegSpillsSize() const { return regSpillsSize; }
+ inline unsigned getMaxOptionalArgsSize() const { return maxOptionalArgsSize;}
+ inline unsigned getCurrentOptionalArgsSize() const
+ { return currentOptionalArgsSize;}
inline const hash_set<const ConstPoolVal*>&
getConstantPoolValues() const {return constantsForConstPool;}
+ //
+ // Modifiers used during code generation
+ //
+ void initializeFrameLayout (const TargetMachine& target);
+
void addToConstantPool (const ConstPoolVal* constVal)
{ constantsForConstPool.insert(constVal); }
inline void markAsLeafMethod() { compiledAsLeaf = true; }
- inline void incrementStackSize(int incr) { staticStackSize += incr; }
+ int allocateLocalVar (const TargetMachine& target,
+ const Value* local);
- inline void incrementRegSpillsSize(int incr)
- { regSpillsSize+= incr;
- staticStackSize += incr; }
+ int allocateSpilledValue (const TargetMachine& target,
+ const Type* type);
- inline void incrementOptionalOutgoingArgsSize(int incr)
- { optionalOutgoingArgsSize+= incr;
- staticStackSize += incr; }
+ int allocateOptionalArg (const TargetMachine& target,
+ const Type* type);
- void putLocalVarAtOffsetFromFP(const Value* local,
- int offset,
- unsigned int size);
+ void resetOptionalArgs (const TargetMachine& target);
- void putLocalVarAtOffsetFromSP(const Value* local,
- int offset,
+ int pushTempValue (const TargetMachine& target,
unsigned int size);
- int getOffsetFromFP (const Value* local) const;
+ void popAllTempValues (const TargetMachine& target);
- int getOffsetFromSP (const Value* local) const;
+ int getOffset (const Value* val) const;
+
+ // int getOffsetFromFP (const Value* val) const;
void dump () const;
+
+private:
+ inline void incrementAutomaticVarsSize(int incr) {
+ automaticVarsSize+= incr;
+ staticStackSize += incr;
+ }
+ inline void incrementRegSpillsSize(int incr) {
+ regSpillsSize+= incr;
+ staticStackSize += incr;
+ }
+ inline void incrementCurrentOptionalArgsSize(int incr) {
+ currentOptionalArgsSize+= incr; // stack size already includes this!
+ }
};
{
opLabel = opLabel + 100; // load/getElem with index vector
}
+ else if (opLabel == Instruction::And ||
+ opLabel == Instruction::Or ||
+ opLabel == Instruction::Xor ||
+ opLabel == Instruction::Not)
+ {
+ // Distinguish bitwise operators from logical operators!
+ if (I->getType() != Type::BoolTy)
+ opLabel = opLabel + 100; // bitwise operator
+ }
else if (opLabel == Instruction::Cast)
{
const Type *ITy = I->getType();
{
cout << endl
<< "*** Machine instructions after INSTRUCTION SELECTION" << endl;
- method->getMachineCode().dump();
+ MachineCodeForMethod::get(method).dump();
}
return false;
if (constantThatMustBeLoaded)
{ // register the value so it is emitted in the assembly
- method->getMachineCode().addToConstantPool(
+ MachineCodeForMethod::get(method).addToConstantPool(
cast<ConstPoolVal>(opValue));
}
}
if (isa<ConstPoolVal>(oldVal))
{ // register the value so it is emitted in the assembly
- method->getMachineCode().addToConstantPool(
+ MachineCodeForMethod::get(method).addToConstantPool(
cast<ConstPoolVal>(oldVal));
}
}
#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/Target/MachineFrameInfo.h"
#include "llvm/Target/MachineRegInfo.h"
#include "llvm/Method.h"
+#include "llvm/iOther.h"
#include "llvm/Instruction.h"
+AnnotationID MachineCodeForMethod::AID(
+ AnnotationManager::getID("MachineCodeForMethodAnnotation"));
//************************ Class Implementations **************************/
return os;
}
+static unsigned int
+ComputeMaxOptionalArgsSize(const TargetMachine& target, const Method* method)
+{
+ const MachineFrameInfo& frameInfo = target.getFrameInfo();
+
+ unsigned int maxSize = 0;
+
+ for (Method::inst_const_iterator I=method->inst_begin(),E=method->inst_end();
+ I != E; ++I)
+ if ((*I)->getOpcode() == Instruction::Call)
+ {
+ CallInst* callInst = cast<CallInst>(*I);
+ unsigned int numOperands = callInst->getNumOperands() - 1;
+ unsigned int numExtra = numOperands
+ - frameInfo.getNumFixedOutgoingArgs();
+
+ 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;
+ }
+
+ return maxSize;
+}
-void
-MachineCodeForMethod::putLocalVarAtOffsetFromFP(const Value* local,
- int offset,
- unsigned int size)
+
+/*ctor*/
+MachineCodeForMethod::MachineCodeForMethod(const Method* _M,
+ const TargetMachine& target)
+ : Annotation(AID),
+ method(_M), compiledAsLeaf(false), staticStackSize(0),
+ automaticVarsSize(0), regSpillsSize(0),
+ currentOptionalArgsSize(0), maxOptionalArgsSize(0),
+ currentTmpValuesSize(0)
{
- offsetsFromFP[local] = offset;
- incrementAutomaticVarsSize(size);
+ maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method);
+ staticStackSize = maxOptionalArgsSize +
+ target.getFrameInfo().getMinStackFrameSize();
}
+int
+MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
+ const Value* val)
+{
+ // Check if we've allocated a stack slot for this value already
+ //
+ int offset = getOffset(val);
+ if (offset == INVALID_FRAME_OFFSET)
+ {
+ bool growUp;
+ int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
+ growUp);
+ offset = growUp? firstOffset + getAutomaticVarsSize()
+ : firstOffset - getAutomaticVarsSize();
+ offsets[val] = offset;
+
+ unsigned int size = target.findOptimalStorageSize(val->getType());
+ incrementAutomaticVarsSize(size);
+ }
+ return offset;
+}
-void
-MachineCodeForMethod::putLocalVarAtOffsetFromSP(const Value* local,
- int offset,
- unsigned int size)
+int
+MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
+ const Type* type)
{
- offsetsFromSP[local] = offset;
- incrementAutomaticVarsSize(size);
+ bool growUp;
+ int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
+ int offset = growUp? firstOffset + getRegSpillsSize()
+ : firstOffset - getRegSpillsSize();
+
+ unsigned int size = target.findOptimalStorageSize(type);
+ incrementRegSpillsSize(size);
+
+ return offset;
+}
+
+int
+MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target,
+ const Type* type)
+{
+ const MachineFrameInfo& frameInfo = target.getFrameInfo();
+ bool growUp;
+ int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp);
+ int offset = growUp? firstOffset + getCurrentOptionalArgsSize()
+ : firstOffset - getCurrentOptionalArgsSize();
+
+ int size = MAXINT;
+ if (frameInfo.argsOnStackHaveFixedSize())
+ size = frameInfo.getSizeOfEachArgOnStack();
+ else
+ {
+ assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets");
+ size = target.findOptimalStorageSize(type);
+ }
+
+ incrementCurrentOptionalArgsSize(size);
+
+ return offset;
}
+void
+MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target)
+{
+ currentOptionalArgsSize = 0;
+}
int
-MachineCodeForMethod::getOffsetFromFP(const Value* local) const
+MachineCodeForMethod::pushTempValue(const TargetMachine& target,
+ unsigned int size)
+{
+ bool growUp;
+ int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
+ int offset = growUp? firstTmpOffset + currentTmpValuesSize
+ : firstTmpOffset - currentTmpValuesSize;
+ currentTmpValuesSize += size;
+ return offset;
+}
+
+void
+MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
{
- hash_map<const Value*, int>::const_iterator pair = offsetsFromFP.find(local);
- assert(pair != offsetsFromFP.end() && "Offset from FP unknown for Value");
- return (*pair).second;
+ currentTmpValuesSize = 0;
}
+// void
+// MachineCodeForMethod::putLocalVarAtOffsetFromSP(const Value* local,
+// int offset,
+// unsigned int size)
+// {
+// offsetsFromSP[local] = offset;
+// incrementAutomaticVarsSize(size);
+// }
+//
+
int
-MachineCodeForMethod::getOffsetFromSP(const Value* local) const
+MachineCodeForMethod::getOffset(const Value* val) const
{
- hash_map<const Value*, int>::const_iterator pair = offsetsFromSP.find(local);
- assert(pair != offsetsFromSP.end() && "Offset from SP unknown for Value");
- return (*pair).second;
+ hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
+ return (pair == offsets.end())? INVALID_FRAME_OFFSET : (*pair).second;
}
+// int
+// MachineCodeForMethod::getOffsetFromSP(const Value* local) const
+// {
+// hash_map<const Value*, int>::const_iterator pair = offsetsFromSP.find(local);
+// return (pair == offsetsFromSP.end())? INVALID_FRAME_OFFSET : (*pair).second;
+// }
+
+
void
MachineCodeForMethod::dump() const
{
{
opLabel = opLabel + 100; // load/getElem with index vector
}
+ else if (opLabel == Instruction::And ||
+ opLabel == Instruction::Or ||
+ opLabel == Instruction::Xor ||
+ opLabel == Instruction::Not)
+ {
+ // Distinguish bitwise operators from logical operators!
+ if (I->getType() != Type::BoolTy)
+ opLabel = opLabel + 100; // bitwise operator
+ }
else if (opLabel == Instruction::Cast)
{
const Type *ITy = I->getType();
{
cout << endl
<< "*** Machine instructions after INSTRUCTION SELECTION" << endl;
- method->getMachineCode().dump();
+ MachineCodeForMethod::get(method).dump();
}
return false;
if (constantThatMustBeLoaded)
{ // register the value so it is emitted in the assembly
- method->getMachineCode().addToConstantPool(
+ MachineCodeForMethod::get(method).addToConstantPool(
cast<ConstPoolVal>(opValue));
}
}
if (isa<ConstPoolVal>(oldVal))
{ // register the value so it is emitted in the assembly
- method->getMachineCode().addToConstantPool(
+ MachineCodeForMethod::get(method).addToConstantPool(
cast<ConstPoolVal>(oldVal));
}
}