[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / utils / TableGen / DAGISelMatcherEmitter.cpp
blobe83e773122924a56b4385bac30f168b3983e4dba
1 //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains code to generate C++ code for a matcher.
11 //===----------------------------------------------------------------------===//
13 #include "CodeGenDAGPatterns.h"
14 #include "DAGISelMatcher.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/TinyPtrVector.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 using namespace llvm;
28 enum {
29 IndexWidth = 6,
30 FullIndexWidth = IndexWidth + 4,
31 HistOpcWidth = 40,
34 cl::OptionCategory DAGISelCat("Options for -gen-dag-isel");
36 // To reduce generated source code size.
37 static cl::opt<bool> OmitComments("omit-comments",
38 cl::desc("Do not generate comments"),
39 cl::init(false), cl::cat(DAGISelCat));
41 static cl::opt<bool> InstrumentCoverage(
42 "instrument-coverage",
43 cl::desc("Generates tables to help identify patterns matched"),
44 cl::init(false), cl::cat(DAGISelCat));
46 namespace {
47 class MatcherTableEmitter {
48 const CodeGenDAGPatterns &CGP;
50 DenseMap<TreePattern *, unsigned> NodePredicateMap;
51 std::vector<TreePredicateFn> NodePredicates;
52 std::vector<TreePredicateFn> NodePredicatesWithOperands;
54 // We de-duplicate the predicates by code string, and use this map to track
55 // all the patterns with "identical" predicates.
56 StringMap<TinyPtrVector<TreePattern *>> NodePredicatesByCodeToRun;
58 StringMap<unsigned> PatternPredicateMap;
59 std::vector<std::string> PatternPredicates;
61 DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
62 std::vector<const ComplexPattern*> ComplexPatterns;
65 DenseMap<Record*, unsigned> NodeXFormMap;
66 std::vector<Record*> NodeXForms;
68 std::vector<std::string> VecIncludeStrings;
69 MapVector<std::string, unsigned, StringMap<unsigned> > VecPatterns;
71 unsigned getPatternIdxFromTable(std::string &&P, std::string &&include_loc) {
72 const auto It = VecPatterns.find(P);
73 if (It == VecPatterns.end()) {
74 VecPatterns.insert(make_pair(std::move(P), VecPatterns.size()));
75 VecIncludeStrings.push_back(std::move(include_loc));
76 return VecIncludeStrings.size() - 1;
78 return It->second;
81 public:
82 MatcherTableEmitter(const CodeGenDAGPatterns &cgp)
83 : CGP(cgp) {}
85 unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
86 unsigned StartIdx, raw_ostream &OS);
88 void EmitPredicateFunctions(raw_ostream &OS);
90 void EmitHistogram(const Matcher *N, raw_ostream &OS);
92 void EmitPatternMatchTable(raw_ostream &OS);
94 private:
95 void EmitNodePredicatesFunction(const std::vector<TreePredicateFn> &Preds,
96 StringRef Decl, raw_ostream &OS);
98 unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
99 raw_ostream &OS);
101 unsigned getNodePredicate(TreePredicateFn Pred) {
102 TreePattern *TP = Pred.getOrigPatFragRecord();
103 unsigned &Entry = NodePredicateMap[TP];
104 if (Entry == 0) {
105 TinyPtrVector<TreePattern *> &SameCodePreds =
106 NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode()];
107 if (SameCodePreds.empty()) {
108 // We've never seen a predicate with the same code: allocate an entry.
109 if (Pred.usesOperands()) {
110 NodePredicatesWithOperands.push_back(Pred);
111 Entry = NodePredicatesWithOperands.size();
112 } else {
113 NodePredicates.push_back(Pred);
114 Entry = NodePredicates.size();
116 } else {
117 // We did see an identical predicate: re-use it.
118 Entry = NodePredicateMap[SameCodePreds.front()];
119 assert(Entry != 0);
120 assert(TreePredicateFn(SameCodePreds.front()).usesOperands() ==
121 Pred.usesOperands() &&
122 "PatFrags with some code must have same usesOperands setting");
124 // In both cases, we've never seen this particular predicate before, so
125 // mark it in the list of predicates sharing the same code.
126 SameCodePreds.push_back(TP);
128 return Entry-1;
131 unsigned getPatternPredicate(StringRef PredName) {
132 unsigned &Entry = PatternPredicateMap[PredName];
133 if (Entry == 0) {
134 PatternPredicates.push_back(PredName.str());
135 Entry = PatternPredicates.size();
137 return Entry-1;
139 unsigned getComplexPat(const ComplexPattern &P) {
140 unsigned &Entry = ComplexPatternMap[&P];
141 if (Entry == 0) {
142 ComplexPatterns.push_back(&P);
143 Entry = ComplexPatterns.size();
145 return Entry-1;
148 unsigned getNodeXFormID(Record *Rec) {
149 unsigned &Entry = NodeXFormMap[Rec];
150 if (Entry == 0) {
151 NodeXForms.push_back(Rec);
152 Entry = NodeXForms.size();
154 return Entry-1;
158 } // end anonymous namespace.
160 static std::string GetPatFromTreePatternNode(const TreePatternNode *N) {
161 std::string str;
162 raw_string_ostream Stream(str);
163 Stream << *N;
164 Stream.str();
165 return str;
168 static unsigned GetVBRSize(unsigned Val) {
169 if (Val <= 127) return 1;
171 unsigned NumBytes = 0;
172 while (Val >= 128) {
173 Val >>= 7;
174 ++NumBytes;
176 return NumBytes+1;
179 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
180 /// bytes emitted.
181 static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
182 if (Val <= 127) {
183 OS << Val << ", ";
184 return 1;
187 uint64_t InVal = Val;
188 unsigned NumBytes = 0;
189 while (Val >= 128) {
190 OS << (Val&127) << "|128,";
191 Val >>= 7;
192 ++NumBytes;
194 OS << Val;
195 if (!OmitComments)
196 OS << "/*" << InVal << "*/";
197 OS << ", ";
198 return NumBytes+1;
201 // This is expensive and slow.
202 static std::string getIncludePath(const Record *R) {
203 std::string str;
204 raw_string_ostream Stream(str);
205 auto Locs = R->getLoc();
206 SMLoc L;
207 if (Locs.size() > 1) {
208 // Get where the pattern prototype was instantiated
209 L = Locs[1];
210 } else if (Locs.size() == 1) {
211 L = Locs[0];
213 unsigned CurBuf = SrcMgr.FindBufferContainingLoc(L);
214 assert(CurBuf && "Invalid or unspecified location!");
216 Stream << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
217 << SrcMgr.FindLineNumber(L, CurBuf);
218 Stream.str();
219 return str;
222 static void BeginEmitFunction(raw_ostream &OS, StringRef RetType,
223 StringRef Decl, bool AddOverride) {
224 OS << "#ifdef GET_DAGISEL_DECL\n";
225 OS << RetType << ' ' << Decl;
226 if (AddOverride)
227 OS << " override";
228 OS << ";\n"
229 "#endif\n"
230 "#if defined(GET_DAGISEL_BODY) || DAGISEL_INLINE\n";
231 OS << RetType << " DAGISEL_CLASS_COLONCOLON " << Decl << "\n";
232 if (AddOverride) {
233 OS << "#if DAGISEL_INLINE\n"
234 " override\n"
235 "#endif\n";
239 static void EndEmitFunction(raw_ostream &OS) {
240 OS << "#endif // GET_DAGISEL_BODY\n\n";
243 void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
245 assert(isUInt<16>(VecPatterns.size()) &&
246 "Using only 16 bits to encode offset into Pattern Table");
247 assert(VecPatterns.size() == VecIncludeStrings.size() &&
248 "The sizes of Pattern and include vectors should be the same");
250 BeginEmitFunction(OS, "StringRef", "getPatternForIndex(unsigned Index)",
251 true/*AddOverride*/);
252 OS << "{\n";
253 OS << "static const char * PATTERN_MATCH_TABLE[] = {\n";
255 for (const auto &It : VecPatterns) {
256 OS << "\"" << It.first << "\",\n";
259 OS << "\n};";
260 OS << "\nreturn StringRef(PATTERN_MATCH_TABLE[Index]);";
261 OS << "\n}\n";
262 EndEmitFunction(OS);
264 BeginEmitFunction(OS, "StringRef", "getIncludePathForIndex(unsigned Index)",
265 true/*AddOverride*/);
266 OS << "{\n";
267 OS << "static const char * INCLUDE_PATH_TABLE[] = {\n";
269 for (const auto &It : VecIncludeStrings) {
270 OS << "\"" << It << "\",\n";
273 OS << "\n};";
274 OS << "\nreturn StringRef(INCLUDE_PATH_TABLE[Index]);";
275 OS << "\n}\n";
276 EndEmitFunction(OS);
279 /// EmitMatcher - Emit bytes for the specified matcher and return
280 /// the number of bytes emitted.
281 unsigned MatcherTableEmitter::
282 EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
283 raw_ostream &OS) {
284 OS.indent(Indent*2);
286 switch (N->getKind()) {
287 case Matcher::Scope: {
288 const ScopeMatcher *SM = cast<ScopeMatcher>(N);
289 assert(SM->getNext() == nullptr && "Shouldn't have next after scope");
291 unsigned StartIdx = CurrentIdx;
293 // Emit all of the children.
294 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
295 if (i == 0) {
296 OS << "OPC_Scope, ";
297 ++CurrentIdx;
298 } else {
299 if (!OmitComments) {
300 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
301 OS.indent(Indent*2) << "/*Scope*/ ";
302 } else
303 OS.indent(Indent*2);
306 // We need to encode the child and the offset of the failure code before
307 // emitting either of them. Handle this by buffering the output into a
308 // string while we get the size. Unfortunately, the offset of the
309 // children depends on the VBR size of the child, so for large children we
310 // have to iterate a bit.
311 SmallString<128> TmpBuf;
312 unsigned ChildSize = 0;
313 unsigned VBRSize = 0;
314 do {
315 VBRSize = GetVBRSize(ChildSize);
317 TmpBuf.clear();
318 raw_svector_ostream OS(TmpBuf);
319 ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
320 CurrentIdx+VBRSize, OS);
321 } while (GetVBRSize(ChildSize) != VBRSize);
323 assert(ChildSize != 0 && "Should not have a zero-sized child!");
325 CurrentIdx += EmitVBRValue(ChildSize, OS);
326 if (!OmitComments) {
327 OS << "/*->" << CurrentIdx+ChildSize << "*/";
329 if (i == 0)
330 OS << " // " << SM->getNumChildren() << " children in Scope";
333 OS << '\n' << TmpBuf;
334 CurrentIdx += ChildSize;
337 // Emit a zero as a sentinel indicating end of 'Scope'.
338 if (!OmitComments)
339 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
340 OS.indent(Indent*2) << "0, ";
341 if (!OmitComments)
342 OS << "/*End of Scope*/";
343 OS << '\n';
344 return CurrentIdx - StartIdx + 1;
347 case Matcher::RecordNode:
348 OS << "OPC_RecordNode,";
349 if (!OmitComments)
350 OS << " // #"
351 << cast<RecordMatcher>(N)->getResultNo() << " = "
352 << cast<RecordMatcher>(N)->getWhatFor();
353 OS << '\n';
354 return 1;
356 case Matcher::RecordChild:
357 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
358 << ',';
359 if (!OmitComments)
360 OS << " // #"
361 << cast<RecordChildMatcher>(N)->getResultNo() << " = "
362 << cast<RecordChildMatcher>(N)->getWhatFor();
363 OS << '\n';
364 return 1;
366 case Matcher::RecordMemRef:
367 OS << "OPC_RecordMemRef,\n";
368 return 1;
370 case Matcher::CaptureGlueInput:
371 OS << "OPC_CaptureGlueInput,\n";
372 return 1;
374 case Matcher::MoveChild: {
375 const auto *MCM = cast<MoveChildMatcher>(N);
377 OS << "OPC_MoveChild";
378 // Handle the specialized forms.
379 if (MCM->getChildNo() >= 8)
380 OS << ", ";
381 OS << MCM->getChildNo() << ",\n";
382 return (MCM->getChildNo() >= 8) ? 2 : 1;
385 case Matcher::MoveParent:
386 OS << "OPC_MoveParent,\n";
387 return 1;
389 case Matcher::CheckSame:
390 OS << "OPC_CheckSame, "
391 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
392 return 2;
394 case Matcher::CheckChildSame:
395 OS << "OPC_CheckChild"
396 << cast<CheckChildSameMatcher>(N)->getChildNo() << "Same, "
397 << cast<CheckChildSameMatcher>(N)->getMatchNumber() << ",\n";
398 return 2;
400 case Matcher::CheckPatternPredicate: {
401 StringRef Pred =cast<CheckPatternPredicateMatcher>(N)->getPredicate();
402 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
403 if (!OmitComments)
404 OS << " // " << Pred;
405 OS << '\n';
406 return 2;
408 case Matcher::CheckPredicate: {
409 TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate();
410 unsigned OperandBytes = 0;
412 if (Pred.usesOperands()) {
413 unsigned NumOps = cast<CheckPredicateMatcher>(N)->getNumOperands();
414 OS << "OPC_CheckPredicateWithOperands, " << NumOps << "/*#Ops*/, ";
415 for (unsigned i = 0; i < NumOps; ++i)
416 OS << cast<CheckPredicateMatcher>(N)->getOperandNo(i) << ", ";
417 OperandBytes = 1 + NumOps;
418 } else {
419 OS << "OPC_CheckPredicate, ";
422 OS << getNodePredicate(Pred) << ',';
423 if (!OmitComments)
424 OS << " // " << Pred.getFnName();
425 OS << '\n';
426 return 2 + OperandBytes;
429 case Matcher::CheckOpcode:
430 OS << "OPC_CheckOpcode, TARGET_VAL("
431 << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << "),\n";
432 return 3;
434 case Matcher::SwitchOpcode:
435 case Matcher::SwitchType: {
436 unsigned StartIdx = CurrentIdx;
438 unsigned NumCases;
439 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
440 OS << "OPC_SwitchOpcode ";
441 NumCases = SOM->getNumCases();
442 } else {
443 OS << "OPC_SwitchType ";
444 NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
447 if (!OmitComments)
448 OS << "/*" << NumCases << " cases */";
449 OS << ", ";
450 ++CurrentIdx;
452 // For each case we emit the size, then the opcode, then the matcher.
453 for (unsigned i = 0, e = NumCases; i != e; ++i) {
454 const Matcher *Child;
455 unsigned IdxSize;
456 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
457 Child = SOM->getCaseMatcher(i);
458 IdxSize = 2; // size of opcode in table is 2 bytes.
459 } else {
460 Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
461 IdxSize = 1; // size of type in table is 1 byte.
464 // We need to encode the opcode and the offset of the case code before
465 // emitting the case code. Handle this by buffering the output into a
466 // string while we get the size. Unfortunately, the offset of the
467 // children depends on the VBR size of the child, so for large children we
468 // have to iterate a bit.
469 SmallString<128> TmpBuf;
470 unsigned ChildSize = 0;
471 unsigned VBRSize = 0;
472 do {
473 VBRSize = GetVBRSize(ChildSize);
475 TmpBuf.clear();
476 raw_svector_ostream OS(TmpBuf);
477 ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+IdxSize,
478 OS);
479 } while (GetVBRSize(ChildSize) != VBRSize);
481 assert(ChildSize != 0 && "Should not have a zero-sized child!");
483 if (i != 0) {
484 if (!OmitComments)
485 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
486 OS.indent(Indent*2);
487 if (!OmitComments)
488 OS << (isa<SwitchOpcodeMatcher>(N) ?
489 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
492 // Emit the VBR.
493 CurrentIdx += EmitVBRValue(ChildSize, OS);
495 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
496 OS << "TARGET_VAL(" << SOM->getCaseOpcode(i).getEnumName() << "),";
497 else
498 OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i)) << ',';
500 CurrentIdx += IdxSize;
502 if (!OmitComments)
503 OS << "// ->" << CurrentIdx+ChildSize;
504 OS << '\n';
505 OS << TmpBuf;
506 CurrentIdx += ChildSize;
509 // Emit the final zero to terminate the switch.
510 if (!OmitComments)
511 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
512 OS.indent(Indent*2) << "0,";
513 if (!OmitComments)
514 OS << (isa<SwitchOpcodeMatcher>(N) ?
515 " // EndSwitchOpcode" : " // EndSwitchType");
517 OS << '\n';
518 ++CurrentIdx;
519 return CurrentIdx-StartIdx;
522 case Matcher::CheckType:
523 if (cast<CheckTypeMatcher>(N)->getResNo() == 0) {
524 OS << "OPC_CheckType, "
525 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
526 return 2;
528 OS << "OPC_CheckTypeRes, " << cast<CheckTypeMatcher>(N)->getResNo()
529 << ", " << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
530 return 3;
532 case Matcher::CheckChildType:
533 OS << "OPC_CheckChild"
534 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
535 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
536 return 2;
538 case Matcher::CheckInteger: {
539 OS << "OPC_CheckInteger, ";
540 unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
541 OS << '\n';
542 return Bytes;
544 case Matcher::CheckChildInteger: {
545 OS << "OPC_CheckChild" << cast<CheckChildIntegerMatcher>(N)->getChildNo()
546 << "Integer, ";
547 unsigned Bytes=1+EmitVBRValue(cast<CheckChildIntegerMatcher>(N)->getValue(),
548 OS);
549 OS << '\n';
550 return Bytes;
552 case Matcher::CheckCondCode:
553 OS << "OPC_CheckCondCode, ISD::"
554 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
555 return 2;
557 case Matcher::CheckValueType:
558 OS << "OPC_CheckValueType, MVT::"
559 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
560 return 2;
562 case Matcher::CheckComplexPat: {
563 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
564 const ComplexPattern &Pattern = CCPM->getPattern();
565 OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
566 << CCPM->getMatchNumber() << ',';
568 if (!OmitComments) {
569 OS << " // " << Pattern.getSelectFunc();
570 OS << ":$" << CCPM->getName();
571 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
572 OS << " #" << CCPM->getFirstResult()+i;
574 if (Pattern.hasProperty(SDNPHasChain))
575 OS << " + chain result";
577 OS << '\n';
578 return 3;
581 case Matcher::CheckAndImm: {
582 OS << "OPC_CheckAndImm, ";
583 unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
584 OS << '\n';
585 return Bytes;
588 case Matcher::CheckOrImm: {
589 OS << "OPC_CheckOrImm, ";
590 unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
591 OS << '\n';
592 return Bytes;
595 case Matcher::CheckFoldableChainNode:
596 OS << "OPC_CheckFoldableChainNode,\n";
597 return 1;
599 case Matcher::EmitInteger: {
600 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
601 OS << "OPC_EmitInteger, "
602 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
603 unsigned Bytes = 2+EmitVBRValue(Val, OS);
604 OS << '\n';
605 return Bytes;
607 case Matcher::EmitStringInteger: {
608 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
609 // These should always fit into one byte.
610 OS << "OPC_EmitInteger, "
611 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
612 << Val << ",\n";
613 return 3;
616 case Matcher::EmitRegister: {
617 const EmitRegisterMatcher *Matcher = cast<EmitRegisterMatcher>(N);
618 const CodeGenRegister *Reg = Matcher->getReg();
619 // If the enum value of the register is larger than one byte can handle,
620 // use EmitRegister2.
621 if (Reg && Reg->EnumValue > 255) {
622 OS << "OPC_EmitRegister2, " << getEnumName(Matcher->getVT()) << ", ";
623 OS << "TARGET_VAL(" << getQualifiedName(Reg->TheDef) << "),\n";
624 return 4;
625 } else {
626 OS << "OPC_EmitRegister, " << getEnumName(Matcher->getVT()) << ", ";
627 if (Reg) {
628 OS << getQualifiedName(Reg->TheDef) << ",\n";
629 } else {
630 OS << "0 ";
631 if (!OmitComments)
632 OS << "/*zero_reg*/";
633 OS << ",\n";
635 return 3;
639 case Matcher::EmitConvertToTarget:
640 OS << "OPC_EmitConvertToTarget, "
641 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
642 return 2;
644 case Matcher::EmitMergeInputChains: {
645 const EmitMergeInputChainsMatcher *MN =
646 cast<EmitMergeInputChainsMatcher>(N);
648 // Handle the specialized forms OPC_EmitMergeInputChains1_0, 1_1, and 1_2.
649 if (MN->getNumNodes() == 1 && MN->getNode(0) < 3) {
650 OS << "OPC_EmitMergeInputChains1_" << MN->getNode(0) << ",\n";
651 return 1;
654 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
655 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
656 OS << MN->getNode(i) << ", ";
657 OS << '\n';
658 return 2+MN->getNumNodes();
660 case Matcher::EmitCopyToReg:
661 OS << "OPC_EmitCopyToReg, "
662 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
663 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
664 << ",\n";
665 return 3;
666 case Matcher::EmitNodeXForm: {
667 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
668 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
669 << XF->getSlot() << ',';
670 if (!OmitComments)
671 OS << " // "<<XF->getNodeXForm()->getName();
672 OS <<'\n';
673 return 3;
676 case Matcher::EmitNode:
677 case Matcher::MorphNodeTo: {
678 auto NumCoveredBytes = 0;
679 if (InstrumentCoverage) {
680 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
681 NumCoveredBytes = 3;
682 OS << "OPC_Coverage, ";
683 std::string src =
684 GetPatFromTreePatternNode(SNT->getPattern().getSrcPattern());
685 std::string dst =
686 GetPatFromTreePatternNode(SNT->getPattern().getDstPattern());
687 Record *PatRecord = SNT->getPattern().getSrcRecord();
688 std::string include_src = getIncludePath(PatRecord);
689 unsigned Offset =
690 getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
691 OS << "TARGET_VAL(" << Offset << "),\n";
692 OS.indent(FullIndexWidth + Indent * 2);
695 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
696 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
697 bool CompressVTs = EN->getNumVTs() < 3;
698 if (CompressVTs)
699 OS << EN->getNumVTs();
701 OS << ", TARGET_VAL(" << EN->getOpcodeName() << "), 0";
703 if (EN->hasChain()) OS << "|OPFL_Chain";
704 if (EN->hasInFlag()) OS << "|OPFL_GlueInput";
705 if (EN->hasOutFlag()) OS << "|OPFL_GlueOutput";
706 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
707 if (EN->getNumFixedArityOperands() != -1)
708 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
709 OS << ",\n";
711 OS.indent(FullIndexWidth + Indent*2+4);
712 if (!CompressVTs) {
713 OS << EN->getNumVTs();
714 if (!OmitComments)
715 OS << "/*#VTs*/";
716 OS << ", ";
718 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
719 OS << getEnumName(EN->getVT(i)) << ", ";
721 OS << EN->getNumOperands();
722 if (!OmitComments)
723 OS << "/*#Ops*/";
724 OS << ", ";
725 unsigned NumOperandBytes = 0;
726 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
727 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
729 if (!OmitComments) {
730 // Print the result #'s for EmitNode.
731 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
732 if (unsigned NumResults = EN->getNumVTs()) {
733 OS << " // Results =";
734 unsigned First = E->getFirstResultSlot();
735 for (unsigned i = 0; i != NumResults; ++i)
736 OS << " #" << First+i;
739 OS << '\n';
741 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
742 OS.indent(FullIndexWidth + Indent*2) << "// Src: "
743 << *SNT->getPattern().getSrcPattern() << " - Complexity = "
744 << SNT->getPattern().getPatternComplexity(CGP) << '\n';
745 OS.indent(FullIndexWidth + Indent*2) << "// Dst: "
746 << *SNT->getPattern().getDstPattern() << '\n';
748 } else
749 OS << '\n';
751 return 5 + !CompressVTs + EN->getNumVTs() + NumOperandBytes +
752 NumCoveredBytes;
754 case Matcher::CompleteMatch: {
755 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
756 auto NumCoveredBytes = 0;
757 if (InstrumentCoverage) {
758 NumCoveredBytes = 3;
759 OS << "OPC_Coverage, ";
760 std::string src =
761 GetPatFromTreePatternNode(CM->getPattern().getSrcPattern());
762 std::string dst =
763 GetPatFromTreePatternNode(CM->getPattern().getDstPattern());
764 Record *PatRecord = CM->getPattern().getSrcRecord();
765 std::string include_src = getIncludePath(PatRecord);
766 unsigned Offset =
767 getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
768 OS << "TARGET_VAL(" << Offset << "),\n";
769 OS.indent(FullIndexWidth + Indent * 2);
771 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
772 unsigned NumResultBytes = 0;
773 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
774 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
775 OS << '\n';
776 if (!OmitComments) {
777 OS.indent(FullIndexWidth + Indent*2) << " // Src: "
778 << *CM->getPattern().getSrcPattern() << " - Complexity = "
779 << CM->getPattern().getPatternComplexity(CGP) << '\n';
780 OS.indent(FullIndexWidth + Indent*2) << " // Dst: "
781 << *CM->getPattern().getDstPattern();
783 OS << '\n';
784 return 2 + NumResultBytes + NumCoveredBytes;
787 llvm_unreachable("Unreachable");
790 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
791 unsigned MatcherTableEmitter::
792 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
793 raw_ostream &OS) {
794 unsigned Size = 0;
795 while (N) {
796 if (!OmitComments)
797 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
798 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
799 Size += MatcherSize;
800 CurrentIdx += MatcherSize;
802 // If there are other nodes in this list, iterate to them, otherwise we're
803 // done.
804 N = N->getNext();
806 return Size;
809 void MatcherTableEmitter::EmitNodePredicatesFunction(
810 const std::vector<TreePredicateFn> &Preds, StringRef Decl,
811 raw_ostream &OS) {
812 if (Preds.empty())
813 return;
815 BeginEmitFunction(OS, "bool", Decl, true/*AddOverride*/);
816 OS << "{\n";
817 OS << " switch (PredNo) {\n";
818 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
819 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
820 // Emit the predicate code corresponding to this pattern.
821 TreePredicateFn PredFn = Preds[i];
823 assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
824 OS << " case " << i << ": { \n";
825 for (auto *SimilarPred :
826 NodePredicatesByCodeToRun[PredFn.getCodeToRunOnSDNode()])
827 OS << " // " << TreePredicateFn(SimilarPred).getFnName() <<'\n';
829 OS << PredFn.getCodeToRunOnSDNode() << "\n }\n";
831 OS << " }\n";
832 OS << "}\n";
833 EndEmitFunction(OS);
836 void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
837 // Emit pattern predicates.
838 if (!PatternPredicates.empty()) {
839 BeginEmitFunction(OS, "bool",
840 "CheckPatternPredicate(unsigned PredNo) const", true/*AddOverride*/);
841 OS << "{\n";
842 OS << " switch (PredNo) {\n";
843 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
844 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
845 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
846 OS << " }\n";
847 OS << "}\n";
848 EndEmitFunction(OS);
851 // Emit Node predicates.
852 EmitNodePredicatesFunction(
853 NodePredicates, "CheckNodePredicate(SDNode *Node, unsigned PredNo) const",
854 OS);
855 EmitNodePredicatesFunction(
856 NodePredicatesWithOperands,
857 "CheckNodePredicateWithOperands(SDNode *Node, unsigned PredNo, "
858 "const SmallVectorImpl<SDValue> &Operands) const",
859 OS);
861 // Emit CompletePattern matchers.
862 // FIXME: This should be const.
863 if (!ComplexPatterns.empty()) {
864 BeginEmitFunction(OS, "bool",
865 "CheckComplexPattern(SDNode *Root, SDNode *Parent,\n"
866 " SDValue N, unsigned PatternNo,\n"
867 " SmallVectorImpl<std::pair<SDValue, SDNode*>> &Result)",
868 true/*AddOverride*/);
869 OS << "{\n";
870 OS << " unsigned NextRes = Result.size();\n";
871 OS << " switch (PatternNo) {\n";
872 OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
873 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
874 const ComplexPattern &P = *ComplexPatterns[i];
875 unsigned NumOps = P.getNumOperands();
877 if (P.hasProperty(SDNPHasChain))
878 ++NumOps; // Get the chained node too.
880 OS << " case " << i << ":\n";
881 if (InstrumentCoverage)
882 OS << " {\n";
883 OS << " Result.resize(NextRes+" << NumOps << ");\n";
884 if (InstrumentCoverage)
885 OS << " bool Succeeded = " << P.getSelectFunc();
886 else
887 OS << " return " << P.getSelectFunc();
889 OS << "(";
890 // If the complex pattern wants the root of the match, pass it in as the
891 // first argument.
892 if (P.hasProperty(SDNPWantRoot))
893 OS << "Root, ";
895 // If the complex pattern wants the parent of the operand being matched,
896 // pass it in as the next argument.
897 if (P.hasProperty(SDNPWantParent))
898 OS << "Parent, ";
900 OS << "N";
901 for (unsigned i = 0; i != NumOps; ++i)
902 OS << ", Result[NextRes+" << i << "].first";
903 OS << ");\n";
904 if (InstrumentCoverage) {
905 OS << " if (Succeeded)\n";
906 OS << " dbgs() << \"\\nCOMPLEX_PATTERN: " << P.getSelectFunc()
907 << "\\n\" ;\n";
908 OS << " return Succeeded;\n";
909 OS << " }\n";
912 OS << " }\n";
913 OS << "}\n";
914 EndEmitFunction(OS);
918 // Emit SDNodeXForm handlers.
919 // FIXME: This should be const.
920 if (!NodeXForms.empty()) {
921 BeginEmitFunction(OS, "SDValue",
922 "RunSDNodeXForm(SDValue V, unsigned XFormNo)", true/*AddOverride*/);
923 OS << "{\n";
924 OS << " switch (XFormNo) {\n";
925 OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
927 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
928 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
929 const CodeGenDAGPatterns::NodeXForm &Entry =
930 CGP.getSDNodeTransform(NodeXForms[i]);
932 Record *SDNode = Entry.first;
933 const std::string &Code = Entry.second;
935 OS << " case " << i << ": { ";
936 if (!OmitComments)
937 OS << "// " << NodeXForms[i]->getName();
938 OS << '\n';
940 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
941 if (ClassName == "SDNode")
942 OS << " SDNode *N = V.getNode();\n";
943 else
944 OS << " " << ClassName << " *N = cast<" << ClassName
945 << ">(V.getNode());\n";
946 OS << Code << "\n }\n";
948 OS << " }\n";
949 OS << "}\n";
950 EndEmitFunction(OS);
954 static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
955 for (; M != nullptr; M = M->getNext()) {
956 // Count this node.
957 if (unsigned(M->getKind()) >= OpcodeFreq.size())
958 OpcodeFreq.resize(M->getKind()+1);
959 OpcodeFreq[M->getKind()]++;
961 // Handle recursive nodes.
962 if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
963 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
964 BuildHistogram(SM->getChild(i), OpcodeFreq);
965 } else if (const SwitchOpcodeMatcher *SOM =
966 dyn_cast<SwitchOpcodeMatcher>(M)) {
967 for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
968 BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
969 } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
970 for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
971 BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
976 static StringRef getOpcodeString(Matcher::KindTy Kind) {
977 switch (Kind) {
978 case Matcher::Scope: return "OPC_Scope"; break;
979 case Matcher::RecordNode: return "OPC_RecordNode"; break;
980 case Matcher::RecordChild: return "OPC_RecordChild"; break;
981 case Matcher::RecordMemRef: return "OPC_RecordMemRef"; break;
982 case Matcher::CaptureGlueInput: return "OPC_CaptureGlueInput"; break;
983 case Matcher::MoveChild: return "OPC_MoveChild"; break;
984 case Matcher::MoveParent: return "OPC_MoveParent"; break;
985 case Matcher::CheckSame: return "OPC_CheckSame"; break;
986 case Matcher::CheckChildSame: return "OPC_CheckChildSame"; break;
987 case Matcher::CheckPatternPredicate:
988 return "OPC_CheckPatternPredicate"; break;
989 case Matcher::CheckPredicate: return "OPC_CheckPredicate"; break;
990 case Matcher::CheckOpcode: return "OPC_CheckOpcode"; break;
991 case Matcher::SwitchOpcode: return "OPC_SwitchOpcode"; break;
992 case Matcher::CheckType: return "OPC_CheckType"; break;
993 case Matcher::SwitchType: return "OPC_SwitchType"; break;
994 case Matcher::CheckChildType: return "OPC_CheckChildType"; break;
995 case Matcher::CheckInteger: return "OPC_CheckInteger"; break;
996 case Matcher::CheckChildInteger: return "OPC_CheckChildInteger"; break;
997 case Matcher::CheckCondCode: return "OPC_CheckCondCode"; break;
998 case Matcher::CheckValueType: return "OPC_CheckValueType"; break;
999 case Matcher::CheckComplexPat: return "OPC_CheckComplexPat"; break;
1000 case Matcher::CheckAndImm: return "OPC_CheckAndImm"; break;
1001 case Matcher::CheckOrImm: return "OPC_CheckOrImm"; break;
1002 case Matcher::CheckFoldableChainNode:
1003 return "OPC_CheckFoldableChainNode"; break;
1004 case Matcher::EmitInteger: return "OPC_EmitInteger"; break;
1005 case Matcher::EmitStringInteger: return "OPC_EmitStringInteger"; break;
1006 case Matcher::EmitRegister: return "OPC_EmitRegister"; break;
1007 case Matcher::EmitConvertToTarget: return "OPC_EmitConvertToTarget"; break;
1008 case Matcher::EmitMergeInputChains: return "OPC_EmitMergeInputChains"; break;
1009 case Matcher::EmitCopyToReg: return "OPC_EmitCopyToReg"; break;
1010 case Matcher::EmitNode: return "OPC_EmitNode"; break;
1011 case Matcher::MorphNodeTo: return "OPC_MorphNodeTo"; break;
1012 case Matcher::EmitNodeXForm: return "OPC_EmitNodeXForm"; break;
1013 case Matcher::CompleteMatch: return "OPC_CompleteMatch"; break;
1016 llvm_unreachable("Unhandled opcode?");
1019 void MatcherTableEmitter::EmitHistogram(const Matcher *M,
1020 raw_ostream &OS) {
1021 if (OmitComments)
1022 return;
1024 std::vector<unsigned> OpcodeFreq;
1025 BuildHistogram(M, OpcodeFreq);
1027 OS << " // Opcode Histogram:\n";
1028 for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
1029 OS << " // #"
1030 << left_justify(getOpcodeString((Matcher::KindTy)i), HistOpcWidth)
1031 << " = " << OpcodeFreq[i] << '\n';
1033 OS << '\n';
1037 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
1038 const CodeGenDAGPatterns &CGP,
1039 raw_ostream &OS) {
1040 OS << "#if defined(GET_DAGISEL_DECL) && defined(GET_DAGISEL_BODY)\n";
1041 OS << "#error GET_DAGISEL_DECL and GET_DAGISEL_BODY cannot be both defined, ";
1042 OS << "undef both for inline definitions\n";
1043 OS << "#endif\n\n";
1045 // Emit a check for omitted class name.
1046 OS << "#ifdef GET_DAGISEL_BODY\n";
1047 OS << "#define LOCAL_DAGISEL_STRINGIZE(X) LOCAL_DAGISEL_STRINGIZE_(X)\n";
1048 OS << "#define LOCAL_DAGISEL_STRINGIZE_(X) #X\n";
1049 OS << "static_assert(sizeof(LOCAL_DAGISEL_STRINGIZE(GET_DAGISEL_BODY)) > 1,"
1050 "\n";
1051 OS << " \"GET_DAGISEL_BODY is empty: it should be defined with the class "
1052 "name\");\n";
1053 OS << "#undef LOCAL_DAGISEL_STRINGIZE_\n";
1054 OS << "#undef LOCAL_DAGISEL_STRINGIZE\n";
1055 OS << "#endif\n\n";
1057 OS << "#if !defined(GET_DAGISEL_DECL) && !defined(GET_DAGISEL_BODY)\n";
1058 OS << "#define DAGISEL_INLINE 1\n";
1059 OS << "#else\n";
1060 OS << "#define DAGISEL_INLINE 0\n";
1061 OS << "#endif\n\n";
1063 OS << "#if !DAGISEL_INLINE\n";
1064 OS << "#define DAGISEL_CLASS_COLONCOLON GET_DAGISEL_BODY ::\n";
1065 OS << "#else\n";
1066 OS << "#define DAGISEL_CLASS_COLONCOLON\n";
1067 OS << "#endif\n\n";
1069 BeginEmitFunction(OS, "void", "SelectCode(SDNode *N)", false/*AddOverride*/);
1070 MatcherTableEmitter MatcherEmitter(CGP);
1072 OS << "{\n";
1073 OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
1074 OS << " // this.\n";
1075 OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
1076 OS << " static const unsigned char MatcherTable[] = {\n";
1077 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 1, 0, OS);
1078 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
1080 MatcherEmitter.EmitHistogram(TheMatcher, OS);
1082 OS << " #undef TARGET_VAL\n";
1083 OS << " SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n";
1084 OS << "}\n";
1085 EndEmitFunction(OS);
1087 // Next up, emit the function for node and pattern predicates:
1088 MatcherEmitter.EmitPredicateFunctions(OS);
1090 if (InstrumentCoverage)
1091 MatcherEmitter.EmitPatternMatchTable(OS);
1093 // Clean up the preprocessor macros.
1094 OS << "\n";
1095 OS << "#ifdef DAGISEL_INLINE\n";
1096 OS << "#undef DAGISEL_INLINE\n";
1097 OS << "#endif\n";
1098 OS << "#ifdef DAGISEL_CLASS_COLONCOLON\n";
1099 OS << "#undef DAGISEL_CLASS_COLONCOLON\n";
1100 OS << "#endif\n";
1101 OS << "#ifdef GET_DAGISEL_DECL\n";
1102 OS << "#undef GET_DAGISEL_DECL\n";
1103 OS << "#endif\n";
1104 OS << "#ifdef GET_DAGISEL_BODY\n";
1105 OS << "#undef GET_DAGISEL_BODY\n";
1106 OS << "#endif\n";