summaryrefslogtreecommitdiff
path: root/utils/TableGen/DAGISelMatcherEmitter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/TableGen/DAGISelMatcherEmitter.cpp')
-rw-r--r--utils/TableGen/DAGISelMatcherEmitter.cpp324
1 files changed, 207 insertions, 117 deletions
diff --git a/utils/TableGen/DAGISelMatcherEmitter.cpp b/utils/TableGen/DAGISelMatcherEmitter.cpp
index 67e8f15b248e..e64943c1d025 100644
--- a/utils/TableGen/DAGISelMatcherEmitter.cpp
+++ b/utils/TableGen/DAGISelMatcherEmitter.cpp
@@ -20,14 +20,16 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/Format.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
using namespace llvm;
enum {
- CommentIndent = 30
+ IndexWidth = 6,
+ FullIndexWidth = IndexWidth + 4,
+ HistOpcWidth = 40,
};
cl::OptionCategory DAGISelCat("Options for -gen-dag-isel");
@@ -45,14 +47,14 @@ static cl::opt<bool> InstrumentCoverage(
namespace {
class MatcherTableEmitter {
const CodeGenDAGPatterns &CGP;
-
+
DenseMap<TreePattern *, unsigned> NodePredicateMap;
std::vector<TreePredicateFn> NodePredicates;
// We de-duplicate the predicates by code string, and use this map to track
// all the patterns with "identical" predicates.
StringMap<TinyPtrVector<TreePattern *>> NodePredicatesByCodeToRun;
-
+
StringMap<unsigned> PatternPredicateMap;
std::vector<std::string> PatternPredicates;
@@ -81,17 +83,17 @@ public:
: CGP(cgp) {}
unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
- unsigned StartIdx, formatted_raw_ostream &OS);
+ unsigned StartIdx, raw_ostream &OS);
- void EmitPredicateFunctions(formatted_raw_ostream &OS);
+ void EmitPredicateFunctions(raw_ostream &OS);
- void EmitHistogram(const Matcher *N, formatted_raw_ostream &OS);
+ void EmitHistogram(const Matcher *N, raw_ostream &OS);
void EmitPatternMatchTable(raw_ostream &OS);
private:
unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
- formatted_raw_ostream &OS);
+ raw_ostream &OS);
unsigned getNodePredicate(TreePredicateFn Pred) {
TreePattern *TP = Pred.getOrigPatFragRecord();
@@ -114,7 +116,7 @@ private:
}
return Entry-1;
}
-
+
unsigned getPatternPredicate(StringRef PredName) {
unsigned &Entry = PatternPredicateMap[PredName];
if (Entry == 0) {
@@ -206,13 +208,37 @@ static std::string getIncludePath(const Record *R) {
return str;
}
+static void BeginEmitFunction(raw_ostream &OS, StringRef RetType,
+ StringRef Decl, bool AddOverride) {
+ OS << "#ifdef GET_DAGISEL_DECL\n";
+ OS << RetType << ' ' << Decl;
+ if (AddOverride)
+ OS << " override";
+ OS << ";\n"
+ "#endif\n"
+ "#if defined(GET_DAGISEL_BODY) || DAGISEL_INLINE\n";
+ OS << RetType << " DAGISEL_CLASS_COLONCOLON " << Decl << "\n";
+ if (AddOverride) {
+ OS << "#if DAGISEL_INLINE\n"
+ " override\n"
+ "#endif\n";
+ }
+}
+
+static void EndEmitFunction(raw_ostream &OS) {
+ OS << "#endif // GET_DAGISEL_BODY\n\n";
+}
+
void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
assert(isUInt<16>(VecPatterns.size()) &&
"Using only 16 bits to encode offset into Pattern Table");
assert(VecPatterns.size() == VecIncludeStrings.size() &&
"The sizes of Pattern and include vectors should be the same");
- OS << "StringRef getPatternForIndex(unsigned Index) override {\n";
+
+ BeginEmitFunction(OS, "StringRef", "getPatternForIndex(unsigned Index)",
+ true/*AddOverride*/);
+ OS << "{\n";
OS << "static const char * PATTERN_MATCH_TABLE[] = {\n";
for (const auto &It : VecPatterns) {
@@ -222,8 +248,11 @@ void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
OS << "\n};";
OS << "\nreturn StringRef(PATTERN_MATCH_TABLE[Index]);";
OS << "\n}";
+ EndEmitFunction(OS);
- OS << "\nStringRef getIncludePathForIndex(unsigned Index) override {\n";
+ BeginEmitFunction(OS, "StringRef", "getIncludePathForIndex(unsigned Index)",
+ true/*AddOverride*/);
+ OS << "{\n";
OS << "static const char * INCLUDE_PATH_TABLE[] = {\n";
for (const auto &It : VecIncludeStrings) {
@@ -233,14 +262,15 @@ void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
OS << "\n};";
OS << "\nreturn StringRef(INCLUDE_PATH_TABLE[Index]);";
OS << "\n}";
+ EndEmitFunction(OS);
}
/// EmitMatcher - Emit bytes for the specified matcher and return
/// the number of bytes emitted.
unsigned MatcherTableEmitter::
EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
- formatted_raw_ostream &OS) {
- OS.PadToColumn(Indent*2);
+ raw_ostream &OS) {
+ OS.indent(Indent*2);
switch (N->getKind()) {
case Matcher::Scope: {
@@ -256,10 +286,10 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
++CurrentIdx;
} else {
if (!OmitComments) {
- OS << "/*" << CurrentIdx << "*/";
- OS.PadToColumn(Indent*2) << "/*Scope*/ ";
+ OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
+ OS.indent(Indent*2) << "/*Scope*/ ";
} else
- OS.PadToColumn(Indent*2);
+ OS.indent(Indent*2);
}
// We need to encode the child and the offset of the failure code before
@@ -275,9 +305,8 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
TmpBuf.clear();
raw_svector_ostream OS(TmpBuf);
- formatted_raw_ostream FOS(OS);
ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
- CurrentIdx+VBRSize, FOS);
+ CurrentIdx+VBRSize, OS);
} while (GetVBRSize(ChildSize) != VBRSize);
assert(ChildSize != 0 && "Should not have a zero-sized child!");
@@ -287,8 +316,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
OS << "/*->" << CurrentIdx+ChildSize << "*/";
if (i == 0)
- OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
- << " children in Scope";
+ OS << " // " << SM->getNumChildren() << " children in Scope";
}
OS << '\n' << TmpBuf;
@@ -297,8 +325,8 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
// Emit a zero as a sentinel indicating end of 'Scope'.
if (!OmitComments)
- OS << "/*" << CurrentIdx << "*/";
- OS.PadToColumn(Indent*2) << "0, ";
+ OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
+ OS.indent(Indent*2) << "0, ";
if (!OmitComments)
OS << "/*End of Scope*/";
OS << '\n';
@@ -308,9 +336,9 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
case Matcher::RecordNode:
OS << "OPC_RecordNode,";
if (!OmitComments)
- OS.PadToColumn(CommentIndent) << "// #"
- << cast<RecordMatcher>(N)->getResultNo() << " = "
- << cast<RecordMatcher>(N)->getWhatFor();
+ OS << " // #"
+ << cast<RecordMatcher>(N)->getResultNo() << " = "
+ << cast<RecordMatcher>(N)->getWhatFor();
OS << '\n';
return 1;
@@ -318,9 +346,9 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
<< ',';
if (!OmitComments)
- OS.PadToColumn(CommentIndent) << "// #"
- << cast<RecordChildMatcher>(N)->getResultNo() << " = "
- << cast<RecordChildMatcher>(N)->getWhatFor();
+ OS << " // #"
+ << cast<RecordChildMatcher>(N)->getResultNo() << " = "
+ << cast<RecordChildMatcher>(N)->getWhatFor();
OS << '\n';
return 1;
@@ -362,7 +390,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
StringRef Pred =cast<CheckPatternPredicateMatcher>(N)->getPredicate();
OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
if (!OmitComments)
- OS.PadToColumn(CommentIndent) << "// " << Pred;
+ OS << " // " << Pred;
OS << '\n';
return 2;
}
@@ -370,7 +398,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate();
OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
if (!OmitComments)
- OS.PadToColumn(CommentIndent) << "// " << Pred.getFnName();
+ OS << " // " << Pred.getFnName();
OS << '\n';
return 2;
}
@@ -423,17 +451,16 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
TmpBuf.clear();
raw_svector_ostream OS(TmpBuf);
- formatted_raw_ostream FOS(OS);
ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+IdxSize,
- FOS);
+ OS);
} while (GetVBRSize(ChildSize) != VBRSize);
assert(ChildSize != 0 && "Should not have a zero-sized child!");
if (i != 0) {
if (!OmitComments)
- OS << "/*" << CurrentIdx << "*/";
- OS.PadToColumn(Indent*2);
+ OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
+ OS.indent(Indent*2);
if (!OmitComments)
OS << (isa<SwitchOpcodeMatcher>(N) ?
"/*SwitchOpcode*/ " : "/*SwitchType*/ ");
@@ -458,11 +485,11 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
// Emit the final zero to terminate the switch.
if (!OmitComments)
- OS << "/*" << CurrentIdx << "*/";
- OS.PadToColumn(Indent*2) << "0, ";
+ OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
+ OS.indent(Indent*2) << "0,";
if (!OmitComments)
OS << (isa<SwitchOpcodeMatcher>(N) ?
- "// EndSwitchOpcode" : "// EndSwitchType");
+ " // EndSwitchOpcode" : " // EndSwitchType");
OS << '\n';
++CurrentIdx;
@@ -470,11 +497,14 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
}
case Matcher::CheckType:
- assert(cast<CheckTypeMatcher>(N)->getResNo() == 0 &&
- "FIXME: Add support for CheckType of resno != 0");
- OS << "OPC_CheckType, "
- << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
- return 2;
+ if (cast<CheckTypeMatcher>(N)->getResNo() == 0) {
+ OS << "OPC_CheckType, "
+ << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
+ return 2;
+ }
+ OS << "OPC_CheckTypeRes, " << cast<CheckTypeMatcher>(N)->getResNo()
+ << ", " << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
+ return 3;
case Matcher::CheckChildType:
OS << "OPC_CheckChild"
@@ -513,7 +543,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
<< CCPM->getMatchNumber() << ',';
if (!OmitComments) {
- OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
+ OS << " // " << Pattern.getSelectFunc();
OS << ":$" << CCPM->getName();
for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
OS << " #" << CCPM->getFirstResult()+i;
@@ -615,7 +645,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
<< XF->getSlot() << ',';
if (!OmitComments)
- OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
+ OS << " // "<<XF->getNodeXForm()->getName();
OS <<'\n';
return 3;
}
@@ -636,7 +666,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
unsigned Offset =
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
OS << "TARGET_VAL(" << Offset << "),\n";
- OS.PadToColumn(Indent * 2);
+ OS.indent(FullIndexWidth + Indent * 2);
}
}
const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
@@ -655,7 +685,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
OS << ",\n";
- OS.PadToColumn(Indent*2+4);
+ OS.indent(FullIndexWidth + Indent*2+4);
if (!CompressVTs) {
OS << EN->getNumVTs();
if (!OmitComments)
@@ -677,7 +707,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
// Print the result #'s for EmitNode.
if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
if (unsigned NumResults = EN->getNumVTs()) {
- OS.PadToColumn(CommentIndent) << "// Results =";
+ OS << " // Results =";
unsigned First = E->getFirstResultSlot();
for (unsigned i = 0; i != NumResults; ++i)
OS << " #" << First+i;
@@ -686,10 +716,10 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
OS << '\n';
if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
- OS.PadToColumn(Indent*2) << "// Src: "
+ OS.indent(FullIndexWidth + Indent*2) << "// Src: "
<< *SNT->getPattern().getSrcPattern() << " - Complexity = "
<< SNT->getPattern().getPatternComplexity(CGP) << '\n';
- OS.PadToColumn(Indent*2) << "// Dst: "
+ OS.indent(FullIndexWidth + Indent*2) << "// Dst: "
<< *SNT->getPattern().getDstPattern() << '\n';
}
} else
@@ -713,7 +743,7 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
unsigned Offset =
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
OS << "TARGET_VAL(" << Offset << "),\n";
- OS.PadToColumn(Indent * 2);
+ OS.indent(FullIndexWidth + Indent * 2);
}
OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
unsigned NumResultBytes = 0;
@@ -721,10 +751,10 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
OS << '\n';
if (!OmitComments) {
- OS.PadToColumn(Indent*2) << "// Src: "
+ OS.indent(FullIndexWidth + Indent*2) << " // Src: "
<< *CM->getPattern().getSrcPattern() << " - Complexity = "
<< CM->getPattern().getPatternComplexity(CGP) << '\n';
- OS.PadToColumn(Indent*2) << "// Dst: "
+ OS.indent(FullIndexWidth + Indent*2) << " // Dst: "
<< *CM->getPattern().getDstPattern();
}
OS << '\n';
@@ -737,11 +767,11 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
unsigned MatcherTableEmitter::
EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
- formatted_raw_ostream &OS) {
+ raw_ostream &OS) {
unsigned Size = 0;
while (N) {
if (!OmitComments)
- OS << "/*" << CurrentIdx << "*/";
+ OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
Size += MatcherSize;
CurrentIdx += MatcherSize;
@@ -753,46 +783,55 @@ EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
return Size;
}
-void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
+void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
// Emit pattern predicates.
if (!PatternPredicates.empty()) {
- OS << "bool CheckPatternPredicate(unsigned PredNo) const override {\n";
+ BeginEmitFunction(OS, "bool",
+ "CheckPatternPredicate(unsigned PredNo) const", true/*AddOverride*/);
+ OS << "{\n";
OS << " switch (PredNo) {\n";
OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
OS << " }\n";
- OS << "}\n\n";
+ OS << "}\n";
+ EndEmitFunction(OS);
}
// Emit Node predicates.
if (!NodePredicates.empty()) {
- OS << "bool CheckNodePredicate(SDNode *Node,\n";
- OS << " unsigned PredNo) const override {\n";
+ BeginEmitFunction(OS, "bool",
+ "CheckNodePredicate(SDNode *Node, unsigned PredNo) const",
+ true/*AddOverride*/);
+ OS << "{\n";
OS << " switch (PredNo) {\n";
OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
// Emit the predicate code corresponding to this pattern.
TreePredicateFn PredFn = NodePredicates[i];
-
+
assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
OS << " case " << i << ": { \n";
for (auto *SimilarPred :
NodePredicatesByCodeToRun[PredFn.getCodeToRunOnSDNode()])
OS << " // " << TreePredicateFn(SimilarPred).getFnName() <<'\n';
-
+
OS << PredFn.getCodeToRunOnSDNode() << "\n }\n";
}
OS << " }\n";
- OS << "}\n\n";
+ OS << "}\n";
+ EndEmitFunction(OS);
}
// Emit CompletePattern matchers.
// FIXME: This should be const.
if (!ComplexPatterns.empty()) {
- OS << "bool CheckComplexPattern(SDNode *Root, SDNode *Parent,\n";
- OS << " SDValue N, unsigned PatternNo,\n";
- OS << " SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) override {\n";
+ BeginEmitFunction(OS, "bool",
+ "CheckComplexPattern(SDNode *Root, SDNode *Parent,\n"
+ " SDValue N, unsigned PatternNo,\n"
+ " SmallVectorImpl<std::pair<SDValue, SDNode*>> &Result)",
+ true/*AddOverride*/);
+ OS << "{\n";
OS << " unsigned NextRes = Result.size();\n";
OS << " switch (PatternNo) {\n";
OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
@@ -836,14 +875,17 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
}
}
OS << " }\n";
- OS << "}\n\n";
+ OS << "}\n";
+ EndEmitFunction(OS);
}
// Emit SDNodeXForm handlers.
// FIXME: This should be const.
if (!NodeXForms.empty()) {
- OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) override {\n";
+ BeginEmitFunction(OS, "SDValue",
+ "RunSDNodeXForm(SDValue V, unsigned XFormNo)", true/*AddOverride*/);
+ OS << "{\n";
OS << " switch (XFormNo) {\n";
OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
@@ -869,7 +911,8 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
OS << Code << "\n }\n";
}
OS << " }\n";
- OS << "}\n\n";
+ OS << "}\n";
+ EndEmitFunction(OS);
}
}
@@ -895,8 +938,51 @@ static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
}
}
+static StringRef getOpcodeString(Matcher::KindTy Kind) {
+ switch (Kind) {
+ case Matcher::Scope: return "OPC_Scope"; break;
+ case Matcher::RecordNode: return "OPC_RecordNode"; break;
+ case Matcher::RecordChild: return "OPC_RecordChild"; break;
+ case Matcher::RecordMemRef: return "OPC_RecordMemRef"; break;
+ case Matcher::CaptureGlueInput: return "OPC_CaptureGlueInput"; break;
+ case Matcher::MoveChild: return "OPC_MoveChild"; break;
+ case Matcher::MoveParent: return "OPC_MoveParent"; break;
+ case Matcher::CheckSame: return "OPC_CheckSame"; break;
+ case Matcher::CheckChildSame: return "OPC_CheckChildSame"; break;
+ case Matcher::CheckPatternPredicate:
+ return "OPC_CheckPatternPredicate"; break;
+ case Matcher::CheckPredicate: return "OPC_CheckPredicate"; break;
+ case Matcher::CheckOpcode: return "OPC_CheckOpcode"; break;
+ case Matcher::SwitchOpcode: return "OPC_SwitchOpcode"; break;
+ case Matcher::CheckType: return "OPC_CheckType"; break;
+ case Matcher::SwitchType: return "OPC_SwitchType"; break;
+ case Matcher::CheckChildType: return "OPC_CheckChildType"; break;
+ case Matcher::CheckInteger: return "OPC_CheckInteger"; break;
+ case Matcher::CheckChildInteger: return "OPC_CheckChildInteger"; break;
+ case Matcher::CheckCondCode: return "OPC_CheckCondCode"; break;
+ case Matcher::CheckValueType: return "OPC_CheckValueType"; break;
+ case Matcher::CheckComplexPat: return "OPC_CheckComplexPat"; break;
+ case Matcher::CheckAndImm: return "OPC_CheckAndImm"; break;
+ case Matcher::CheckOrImm: return "OPC_CheckOrImm"; break;
+ case Matcher::CheckFoldableChainNode:
+ return "OPC_CheckFoldableChainNode"; break;
+ case Matcher::EmitInteger: return "OPC_EmitInteger"; break;
+ case Matcher::EmitStringInteger: return "OPC_EmitStringInteger"; break;
+ case Matcher::EmitRegister: return "OPC_EmitRegister"; break;
+ case Matcher::EmitConvertToTarget: return "OPC_EmitConvertToTarget"; break;
+ case Matcher::EmitMergeInputChains: return "OPC_EmitMergeInputChains"; break;
+ case Matcher::EmitCopyToReg: return "OPC_EmitCopyToReg"; break;
+ case Matcher::EmitNode: return "OPC_EmitNode"; break;
+ case Matcher::MorphNodeTo: return "OPC_MorphNodeTo"; break;
+ case Matcher::EmitNodeXForm: return "OPC_EmitNodeXForm"; break;
+ case Matcher::CompleteMatch: return "OPC_CompleteMatch"; break;
+ }
+
+ llvm_unreachable("Unhandled opcode?");
+}
+
void MatcherTableEmitter::EmitHistogram(const Matcher *M,
- formatted_raw_ostream &OS) {
+ raw_ostream &OS) {
if (OmitComments)
return;
@@ -905,47 +991,9 @@ void MatcherTableEmitter::EmitHistogram(const Matcher *M,
OS << " // Opcode Histogram:\n";
for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
- OS << " // #";
- switch ((Matcher::KindTy)i) {
- case Matcher::Scope: OS << "OPC_Scope"; break;
- case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
- case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
- case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
- case Matcher::CaptureGlueInput: OS << "OPC_CaptureGlueInput"; break;
- case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
- case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
- case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
- case Matcher::CheckChildSame: OS << "OPC_CheckChildSame"; break;
- case Matcher::CheckPatternPredicate:
- OS << "OPC_CheckPatternPredicate"; break;
- case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
- case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
- case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
- case Matcher::CheckType: OS << "OPC_CheckType"; break;
- case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
- case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
- case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
- case Matcher::CheckChildInteger: OS << "OPC_CheckChildInteger"; break;
- case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
- case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
- case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
- case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
- case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
- case Matcher::CheckFoldableChainNode:
- OS << "OPC_CheckFoldableChainNode"; break;
- case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
- case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
- case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
- case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
- case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
- case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
- case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
- case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
- case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
- case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
- }
-
- OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
+ OS << " // #"
+ << left_justify(getOpcodeString((Matcher::KindTy)i), HistOpcWidth)
+ << " = " << OpcodeFreq[i] << '\n';
}
OS << '\n';
}
@@ -953,19 +1001,45 @@ void MatcherTableEmitter::EmitHistogram(const Matcher *M,
void llvm::EmitMatcherTable(const Matcher *TheMatcher,
const CodeGenDAGPatterns &CGP,
- raw_ostream &O) {
- formatted_raw_ostream OS(O);
-
- OS << "// The main instruction selector code.\n";
- OS << "void SelectCode(SDNode *N) {\n";
-
+ raw_ostream &OS) {
+ OS << "#if defined(GET_DAGISEL_DECL) && defined(GET_DAGISEL_BODY)\n";
+ OS << "#error GET_DAGISEL_DECL and GET_DAGISEL_BODY cannot be both defined, ";
+ OS << "undef both for inline definitions\n";
+ OS << "#endif\n\n";
+
+ // Emit a check for omitted class name.
+ OS << "#ifdef GET_DAGISEL_BODY\n";
+ OS << "#define LOCAL_DAGISEL_STRINGIZE(X) LOCAL_DAGISEL_STRINGIZE_(X)\n";
+ OS << "#define LOCAL_DAGISEL_STRINGIZE_(X) #X\n";
+ OS << "static_assert(sizeof(LOCAL_DAGISEL_STRINGIZE(GET_DAGISEL_BODY)) > 1,"
+ "\n";
+ OS << " \"GET_DAGISEL_BODY is empty: it should be defined with the class "
+ "name\");\n";
+ OS << "#undef LOCAL_DAGISEL_STRINGIZE_\n";
+ OS << "#undef LOCAL_DAGISEL_STRINGIZE\n";
+ OS << "#endif\n\n";
+
+ OS << "#if !defined(GET_DAGISEL_DECL) && !defined(GET_DAGISEL_BODY)\n";
+ OS << "#define DAGISEL_INLINE 1\n";
+ OS << "#else\n";
+ OS << "#define DAGISEL_INLINE 0\n";
+ OS << "#endif\n\n";
+
+ OS << "#if !DAGISEL_INLINE\n";
+ OS << "#define DAGISEL_CLASS_COLONCOLON GET_DAGISEL_BODY ::\n";
+ OS << "#else\n";
+ OS << "#define DAGISEL_CLASS_COLONCOLON\n";
+ OS << "#endif\n\n";
+
+ BeginEmitFunction(OS, "void", "SelectCode(SDNode *N)", false/*AddOverride*/);
MatcherTableEmitter MatcherEmitter(CGP);
+ OS << "{\n";
OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
OS << " // this.\n";
OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
OS << " static const unsigned char MatcherTable[] = {\n";
- unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 6, 0, OS);
+ unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 1, 0, OS);
OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
MatcherEmitter.EmitHistogram(TheMatcher, OS);
@@ -973,10 +1047,26 @@ void llvm::EmitMatcherTable(const Matcher *TheMatcher,
OS << " #undef TARGET_VAL\n";
OS << " SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n";
OS << "}\n";
+ EndEmitFunction(OS);
// Next up, emit the function for node and pattern predicates:
MatcherEmitter.EmitPredicateFunctions(OS);
if (InstrumentCoverage)
MatcherEmitter.EmitPatternMatchTable(OS);
+
+ // Clean up the preprocessor macros.
+ OS << "\n";
+ OS << "#ifdef DAGISEL_INLINE\n";
+ OS << "#undef DAGISEL_INLINE\n";
+ OS << "#endif\n";
+ OS << "#ifdef DAGISEL_CLASS_COLONCOLON\n";
+ OS << "#undef DAGISEL_CLASS_COLONCOLON\n";
+ OS << "#endif\n";
+ OS << "#ifdef GET_DAGISEL_DECL\n";
+ OS << "#undef GET_DAGISEL_DECL\n";
+ OS << "#endif\n";
+ OS << "#ifdef GET_DAGISEL_BODY\n";
+ OS << "#undef GET_DAGISEL_BODY\n";
+ OS << "#endif\n";
}