2003-08-06 05:42:05 +00:00
|
|
|
//===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// This tablegen backend is responsible for emitting a description of the target
|
|
|
|
// instruction set for the code generator.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef INSTRSELECTOR_EMITTER_H
|
|
|
|
#define INSTRSELECTOR_EMITTER_H
|
|
|
|
|
|
|
|
#include "TableGenBackend.h"
|
2003-08-07 06:02:15 +00:00
|
|
|
#include "CodeGenWrappers.h"
|
2003-08-06 05:42:05 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2003-08-07 05:40:14 +00:00
|
|
|
class DagInit;
|
|
|
|
class Init;
|
2003-08-07 19:12:24 +00:00
|
|
|
class InstrSelectorEmitter;
|
2003-08-06 05:42:05 +00:00
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
/// NodeType - Represents Information parsed from the DagNode entries.
|
|
|
|
///
|
2003-08-06 05:42:05 +00:00
|
|
|
struct NodeType {
|
|
|
|
enum ArgResultTypes {
|
2003-08-12 04:56:42 +00:00
|
|
|
Any, // No constraint on type
|
2003-08-06 05:42:05 +00:00
|
|
|
Val, // A non-void type
|
|
|
|
Arg0, // Value matches the type of Arg0
|
2003-08-11 20:25:52 +00:00
|
|
|
Arg1, // Value matches the type of Arg1
|
2003-08-06 06:16:35 +00:00
|
|
|
Ptr, // Tree node is the type of the target pointer
|
2003-08-12 04:28:21 +00:00
|
|
|
I8, // Always bool
|
2003-08-06 05:42:05 +00:00
|
|
|
Void, // Tree node always returns void
|
|
|
|
};
|
|
|
|
|
|
|
|
ArgResultTypes ResultType;
|
|
|
|
std::vector<ArgResultTypes> ArgTypes;
|
|
|
|
|
|
|
|
NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
|
|
|
|
AT.swap(ArgTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeType() : ResultType(Val) {}
|
|
|
|
NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
|
|
|
|
|
|
|
|
static ArgResultTypes Translate(Record *R);
|
|
|
|
};
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// TreePatternNode - Represent a node of the tree patterns.
|
|
|
|
///
|
2003-08-07 05:40:14 +00:00
|
|
|
class TreePatternNode {
|
|
|
|
/// Operator - The operation that this node represents... this is null if this
|
|
|
|
/// is a leaf.
|
|
|
|
Record *Operator;
|
|
|
|
|
|
|
|
/// Type - The inferred value type...
|
2003-08-07 19:12:24 +00:00
|
|
|
///
|
2003-08-07 05:40:14 +00:00
|
|
|
MVT::ValueType Type;
|
|
|
|
|
|
|
|
/// Children - If this is not a leaf (Operator != 0), this is the subtrees
|
|
|
|
/// that we contain.
|
2003-08-10 23:51:52 +00:00
|
|
|
std::vector<std::pair<TreePatternNode*, std::string> > Children;
|
2003-08-07 05:40:14 +00:00
|
|
|
|
|
|
|
/// Value - If this node is a leaf, this indicates what the thing is.
|
2003-08-07 19:12:24 +00:00
|
|
|
///
|
2003-08-07 05:40:14 +00:00
|
|
|
Init *Value;
|
|
|
|
public:
|
2003-08-10 23:51:52 +00:00
|
|
|
TreePatternNode(Record *o, const std::vector<std::pair<TreePatternNode*,
|
|
|
|
std::string> > &c)
|
2003-08-07 05:40:14 +00:00
|
|
|
: Operator(o), Type(MVT::Other), Children(c), Value(0) {}
|
|
|
|
TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
|
|
|
|
|
2003-08-08 16:30:10 +00:00
|
|
|
Record *getOperator() const {
|
|
|
|
assert(Operator && "This is a leaf node!");
|
|
|
|
return Operator;
|
|
|
|
}
|
2003-08-07 05:40:14 +00:00
|
|
|
MVT::ValueType getType() const { return Type; }
|
|
|
|
void setType(MVT::ValueType T) { Type = T; }
|
|
|
|
|
|
|
|
bool isLeaf() const { return Operator == 0; }
|
|
|
|
|
2003-08-08 22:29:23 +00:00
|
|
|
unsigned getNumChildren() const { return Children.size(); }
|
2003-08-07 19:12:24 +00:00
|
|
|
TreePatternNode *getChild(unsigned c) const {
|
2003-08-10 23:51:52 +00:00
|
|
|
assert(Operator != 0 && "This is a leaf node!");
|
2003-08-07 19:12:24 +00:00
|
|
|
assert(c < Children.size() && "Child access out of range!");
|
2003-08-10 23:51:52 +00:00
|
|
|
return Children[c].first;
|
|
|
|
}
|
|
|
|
const std::string &getChildName(unsigned c) const {
|
|
|
|
assert(Operator != 0 && "This is a leaf node!");
|
|
|
|
assert(c < Children.size() && "Child access out of range!");
|
|
|
|
return Children[c].second;
|
2003-08-07 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2003-08-07 05:40:14 +00:00
|
|
|
Init *getValue() const {
|
|
|
|
assert(Operator == 0 && "This is not a leaf node!");
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2003-08-08 22:29:23 +00:00
|
|
|
/// getValueRecord - Returns the value of this tree node as a record. For now
|
|
|
|
/// we only allow DefInit's as our leaf values, so this is used.
|
|
|
|
Record *getValueRecord() const;
|
|
|
|
|
2003-08-07 23:16:20 +00:00
|
|
|
/// clone - Make a copy of this tree and all of its children.
|
|
|
|
///
|
|
|
|
TreePatternNode *clone() const;
|
|
|
|
|
2003-08-07 05:40:14 +00:00
|
|
|
void dump() const;
|
2003-08-07 19:28:55 +00:00
|
|
|
|
2003-08-07 23:16:20 +00:00
|
|
|
/// InstantiateNonterminals - If this pattern refers to any nonterminals which
|
|
|
|
/// are not themselves completely resolved, clone the nonterminal and resolve
|
|
|
|
/// it with the using context we provide.
|
|
|
|
void InstantiateNonterminals(InstrSelectorEmitter &ISE);
|
2003-08-07 19:28:55 +00:00
|
|
|
|
2003-08-08 22:29:23 +00:00
|
|
|
/// UpdateNodeType - Set the node type of N to VT if VT contains information.
|
|
|
|
/// If N already contains a conflicting type, then throw an exception. This
|
|
|
|
/// returns true if any information was updated.
|
|
|
|
///
|
2003-08-07 19:28:55 +00:00
|
|
|
bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
|
2003-08-07 05:40:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
/// Pattern - Represent a pattern of one form or another. Currently, three
|
|
|
|
/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
|
|
|
|
///
|
|
|
|
struct Pattern {
|
|
|
|
enum PatternType {
|
|
|
|
Nonterminal, Instruction, Expander
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
/// PTy - The type of pattern this is.
|
|
|
|
///
|
|
|
|
PatternType PTy;
|
|
|
|
|
|
|
|
/// Tree - The tree pattern which corresponds to this pattern. Note that if
|
|
|
|
/// there was a (set) node on the outside level that it has been stripped off.
|
|
|
|
///
|
|
|
|
TreePatternNode *Tree;
|
|
|
|
|
|
|
|
/// Result - If this is an instruction or expander pattern, this is the
|
|
|
|
/// register result, specified with a (set) in the pattern.
|
|
|
|
///
|
2003-08-11 21:28:59 +00:00
|
|
|
std::string ResultName; // The name of the result value...
|
|
|
|
TreePatternNode *ResultNode; // The leaf node for the result register...
|
2003-08-07 19:12:24 +00:00
|
|
|
|
|
|
|
/// TheRecord - The actual TableGen record corresponding to this pattern.
|
|
|
|
///
|
|
|
|
Record *TheRecord;
|
|
|
|
|
|
|
|
/// Resolved - This is true of the pattern is useful in practice. In
|
|
|
|
/// particular, some non-terminals will have non-resolvable types. When a
|
|
|
|
/// user of the non-terminal is later found, they will have inferred a type
|
|
|
|
/// for the result of the non-terminal, which cause a clone of an unresolved
|
|
|
|
/// nonterminal to be made which is "resolved".
|
|
|
|
///
|
|
|
|
bool Resolved;
|
|
|
|
|
2003-08-10 23:51:52 +00:00
|
|
|
/// Args - This is a list of all of the arguments to this pattern, which are
|
|
|
|
/// the non-void leaf nodes in this pattern.
|
|
|
|
std::vector<std::pair<TreePatternNode*, std::string> > Args;
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
/// ISE - the instruction selector emitter coordinating this madness.
|
|
|
|
///
|
|
|
|
InstrSelectorEmitter &ISE;
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Pattern constructor - Parse the specified DagInitializer into the current
|
|
|
|
/// record.
|
|
|
|
Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
|
|
|
|
InstrSelectorEmitter &ise);
|
|
|
|
|
2003-08-07 23:16:20 +00:00
|
|
|
/// Pattern - Constructor used for cloning nonterminal patterns
|
|
|
|
Pattern(TreePatternNode *tree, Record *rec, bool res,
|
2003-08-11 21:28:59 +00:00
|
|
|
InstrSelectorEmitter &ise)
|
|
|
|
: PTy(Nonterminal), Tree(tree), ResultNode(0), TheRecord(rec),
|
|
|
|
Resolved(res), ISE(ise) {
|
2003-08-10 23:51:52 +00:00
|
|
|
calculateArgs(Tree, "");
|
|
|
|
}
|
2003-08-07 23:16:20 +00:00
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
/// getPatternType - Return what flavor of Record this pattern originated from
|
|
|
|
///
|
|
|
|
PatternType getPatternType() const { return PTy; }
|
|
|
|
|
|
|
|
/// getTree - Return the tree pattern which corresponds to this pattern.
|
|
|
|
///
|
|
|
|
TreePatternNode *getTree() const { return Tree; }
|
|
|
|
|
2003-08-11 21:28:59 +00:00
|
|
|
Record *getResult() const {
|
|
|
|
return ResultNode ? ResultNode->getValueRecord() : 0;
|
|
|
|
}
|
2003-08-11 20:32:02 +00:00
|
|
|
const std::string &getResultName() const { return ResultName; }
|
2003-08-11 21:28:59 +00:00
|
|
|
TreePatternNode *getResultNode() const { return ResultNode; }
|
2003-08-07 19:12:24 +00:00
|
|
|
|
|
|
|
/// getRecord - Return the actual TableGen record corresponding to this
|
|
|
|
/// pattern.
|
|
|
|
///
|
|
|
|
Record *getRecord() const { return TheRecord; }
|
|
|
|
|
2003-08-10 23:51:52 +00:00
|
|
|
unsigned getNumArgs() const { return Args.size(); }
|
|
|
|
TreePatternNode *getArg(unsigned i) const {
|
|
|
|
assert(i < Args.size() && "Argument reference out of range!");
|
|
|
|
return Args[i].first;
|
|
|
|
}
|
|
|
|
Record *getArgRec(unsigned i) const {
|
|
|
|
return getArg(i)->getValueRecord();
|
|
|
|
}
|
2003-08-11 21:28:59 +00:00
|
|
|
Init *getArgVal(unsigned i) const {
|
|
|
|
return getArg(i)->getValue();
|
|
|
|
}
|
2003-08-10 23:51:52 +00:00
|
|
|
const std::string &getArgName(unsigned i) const {
|
|
|
|
assert(i < Args.size() && "Argument reference out of range!");
|
|
|
|
return Args[i].second;
|
|
|
|
}
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
bool isResolved() const { return Resolved; }
|
|
|
|
|
2003-08-07 23:16:20 +00:00
|
|
|
/// InferAllTypes - Runs the type inference engine on the current pattern,
|
|
|
|
/// stopping when nothing can be inferred, then updating the Resolved field.
|
|
|
|
void InferAllTypes();
|
|
|
|
|
|
|
|
/// InstantiateNonterminals - If this pattern refers to any nonterminals which
|
|
|
|
/// are not themselves completely resolved, clone the nonterminal and resolve
|
|
|
|
/// it with the using context we provide.
|
|
|
|
void InstantiateNonterminals() {
|
|
|
|
Tree->InstantiateNonterminals(ISE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clone - This method is used to make an exact copy of the current pattern,
|
|
|
|
/// then change the "TheRecord" instance variable to the specified record.
|
|
|
|
///
|
|
|
|
Pattern *clone(Record *R) const;
|
|
|
|
|
|
|
|
/// error - Throw an exception, prefixing it with information about this
|
|
|
|
/// pattern.
|
|
|
|
void error(const std::string &Msg) const;
|
|
|
|
|
2003-08-08 22:29:23 +00:00
|
|
|
/// getSlotName - If this is a leaf node, return the slot name that the
|
|
|
|
/// operand will update.
|
|
|
|
std::string getSlotName() const;
|
|
|
|
static std::string getSlotName(Record *R);
|
|
|
|
|
2003-08-10 19:50:51 +00:00
|
|
|
void dump() const;
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
private:
|
2003-08-10 23:51:52 +00:00
|
|
|
void calculateArgs(TreePatternNode *N, const std::string &Name);
|
2003-08-07 21:02:56 +00:00
|
|
|
MVT::ValueType getIntrinsicType(Record *R) const;
|
2003-08-07 19:12:24 +00:00
|
|
|
TreePatternNode *ParseTreePattern(DagInit *DI);
|
|
|
|
bool InferTypes(TreePatternNode *N, bool &MadeChange);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &OS, const Pattern &P);
|
|
|
|
|
|
|
|
|
2003-08-08 16:30:10 +00:00
|
|
|
/// PatternOrganizer - This class represents all of the patterns which are
|
|
|
|
/// useful for the instruction selector, neatly catagorized in a hierarchical
|
|
|
|
/// structure.
|
|
|
|
struct PatternOrganizer {
|
|
|
|
/// PatternsForNode - The list of patterns which can produce a value of a
|
|
|
|
/// particular slot type, given a particular root node in the tree. All of
|
|
|
|
/// the patterns in this vector produce the same value type and have the same
|
|
|
|
/// root DAG node.
|
|
|
|
typedef std::vector<Pattern*> PatternsForNode;
|
|
|
|
|
|
|
|
/// NodesForSlot - This map keeps track of all of the root DAG nodes which can
|
|
|
|
/// lead to the production of a value for this slot. All of the patterns in
|
|
|
|
/// this data structure produces values of the same slot.
|
|
|
|
typedef std::map<Record*, PatternsForNode> NodesForSlot;
|
|
|
|
|
|
|
|
/// AllPatterns - This data structure contains all patterns in the instruction
|
|
|
|
/// selector.
|
|
|
|
std::map<std::string, NodesForSlot> AllPatterns;
|
|
|
|
|
|
|
|
// Forwarding functions...
|
|
|
|
typedef std::map<std::string, NodesForSlot>::iterator iterator;
|
|
|
|
iterator begin() { return AllPatterns.begin(); }
|
|
|
|
iterator end() { return AllPatterns.end(); }
|
|
|
|
|
|
|
|
|
|
|
|
/// addPattern - Add the specified pattern to the appropriate location in the
|
|
|
|
/// collection.
|
|
|
|
void addPattern(Pattern *P);
|
|
|
|
};
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
|
|
|
|
/// InstrSelectorEmitter - The top-level class which coordinates construction
|
|
|
|
/// and emission of the instruction selector.
|
|
|
|
///
|
2003-08-06 05:42:05 +00:00
|
|
|
class InstrSelectorEmitter : public TableGenBackend {
|
|
|
|
RecordKeeper &Records;
|
2003-08-07 06:02:15 +00:00
|
|
|
CodeGenTarget Target;
|
2003-08-06 05:42:05 +00:00
|
|
|
|
|
|
|
std::map<Record*, NodeType> NodeTypes;
|
2003-08-07 19:12:24 +00:00
|
|
|
|
|
|
|
/// Patterns - a list of all of the patterns defined by the target description
|
|
|
|
///
|
|
|
|
std::map<Record*, Pattern*> Patterns;
|
2003-08-07 23:16:20 +00:00
|
|
|
|
|
|
|
/// InstantiatedNTs - A data structure to keep track of which nonterminals
|
|
|
|
/// have been instantiated already...
|
|
|
|
///
|
|
|
|
std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
|
2003-08-08 16:30:10 +00:00
|
|
|
|
|
|
|
/// ComputableValues - This map indicates which patterns can be used to
|
|
|
|
/// generate a value that is used by the selector. The keys of this map
|
|
|
|
/// implicitly define the values that are used by the selector.
|
|
|
|
///
|
|
|
|
PatternOrganizer ComputableValues;
|
|
|
|
|
2003-08-06 05:42:05 +00:00
|
|
|
public:
|
|
|
|
InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
|
|
|
|
|
|
|
|
// run - Output the instruction set description, returning true on failure.
|
|
|
|
void run(std::ostream &OS);
|
|
|
|
|
2003-08-07 19:12:24 +00:00
|
|
|
const CodeGenTarget &getTarget() const { return Target; }
|
|
|
|
std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
|
2003-08-08 22:29:23 +00:00
|
|
|
const NodeType &getNodeType(Record *R) const {
|
|
|
|
std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
|
|
|
|
assert(I != NodeTypes.end() && "Unknown node type!");
|
|
|
|
return I->second;
|
|
|
|
}
|
2003-08-07 19:12:24 +00:00
|
|
|
|
2003-08-07 21:02:56 +00:00
|
|
|
/// getPattern - return the pattern corresponding to the specified record, or
|
|
|
|
/// null if there is none.
|
|
|
|
Pattern *getPattern(Record *R) const {
|
|
|
|
std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
|
|
|
|
return I != Patterns.end() ? I->second : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReadNonterminal - This method parses the specified record as a
|
|
|
|
/// nonterminal, but only if it hasn't been read in already.
|
|
|
|
Pattern *ReadNonterminal(Record *R);
|
|
|
|
|
2003-08-07 23:16:20 +00:00
|
|
|
/// InstantiateNonterminal - This method takes the nonterminal specified by
|
|
|
|
/// NT, which should not be completely resolved, clones it, applies ResultTy
|
|
|
|
/// to its root, then runs the type inference stuff on it. This should
|
|
|
|
/// produce a newly resolved nonterminal, which we make a record for and
|
|
|
|
/// return. To be extra fancy and efficient, this only makes one clone for
|
|
|
|
/// each type it is instantiated with.
|
|
|
|
Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
|
|
|
|
|
2003-08-06 05:42:05 +00:00
|
|
|
private:
|
2003-08-07 20:42:23 +00:00
|
|
|
// ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
|
|
|
|
// turning them into the more accessible NodeTypes data structure.
|
|
|
|
void ReadNodeTypes();
|
2003-08-06 06:16:35 +00:00
|
|
|
|
2003-08-07 20:42:23 +00:00
|
|
|
// ReadNonTerminals - Read in all nonterminals and incorporate them into our
|
|
|
|
// pattern database.
|
|
|
|
void ReadNonterminals();
|
2003-08-07 06:02:15 +00:00
|
|
|
|
2003-08-07 20:42:23 +00:00
|
|
|
// ReadInstructionPatterns - Read in all subclasses of Instruction, and
|
2003-08-06 06:16:35 +00:00
|
|
|
// process those with a useful Pattern field.
|
2003-08-07 20:42:23 +00:00
|
|
|
void ReadInstructionPatterns();
|
2003-08-07 19:21:10 +00:00
|
|
|
|
2003-08-07 20:42:23 +00:00
|
|
|
// ReadExpanderPatterns - Read in all of the expanded patterns.
|
|
|
|
void ReadExpanderPatterns();
|
|
|
|
|
|
|
|
// InstantiateNonterminals - Instantiate any unresolved nonterminals with
|
|
|
|
// information from the context that they are used in.
|
|
|
|
void InstantiateNonterminals();
|
2003-08-08 16:30:10 +00:00
|
|
|
|
|
|
|
// CalculateComputableValues - Fill in the ComputableValues map through
|
|
|
|
// analysis of the patterns we are playing with.
|
|
|
|
void CalculateComputableValues();
|
2003-08-08 22:29:23 +00:00
|
|
|
|
|
|
|
// EmitMatchCosters - Given a list of patterns, which all have the same root
|
|
|
|
// pattern operator, emit an efficient decision tree to decide which one to
|
|
|
|
// pick. This is structured this way to avoid reevaluations of non-obvious
|
|
|
|
// subexpressions.
|
|
|
|
void EmitMatchCosters(std::ostream &OS,
|
|
|
|
const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
|
|
|
|
const std::string &VarPrefix, unsigned Indent);
|
2003-08-10 23:51:52 +00:00
|
|
|
|
|
|
|
/// PrintExpanderOperand - Print out Arg as part of the instruction emission
|
|
|
|
/// process for the expander pattern P. This argument may be referencing some
|
|
|
|
/// values defined in P, or may just be physical register references or
|
|
|
|
/// something like that. If PrintArg is true, we are printing out arguments
|
|
|
|
/// to the BuildMI call. If it is false, we are printing the result register
|
|
|
|
/// name.
|
|
|
|
void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
|
2003-08-11 21:28:59 +00:00
|
|
|
TreePatternNode *ArgDecl, Pattern *P,
|
2003-08-10 23:51:52 +00:00
|
|
|
bool PrintArg, std::ostream &OS);
|
2003-08-06 05:42:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|