[llvm-objdump] --disassemble-symbols: skip inline relocs from symbols that are not...
[llvm-project.git] / llvm / utils / TableGen / DAGISelMatcherEmitter.cpp
blob94799267e896094091f91460a27fd2fed966cd0b
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 "CodeGenInstruction.h"
15 #include "CodeGenRegisters.h"
16 #include "CodeGenTarget.h"
17 #include "DAGISelMatcher.h"
18 #include "SDNodeProperties.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/TinyPtrVector.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/TableGen/Error.h"
27 #include "llvm/TableGen/Record.h"
29 using namespace llvm;
31 enum {
32 IndexWidth = 6,
33 FullIndexWidth = IndexWidth + 4,
34 HistOpcWidth = 40,
37 cl::OptionCategory DAGISelCat("Options for -gen-dag-isel");
39 // To reduce generated source code size.
40 static cl::opt<bool> OmitComments("omit-comments",
41 cl::desc("Do not generate comments"),
42 cl::init(false), cl::cat(DAGISelCat));
44 static cl::opt<bool> InstrumentCoverage(
45 "instrument-coverage",
46 cl::desc("Generates tables to help identify patterns matched"),
47 cl::init(false), cl::cat(DAGISelCat));
49 namespace {
50 class MatcherTableEmitter {
51 const CodeGenDAGPatterns &CGP;
53 SmallVector<unsigned, Matcher::HighestKind+1> OpcodeCounts;
55 DenseMap<TreePattern *, unsigned> NodePredicateMap;
56 std::vector<TreePredicateFn> NodePredicates;
57 std::vector<TreePredicateFn> NodePredicatesWithOperands;
59 // We de-duplicate the predicates by code string, and use this map to track
60 // all the patterns with "identical" predicates.
61 StringMap<TinyPtrVector<TreePattern *>> NodePredicatesByCodeToRun;
63 StringMap<unsigned> PatternPredicateMap;
64 std::vector<std::string> PatternPredicates;
66 DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
67 std::vector<const ComplexPattern*> ComplexPatterns;
70 DenseMap<Record*, unsigned> NodeXFormMap;
71 std::vector<Record*> NodeXForms;
73 std::vector<std::string> VecIncludeStrings;
74 MapVector<std::string, unsigned, StringMap<unsigned> > VecPatterns;
76 unsigned getPatternIdxFromTable(std::string &&P, std::string &&include_loc) {
77 const auto It = VecPatterns.find(P);
78 if (It == VecPatterns.end()) {
79 VecPatterns.insert(make_pair(std::move(P), VecPatterns.size()));
80 VecIncludeStrings.push_back(std::move(include_loc));
81 return VecIncludeStrings.size() - 1;
83 return It->second;
86 public:
87 MatcherTableEmitter(const CodeGenDAGPatterns &cgp)
88 : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1, 0) {}
90 unsigned EmitMatcherList(const Matcher *N, const unsigned Indent,
91 unsigned StartIdx, raw_ostream &OS);
93 unsigned SizeMatcherList(Matcher *N, raw_ostream &OS);
95 void EmitPredicateFunctions(raw_ostream &OS);
97 void EmitHistogram(const Matcher *N, raw_ostream &OS);
99 void EmitPatternMatchTable(raw_ostream &OS);
101 private:
102 void EmitNodePredicatesFunction(const std::vector<TreePredicateFn> &Preds,
103 StringRef Decl, raw_ostream &OS);
105 unsigned SizeMatcher(Matcher *N, raw_ostream &OS);
107 unsigned EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
108 raw_ostream &OS);
110 unsigned getNodePredicate(TreePredicateFn Pred) {
111 TreePattern *TP = Pred.getOrigPatFragRecord();
112 unsigned &Entry = NodePredicateMap[TP];
113 if (Entry == 0) {
114 TinyPtrVector<TreePattern *> &SameCodePreds =
115 NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode()];
116 if (SameCodePreds.empty()) {
117 // We've never seen a predicate with the same code: allocate an entry.
118 if (Pred.usesOperands()) {
119 NodePredicatesWithOperands.push_back(Pred);
120 Entry = NodePredicatesWithOperands.size();
121 } else {
122 NodePredicates.push_back(Pred);
123 Entry = NodePredicates.size();
125 } else {
126 // We did see an identical predicate: re-use it.
127 Entry = NodePredicateMap[SameCodePreds.front()];
128 assert(Entry != 0);
129 assert(TreePredicateFn(SameCodePreds.front()).usesOperands() ==
130 Pred.usesOperands() &&
131 "PatFrags with some code must have same usesOperands setting");
133 // In both cases, we've never seen this particular predicate before, so
134 // mark it in the list of predicates sharing the same code.
135 SameCodePreds.push_back(TP);
137 return Entry-1;
140 unsigned getPatternPredicate(StringRef PredName) {
141 unsigned &Entry = PatternPredicateMap[PredName];
142 if (Entry == 0) {
143 PatternPredicates.push_back(PredName.str());
144 Entry = PatternPredicates.size();
146 return Entry-1;
148 unsigned getComplexPat(const ComplexPattern &P) {
149 unsigned &Entry = ComplexPatternMap[&P];
150 if (Entry == 0) {
151 ComplexPatterns.push_back(&P);
152 Entry = ComplexPatterns.size();
154 return Entry-1;
157 unsigned getNodeXFormID(Record *Rec) {
158 unsigned &Entry = NodeXFormMap[Rec];
159 if (Entry == 0) {
160 NodeXForms.push_back(Rec);
161 Entry = NodeXForms.size();
163 return Entry-1;
167 } // end anonymous namespace.
169 static std::string GetPatFromTreePatternNode(const TreePatternNode *N) {
170 std::string str;
171 raw_string_ostream Stream(str);
172 Stream << *N;
173 return str;
176 static unsigned GetVBRSize(unsigned Val) {
177 if (Val <= 127) return 1;
179 unsigned NumBytes = 0;
180 while (Val >= 128) {
181 Val >>= 7;
182 ++NumBytes;
184 return NumBytes+1;
187 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
188 /// bytes emitted.
189 static unsigned EmitVBRValue(uint64_t Val, raw_ostream &OS) {
190 if (Val <= 127) {
191 OS << Val << ", ";
192 return 1;
195 uint64_t InVal = Val;
196 unsigned NumBytes = 0;
197 while (Val >= 128) {
198 OS << (Val&127) << "|128,";
199 Val >>= 7;
200 ++NumBytes;
202 OS << Val;
203 if (!OmitComments)
204 OS << "/*" << InVal << "*/";
205 OS << ", ";
206 return NumBytes+1;
209 /// Emit the specified signed value as a VBR. To improve compression we encode
210 /// positive numbers shifted left by 1 and negative numbers negated and shifted
211 /// left by 1 with bit 0 set.
212 static unsigned EmitSignedVBRValue(uint64_t Val, raw_ostream &OS) {
213 if ((int64_t)Val >= 0)
214 Val = Val << 1;
215 else
216 Val = (-Val << 1) | 1;
218 return EmitVBRValue(Val, OS);
221 // This is expensive and slow.
222 static std::string getIncludePath(const Record *R) {
223 std::string str;
224 raw_string_ostream Stream(str);
225 auto Locs = R->getLoc();
226 SMLoc L;
227 if (Locs.size() > 1) {
228 // Get where the pattern prototype was instantiated
229 L = Locs[1];
230 } else if (Locs.size() == 1) {
231 L = Locs[0];
233 unsigned CurBuf = SrcMgr.FindBufferContainingLoc(L);
234 assert(CurBuf && "Invalid or unspecified location!");
236 Stream << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
237 << SrcMgr.FindLineNumber(L, CurBuf);
238 return str;
241 /// This function traverses the matcher tree and sizes all the nodes
242 /// that are children of the three kinds of nodes that have them.
243 unsigned MatcherTableEmitter::
244 SizeMatcherList(Matcher *N, raw_ostream &OS) {
245 unsigned Size = 0;
246 while (N) {
247 Size += SizeMatcher(N, OS);
248 N = N->getNext();
250 return Size;
253 /// This function sizes the children of the three kinds of nodes that
254 /// have them. It does so by using special cases for those three
255 /// nodes, but sharing the code in EmitMatcher() for the other kinds.
256 unsigned MatcherTableEmitter::
257 SizeMatcher(Matcher *N, raw_ostream &OS) {
258 unsigned Idx = 0;
260 ++OpcodeCounts[N->getKind()];
261 switch (N->getKind()) {
262 // The Scope matcher has its kind, a series of child size + child,
263 // and a trailing zero.
264 case Matcher::Scope: {
265 ScopeMatcher *SM = cast<ScopeMatcher>(N);
266 assert(SM->getNext() == nullptr && "Scope matcher should not have next");
267 unsigned Size = 1; // Count the kind.
268 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
269 const unsigned ChildSize = SizeMatcherList(SM->getChild(i), OS);
270 assert(ChildSize != 0 && "Matcher cannot have child of size 0");
271 SM->getChild(i)->setSize(ChildSize);
272 Size += GetVBRSize(ChildSize) + ChildSize; // Count VBR and child size.
274 ++Size; // Count the zero sentinel.
275 return Size;
278 // SwitchOpcode and SwitchType have their kind, a series of child size +
279 // opcode/type + child, and a trailing zero.
280 case Matcher::SwitchOpcode:
281 case Matcher::SwitchType: {
282 unsigned Size = 1; // Count the kind.
283 unsigned NumCases;
284 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
285 NumCases = SOM->getNumCases();
286 else
287 NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
288 for (unsigned i = 0, e = NumCases; i != e; ++i) {
289 Matcher *Child;
290 if (SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
291 Child = SOM->getCaseMatcher(i);
292 Size += 2; // Count the child's opcode.
293 } else {
294 Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
295 ++Size; // Count the child's type.
297 const unsigned ChildSize = SizeMatcherList(Child, OS);
298 assert(ChildSize != 0 && "Matcher cannot have child of size 0");
299 Child->setSize(ChildSize);
300 Size += GetVBRSize(ChildSize) + ChildSize; // Count VBR and child size.
302 ++Size; // Count the zero sentinel.
303 return Size;
306 default:
307 // Employ the matcher emitter to size other matchers.
308 return EmitMatcher(N, 0, Idx, OS);
310 llvm_unreachable("Unreachable");
313 static void BeginEmitFunction(raw_ostream &OS, StringRef RetType,
314 StringRef Decl, bool AddOverride) {
315 OS << "#ifdef GET_DAGISEL_DECL\n";
316 OS << RetType << ' ' << Decl;
317 if (AddOverride)
318 OS << " override";
319 OS << ";\n"
320 "#endif\n"
321 "#if defined(GET_DAGISEL_BODY) || DAGISEL_INLINE\n";
322 OS << RetType << " DAGISEL_CLASS_COLONCOLON " << Decl << "\n";
323 if (AddOverride) {
324 OS << "#if DAGISEL_INLINE\n"
325 " override\n"
326 "#endif\n";
330 static void EndEmitFunction(raw_ostream &OS) {
331 OS << "#endif // GET_DAGISEL_BODY\n\n";
334 void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
336 assert(isUInt<16>(VecPatterns.size()) &&
337 "Using only 16 bits to encode offset into Pattern Table");
338 assert(VecPatterns.size() == VecIncludeStrings.size() &&
339 "The sizes of Pattern and include vectors should be the same");
341 BeginEmitFunction(OS, "StringRef", "getPatternForIndex(unsigned Index)",
342 true/*AddOverride*/);
343 OS << "{\n";
344 OS << "static const char *PATTERN_MATCH_TABLE[] = {\n";
346 for (const auto &It : VecPatterns) {
347 OS << "\"" << It.first << "\",\n";
350 OS << "\n};";
351 OS << "\nreturn StringRef(PATTERN_MATCH_TABLE[Index]);";
352 OS << "\n}\n";
353 EndEmitFunction(OS);
355 BeginEmitFunction(OS, "StringRef", "getIncludePathForIndex(unsigned Index)",
356 true/*AddOverride*/);
357 OS << "{\n";
358 OS << "static const char *INCLUDE_PATH_TABLE[] = {\n";
360 for (const auto &It : VecIncludeStrings) {
361 OS << "\"" << It << "\",\n";
364 OS << "\n};";
365 OS << "\nreturn StringRef(INCLUDE_PATH_TABLE[Index]);";
366 OS << "\n}\n";
367 EndEmitFunction(OS);
370 /// EmitMatcher - Emit bytes for the specified matcher and return
371 /// the number of bytes emitted.
372 unsigned MatcherTableEmitter::
373 EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
374 raw_ostream &OS) {
375 OS.indent(Indent);
377 switch (N->getKind()) {
378 case Matcher::Scope: {
379 const ScopeMatcher *SM = cast<ScopeMatcher>(N);
380 unsigned StartIdx = CurrentIdx;
382 // Emit all of the children.
383 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
384 if (i == 0) {
385 OS << "OPC_Scope, ";
386 ++CurrentIdx;
387 } else {
388 if (!OmitComments) {
389 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
390 OS.indent(Indent) << "/*Scope*/ ";
391 } else
392 OS.indent(Indent);
395 unsigned ChildSize = SM->getChild(i)->getSize();
396 unsigned VBRSize = EmitVBRValue(ChildSize, OS);
397 if (!OmitComments) {
398 OS << "/*->" << CurrentIdx + VBRSize + ChildSize << "*/";
399 if (i == 0)
400 OS << " // " << SM->getNumChildren() << " children in Scope";
402 OS << '\n';
404 ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
405 CurrentIdx + VBRSize, OS);
406 assert(ChildSize == SM->getChild(i)->getSize() &&
407 "Emitted child size does not match calculated size");
408 CurrentIdx += VBRSize + ChildSize;
411 // Emit a zero as a sentinel indicating end of 'Scope'.
412 if (!OmitComments)
413 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
414 OS.indent(Indent) << "0, ";
415 if (!OmitComments)
416 OS << "/*End of Scope*/";
417 OS << '\n';
418 return CurrentIdx - StartIdx + 1;
421 case Matcher::RecordNode:
422 OS << "OPC_RecordNode,";
423 if (!OmitComments)
424 OS << " // #"
425 << cast<RecordMatcher>(N)->getResultNo() << " = "
426 << cast<RecordMatcher>(N)->getWhatFor();
427 OS << '\n';
428 return 1;
430 case Matcher::RecordChild:
431 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
432 << ',';
433 if (!OmitComments)
434 OS << " // #"
435 << cast<RecordChildMatcher>(N)->getResultNo() << " = "
436 << cast<RecordChildMatcher>(N)->getWhatFor();
437 OS << '\n';
438 return 1;
440 case Matcher::RecordMemRef:
441 OS << "OPC_RecordMemRef,\n";
442 return 1;
444 case Matcher::CaptureGlueInput:
445 OS << "OPC_CaptureGlueInput,\n";
446 return 1;
448 case Matcher::MoveChild: {
449 const auto *MCM = cast<MoveChildMatcher>(N);
451 OS << "OPC_MoveChild";
452 // Handle the specialized forms.
453 if (MCM->getChildNo() >= 8)
454 OS << ", ";
455 OS << MCM->getChildNo() << ",\n";
456 return (MCM->getChildNo() >= 8) ? 2 : 1;
459 case Matcher::MoveSibling: {
460 const auto *MSM = cast<MoveSiblingMatcher>(N);
462 OS << "OPC_MoveSibling";
463 // Handle the specialized forms.
464 if (MSM->getSiblingNo() >= 8)
465 OS << ", ";
466 OS << MSM->getSiblingNo() << ",\n";
467 return (MSM->getSiblingNo() >= 8) ? 2 : 1;
470 case Matcher::MoveParent:
471 OS << "OPC_MoveParent,\n";
472 return 1;
474 case Matcher::CheckSame:
475 OS << "OPC_CheckSame, "
476 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
477 return 2;
479 case Matcher::CheckChildSame:
480 OS << "OPC_CheckChild"
481 << cast<CheckChildSameMatcher>(N)->getChildNo() << "Same, "
482 << cast<CheckChildSameMatcher>(N)->getMatchNumber() << ",\n";
483 return 2;
485 case Matcher::CheckPatternPredicate: {
486 StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
487 unsigned PredNo = getPatternPredicate(Pred);
488 if (PredNo > 255)
489 OS << "OPC_CheckPatternPredicate2, TARGET_VAL(" << PredNo << "),";
490 else
491 OS << "OPC_CheckPatternPredicate, " << PredNo << ',';
492 if (!OmitComments)
493 OS << " // " << Pred;
494 OS << '\n';
495 return 2 + (PredNo > 255);
497 case Matcher::CheckPredicate: {
498 TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate();
499 unsigned OperandBytes = 0;
501 if (Pred.usesOperands()) {
502 unsigned NumOps = cast<CheckPredicateMatcher>(N)->getNumOperands();
503 OS << "OPC_CheckPredicateWithOperands, " << NumOps << "/*#Ops*/, ";
504 for (unsigned i = 0; i < NumOps; ++i)
505 OS << cast<CheckPredicateMatcher>(N)->getOperandNo(i) << ", ";
506 OperandBytes = 1 + NumOps;
507 } else {
508 OS << "OPC_CheckPredicate, ";
511 OS << getNodePredicate(Pred) << ',';
512 if (!OmitComments)
513 OS << " // " << Pred.getFnName();
514 OS << '\n';
515 return 2 + OperandBytes;
518 case Matcher::CheckOpcode:
519 OS << "OPC_CheckOpcode, TARGET_VAL("
520 << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << "),\n";
521 return 3;
523 case Matcher::SwitchOpcode:
524 case Matcher::SwitchType: {
525 unsigned StartIdx = CurrentIdx;
527 unsigned NumCases;
528 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
529 OS << "OPC_SwitchOpcode ";
530 NumCases = SOM->getNumCases();
531 } else {
532 OS << "OPC_SwitchType ";
533 NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
536 if (!OmitComments)
537 OS << "/*" << NumCases << " cases */";
538 OS << ", ";
539 ++CurrentIdx;
541 // For each case we emit the size, then the opcode, then the matcher.
542 for (unsigned i = 0, e = NumCases; i != e; ++i) {
543 const Matcher *Child;
544 unsigned IdxSize;
545 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
546 Child = SOM->getCaseMatcher(i);
547 IdxSize = 2; // size of opcode in table is 2 bytes.
548 } else {
549 Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
550 IdxSize = 1; // size of type in table is 1 byte.
553 if (i != 0) {
554 if (!OmitComments)
555 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
556 OS.indent(Indent);
557 if (!OmitComments)
558 OS << (isa<SwitchOpcodeMatcher>(N) ?
559 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
562 unsigned ChildSize = Child->getSize();
563 CurrentIdx += EmitVBRValue(ChildSize, OS) + IdxSize;
564 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
565 OS << "TARGET_VAL(" << SOM->getCaseOpcode(i).getEnumName() << "),";
566 else
567 OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i)) << ',';
568 if (!OmitComments)
569 OS << "// ->" << CurrentIdx + ChildSize;
570 OS << '\n';
572 ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx, OS);
573 assert(ChildSize == Child->getSize() &&
574 "Emitted child size does not match calculated size");
575 CurrentIdx += ChildSize;
578 // Emit the final zero to terminate the switch.
579 if (!OmitComments)
580 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
581 OS.indent(Indent) << "0,";
582 if (!OmitComments)
583 OS << (isa<SwitchOpcodeMatcher>(N) ?
584 " // EndSwitchOpcode" : " // EndSwitchType");
586 OS << '\n';
587 return CurrentIdx - StartIdx + 1;
590 case Matcher::CheckType:
591 if (cast<CheckTypeMatcher>(N)->getResNo() == 0) {
592 MVT::SimpleValueType VT = cast<CheckTypeMatcher>(N)->getType();
593 switch (VT) {
594 case MVT::i32:
595 case MVT::i64:
596 OS << "OPC_CheckTypeI" << MVT(VT).getSizeInBits() << ",\n";
597 return 1;
598 default:
599 OS << "OPC_CheckType, " << getEnumName(VT) << ",\n";
600 return 2;
603 OS << "OPC_CheckTypeRes, " << cast<CheckTypeMatcher>(N)->getResNo() << ", "
604 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
605 return 3;
607 case Matcher::CheckChildType: {
608 MVT::SimpleValueType VT = cast<CheckChildTypeMatcher>(N)->getType();
609 switch (VT) {
610 case MVT::i32:
611 case MVT::i64:
612 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(N)->getChildNo()
613 << "TypeI" << MVT(VT).getSizeInBits() << ",\n";
614 return 1;
615 default:
616 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(N)->getChildNo()
617 << "Type, " << getEnumName(VT) << ",\n";
618 return 2;
622 case Matcher::CheckInteger: {
623 OS << "OPC_CheckInteger, ";
624 unsigned Bytes =
625 1 + EmitSignedVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
626 OS << '\n';
627 return Bytes;
629 case Matcher::CheckChildInteger: {
630 OS << "OPC_CheckChild" << cast<CheckChildIntegerMatcher>(N)->getChildNo()
631 << "Integer, ";
632 unsigned Bytes = 1 + EmitSignedVBRValue(
633 cast<CheckChildIntegerMatcher>(N)->getValue(), OS);
634 OS << '\n';
635 return Bytes;
637 case Matcher::CheckCondCode:
638 OS << "OPC_CheckCondCode, ISD::"
639 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
640 return 2;
642 case Matcher::CheckChild2CondCode:
643 OS << "OPC_CheckChild2CondCode, ISD::"
644 << cast<CheckChild2CondCodeMatcher>(N)->getCondCodeName() << ",\n";
645 return 2;
647 case Matcher::CheckValueType:
648 OS << "OPC_CheckValueType, MVT::"
649 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
650 return 2;
652 case Matcher::CheckComplexPat: {
653 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
654 const ComplexPattern &Pattern = CCPM->getPattern();
655 OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
656 << CCPM->getMatchNumber() << ',';
658 if (!OmitComments) {
659 OS << " // " << Pattern.getSelectFunc();
660 OS << ":$" << CCPM->getName();
661 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
662 OS << " #" << CCPM->getFirstResult()+i;
664 if (Pattern.hasProperty(SDNPHasChain))
665 OS << " + chain result";
667 OS << '\n';
668 return 3;
671 case Matcher::CheckAndImm: {
672 OS << "OPC_CheckAndImm, ";
673 unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
674 OS << '\n';
675 return Bytes;
678 case Matcher::CheckOrImm: {
679 OS << "OPC_CheckOrImm, ";
680 unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
681 OS << '\n';
682 return Bytes;
685 case Matcher::CheckFoldableChainNode:
686 OS << "OPC_CheckFoldableChainNode,\n";
687 return 1;
689 case Matcher::CheckImmAllOnesV:
690 OS << "OPC_CheckImmAllOnesV,\n";
691 return 1;
693 case Matcher::CheckImmAllZerosV:
694 OS << "OPC_CheckImmAllZerosV,\n";
695 return 1;
697 case Matcher::EmitInteger: {
698 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
699 MVT::SimpleValueType VT = cast<EmitIntegerMatcher>(N)->getVT();
700 unsigned OpBytes;
701 switch (VT) {
702 case MVT::i8:
703 case MVT::i16:
704 case MVT::i32:
705 case MVT::i64:
706 OpBytes = 1;
707 OS << "OPC_EmitInteger" << MVT(VT).getSizeInBits() << ", ";
708 break;
709 default:
710 OpBytes = 2;
711 OS << "OPC_EmitInteger, " << getEnumName(VT) << ", ";
712 break;
714 unsigned Bytes = OpBytes + EmitSignedVBRValue(Val, OS);
715 OS << '\n';
716 return Bytes;
718 case Matcher::EmitStringInteger: {
719 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
720 MVT::SimpleValueType VT = cast<EmitStringIntegerMatcher>(N)->getVT();
721 // These should always fit into 7 bits.
722 unsigned OpBytes;
723 switch (VT) {
724 case MVT::i32:
725 OpBytes = 1;
726 OS << "OPC_EmitStringInteger" << MVT(VT).getSizeInBits() << ", ";
727 break;
728 default:
729 OpBytes = 2;
730 OS << "OPC_EmitStringInteger, " << getEnumName(VT) << ", ";
731 break;
733 OS << Val << ",\n";
734 return OpBytes + 1;
737 case Matcher::EmitRegister: {
738 const EmitRegisterMatcher *Matcher = cast<EmitRegisterMatcher>(N);
739 const CodeGenRegister *Reg = Matcher->getReg();
740 // If the enum value of the register is larger than one byte can handle,
741 // use EmitRegister2.
742 if (Reg && Reg->EnumValue > 255) {
743 OS << "OPC_EmitRegister2, " << getEnumName(Matcher->getVT()) << ", ";
744 OS << "TARGET_VAL(" << getQualifiedName(Reg->TheDef) << "),\n";
745 return 4;
746 } else {
747 OS << "OPC_EmitRegister, " << getEnumName(Matcher->getVT()) << ", ";
748 if (Reg) {
749 OS << getQualifiedName(Reg->TheDef) << ",\n";
750 } else {
751 OS << "0 ";
752 if (!OmitComments)
753 OS << "/*zero_reg*/";
754 OS << ",\n";
756 return 3;
760 case Matcher::EmitConvertToTarget: {
761 unsigned Slot = cast<EmitConvertToTargetMatcher>(N)->getSlot();
762 if (Slot < 8) {
763 OS << "OPC_EmitConvertToTarget" << Slot << ",\n";
764 return 1;
766 OS << "OPC_EmitConvertToTarget, " << Slot << ",\n";
767 return 2;
770 case Matcher::EmitMergeInputChains: {
771 const EmitMergeInputChainsMatcher *MN =
772 cast<EmitMergeInputChainsMatcher>(N);
774 // Handle the specialized forms OPC_EmitMergeInputChains1_0, 1_1, and 1_2.
775 if (MN->getNumNodes() == 1 && MN->getNode(0) < 3) {
776 OS << "OPC_EmitMergeInputChains1_" << MN->getNode(0) << ",\n";
777 return 1;
780 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
781 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
782 OS << MN->getNode(i) << ", ";
783 OS << '\n';
784 return 2+MN->getNumNodes();
786 case Matcher::EmitCopyToReg: {
787 const auto *C2RMatcher = cast<EmitCopyToRegMatcher>(N);
788 int Bytes = 3;
789 const CodeGenRegister *Reg = C2RMatcher->getDestPhysReg();
790 unsigned Slot = C2RMatcher->getSrcSlot();
791 if (Reg->EnumValue > 255) {
792 assert(isUInt<16>(Reg->EnumValue) && "not handled");
793 OS << "OPC_EmitCopyToRegTwoByte, " << Slot << ", "
794 << "TARGET_VAL(" << getQualifiedName(Reg->TheDef) << "),\n";
795 ++Bytes;
796 } else {
797 if (Slot < 8) {
798 OS << "OPC_EmitCopyToReg" << Slot << ", "
799 << getQualifiedName(Reg->TheDef) << ",\n";
800 --Bytes;
801 } else
802 OS << "OPC_EmitCopyToReg, " << Slot << ", "
803 << getQualifiedName(Reg->TheDef) << ",\n";
806 return Bytes;
808 case Matcher::EmitNodeXForm: {
809 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
810 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
811 << XF->getSlot() << ',';
812 if (!OmitComments)
813 OS << " // "<<XF->getNodeXForm()->getName();
814 OS <<'\n';
815 return 3;
818 case Matcher::EmitNode:
819 case Matcher::MorphNodeTo: {
820 auto NumCoveredBytes = 0;
821 if (InstrumentCoverage) {
822 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
823 NumCoveredBytes = 3;
824 OS << "OPC_Coverage, ";
825 std::string src =
826 GetPatFromTreePatternNode(SNT->getPattern().getSrcPattern());
827 std::string dst =
828 GetPatFromTreePatternNode(SNT->getPattern().getDstPattern());
829 Record *PatRecord = SNT->getPattern().getSrcRecord();
830 std::string include_src = getIncludePath(PatRecord);
831 unsigned Offset =
832 getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
833 OS << "TARGET_VAL(" << Offset << "),\n";
834 OS.indent(FullIndexWidth + Indent);
837 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
838 bool IsEmitNode = isa<EmitNodeMatcher>(EN);
839 OS << (IsEmitNode ? "OPC_EmitNode" : "OPC_MorphNodeTo");
840 bool CompressVTs = EN->getNumVTs() < 3;
841 bool CompressNodeInfo = false;
842 if (CompressVTs) {
843 OS << EN->getNumVTs();
844 if (!EN->hasChain() && !EN->hasInGlue() && !EN->hasOutGlue() &&
845 !EN->hasMemRefs() && EN->getNumFixedArityOperands() == -1) {
846 CompressNodeInfo = true;
847 OS << "None";
848 } else if (EN->hasChain() && !EN->hasInGlue() && !EN->hasOutGlue() &&
849 !EN->hasMemRefs() && EN->getNumFixedArityOperands() == -1) {
850 CompressNodeInfo = true;
851 OS << "Chain";
852 } else if (!IsEmitNode && !EN->hasChain() && EN->hasInGlue() &&
853 !EN->hasOutGlue() && !EN->hasMemRefs() &&
854 EN->getNumFixedArityOperands() == -1) {
855 CompressNodeInfo = true;
856 OS << "GlueInput";
857 } else if (!IsEmitNode && !EN->hasChain() && !EN->hasInGlue() &&
858 EN->hasOutGlue() && !EN->hasMemRefs() &&
859 EN->getNumFixedArityOperands() == -1) {
860 CompressNodeInfo = true;
861 OS << "GlueOutput";
865 const CodeGenInstruction &CGI = EN->getInstruction();
866 OS << ", TARGET_VAL(" << CGI.Namespace << "::" << CGI.TheDef->getName()
867 << ")";
869 if (!CompressNodeInfo) {
870 OS << ", 0";
871 if (EN->hasChain())
872 OS << "|OPFL_Chain";
873 if (EN->hasInGlue())
874 OS << "|OPFL_GlueInput";
875 if (EN->hasOutGlue())
876 OS << "|OPFL_GlueOutput";
877 if (EN->hasMemRefs())
878 OS << "|OPFL_MemRefs";
879 if (EN->getNumFixedArityOperands() != -1)
880 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
882 OS << ",\n";
884 OS.indent(FullIndexWidth + Indent+4);
885 if (!CompressVTs) {
886 OS << EN->getNumVTs();
887 if (!OmitComments)
888 OS << "/*#VTs*/";
889 OS << ", ";
891 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
892 OS << getEnumName(EN->getVT(i)) << ", ";
894 OS << EN->getNumOperands();
895 if (!OmitComments)
896 OS << "/*#Ops*/";
897 OS << ", ";
898 unsigned NumOperandBytes = 0;
899 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
900 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
902 if (!OmitComments) {
903 // Print the result #'s for EmitNode.
904 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
905 if (unsigned NumResults = EN->getNumVTs()) {
906 OS << " // Results =";
907 unsigned First = E->getFirstResultSlot();
908 for (unsigned i = 0; i != NumResults; ++i)
909 OS << " #" << First+i;
912 OS << '\n';
914 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
915 OS.indent(FullIndexWidth + Indent) << "// Src: "
916 << *SNT->getPattern().getSrcPattern() << " - Complexity = "
917 << SNT->getPattern().getPatternComplexity(CGP) << '\n';
918 OS.indent(FullIndexWidth + Indent) << "// Dst: "
919 << *SNT->getPattern().getDstPattern() << '\n';
921 } else
922 OS << '\n';
924 return 4 + !CompressVTs + !CompressNodeInfo + EN->getNumVTs() +
925 NumOperandBytes + NumCoveredBytes;
927 case Matcher::CompleteMatch: {
928 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
929 auto NumCoveredBytes = 0;
930 if (InstrumentCoverage) {
931 NumCoveredBytes = 3;
932 OS << "OPC_Coverage, ";
933 std::string src =
934 GetPatFromTreePatternNode(CM->getPattern().getSrcPattern());
935 std::string dst =
936 GetPatFromTreePatternNode(CM->getPattern().getDstPattern());
937 Record *PatRecord = CM->getPattern().getSrcRecord();
938 std::string include_src = getIncludePath(PatRecord);
939 unsigned Offset =
940 getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
941 OS << "TARGET_VAL(" << Offset << "),\n";
942 OS.indent(FullIndexWidth + Indent);
944 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
945 unsigned NumResultBytes = 0;
946 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
947 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
948 OS << '\n';
949 if (!OmitComments) {
950 OS.indent(FullIndexWidth + Indent) << " // Src: "
951 << *CM->getPattern().getSrcPattern() << " - Complexity = "
952 << CM->getPattern().getPatternComplexity(CGP) << '\n';
953 OS.indent(FullIndexWidth + Indent) << " // Dst: "
954 << *CM->getPattern().getDstPattern();
956 OS << '\n';
957 return 2 + NumResultBytes + NumCoveredBytes;
960 llvm_unreachable("Unreachable");
963 /// This function traverses the matcher tree and emits all the nodes.
964 /// The nodes have already been sized.
965 unsigned MatcherTableEmitter::
966 EmitMatcherList(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
967 raw_ostream &OS) {
968 unsigned Size = 0;
969 while (N) {
970 if (!OmitComments)
971 OS << "/*" << format_decimal(CurrentIdx, IndexWidth) << "*/";
972 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
973 Size += MatcherSize;
974 CurrentIdx += MatcherSize;
976 // If there are other nodes in this list, iterate to them, otherwise we're
977 // done.
978 N = N->getNext();
980 return Size;
983 void MatcherTableEmitter::EmitNodePredicatesFunction(
984 const std::vector<TreePredicateFn> &Preds, StringRef Decl,
985 raw_ostream &OS) {
986 if (Preds.empty())
987 return;
989 BeginEmitFunction(OS, "bool", Decl, true/*AddOverride*/);
990 OS << "{\n";
991 OS << " switch (PredNo) {\n";
992 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
993 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
994 // Emit the predicate code corresponding to this pattern.
995 const TreePredicateFn PredFn = Preds[i];
996 assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
997 std::string PredFnCodeStr = PredFn.getCodeToRunOnSDNode();
999 OS << " case " << i << ": {\n";
1000 for (auto *SimilarPred : NodePredicatesByCodeToRun[PredFnCodeStr])
1001 OS << " // " << TreePredicateFn(SimilarPred).getFnName() << '\n';
1002 OS << PredFnCodeStr << "\n }\n";
1004 OS << " }\n";
1005 OS << "}\n";
1006 EndEmitFunction(OS);
1009 void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
1010 // Emit pattern predicates.
1011 if (!PatternPredicates.empty()) {
1012 BeginEmitFunction(OS, "bool",
1013 "CheckPatternPredicate(unsigned PredNo) const", true/*AddOverride*/);
1014 OS << "{\n";
1015 OS << " switch (PredNo) {\n";
1016 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
1017 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
1018 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
1019 OS << " }\n";
1020 OS << "}\n";
1021 EndEmitFunction(OS);
1024 // Emit Node predicates.
1025 EmitNodePredicatesFunction(
1026 NodePredicates, "CheckNodePredicate(SDNode *Node, unsigned PredNo) const",
1027 OS);
1028 EmitNodePredicatesFunction(
1029 NodePredicatesWithOperands,
1030 "CheckNodePredicateWithOperands(SDNode *Node, unsigned PredNo, "
1031 "const SmallVectorImpl<SDValue> &Operands) const",
1032 OS);
1034 // Emit CompletePattern matchers.
1035 // FIXME: This should be const.
1036 if (!ComplexPatterns.empty()) {
1037 BeginEmitFunction(OS, "bool",
1038 "CheckComplexPattern(SDNode *Root, SDNode *Parent,\n"
1039 " SDValue N, unsigned PatternNo,\n"
1040 " SmallVectorImpl<std::pair<SDValue, SDNode *>> &Result)",
1041 true/*AddOverride*/);
1042 OS << "{\n";
1043 OS << " unsigned NextRes = Result.size();\n";
1044 OS << " switch (PatternNo) {\n";
1045 OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
1046 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
1047 const ComplexPattern &P = *ComplexPatterns[i];
1048 unsigned NumOps = P.getNumOperands();
1050 if (P.hasProperty(SDNPHasChain))
1051 ++NumOps; // Get the chained node too.
1053 OS << " case " << i << ":\n";
1054 if (InstrumentCoverage)
1055 OS << " {\n";
1056 OS << " Result.resize(NextRes+" << NumOps << ");\n";
1057 if (InstrumentCoverage)
1058 OS << " bool Succeeded = " << P.getSelectFunc();
1059 else
1060 OS << " return " << P.getSelectFunc();
1062 OS << "(";
1063 // If the complex pattern wants the root of the match, pass it in as the
1064 // first argument.
1065 if (P.hasProperty(SDNPWantRoot))
1066 OS << "Root, ";
1068 // If the complex pattern wants the parent of the operand being matched,
1069 // pass it in as the next argument.
1070 if (P.hasProperty(SDNPWantParent))
1071 OS << "Parent, ";
1073 OS << "N";
1074 for (unsigned i = 0; i != NumOps; ++i)
1075 OS << ", Result[NextRes+" << i << "].first";
1076 OS << ");\n";
1077 if (InstrumentCoverage) {
1078 OS << " if (Succeeded)\n";
1079 OS << " dbgs() << \"\\nCOMPLEX_PATTERN: " << P.getSelectFunc()
1080 << "\\n\" ;\n";
1081 OS << " return Succeeded;\n";
1082 OS << " }\n";
1085 OS << " }\n";
1086 OS << "}\n";
1087 EndEmitFunction(OS);
1091 // Emit SDNodeXForm handlers.
1092 // FIXME: This should be const.
1093 if (!NodeXForms.empty()) {
1094 BeginEmitFunction(OS, "SDValue",
1095 "RunSDNodeXForm(SDValue V, unsigned XFormNo)", true/*AddOverride*/);
1096 OS << "{\n";
1097 OS << " switch (XFormNo) {\n";
1098 OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
1100 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
1101 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
1102 const CodeGenDAGPatterns::NodeXForm &Entry =
1103 CGP.getSDNodeTransform(NodeXForms[i]);
1105 Record *SDNode = Entry.first;
1106 const std::string &Code = Entry.second;
1108 OS << " case " << i << ": { ";
1109 if (!OmitComments)
1110 OS << "// " << NodeXForms[i]->getName();
1111 OS << '\n';
1113 std::string ClassName =
1114 std::string(CGP.getSDNodeInfo(SDNode).getSDClassName());
1115 if (ClassName == "SDNode")
1116 OS << " SDNode *N = V.getNode();\n";
1117 else
1118 OS << " " << ClassName << " *N = cast<" << ClassName
1119 << ">(V.getNode());\n";
1120 OS << Code << "\n }\n";
1122 OS << " }\n";
1123 OS << "}\n";
1124 EndEmitFunction(OS);
1128 static StringRef getOpcodeString(Matcher::KindTy Kind) {
1129 switch (Kind) {
1130 case Matcher::Scope:
1131 return "OPC_Scope";
1132 case Matcher::RecordNode:
1133 return "OPC_RecordNode";
1134 case Matcher::RecordChild:
1135 return "OPC_RecordChild";
1136 case Matcher::RecordMemRef:
1137 return "OPC_RecordMemRef";
1138 case Matcher::CaptureGlueInput:
1139 return "OPC_CaptureGlueInput";
1140 case Matcher::MoveChild:
1141 return "OPC_MoveChild";
1142 case Matcher::MoveSibling:
1143 return "OPC_MoveSibling";
1144 case Matcher::MoveParent:
1145 return "OPC_MoveParent";
1146 case Matcher::CheckSame:
1147 return "OPC_CheckSame";
1148 case Matcher::CheckChildSame:
1149 return "OPC_CheckChildSame";
1150 case Matcher::CheckPatternPredicate:
1151 return "OPC_CheckPatternPredicate";
1152 case Matcher::CheckPredicate:
1153 return "OPC_CheckPredicate";
1154 case Matcher::CheckOpcode:
1155 return "OPC_CheckOpcode";
1156 case Matcher::SwitchOpcode:
1157 return "OPC_SwitchOpcode";
1158 case Matcher::CheckType:
1159 return "OPC_CheckType";
1160 case Matcher::SwitchType:
1161 return "OPC_SwitchType";
1162 case Matcher::CheckChildType:
1163 return "OPC_CheckChildType";
1164 case Matcher::CheckInteger:
1165 return "OPC_CheckInteger";
1166 case Matcher::CheckChildInteger:
1167 return "OPC_CheckChildInteger";
1168 case Matcher::CheckCondCode:
1169 return "OPC_CheckCondCode";
1170 case Matcher::CheckChild2CondCode:
1171 return "OPC_CheckChild2CondCode";
1172 case Matcher::CheckValueType:
1173 return "OPC_CheckValueType";
1174 case Matcher::CheckComplexPat:
1175 return "OPC_CheckComplexPat";
1176 case Matcher::CheckAndImm:
1177 return "OPC_CheckAndImm";
1178 case Matcher::CheckOrImm:
1179 return "OPC_CheckOrImm";
1180 case Matcher::CheckFoldableChainNode:
1181 return "OPC_CheckFoldableChainNode";
1182 case Matcher::CheckImmAllOnesV:
1183 return "OPC_CheckImmAllOnesV";
1184 case Matcher::CheckImmAllZerosV:
1185 return "OPC_CheckImmAllZerosV";
1186 case Matcher::EmitInteger:
1187 return "OPC_EmitInteger";
1188 case Matcher::EmitStringInteger:
1189 return "OPC_EmitStringInteger";
1190 case Matcher::EmitRegister:
1191 return "OPC_EmitRegister";
1192 case Matcher::EmitConvertToTarget:
1193 return "OPC_EmitConvertToTarget";
1194 case Matcher::EmitMergeInputChains:
1195 return "OPC_EmitMergeInputChains";
1196 case Matcher::EmitCopyToReg:
1197 return "OPC_EmitCopyToReg";
1198 case Matcher::EmitNode:
1199 return "OPC_EmitNode";
1200 case Matcher::MorphNodeTo:
1201 return "OPC_MorphNodeTo";
1202 case Matcher::EmitNodeXForm:
1203 return "OPC_EmitNodeXForm";
1204 case Matcher::CompleteMatch:
1205 return "OPC_CompleteMatch";
1208 llvm_unreachable("Unhandled opcode?");
1211 void MatcherTableEmitter::EmitHistogram(const Matcher *M,
1212 raw_ostream &OS) {
1213 if (OmitComments)
1214 return;
1216 OS << " // Opcode Histogram:\n";
1217 for (unsigned i = 0, e = OpcodeCounts.size(); i != e; ++i) {
1218 OS << " // #"
1219 << left_justify(getOpcodeString((Matcher::KindTy)i), HistOpcWidth)
1220 << " = " << OpcodeCounts[i] << '\n';
1222 OS << '\n';
1226 void llvm::EmitMatcherTable(Matcher *TheMatcher,
1227 const CodeGenDAGPatterns &CGP,
1228 raw_ostream &OS) {
1229 OS << "#if defined(GET_DAGISEL_DECL) && defined(GET_DAGISEL_BODY)\n";
1230 OS << "#error GET_DAGISEL_DECL and GET_DAGISEL_BODY cannot be both defined, ";
1231 OS << "undef both for inline definitions\n";
1232 OS << "#endif\n\n";
1234 // Emit a check for omitted class name.
1235 OS << "#ifdef GET_DAGISEL_BODY\n";
1236 OS << "#define LOCAL_DAGISEL_STRINGIZE(X) LOCAL_DAGISEL_STRINGIZE_(X)\n";
1237 OS << "#define LOCAL_DAGISEL_STRINGIZE_(X) #X\n";
1238 OS << "static_assert(sizeof(LOCAL_DAGISEL_STRINGIZE(GET_DAGISEL_BODY)) > 1,"
1239 "\n";
1240 OS << " \"GET_DAGISEL_BODY is empty: it should be defined with the class "
1241 "name\");\n";
1242 OS << "#undef LOCAL_DAGISEL_STRINGIZE_\n";
1243 OS << "#undef LOCAL_DAGISEL_STRINGIZE\n";
1244 OS << "#endif\n\n";
1246 OS << "#if !defined(GET_DAGISEL_DECL) && !defined(GET_DAGISEL_BODY)\n";
1247 OS << "#define DAGISEL_INLINE 1\n";
1248 OS << "#else\n";
1249 OS << "#define DAGISEL_INLINE 0\n";
1250 OS << "#endif\n\n";
1252 OS << "#if !DAGISEL_INLINE\n";
1253 OS << "#define DAGISEL_CLASS_COLONCOLON GET_DAGISEL_BODY ::\n";
1254 OS << "#else\n";
1255 OS << "#define DAGISEL_CLASS_COLONCOLON\n";
1256 OS << "#endif\n\n";
1258 BeginEmitFunction(OS, "void", "SelectCode(SDNode *N)", false/*AddOverride*/);
1259 MatcherTableEmitter MatcherEmitter(CGP);
1261 // First we size all the children of the three kinds of matchers that have
1262 // them. This is done by sharing the code in EmitMatcher(). but we don't
1263 // want to emit anything, so we turn off comments and use a null stream.
1264 bool SaveOmitComments = OmitComments;
1265 OmitComments = true;
1266 raw_null_ostream NullOS;
1267 unsigned TotalSize = MatcherEmitter.SizeMatcherList(TheMatcher, NullOS);
1268 OmitComments = SaveOmitComments;
1270 // Now that the matchers are sized, we can emit the code for them to the
1271 // final stream.
1272 OS << "{\n";
1273 OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
1274 OS << " // this.\n";
1275 OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
1276 OS << " static const unsigned char MatcherTable[] = {\n";
1277 TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 1, 0, OS);
1278 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
1280 MatcherEmitter.EmitHistogram(TheMatcher, OS);
1282 OS << " #undef TARGET_VAL\n";
1283 OS << " SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n";
1284 OS << "}\n";
1285 EndEmitFunction(OS);
1287 // Next up, emit the function for node and pattern predicates:
1288 MatcherEmitter.EmitPredicateFunctions(OS);
1290 if (InstrumentCoverage)
1291 MatcherEmitter.EmitPatternMatchTable(OS);
1293 // Clean up the preprocessor macros.
1294 OS << "\n";
1295 OS << "#ifdef DAGISEL_INLINE\n";
1296 OS << "#undef DAGISEL_INLINE\n";
1297 OS << "#endif\n";
1298 OS << "#ifdef DAGISEL_CLASS_COLONCOLON\n";
1299 OS << "#undef DAGISEL_CLASS_COLONCOLON\n";
1300 OS << "#endif\n";
1301 OS << "#ifdef GET_DAGISEL_DECL\n";
1302 OS << "#undef GET_DAGISEL_DECL\n";
1303 OS << "#endif\n";
1304 OS << "#ifdef GET_DAGISEL_BODY\n";
1305 OS << "#undef GET_DAGISEL_BODY\n";
1306 OS << "#endif\n";