2001-09-18 12:41:43 +00:00
|
|
|
// $Id$ -*-c++-*-
|
|
|
|
//***************************************************************************
|
|
|
|
// File:
|
|
|
|
// InstrForest.h
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Convert SSA graph to instruction trees for instruction selection.
|
|
|
|
//
|
|
|
|
// Strategy:
|
|
|
|
// The basic idea is that we would like to group instructions into a single
|
|
|
|
// tree if one or more of them might be potentially combined into a single
|
|
|
|
// complex instruction in the target machine.
|
|
|
|
// Since this grouping is completely machine-independent, it is as
|
|
|
|
// aggressive as possible. In particular, we group two instructions
|
|
|
|
// O and I if:
|
|
|
|
// (1) Instruction O computes an operand of instruction I, and
|
|
|
|
// (2) O and I are part of the same basic block, and
|
|
|
|
// (3) O has only a single use, viz., I.
|
|
|
|
//
|
|
|
|
// History:
|
|
|
|
// 6/28/01 - Vikram Adve - Created
|
|
|
|
//**************************************************************************/
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_INSTRFOREST_H
|
|
|
|
#define LLVM_CODEGEN_INSTRFOREST_H
|
|
|
|
|
2001-07-28 04:20:33 +00:00
|
|
|
#include "llvm/Support/NonCopyable.h"
|
|
|
|
#include "llvm/Support/HashExtras.h"
|
|
|
|
#include "llvm/Instruction.h"
|
2001-09-11 23:22:43 +00:00
|
|
|
#include <hash_map>
|
|
|
|
#include <hash_set>
|
2001-07-28 04:20:33 +00:00
|
|
|
|
2001-07-21 12:39:03 +00:00
|
|
|
class ConstPoolVal;
|
|
|
|
class BasicBlock;
|
|
|
|
class Method;
|
|
|
|
class InstrTreeNode;
|
|
|
|
class InstrForest;
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// OpLabel values for special-case nodes created for instruction selection.
|
|
|
|
// All op-labels not defined here are identical to the instruction
|
|
|
|
// opcode returned by Instruction::getInstType()
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const int InvalidOp = -1;
|
|
|
|
const int VRegListOp = 97;
|
|
|
|
const int VRegNodeOp = 98;
|
|
|
|
const int ConstantNodeOp= 99;
|
|
|
|
const int LabelNodeOp = 100;
|
|
|
|
|
|
|
|
const int RetValueOp = 100 + Instruction::Ret;
|
|
|
|
const int BrCondOp = 100 + Instruction::Br;
|
|
|
|
|
2001-11-08 04:51:39 +00:00
|
|
|
const int BAndOp = 100 + Instruction::And;
|
|
|
|
const int BOrOp = 100 + Instruction::Or;
|
|
|
|
const int BXorOp = 100 + Instruction::Xor;
|
|
|
|
const int BNotOp = 100 + Instruction::Not;
|
|
|
|
|
2001-07-21 12:39:03 +00:00
|
|
|
const int SetCCOp = 100 + Instruction::SetEQ;
|
|
|
|
|
|
|
|
const int AllocaN = 100 + Instruction::Alloca; // 121
|
|
|
|
const int LoadIdx = 100 + Instruction::Load; // 122
|
|
|
|
const int GetElemPtrIdx= 100 + Instruction::GetElementPtr; // 124
|
|
|
|
|
|
|
|
const int ToBoolTy = 100 + Instruction::Cast; // 126
|
|
|
|
const int ToUByteTy = ToBoolTy + 1;
|
|
|
|
const int ToSByteTy = ToBoolTy + 2;
|
|
|
|
const int ToUShortTy = ToBoolTy + 3;
|
|
|
|
const int ToShortTy = ToBoolTy + 4;
|
|
|
|
const int ToUIntTy = ToBoolTy + 5;
|
|
|
|
const int ToIntTy = ToBoolTy + 6;
|
|
|
|
const int ToULongTy = ToBoolTy + 7;
|
|
|
|
const int ToLongTy = ToBoolTy + 8;
|
|
|
|
const int ToFloatTy = ToBoolTy + 9;
|
|
|
|
const int ToDoubleTy = ToBoolTy + 10;
|
|
|
|
const int ToArrayTy = ToBoolTy + 11;
|
|
|
|
const int ToPointerTy = ToBoolTy + 12;
|
|
|
|
|
2001-07-23 03:50:57 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Data types needed by BURG and implemented by us
|
|
|
|
//-------------------------------------------------------------------------
|
2001-07-21 12:39:03 +00:00
|
|
|
|
2001-07-23 03:50:57 +00:00
|
|
|
typedef int OpLabel;
|
|
|
|
typedef int StateLabel;
|
2001-07-21 12:39:03 +00:00
|
|
|
|
2001-07-23 03:50:57 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Declarations of data and functions created by BURG
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
extern short* burm_nts[];
|
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
extern StateLabel burm_label (InstrTreeNode* p);
|
2001-07-23 03:50:57 +00:00
|
|
|
|
|
|
|
extern StateLabel burm_state (OpLabel op, StateLabel leftState,
|
|
|
|
StateLabel rightState);
|
|
|
|
|
|
|
|
extern StateLabel burm_rule (StateLabel state, int goalNT);
|
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
extern InstrTreeNode** burm_kids (InstrTreeNode* p, int eruleno,
|
|
|
|
InstrTreeNode* kids[]);
|
2001-07-23 03:50:57 +00:00
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
extern void printcover (InstrTreeNode*, int, int);
|
|
|
|
extern void printtree (InstrTreeNode*);
|
|
|
|
extern int treecost (InstrTreeNode*, int, int);
|
|
|
|
extern void printMatches (InstrTreeNode*);
|
2001-07-23 03:50:57 +00:00
|
|
|
|
|
|
|
|
2001-07-21 12:39:03 +00:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// class InstrTreeNode
|
|
|
|
//
|
|
|
|
// A single tree node in the instruction tree used for
|
|
|
|
// instruction selection via BURG.
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2001-07-23 18:26:21 +00:00
|
|
|
class InstrTreeNode : public NonCopyableV {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
|
|
|
enum InstrTreeNodeType { NTInstructionNode,
|
|
|
|
NTVRegListNode,
|
|
|
|
NTVRegNode,
|
|
|
|
NTConstNode,
|
|
|
|
NTLabelNode };
|
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
// BASIC TREE NODE START
|
|
|
|
InstrTreeNode* LeftChild;
|
|
|
|
InstrTreeNode* RightChild;
|
|
|
|
InstrTreeNode* Parent;
|
|
|
|
OpLabel opLabel;
|
|
|
|
StateLabel state;
|
|
|
|
// BASIC TREE NODE END
|
|
|
|
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
|
|
|
InstrTreeNodeType treeNodeType;
|
|
|
|
Value* val;
|
|
|
|
|
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
InstrTreeNode(InstrTreeNodeType nodeType, Value* _val)
|
|
|
|
: treeNodeType(nodeType), val(_val) {
|
|
|
|
LeftChild = RightChild = Parent = 0;
|
|
|
|
opLabel = InvalidOp;
|
|
|
|
}
|
|
|
|
virtual ~InstrTreeNode() {}
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
InstrTreeNodeType getNodeType () const { return treeNodeType; }
|
|
|
|
|
|
|
|
Value* getValue () const { return val; }
|
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
inline OpLabel getOpLabel () const { return opLabel; }
|
2001-07-21 12:39:03 +00:00
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
inline InstrTreeNode* leftChild() const {
|
2001-09-11 23:52:11 +00:00
|
|
|
return LeftChild;
|
2001-07-21 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If right child is a list node, recursively get its *left* child
|
2001-09-11 23:52:11 +00:00
|
|
|
inline InstrTreeNode* rightChild() const {
|
|
|
|
return (!RightChild ? 0 :
|
|
|
|
(RightChild->getOpLabel() == VRegListOp
|
|
|
|
? RightChild->LeftChild : RightChild));
|
2001-07-21 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
2001-09-11 23:52:11 +00:00
|
|
|
inline InstrTreeNode *parent() const {
|
|
|
|
return Parent;
|
2001-07-21 12:39:03 +00:00
|
|
|
}
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
void dump(int dumpChildren, int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const = 0;
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
friend class InstrForest;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
class InstructionNode : public InstrTreeNode {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
InstructionNode(Instruction *_instr);
|
|
|
|
|
|
|
|
Instruction *getInstruction() const {
|
2001-07-28 04:20:33 +00:00
|
|
|
assert(treeNodeType == NTInstructionNode);
|
2001-10-02 03:41:24 +00:00
|
|
|
return cast<Instruction>(val);
|
2001-07-28 04:20:33 +00:00
|
|
|
}
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
class VRegListNode : public InstrTreeNode {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
VRegListNode() : InstrTreeNode(NTVRegListNode, 0) {
|
|
|
|
opLabel = VRegListOp;
|
|
|
|
}
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
class VRegNode : public InstrTreeNode {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
VRegNode(Value* _val) : InstrTreeNode(NTVRegNode, _val) {
|
|
|
|
opLabel = VRegNodeOp;
|
|
|
|
}
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
class ConstantNode : public InstrTreeNode {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
ConstantNode(ConstPoolVal *constVal)
|
|
|
|
: InstrTreeNode(NTConstNode, (Value*)constVal) {
|
|
|
|
opLabel = ConstantNodeOp;
|
|
|
|
}
|
|
|
|
ConstPoolVal *getConstVal() const { return (ConstPoolVal*) val;}
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
class LabelNode : public InstrTreeNode {
|
2001-07-21 12:39:03 +00:00
|
|
|
public:
|
2001-09-12 01:28:49 +00:00
|
|
|
LabelNode(BasicBlock* BB) : InstrTreeNode(NTLabelNode, (Value*)BB) {
|
|
|
|
opLabel = LabelNodeOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *getBasicBlock() const { return (BasicBlock*)val;}
|
2001-07-21 12:39:03 +00:00
|
|
|
protected:
|
2001-09-12 01:28:49 +00:00
|
|
|
virtual void dumpNode(int indent) const;
|
2001-07-21 12:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// class InstrForest
|
|
|
|
//
|
|
|
|
// A forest of instruction trees, usually for a single method.
|
|
|
|
//
|
|
|
|
// Methods:
|
|
|
|
// buildTreesForMethod() Builds the forest of trees for a method
|
|
|
|
// getTreeNodeForInstr() Returns the tree node for an Instruction
|
|
|
|
// getRootSet() Returns a set of root nodes for all the trees
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2001-10-02 03:41:24 +00:00
|
|
|
class InstrForest : private hash_map<const Instruction *, InstructionNode*> {
|
2001-09-18 12:41:43 +00:00
|
|
|
private:
|
2001-07-23 03:50:57 +00:00
|
|
|
hash_set<InstructionNode*> treeRoots;
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
public:
|
2001-09-18 12:41:43 +00:00
|
|
|
/*ctor*/ InstrForest (Method *M);
|
|
|
|
/*dtor*/ ~InstrForest ();
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
inline InstructionNode *getTreeNodeForInstr(Instruction* instr) {
|
2001-07-21 12:39:03 +00:00
|
|
|
return (*this)[instr];
|
|
|
|
}
|
|
|
|
|
2001-07-23 03:50:57 +00:00
|
|
|
inline const hash_set<InstructionNode*> &getRootSet() const {
|
2001-07-21 12:39:03 +00:00
|
|
|
return treeRoots;
|
|
|
|
}
|
|
|
|
|
2001-09-12 01:28:49 +00:00
|
|
|
void dump() const;
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
//
|
|
|
|
// Private methods for buidling the instruction forest
|
|
|
|
//
|
2001-09-12 01:28:49 +00:00
|
|
|
void setLeftChild (InstrTreeNode* parent, InstrTreeNode* child);
|
|
|
|
void setRightChild(InstrTreeNode* parent, InstrTreeNode* child);
|
|
|
|
void setParent (InstrTreeNode* child, InstrTreeNode* parent);
|
|
|
|
void noteTreeNodeForInstr(Instruction* instr, InstructionNode* treeNode);
|
2001-07-21 12:39:03 +00:00
|
|
|
|
|
|
|
InstructionNode* buildTreeForInstruction(Instruction* instr);
|
|
|
|
};
|
|
|
|
|
2001-07-28 04:20:33 +00:00
|
|
|
#endif
|