#include "llvm/Support/FormattedStream.h"
using namespace llvm;
-namespace {
enum {
CommentIndent = 30
};
-}
/// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
/// fits in 1, 2, 4, or 8 sign extended bytes.
DenseMap<Record*, unsigned> NodeXFormMap;
std::vector<const Record*> NodeXForms;
+ // Per opcode frequence count.
+ std::vector<unsigned> Histogram;
public:
MatcherTableEmitter() {}
- unsigned EmitMatcherList(const MatcherNode *N, unsigned Indent,
+ unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
unsigned StartIdx, formatted_raw_ostream &OS);
void EmitPredicateFunctions(formatted_raw_ostream &OS);
+
+ void EmitHistogram(formatted_raw_ostream &OS);
private:
- unsigned EmitMatcher(const MatcherNode *N, unsigned Indent,
+ unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
formatted_raw_ostream &OS);
unsigned getNodePredicate(StringRef PredName) {
};
} // end anonymous namespace.
+static unsigned GetVBRSize(unsigned Val) {
+ if (Val <= 127) return 1;
+
+ unsigned NumBytes = 0;
+ while (Val >= 128) {
+ Val >>= 7;
+ ++NumBytes;
+ }
+ return NumBytes+1;
+}
+
/// EmitVBRValue - Emit the specified value as a VBR, returning the number of
/// bytes emitted.
static unsigned EmitVBRValue(unsigned Val, raw_ostream &OS) {
unsigned InVal = Val;
unsigned NumBytes = 0;
- while (Val > 128) {
+ while (Val >= 128) {
OS << (Val&127) << "|128,";
Val >>= 7;
++NumBytes;
/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
/// the number of bytes emitted.
unsigned MatcherTableEmitter::
-EmitMatcher(const MatcherNode *N, unsigned Indent, formatted_raw_ostream &OS) {
+EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
+ formatted_raw_ostream &OS) {
OS.PadToColumn(Indent*2);
switch (N->getKind()) {
- case MatcherNode::Push: assert(0 && "Should be handled by caller");
- case MatcherNode::RecordNode:
+ case Matcher::Scope: {
+ const ScopeMatcher *SM = cast<ScopeMatcher>(N);
+ assert(SM->getNext() == 0 && "Shouldn't have next after scope");
+
+ unsigned StartIdx = CurrentIdx;
+
+ // Emit all of the children.
+ for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
+ if (i == 0) {
+ OS << "OPC_Scope, ";
+ ++CurrentIdx;
+ } else {
+ OS << "/*" << CurrentIdx << "*/";
+ OS.PadToColumn(Indent*2) << "/*Scope*/ ";
+ }
+
+ // We need to encode the child and the offset of the failure code before
+ // emitting either of them. Handle this by buffering the output into a
+ // string while we get the size. Unfortunately, the offset of the
+ // children depends on the VBR size of the child, so for large children we
+ // have to iterate a bit.
+ SmallString<128> TmpBuf;
+ unsigned ChildSize = 0;
+ unsigned VBRSize = 0;
+ do {
+ VBRSize = GetVBRSize(ChildSize);
+
+ TmpBuf.clear();
+ raw_svector_ostream OS(TmpBuf);
+ formatted_raw_ostream FOS(OS);
+ ChildSize = EmitMatcherList(cast<ScopeMatcher>(N)->getChild(i),
+ Indent+1, CurrentIdx+VBRSize, FOS);
+ } while (GetVBRSize(ChildSize) != VBRSize);
+
+ assert(ChildSize != 0 && "Should not have a zero-sized child!");
+
+ CurrentIdx += EmitVBRValue(ChildSize, OS);
+ OS << "/*->" << CurrentIdx+ChildSize << "*/";
+
+ if (i == 0)
+ OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
+ << " children in Scope";
+
+ OS << '\n' << TmpBuf.str();
+ CurrentIdx += ChildSize;
+ }
+
+ // Emit a zero as a sentinel indicating end of 'Scope'.
+ OS << "/*" << CurrentIdx << "*/";
+ OS.PadToColumn(Indent*2) << "0, /*End of Scope*/\n";
+ return CurrentIdx - StartIdx + 1;
+ }
+
+ case Matcher::RecordNode:
OS << "OPC_RecordNode,";
OS.PadToColumn(CommentIndent) << "// "
- << cast<RecordMatcherNode>(N)->getWhatFor() << '\n';
+ << cast<RecordMatcher>(N)->getWhatFor() << '\n';
+ return 1;
+
+ case Matcher::RecordChild:
+ OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
+ << ',';
+ OS.PadToColumn(CommentIndent) << "// "
+ << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
return 1;
- case MatcherNode::RecordMemRef:
+ case Matcher::RecordMemRef:
OS << "OPC_RecordMemRef,\n";
return 1;
- case MatcherNode::CaptureFlagInput:
+ case Matcher::CaptureFlagInput:
OS << "OPC_CaptureFlagInput,\n";
return 1;
- case MatcherNode::MoveChild:
- OS << "OPC_MoveChild, "
- << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
+ case Matcher::MoveChild:
+ OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
return 2;
- case MatcherNode::MoveParent:
+ case Matcher::MoveParent:
OS << "OPC_MoveParent,\n";
return 1;
- case MatcherNode::CheckSame:
+ case Matcher::CheckSame:
OS << "OPC_CheckSame, "
- << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
+ << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
return 2;
- case MatcherNode::CheckPatternPredicate: {
- StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate();
+ case Matcher::CheckPatternPredicate: {
+ StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
return 2;
}
- case MatcherNode::CheckPredicate: {
- StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName();
+ case Matcher::CheckPredicate: {
+ StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
return 2;
}
- case MatcherNode::CheckOpcode:
+ case Matcher::CheckOpcode:
OS << "OPC_CheckOpcode, "
- << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
+ << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
return 2;
- case MatcherNode::CheckMultiOpcode: {
- const CheckMultiOpcodeMatcherNode *CMO=cast<CheckMultiOpcodeMatcherNode>(N);
- OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodeNames() << ", ";
- for (unsigned i = 0, e = CMO->getNumOpcodeNames(); i != e; ++i)
- OS << CMO->getOpcodeName(i) << ", ";
+ case Matcher::CheckMultiOpcode: {
+ const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
+ OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodes() << ", ";
+ for (unsigned i = 0, e = CMO->getNumOpcodes(); i != e; ++i)
+ OS << CMO->getOpcode(i).getEnumName() << ", ";
OS << '\n';
- return 2 + CMO->getNumOpcodeNames();
+ return 2 + CMO->getNumOpcodes();
}
- case MatcherNode::CheckType:
+ case Matcher::CheckType:
OS << "OPC_CheckType, "
- << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
+ << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
return 2;
-
- case MatcherNode::CheckInteger: {
- int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
+ case Matcher::CheckChildType:
+ OS << "OPC_CheckChild"
+ << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
+ << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
+ return 2;
+
+ case Matcher::CheckInteger: {
+ int64_t Val = cast<CheckIntegerMatcher>(N)->getValue();
OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
return EmitInt(Val, OS)+1;
}
- case MatcherNode::CheckCondCode:
+ case Matcher::CheckCondCode:
OS << "OPC_CheckCondCode, ISD::"
- << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
+ << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
return 2;
- case MatcherNode::CheckValueType:
+ case Matcher::CheckValueType:
OS << "OPC_CheckValueType, MVT::"
- << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
+ << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
return 2;
- case MatcherNode::CheckComplexPat: {
+ case Matcher::CheckComplexPat: {
const ComplexPattern &Pattern =
- cast<CheckComplexPatMatcherNode>(N)->getPattern();
+ cast<CheckComplexPatMatcher>(N)->getPattern();
OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
OS << ": " << Pattern.getNumOperands() << " operands";
return 2;
}
- case MatcherNode::CheckAndImm: {
- int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
+ case Matcher::CheckAndImm: {
+ int64_t Val = cast<CheckAndImmMatcher>(N)->getValue();
OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
return EmitInt(Val, OS)+1;
}
- case MatcherNode::CheckOrImm: {
- int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
+ case Matcher::CheckOrImm: {
+ int64_t Val = cast<CheckOrImmMatcher>(N)->getValue();
OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
return EmitInt(Val, OS)+1;
}
- case MatcherNode::CheckFoldableChainNode:
+ case Matcher::CheckFoldableChainNode:
OS << "OPC_CheckFoldableChainNode,\n";
return 1;
- case MatcherNode::CheckChainCompatible:
+ case Matcher::CheckChainCompatible:
OS << "OPC_CheckChainCompatible, "
- << cast<CheckChainCompatibleMatcherNode>(N)->getPreviousOp() << ",\n";
+ << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
return 2;
- case MatcherNode::EmitInteger: {
- int64_t Val = cast<EmitIntegerMatcherNode>(N)->getValue();
+ case Matcher::EmitInteger: {
+ int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
OS << "OPC_EmitInteger" << ClassifyInt(Val) << ", "
- << getEnumName(cast<EmitIntegerMatcherNode>(N)->getVT()) << ", ";
+ << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
return EmitInt(Val, OS)+2;
}
- case MatcherNode::EmitStringInteger: {
- const std::string &Val = cast<EmitStringIntegerMatcherNode>(N)->getValue();
+ case Matcher::EmitStringInteger: {
+ const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
// These should always fit into one byte.
OS << "OPC_EmitInteger1, "
- << getEnumName(cast<EmitStringIntegerMatcherNode>(N)->getVT()) << ", "
+ << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
<< Val << ",\n";
return 3;
}
- case MatcherNode::EmitRegister:
+ case Matcher::EmitRegister:
OS << "OPC_EmitRegister, "
- << getEnumName(cast<EmitRegisterMatcherNode>(N)->getVT()) << ", ";
- if (Record *R = cast<EmitRegisterMatcherNode>(N)->getReg())
+ << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
+ if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
OS << getQualifiedName(R) << ",\n";
else
OS << "0 /*zero_reg*/,\n";
return 3;
- case MatcherNode::EmitConvertToTarget:
+ case Matcher::EmitConvertToTarget:
OS << "OPC_EmitConvertToTarget, "
- << cast<EmitConvertToTargetMatcherNode>(N)->getSlot() << ",\n";
+ << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
return 2;
- case MatcherNode::EmitMergeInputChains: {
- const EmitMergeInputChainsMatcherNode *MN =
- cast<EmitMergeInputChainsMatcherNode>(N);
+ case Matcher::EmitMergeInputChains: {
+ const EmitMergeInputChainsMatcher *MN =
+ cast<EmitMergeInputChainsMatcher>(N);
OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
OS << MN->getNode(i) << ", ";
OS << '\n';
return 2+MN->getNumNodes();
}
- case MatcherNode::EmitCopyToReg:
+ case Matcher::EmitCopyToReg:
OS << "OPC_EmitCopyToReg, "
- << cast<EmitCopyToRegMatcherNode>(N)->getSrcSlot() << ", "
- << getQualifiedName(cast<EmitCopyToRegMatcherNode>(N)->getDestPhysReg())
+ << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
+ << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
<< ",\n";
return 3;
- case MatcherNode::EmitNodeXForm: {
- const EmitNodeXFormMatcherNode *XF = cast<EmitNodeXFormMatcherNode>(N);
+ case Matcher::EmitNodeXForm: {
+ const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
<< XF->getSlot() << ',';
OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
return 3;
}
- case MatcherNode::EmitNode: {
- const EmitNodeMatcherNode *EN = cast<EmitNodeMatcherNode>(N);
- OS << "OPC_EmitNode, TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
+ case Matcher::EmitNode:
+ case Matcher::SelectNodeTo: {
+ const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
+ OS << (EN->isSelectNodeTo() ? "OPC_EmitNode" : "OPC_SelectNodeTo");
+ OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
if (EN->hasChain()) OS << "|OPFL_Chain";
if (EN->hasFlag()) OS << "|OPFL_Flag";
OS << '\n';
return 6+EN->getNumVTs()+NumOperandBytes;
}
- case MatcherNode::CompleteMatch: {
- const CompleteMatchMatcherNode *CM = cast<CompleteMatchMatcherNode>(N);
+ case Matcher::MarkFlagResults: {
+ const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
+ OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
+ unsigned NumOperandBytes = 0;
+ for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
+ NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
+ OS << '\n';
+ return 2+NumOperandBytes;
+ }
+ case Matcher::CompleteMatch: {
+ const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
unsigned NumResultBytes = 0;
for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
unsigned MatcherTableEmitter::
-EmitMatcherList(const MatcherNode *N, unsigned Indent, unsigned CurrentIdx,
+EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
formatted_raw_ostream &OS) {
unsigned Size = 0;
while (N) {
- // Push is a special case since it is binary.
- if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
- // We need to encode the child and the offset of the failure code before
- // emitting either of them. Handle this by buffering the output into a
- // string while we get the size.
- SmallString<128> TmpBuf;
- unsigned NextSize;
- {
- raw_svector_ostream OS(TmpBuf);
- formatted_raw_ostream FOS(OS);
- NextSize = EmitMatcherList(cast<PushMatcherNode>(N)->getNext(),
- Indent+1, CurrentIdx+2, FOS);
- }
-
- // In the unlikely event that we have something too big to emit with a
- // one byte offset, regenerate it with a two-byte one.
- if (NextSize > 255) {
- TmpBuf.clear();
- raw_svector_ostream OS(TmpBuf);
- formatted_raw_ostream FOS(OS);
- NextSize = EmitMatcherList(cast<PushMatcherNode>(N)->getNext(),
- Indent+1, CurrentIdx+3, FOS);
- if (NextSize > 65535) {
- errs() <<
- "Tblgen internal error: can't handle pattern this complex yet\n";
- exit(1);
- }
- }
-
- OS << "/*" << CurrentIdx << "*/";
- OS.PadToColumn(Indent*2);
-
- if (NextSize < 256)
- OS << "OPC_Push, " << NextSize << ",\n";
- else
- OS << "OPC_Push2, " << (NextSize&255) << ", " << (NextSize>>8) << ",\n";
- OS << TmpBuf.str();
-
- Size += 2+NextSize;
- CurrentIdx += 2+NextSize;
- N = PMN->getFailure();
- continue;
- }
-
+ if (unsigned(N->getKind()) >= Histogram.size())
+ Histogram.resize(N->getKind()+1);
+ Histogram[N->getKind()]++;
+
OS << "/*" << CurrentIdx << "*/";
- unsigned MatcherSize = EmitMatcher(N, Indent, OS);
+ unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
Size += MatcherSize;
CurrentIdx += MatcherSize;
OS << "}\n\n";
}
+void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
+ OS << " // Opcode Histogram:\n";
+ for (unsigned i = 0, e = Histogram.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::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
+ case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
+ case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
+ case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
+ case Matcher::CheckPatternPredicate:
+ OS << "OPC_CheckPatternPredicate"; break;
+ case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
+ case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
+ case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
+ case Matcher::CheckType: OS << "OPC_CheckType"; break;
+ case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
+ case Matcher::CheckInteger: OS << "OPC_CheckInteger"; 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::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; 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::SelectNodeTo: OS << "OPC_SelectNodeTo"; break;
+ case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
+ case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
+ case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
+ }
+
+ OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
+ }
+ OS << '\n';
+}
-void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
+
+void llvm::EmitMatcherTable(const Matcher *TheMatcher, raw_ostream &O) {
formatted_raw_ostream OS(O);
OS << "// The main instruction selector code.\n";
- OS << "SDNode *SelectCode2(SDNode *N) {\n";
+ OS << "SDNode *SelectCode(SDNode *N) {\n";
MatcherTableEmitter MatcherEmitter;
OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
OS << " static const unsigned char MatcherTable[] = {\n";
- unsigned TotalSize = MatcherEmitter.EmitMatcherList(Matcher, 5, 0, OS);
+ unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
+
+ MatcherEmitter.EmitHistogram(OS);
+
OS << " #undef TARGET_OPCODE\n";
OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
OS << "\n";