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.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Basic/SDNodeProperties.h"
14#include "Basic/SequenceToOffsetTable.h"
15#include "Common/CodeGenDAGPatterns.h"
16#include "Common/CodeGenInstruction.h"
17#include "Common/CodeGenRegisters.h"
18#include "Common/CodeGenTarget.h"
19#include "DAGISelMatcher.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/TinyPtrVector.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Format.h"
26#include "llvm/Support/LEB128.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/TableGen/Error.h"
29#include "llvm/TableGen/Record.h"
30
31using namespace llvm;
32
33enum {
34 IndexWidth = 7,
35 FullIndexWidth = IndexWidth + 4,
36 HistOpcWidth = 40,
37};
38
39static cl::OptionCategory DAGISelCat("Options for -gen-dag-isel");
40
41// To reduce generated source code size.
42static cl::opt<bool> OmitComments("omit-comments",
43 cl::desc("Do not generate comments"),
44 cl::init(Val: false), cl::cat(DAGISelCat));
45
46static cl::opt<bool> InstrumentCoverage(
47 "instrument-coverage",
48 cl::desc("Generates tables to help identify patterns matched"),
49 cl::init(Val: false), cl::cat(DAGISelCat));
50
51namespace {
52class MatcherTableEmitter {
53 const CodeGenDAGPatterns &CGP;
54
55 SmallVector<unsigned, Matcher::HighestKind + 1> OpcodeCounts;
56
57 std::vector<TreePattern *> NodePredicates;
58 std::vector<TreePattern *> NodePredicatesWithOperands;
59
60 // We de-duplicate the predicates by code string, and use this map to track
61 // all the patterns with "identical" predicates.
62 MapVector<std::string, TinyPtrVector<TreePattern *>, StringMap<unsigned>>
63 NodePredicatesByCodeToRun;
64
65 std::vector<std::string> PatternPredicates;
66
67 std::vector<const ComplexPattern *> ComplexPatterns;
68
69 DenseMap<const Record *, unsigned> NodeXFormMap;
70 std::vector<const Record *> NodeXForms;
71
72 std::vector<std::string> VecIncludeStrings;
73 MapVector<std::string, unsigned, StringMap<unsigned>> VecPatterns;
74
75 // Map from ValueTypeByHwMode to (Index, UsageCount) pair.
76 // Index is 1-based (0 means not yet assigned).
77 std::map<ValueTypeByHwMode, std::pair<unsigned, unsigned>> ValueTypeMap;
78
79 SequenceToOffsetTable<std::vector<uint8_t>> OperandTable;
80
81 unsigned getPatternIdxFromTable(std::string &&P, std::string &&include_loc) {
82 const auto [It, Inserted] =
83 VecPatterns.try_emplace(Key: std::move(P), Args: VecPatterns.size());
84 if (Inserted) {
85 VecIncludeStrings.push_back(x: std::move(include_loc));
86 return VecIncludeStrings.size() - 1;
87 }
88 return It->second;
89 }
90
91public:
92 MatcherTableEmitter(const MatcherList &TheMatcherList,
93 const CodeGenDAGPatterns &cgp)
94 : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1, 0),
95 OperandTable(std::nullopt) {
96 // Record the usage of ComplexPattern.
97 MapVector<const ComplexPattern *, unsigned> ComplexPatternUsage;
98 // Record the usage of PatternPredicate.
99 MapVector<StringRef, unsigned> PatternPredicateUsage;
100 // Record the usage of Predicate.
101 MapVector<TreePattern *, unsigned> PredicateUsage;
102
103 // Iterate the whole MatcherTable once and do some statistics.
104 std::function<void(const MatcherList &)> Statistic =
105 [&](const MatcherList &ML) {
106 for (const Matcher *N : ML) {
107 if (auto *SM = dyn_cast<ScopeMatcher>(Val: N))
108 for (unsigned I = 0; I < SM->getNumChildren(); I++)
109 Statistic(SM->getChild(i: I));
110 else if (auto *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N))
111 for (unsigned I = 0; I < SOM->getNumCases(); I++)
112 Statistic(SOM->getCaseMatcher(i: I));
113 else if (auto *STM = dyn_cast<SwitchTypeMatcher>(Val: N))
114 for (unsigned I = 0; I < STM->getNumCases(); I++)
115 Statistic(STM->getCaseMatcher(i: I));
116 else if (auto *SCIM = dyn_cast<SwitchChildIntegerMatcher>(Val: N))
117 for (unsigned I = 0; I < SCIM->getNumCases(); I++)
118 Statistic(SCIM->getCaseMatcher(i: I));
119 else if (auto *CPM = dyn_cast<CheckComplexPatMatcher>(Val: N))
120 ++ComplexPatternUsage[&CPM->getPattern()];
121 else if (auto *CPPM = dyn_cast<CheckPatternPredicateMatcher>(Val: N))
122 ++PatternPredicateUsage[CPPM->getPredicate()];
123 else if (auto *PM = dyn_cast<CheckPredicateMatcher>(Val: N))
124 ++PredicateUsage[PM->getPredicate().getOrigPatFragRecord()];
125
126 // Collect ValueTypeByHwMode usage for remapping.
127 if (auto *CTM = dyn_cast<CheckTypeMatcher>(Val: N)) {
128 if (!CTM->getType().isSimple())
129 getValueTypeID(VT: CTM->getType());
130 } else if (auto *CCTM = dyn_cast<CheckChildTypeMatcher>(Val: N)) {
131 if (!CCTM->getType().isSimple())
132 getValueTypeID(VT: CCTM->getType());
133 } else if (auto *EIM = dyn_cast<EmitIntegerMatcher>(Val: N)) {
134 if (!EIM->getVT().isSimple())
135 getValueTypeID(VT: EIM->getVT());
136 } else if (auto *ERM = dyn_cast<EmitRegisterMatcher>(Val: N)) {
137 if (!ERM->getVT().isSimple())
138 getValueTypeID(VT: ERM->getVT());
139 }
140
141 if (const auto *EN = dyn_cast<EmitNodeMatcherCommon>(Val: N)) {
142 ArrayRef<unsigned> Ops = EN->getOperandList();
143 std::vector<uint8_t> OpBytes;
144 for (unsigned Op : Ops) {
145 uint8_t Buffer[5];
146 unsigned Len = encodeULEB128(Value: Op, p: Buffer);
147 for (unsigned i = 0; i < Len; ++i)
148 OpBytes.push_back(x: Buffer[i]);
149 }
150 OperandTable.add(Seq: OpBytes);
151 }
152 }
153 };
154 Statistic(TheMatcherList);
155
156 sortValueTypeByHwModeByFrequency();
157
158 OperandTable.layout();
159
160 // Sort ComplexPatterns by usage.
161 std::vector<std::pair<const ComplexPattern *, unsigned>> ComplexPatternList(
162 ComplexPatternUsage.begin(), ComplexPatternUsage.end());
163 stable_sort(Range&: ComplexPatternList, C: [](const auto &A, const auto &B) {
164 return A.second > B.second;
165 });
166 for (const auto &ComplexPattern : ComplexPatternList)
167 ComplexPatterns.push_back(x: ComplexPattern.first);
168
169 // Sort PatternPredicates by usage.
170 std::vector<std::pair<std::string, unsigned>> PatternPredicateList(
171 PatternPredicateUsage.begin(), PatternPredicateUsage.end());
172 stable_sort(Range&: PatternPredicateList, C: [](const auto &A, const auto &B) {
173 return A.second > B.second;
174 });
175 for (const auto &PatternPredicate : PatternPredicateList)
176 PatternPredicates.push_back(x: PatternPredicate.first);
177
178 // Sort Predicates by usage.
179 // Merge predicates with same code.
180 for (const auto &Usage : PredicateUsage) {
181 TreePattern *TP = Usage.first;
182 TreePredicateFn Pred(TP);
183 NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode()].push_back(NewVal: TP);
184 }
185
186 std::vector<std::pair<TreePattern *, unsigned>> PredicateList;
187 // Sum the usage.
188 for (auto &Predicate : NodePredicatesByCodeToRun) {
189 TinyPtrVector<TreePattern *> &TPs = Predicate.second;
190 stable_sort(Range&: TPs, C: [](const auto *A, const auto *B) {
191 return A->getRecord()->getName() < B->getRecord()->getName();
192 });
193 unsigned Uses = 0;
194 for (TreePattern *TP : TPs)
195 Uses += PredicateUsage[TP];
196
197 // We only add the first predicate here since they are with the same code.
198 PredicateList.emplace_back(args: TPs[0], args&: Uses);
199 }
200
201 stable_sort(Range&: PredicateList, C: [](const auto &A, const auto &B) {
202 return A.second > B.second;
203 });
204 for (const auto &Predicate : PredicateList) {
205 TreePattern *TP = Predicate.first;
206 if (TreePredicateFn(TP).usesOperands())
207 NodePredicatesWithOperands.push_back(x: TP);
208 else
209 NodePredicates.push_back(x: TP);
210 }
211 }
212
213 unsigned EmitMatcherList(const MatcherList &ML, const unsigned Indent,
214 unsigned StartIdx, raw_ostream &OS);
215
216 void EmitOperandLists(raw_ostream &OS);
217
218 unsigned SizeMatcherList(MatcherList &ML, raw_ostream &OS);
219
220 void EmitPredicateFunctions(raw_ostream &OS);
221
222 void EmitValueTypeFunction(raw_ostream &OS);
223
224 void EmitHistogram(raw_ostream &OS);
225
226 void EmitPatternMatchTable(raw_ostream &OS);
227
228private:
229 // Reorder ValueType indices by usage frequency (most common -> index 0).
230 // Updates the indices directly in ValueTypeMap.
231 void sortValueTypeByHwModeByFrequency() {
232 if (ValueTypeMap.empty())
233 return;
234
235 // Collect pointers to map entries with their counts for sorting.
236 using EntryPtr = std::pair<unsigned, unsigned> *;
237 std::vector<EntryPtr> Entries;
238 for (auto &[VT, IdxAndCount] : ValueTypeMap)
239 Entries.push_back(x: &IdxAndCount);
240
241 // Sort by count descending.
242 llvm::sort(C&: Entries,
243 Comp: [](EntryPtr A, EntryPtr B) { return A->second > B->second; });
244
245 // Assign new indices (1-based) in frequency order.
246 for (unsigned NewIdx = 0; NewIdx < Entries.size(); ++NewIdx)
247 Entries[NewIdx]->first = NewIdx + 1;
248 }
249 void EmitNodePredicatesFunction(const std::vector<TreePattern *> &Preds,
250 StringRef Decl, raw_ostream &OS);
251
252 unsigned SizeMatcher(Matcher *N, raw_ostream &OS);
253
254 unsigned EmitMatcher(const Matcher *N, const unsigned Indent,
255 unsigned CurrentIdx, raw_ostream &OS);
256
257 unsigned getNodePredicate(TreePredicateFn Pred) {
258 // We use the first predicate.
259 TreePattern *PredPat =
260 NodePredicatesByCodeToRun[Pred.getCodeToRunOnSDNode()][0];
261 return Pred.usesOperands()
262 ? llvm::find(Range&: NodePredicatesWithOperands, Val: PredPat) -
263 NodePredicatesWithOperands.begin()
264 : llvm::find(Range&: NodePredicates, Val: PredPat) - NodePredicates.begin();
265 }
266
267 unsigned getPatternPredicate(StringRef PredName) {
268 return llvm::find(Range&: PatternPredicates, Val: PredName) - PatternPredicates.begin();
269 }
270 unsigned getComplexPat(const ComplexPattern &P) {
271 return llvm::find(Range&: ComplexPatterns, Val: &P) - ComplexPatterns.begin();
272 }
273
274 unsigned getNodeXFormID(const Record *Rec) {
275 unsigned &Entry = NodeXFormMap[Rec];
276 if (Entry == 0) {
277 NodeXForms.push_back(x: Rec);
278 Entry = NodeXForms.size();
279 }
280 return Entry - 1;
281 }
282
283 unsigned getValueTypeID(const ValueTypeByHwMode &VT) {
284 auto &[Idx, Count] = ValueTypeMap[VT];
285 if (Idx == 0) {
286 Idx = ValueTypeMap.size();
287 if (Idx > 256)
288 report_fatal_error(
289 reason: "More ValueType by HwMode than fit in a 8-bit index");
290 }
291 ++Count;
292 return Idx - 1;
293 }
294
295 unsigned emitValueTypeByHwMode(const ValueTypeByHwMode &VTBH, unsigned Index,
296 raw_ostream &OS);
297};
298} // end anonymous namespace.
299
300static std::string GetPatFromTreePatternNode(const TreePatternNode &N) {
301 std::string str;
302 raw_string_ostream Stream(str);
303 Stream << N;
304 return str;
305}
306
307static unsigned GetVBRSize(unsigned Val) {
308 if (Val <= 127)
309 return 1;
310
311 unsigned NumBytes = 0;
312 while (Val >= 128) {
313 Val >>= 7;
314 ++NumBytes;
315 }
316 return NumBytes + 1;
317}
318
319/// EmitVBRValue - Emit the specified value as a VBR, returning the number of
320/// bytes emitted.
321static unsigned EmitVBRValue(uint64_t Val, raw_ostream &OS) {
322 if (Val <= 127) {
323 OS << Val << ',';
324 return 1;
325 }
326
327 uint64_t InVal = Val;
328 unsigned NumBytes = 0;
329 while (Val >= 128) {
330 OS << (Val & 127) << "|128,";
331 Val >>= 7;
332 ++NumBytes;
333 }
334 OS << Val;
335 if (!OmitComments)
336 OS << "/*" << InVal << "*/";
337 OS << ',';
338 return NumBytes + 1;
339}
340
341/// Emit the specified signed value as a VBR. To improve compression we encode
342/// positive numbers shifted left by 1 and negative numbers negated and shifted
343/// left by 1 with bit 0 set.
344static unsigned EmitSignedVBRValue(int64_t Val, raw_ostream &OS) {
345 uint8_t Buffer[10];
346 unsigned Len = encodeSLEB128(Value: Val, p: Buffer);
347
348 for (unsigned i = 0; i != Len - 1; ++i)
349 OS << static_cast<unsigned>(Buffer[i] & 127) << "|128,";
350
351 OS << static_cast<unsigned>(Buffer[Len - 1]);
352 if ((Len > 1 || Val < 0) && !OmitComments)
353 OS << "/*" << Val << "*/";
354 OS << ',';
355 return Len;
356}
357
358// This is expensive and slow.
359static std::string getIncludePath(const Record *R) {
360 std::string str;
361 raw_string_ostream Stream(str);
362 auto Locs = R->getLoc();
363 SMLoc L;
364 if (Locs.size() > 1) {
365 // Get where the pattern prototype was instantiated
366 L = Locs[1];
367 } else if (Locs.size() == 1) {
368 L = Locs[0];
369 }
370 unsigned CurBuf = SrcMgr.FindBufferContainingLoc(Loc: L);
371 assert(CurBuf && "Invalid or unspecified location!");
372
373 Stream << SrcMgr.getBufferInfo(i: CurBuf).Buffer->getBufferIdentifier() << ":"
374 << SrcMgr.FindLineNumber(Loc: L, BufferID: CurBuf);
375 return str;
376}
377
378/// This function traverses the matcher tree and sizes all the nodes
379/// that are children of the three kinds of nodes that have them.
380unsigned MatcherTableEmitter::SizeMatcherList(MatcherList &ML,
381 raw_ostream &OS) {
382 unsigned Size = 0;
383 for (Matcher *N : ML)
384 Size += SizeMatcher(N, OS);
385 return Size;
386}
387
388/// This function sizes the children of the three kinds of nodes that
389/// have them. It does so by using special cases for those three
390/// nodes, but sharing the code in EmitMatcher() for the other kinds.
391unsigned MatcherTableEmitter::SizeMatcher(Matcher *N, raw_ostream &OS) {
392 unsigned Idx = 0;
393
394 ++OpcodeCounts[N->getKind()];
395 switch (N->getKind()) {
396 // The Scope matcher has its kind, a series of child size + child,
397 // and a trailing zero.
398 case Matcher::Scope: {
399 ScopeMatcher *SM = cast<ScopeMatcher>(Val: N);
400 unsigned Size = 1; // Count the kind.
401 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
402 const unsigned ChildSize = SizeMatcherList(ML&: SM->getChild(i), OS);
403 assert(ChildSize != 0 && "Matcher cannot have child of size 0");
404 SM->getChild(i).setSize(ChildSize);
405 Size += GetVBRSize(Val: ChildSize) + ChildSize; // Count VBR and child size.
406 }
407 ++Size; // Count the zero sentinel.
408 return Size;
409 }
410
411 // SwitchOpcode and SwitchType have their kind, a series of child size +
412 // opcode/type + child, and a trailing zero.
413 case Matcher::SwitchOpcode:
414 case Matcher::SwitchType: {
415 unsigned Size = 1; // Count the kind.
416 unsigned NumCases;
417 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N))
418 NumCases = SOM->getNumCases();
419 else
420 NumCases = cast<SwitchTypeMatcher>(Val: N)->getNumCases();
421 for (unsigned i = 0, e = NumCases; i != e; ++i) {
422 MatcherList *Child;
423 if (SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N)) {
424 Child = &SOM->getCaseMatcher(i);
425 Size += 2; // Count the child's opcode.
426 } else {
427 Child = &cast<SwitchTypeMatcher>(Val: N)->getCaseMatcher(i);
428 Size += GetVBRSize(Val: cast<SwitchTypeMatcher>(Val: N)
429 ->getCaseType(i)
430 .SimpleTy); // Count the child's type.
431 }
432 const unsigned ChildSize = SizeMatcherList(ML&: *Child, OS);
433 assert(ChildSize != 0 && "Matcher cannot have child of size 0");
434 Child->setSize(ChildSize);
435 Size += GetVBRSize(Val: ChildSize) + ChildSize; // Count VBR and child size.
436 }
437 ++Size; // Count the zero sentinel.
438 return Size;
439 }
440
441 // SwitchChildInteger has its kind, the child number, a series of child size +
442 // value + child, and a trailing zero.
443 case Matcher::SwitchChildInteger: {
444 SwitchChildIntegerMatcher *SCIM = cast<SwitchChildIntegerMatcher>(Val: N);
445 unsigned Size = 2; // Count the kind and the child number.
446 for (unsigned i = 0, e = SCIM->getNumCases(); i != e; ++i) {
447 MatcherList &Child = SCIM->getCaseMatcher(i);
448 const unsigned ChildSize = SizeMatcherList(ML&: Child, OS);
449 assert(ChildSize != 0 && "Matcher cannot have child of size 0");
450 Child.setSize(ChildSize);
451 Size += GetVBRSize(Val: ChildSize) + ChildSize; // Count VBR and child size.
452 Size += getSLEB128Size(Value: SCIM->getCaseValue(i)); // Count the value.
453 }
454 ++Size; // Count the zero sentinel.
455 return Size;
456 }
457
458 default:
459 // Employ the matcher emitter to size other matchers.
460 return EmitMatcher(N, Indent: 0, CurrentIdx: Idx, OS);
461 }
462 llvm_unreachable("Unreachable");
463}
464
465static void BeginEmitFunction(raw_ostream &OS, StringRef RetType,
466 StringRef Decl, bool AddOverride) {
467 OS << "#ifdef GET_DAGISEL_DECL\n";
468 OS << RetType << ' ' << Decl;
469 if (AddOverride)
470 OS << " override";
471 OS << ";\n"
472 "#endif\n"
473 "#if defined(GET_DAGISEL_BODY) || DAGISEL_INLINE\n";
474 OS << RetType << " DAGISEL_CLASS_COLONCOLON " << Decl << "\n";
475 if (AddOverride) {
476 OS << "#if DAGISEL_INLINE\n"
477 " override\n"
478 "#endif\n";
479 }
480}
481
482static void EndEmitFunction(raw_ostream &OS) {
483 OS << "#endif // GET_DAGISEL_BODY\n\n";
484}
485
486void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
487
488 if (!isUInt<32>(x: VecPatterns.size()))
489 report_fatal_error(reason: "More patterns defined that can fit into 32-bit Pattern "
490 "Table index encoding");
491
492 assert(VecPatterns.size() == VecIncludeStrings.size() &&
493 "The sizes of Pattern and include vectors should be the same");
494
495 BeginEmitFunction(OS, RetType: "StringRef", Decl: "getPatternForIndex(unsigned Index)",
496 AddOverride: true /*AddOverride*/);
497 OS << "{\n";
498 OS << "static const char *PATTERN_MATCH_TABLE[] = {\n";
499
500 for (const auto &It : VecPatterns) {
501 OS << "\"" << It.first << "\",\n";
502 }
503
504 OS << "\n};";
505 OS << "\nreturn StringRef(PATTERN_MATCH_TABLE[Index]);";
506 OS << "\n}\n";
507 EndEmitFunction(OS);
508
509 BeginEmitFunction(OS, RetType: "StringRef", Decl: "getIncludePathForIndex(unsigned Index)",
510 AddOverride: true /*AddOverride*/);
511 OS << "{\n";
512 OS << "static const char *INCLUDE_PATH_TABLE[] = {\n";
513
514 for (const auto &It : VecIncludeStrings) {
515 OS << "\"" << It << "\",\n";
516 }
517
518 OS << "\n};";
519 OS << "\nreturn StringRef(INCLUDE_PATH_TABLE[Index]);";
520 OS << "\n}\n";
521 EndEmitFunction(OS);
522}
523
524static unsigned emitMVT(MVT VT, raw_ostream &OS) {
525 // Print the MVT directly if it doesn't require a VBR.
526 if (VT.SimpleTy <= 127) {
527 OS << getEnumName(T: VT) << ',';
528 return 1;
529 }
530
531 if (!OmitComments)
532 OS << "/*" << getEnumName(T: VT) << "*/";
533 return EmitVBRValue(Val: VT.SimpleTy, OS);
534}
535
536unsigned
537MatcherTableEmitter::emitValueTypeByHwMode(const ValueTypeByHwMode &VTBH,
538 unsigned Index, raw_ostream &OS) {
539 if (!OmitComments)
540 OS << "/*" << VTBH << "*/";
541 OS << Index << ',';
542 return 1;
543}
544/// EmitMatcher - Emit bytes for the specified matcher and return
545/// the number of bytes emitted.
546unsigned MatcherTableEmitter::EmitMatcher(const Matcher *N,
547 const unsigned Indent,
548 unsigned CurrentIdx,
549 raw_ostream &OS) {
550 OS.indent(NumSpaces: Indent);
551
552 switch (N->getKind()) {
553 case Matcher::Scope: {
554 const ScopeMatcher *SM = cast<ScopeMatcher>(Val: N);
555 unsigned StartIdx = CurrentIdx;
556
557 OS << "OPC_Scope";
558 if (!OmitComments)
559 OS << " /*" << SM->getNumChildren() << " children */";
560 OS << ", ";
561 ++CurrentIdx;
562
563 // Emit all of the children.
564 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
565 if (i != 0) {
566 if (!OmitComments) {
567 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
568 OS.indent(NumSpaces: Indent) << "/*Scope*/ ";
569 } else {
570 OS.indent(NumSpaces: Indent);
571 }
572 }
573
574 const MatcherList &Child = SM->getChild(i);
575 unsigned ChildSize = Child.getSize();
576 CurrentIdx += EmitVBRValue(Val: ChildSize, OS);
577 if (!OmitComments)
578 OS << " // ->" << CurrentIdx + ChildSize;
579 OS << '\n';
580
581 ChildSize = EmitMatcherList(ML: Child, Indent: Indent + 1, StartIdx: CurrentIdx, OS);
582 assert(ChildSize == Child.getSize() &&
583 "Emitted child size does not match calculated size");
584 CurrentIdx += ChildSize;
585 }
586
587 // Emit a zero as a sentinel indicating end of 'Scope'.
588 if (!OmitComments)
589 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
590 OS.indent(NumSpaces: Indent) << "0,";
591 if (!OmitComments)
592 OS << " // End of Scope";
593 OS << '\n';
594 return CurrentIdx - StartIdx + 1;
595 }
596
597 case Matcher::RecordNode:
598 OS << "OPC_RecordNode,";
599 if (!OmitComments)
600 OS << " // #" << cast<RecordMatcher>(Val: N)->getResultNo() << " = "
601 << cast<RecordMatcher>(Val: N)->getWhatFor();
602 OS << '\n';
603 return 1;
604
605 case Matcher::RecordChild:
606 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(Val: N)->getChildNo() << ',';
607 if (!OmitComments)
608 OS << " // #" << cast<RecordChildMatcher>(Val: N)->getResultNo() << " = "
609 << cast<RecordChildMatcher>(Val: N)->getWhatFor();
610 OS << '\n';
611 return 1;
612
613 case Matcher::RecordMemRef:
614 OS << "OPC_RecordMemRef,\n";
615 return 1;
616
617 case Matcher::CaptureGlueInput:
618 OS << "OPC_CaptureGlueInput,\n";
619 return 1;
620
621 case Matcher::MoveChild: {
622 const auto *MCM = cast<MoveChildMatcher>(Val: N);
623
624 OS << "OPC_MoveChild";
625 // Handle the specialized forms.
626 if (MCM->getChildNo() >= 8)
627 OS << ", ";
628 OS << MCM->getChildNo() << ",\n";
629 return (MCM->getChildNo() >= 8) ? 2 : 1;
630 }
631
632 case Matcher::MoveSibling: {
633 const auto *MSM = cast<MoveSiblingMatcher>(Val: N);
634
635 OS << "OPC_MoveSibling";
636 // Handle the specialized forms.
637 if (MSM->getSiblingNo() >= 8)
638 OS << ", ";
639 OS << MSM->getSiblingNo() << ",\n";
640 return (MSM->getSiblingNo() >= 8) ? 2 : 1;
641 }
642
643 case Matcher::MoveParent:
644 OS << "OPC_MoveParent,\n";
645 return 1;
646
647 case Matcher::CheckSame:
648 OS << "OPC_CheckSame, " << cast<CheckSameMatcher>(Val: N)->getMatchNumber()
649 << ",\n";
650 return 2;
651
652 case Matcher::CheckChildSame:
653 OS << "OPC_CheckChild" << cast<CheckChildSameMatcher>(Val: N)->getChildNo()
654 << "Same, " << cast<CheckChildSameMatcher>(Val: N)->getMatchNumber() << ",\n";
655 return 2;
656
657 case Matcher::CheckPatternPredicate: {
658 StringRef Pred = cast<CheckPatternPredicateMatcher>(Val: N)->getPredicate();
659 unsigned PredNo = getPatternPredicate(PredName: Pred);
660 if (PredNo > 255)
661 OS << "OPC_CheckPatternPredicateTwoByte, TARGET_VAL(" << PredNo << "),";
662 else if (PredNo < 8)
663 OS << "OPC_CheckPatternPredicate" << PredNo << ',';
664 else
665 OS << "OPC_CheckPatternPredicate, " << PredNo << ',';
666 if (!OmitComments)
667 OS << " // " << Pred;
668 OS << '\n';
669 return 2 + (PredNo > 255) - (PredNo < 8);
670 }
671 case Matcher::CheckPredicate: {
672 TreePredicateFn Pred = cast<CheckPredicateMatcher>(Val: N)->getPredicate();
673 unsigned OperandBytes = 0;
674 unsigned PredNo = getNodePredicate(Pred);
675
676 if (Pred.usesOperands()) {
677 unsigned NumOps = cast<CheckPredicateMatcher>(Val: N)->getNumOperands();
678 OS << "OPC_CheckPredicateWithOperands, " << NumOps << "/*#Ops*/, ";
679 for (unsigned i = 0; i < NumOps; ++i)
680 OS << cast<CheckPredicateMatcher>(Val: N)->getOperandNo(i) << ", ";
681 OperandBytes = 1 + NumOps;
682 } else {
683 if (PredNo < 8) {
684 OperandBytes = -1;
685 OS << "OPC_CheckPredicate" << PredNo << ',';
686 } else {
687 OS << "OPC_CheckPredicate, ";
688 }
689 }
690
691 if (PredNo >= 8 || Pred.usesOperands())
692 OS << PredNo << ',';
693 if (!OmitComments)
694 OS << " // " << Pred.getFnName();
695 OS << '\n';
696 return 2 + OperandBytes;
697 }
698
699 case Matcher::CheckOpcode:
700 OS << "OPC_CheckOpcode, TARGET_VAL("
701 << cast<CheckOpcodeMatcher>(Val: N)->getOpcode().getEnumName() << "),\n";
702 return 3;
703
704 case Matcher::SwitchOpcode:
705 case Matcher::SwitchType: {
706 unsigned StartIdx = CurrentIdx;
707
708 unsigned NumCases;
709 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N)) {
710 OS << "OPC_SwitchOpcode ";
711 NumCases = SOM->getNumCases();
712 } else {
713 OS << "OPC_SwitchType ";
714 NumCases = cast<SwitchTypeMatcher>(Val: N)->getNumCases();
715 }
716
717 if (!OmitComments)
718 OS << "/*" << NumCases << " cases */";
719 OS << ", ";
720 ++CurrentIdx;
721
722 // For each case we emit the size, then the opcode, then the matcher.
723 for (unsigned i = 0, e = NumCases; i != e; ++i) {
724 const MatcherList *Child;
725 unsigned IdxSize;
726 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N)) {
727 Child = &SOM->getCaseMatcher(i);
728 IdxSize = 2; // size of opcode in table is 2 bytes.
729 } else {
730 Child = &cast<SwitchTypeMatcher>(Val: N)->getCaseMatcher(i);
731 IdxSize = GetVBRSize(
732 Val: cast<SwitchTypeMatcher>(Val: N)
733 ->getCaseType(i)
734 .SimpleTy); // size of type in table is sizeof(VBR(MVT)) byte.
735 }
736
737 if (i != 0) {
738 if (!OmitComments)
739 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
740 OS.indent(NumSpaces: Indent);
741 if (!OmitComments)
742 OS << (isa<SwitchOpcodeMatcher>(Val: N) ? "/*SwitchOpcode*/ "
743 : "/*SwitchType*/ ");
744 }
745
746 unsigned ChildSize = Child->getSize();
747 CurrentIdx += EmitVBRValue(Val: ChildSize, OS) + IdxSize;
748 OS << ' ';
749 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(Val: N))
750 OS << "TARGET_VAL(" << SOM->getCaseOpcode(i).getEnumName() << "),";
751 else
752 emitMVT(VT: cast<SwitchTypeMatcher>(Val: N)->getCaseType(i), OS);
753 if (!OmitComments)
754 OS << " // ->" << CurrentIdx + ChildSize;
755 OS << '\n';
756
757 ChildSize = EmitMatcherList(ML: *Child, Indent: Indent + 1, StartIdx: CurrentIdx, OS);
758 assert(ChildSize == Child->getSize() &&
759 "Emitted child size does not match calculated size");
760 CurrentIdx += ChildSize;
761 }
762
763 // Emit the final zero to terminate the switch.
764 if (!OmitComments)
765 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
766 OS.indent(NumSpaces: Indent) << "0,";
767 if (!OmitComments)
768 OS << (isa<SwitchOpcodeMatcher>(Val: N) ? " // EndSwitchOpcode"
769 : " // EndSwitchType");
770
771 OS << '\n';
772 return CurrentIdx - StartIdx + 1;
773 }
774
775 case Matcher::SwitchChildInteger: {
776 const SwitchChildIntegerMatcher *SCIM = cast<SwitchChildIntegerMatcher>(Val: N);
777 unsigned StartIdx = CurrentIdx;
778
779 OS << "OPC_SwitchChildInteger, " << SCIM->getChildNo() << ", ";
780 if (!OmitComments)
781 OS << "/*" << SCIM->getNumCases() << " cases */";
782 CurrentIdx += 2;
783
784 // For each case we emit the size, then the value, then the matcher.
785 for (unsigned i = 0, e = SCIM->getNumCases(); i != e; ++i) {
786 const MatcherList &Child = SCIM->getCaseMatcher(i);
787
788 if (i != 0) {
789 if (!OmitComments)
790 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
791 OS.indent(NumSpaces: Indent);
792 if (!OmitComments)
793 OS << "/*SwitchChildInteger*/ ";
794 }
795
796 unsigned ChildSize = Child.getSize();
797 CurrentIdx += EmitVBRValue(Val: ChildSize, OS);
798 OS << ' ';
799 CurrentIdx += EmitSignedVBRValue(Val: SCIM->getCaseValue(i), OS);
800 if (!OmitComments)
801 OS << " // ->" << CurrentIdx + ChildSize;
802 OS << '\n';
803
804 ChildSize = EmitMatcherList(ML: Child, Indent: Indent + 1, StartIdx: CurrentIdx, OS);
805 assert(ChildSize == Child.getSize() &&
806 "Emitted child size does not match calculated size");
807 CurrentIdx += ChildSize;
808 }
809
810 // Emit the final zero to terminate the switch.
811 if (!OmitComments)
812 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
813 OS.indent(NumSpaces: Indent) << "0,";
814 if (!OmitComments)
815 OS << " // EndSwitchChildInteger";
816 OS << '\n';
817 return CurrentIdx - StartIdx + 1;
818 }
819
820 case Matcher::CheckType: {
821 const ValueTypeByHwMode &VTBH = cast<CheckTypeMatcher>(Val: N)->getType();
822 if (VTBH.isSimple()) {
823 MVT VT = VTBH.getSimple();
824 if (cast<CheckTypeMatcher>(Val: N)->getResNo() == 0) {
825 switch (VT.SimpleTy) {
826 case MVT::i32:
827 case MVT::i64:
828 OS << "OPC_CheckTypeI" << MVT(VT).getSizeInBits() << ",\n";
829 return 1;
830 default:
831 OS << "OPC_CheckType, ";
832 unsigned NumBytes = emitMVT(VT, OS);
833 OS << '\n';
834 return NumBytes + 1;
835 }
836 }
837
838 OS << "OPC_CheckTypeRes, " << cast<CheckTypeMatcher>(Val: N)->getResNo()
839 << ", ";
840 unsigned NumBytes =
841 emitMVT(VT: cast<CheckTypeMatcher>(Val: N)->getType().getSimple(), OS);
842 OS << '\n';
843 return NumBytes + 2;
844 }
845
846 unsigned OpSize;
847 if (cast<CheckTypeMatcher>(Val: N)->getResNo() == 0) {
848 unsigned Index = getValueTypeID(VT: VTBH);
849 if (Index == 0) {
850 OS << "OPC_CheckTypeByHwMode0";
851 if (!OmitComments)
852 OS << "/*" << VTBH << "*/";
853 OS << ',';
854 OpSize = 1;
855 } else {
856 OS << "OPC_CheckTypeByHwMode, ";
857 OpSize = 1 + emitValueTypeByHwMode(VTBH, Index, OS);
858 }
859 } else {
860 OS << "OPC_CheckTypeResByHwMode, "
861 << cast<CheckTypeMatcher>(Val: N)->getResNo() << ", ";
862 OpSize = 2 + emitValueTypeByHwMode(VTBH, Index: getValueTypeID(VT: VTBH), OS);
863 }
864 OS << '\n';
865 return OpSize;
866 }
867
868 case Matcher::CheckChildType: {
869 const ValueTypeByHwMode &VTBH = cast<CheckChildTypeMatcher>(Val: N)->getType();
870 if (VTBH.isSimple()) {
871 MVT VT = VTBH.getSimple();
872 switch (VT.SimpleTy) {
873 case MVT::i32:
874 case MVT::i64:
875 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(Val: N)->getChildNo()
876 << "TypeI" << VT.getSizeInBits() << ",\n";
877 return 1;
878 default:
879 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(Val: N)->getChildNo()
880 << "Type, ";
881 unsigned NumBytes = emitMVT(VT, OS);
882 OS << '\n';
883 return NumBytes + 1;
884 }
885 } else {
886 unsigned Index = getValueTypeID(VT: VTBH);
887 if (Index == 0) {
888 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(Val: N)->getChildNo()
889 << "TypeByHwMode0";
890 if (!OmitComments)
891 OS << "/*" << VTBH << "*/";
892 OS << ",\n";
893 return 1;
894 }
895 OS << "OPC_CheckChild" << cast<CheckChildTypeMatcher>(Val: N)->getChildNo()
896 << "TypeByHwMode, ";
897 unsigned NumBytes = emitValueTypeByHwMode(VTBH, Index, OS);
898 OS << '\n';
899 return NumBytes + 1;
900 }
901 }
902
903 case Matcher::CheckInteger: {
904 OS << "OPC_CheckInteger, ";
905 unsigned Bytes =
906 1 + EmitSignedVBRValue(Val: cast<CheckIntegerMatcher>(Val: N)->getValue(), OS);
907 OS << '\n';
908 return Bytes;
909 }
910 case Matcher::CheckChildInteger: {
911 OS << "OPC_CheckChild" << cast<CheckChildIntegerMatcher>(Val: N)->getChildNo()
912 << "Integer, ";
913 unsigned Bytes = 1 + EmitSignedVBRValue(
914 Val: cast<CheckChildIntegerMatcher>(Val: N)->getValue(), OS);
915 OS << '\n';
916 return Bytes;
917 }
918 case Matcher::CheckCondCode:
919 OS << "OPC_CheckCondCode, ISD::"
920 << cast<CheckCondCodeMatcher>(Val: N)->getCondCodeName() << ",\n";
921 return 2;
922
923 case Matcher::CheckChild2CondCode:
924 OS << "OPC_CheckChild2CondCode, ISD::"
925 << cast<CheckChild2CondCodeMatcher>(Val: N)->getCondCodeName() << ",\n";
926 return 2;
927
928 case Matcher::CheckValueType: {
929 OS << "OPC_CheckValueType, ";
930 unsigned NumBytes = emitMVT(VT: cast<CheckValueTypeMatcher>(Val: N)->getVT(), OS);
931 OS << "\n";
932 return NumBytes + 1;
933 }
934
935 case Matcher::CheckComplexPat: {
936 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(Val: N);
937 const ComplexPattern &Pattern = CCPM->getPattern();
938 unsigned PatternNo = getComplexPat(P: Pattern);
939 if (PatternNo < 8)
940 OS << "OPC_CheckComplexPat" << PatternNo << ", /*#*/"
941 << CCPM->getMatchNumber() << ',';
942 else
943 OS << "OPC_CheckComplexPat, /*CP*/" << PatternNo << ", /*#*/"
944 << CCPM->getMatchNumber() << ',';
945
946 if (!OmitComments) {
947 OS << " // " << Pattern.getSelectFunc();
948 OS << ":$" << CCPM->getName();
949 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
950 OS << " #" << CCPM->getFirstResult() + i;
951
952 if (Pattern.hasProperty(Prop: SDNPHasChain))
953 OS << " + chain result";
954 }
955 OS << '\n';
956 return PatternNo < 8 ? 2 : 3;
957 }
958
959 case Matcher::CheckAndImm: {
960 OS << "OPC_CheckAndImm, ";
961 unsigned Bytes =
962 1 + EmitVBRValue(Val: cast<CheckAndImmMatcher>(Val: N)->getValue(), OS);
963 OS << '\n';
964 return Bytes;
965 }
966
967 case Matcher::CheckOrImm: {
968 OS << "OPC_CheckOrImm, ";
969 unsigned Bytes =
970 1 + EmitVBRValue(Val: cast<CheckOrImmMatcher>(Val: N)->getValue(), OS);
971 OS << '\n';
972 return Bytes;
973 }
974
975 case Matcher::CheckFoldableChainNode:
976 OS << "OPC_CheckFoldableChainNode,\n";
977 return 1;
978
979 case Matcher::CheckImmAllOnesV:
980 OS << "OPC_CheckImmAllOnesV,\n";
981 return 1;
982
983 case Matcher::CheckImmAllZerosV:
984 OS << "OPC_CheckImmAllZerosV,\n";
985 return 1;
986
987 case Matcher::CheckUndef:
988 OS << "OPC_CheckUndef,\n";
989 return 1;
990
991 case Matcher::EmitInteger: {
992 const auto *IM = cast<EmitIntegerMatcher>(Val: N);
993 int64_t Val = IM->getValue();
994 const std::string &Str = IM->getString();
995 const ValueTypeByHwMode &VTBH = IM->getVT();
996 unsigned TypeBytes = 0;
997 if (VTBH.isSimple()) {
998 MVT VT = VTBH.getSimple();
999 switch (VT.SimpleTy) {
1000 case MVT::i8:
1001 case MVT::i16:
1002 case MVT::i32:
1003 case MVT::i64:
1004 OS << "OPC_EmitIntegerI" << VT.getSizeInBits() << ", ";
1005 break;
1006 default:
1007 OS << "OPC_EmitInteger, ";
1008 TypeBytes = emitMVT(VT, OS);
1009 OS << ' ';
1010 break;
1011 }
1012 } else {
1013 unsigned Index = getValueTypeID(VT: VTBH);
1014 if (Index == 0) {
1015 OS << "OPC_EmitIntegerByHwMode0";
1016 if (!OmitComments)
1017 OS << "/*" << VTBH << "*/";
1018 OS << ", ";
1019 TypeBytes = 0;
1020 } else {
1021 OS << "OPC_EmitIntegerByHwMode, ";
1022 TypeBytes = emitValueTypeByHwMode(VTBH, Index, OS);
1023 OS << ' ';
1024 }
1025 }
1026 // If the value is 63 or smaller, use the string directly. Otherwise, use
1027 // a VBR.
1028 unsigned ValBytes = 1;
1029 if (!Str.empty() && Val <= 63)
1030 OS << Str << ',';
1031 else
1032 ValBytes = EmitSignedVBRValue(Val, OS);
1033 if (!OmitComments) {
1034 OS << " // #" << IM->getResultNo() << " = ";
1035 if (!Str.empty())
1036 OS << Str;
1037 else
1038 OS << Val;
1039 }
1040 OS << '\n';
1041 return 1 + TypeBytes + ValBytes;
1042 }
1043
1044 case Matcher::EmitRegister: {
1045 const EmitRegisterMatcher *Matcher = cast<EmitRegisterMatcher>(Val: N);
1046 const CodeGenRegister *Reg = Matcher->getReg();
1047 const ValueTypeByHwMode &VTBH = Matcher->getVT();
1048 unsigned OpBytes;
1049 if (VTBH.isSimple()) {
1050 MVT VT = VTBH.getSimple();
1051 // If the enum value of the register is larger than one byte can handle,
1052 // use EmitRegister2.
1053 if (Reg && Reg->EnumValue > 255) {
1054 OS << "OPC_EmitRegister2, ";
1055 OpBytes = emitMVT(VT, OS);
1056 OS << " TARGET_VAL(" << getQualifiedName(R: Reg->TheDef) << "),\n";
1057 return OpBytes + 3;
1058 }
1059 switch (VT.SimpleTy) {
1060 case MVT::i32:
1061 case MVT::i64:
1062 OpBytes = 1;
1063 OS << "OPC_EmitRegisterI" << VT.getSizeInBits() << ", ";
1064 break;
1065 default:
1066 OS << "OPC_EmitRegister, ";
1067 OpBytes = emitMVT(VT, OS) + 1;
1068 OS << ' ';
1069 break;
1070 }
1071 } else {
1072 if (Reg && Reg->EnumValue > 255) {
1073 OS << "OPC_EmitRegisterByHwMode2, ";
1074 OpBytes = emitValueTypeByHwMode(VTBH, Index: getValueTypeID(VT: VTBH), OS);
1075 OS << " TARGET_VAL(" << getQualifiedName(R: Reg->TheDef) << "),\n";
1076 return OpBytes + 3;
1077 }
1078
1079 OS << "OPC_EmitRegisterByHwMode, ";
1080 OpBytes = emitValueTypeByHwMode(VTBH, Index: getValueTypeID(VT: VTBH), OS) + 1;
1081 OS << ' ';
1082 }
1083 if (Reg)
1084 OS << getQualifiedName(R: Reg->TheDef);
1085 else
1086 OS << "MCRegister::NoRegister";
1087
1088 OS << ',';
1089 if (!OmitComments)
1090 OS << " // #" << Matcher->getResultNo();
1091 OS << '\n';
1092 return OpBytes + 1;
1093 }
1094
1095 case Matcher::EmitConvertToTarget: {
1096 const auto *CTTM = cast<EmitConvertToTargetMatcher>(Val: N);
1097 unsigned Slot = CTTM->getSlot();
1098 OS << "OPC_EmitConvertToTarget";
1099 if (Slot >= 8)
1100 OS << ", ";
1101 OS << Slot << ',';
1102 if (!OmitComments)
1103 OS << " // #" << CTTM->getResultNo() << " = ConvertToTarget #" << Slot;
1104 OS << '\n';
1105 return 1 + (Slot >= 8);
1106 }
1107
1108 case Matcher::EmitMergeInputChains: {
1109 const EmitMergeInputChainsMatcher *MN =
1110 cast<EmitMergeInputChainsMatcher>(Val: N);
1111
1112 // Handle the specialized forms OPC_EmitMergeInputChains1_0, 1_1, and 1_2.
1113 if (MN->getNumNodes() == 1 && MN->getNode(i: 0) < 3) {
1114 OS << "OPC_EmitMergeInputChains1_" << MN->getNode(i: 0) << ",\n";
1115 return 1;
1116 }
1117
1118 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ',';
1119 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
1120 OS << ' ' << MN->getNode(i) << ",";
1121 OS << '\n';
1122 return 2 + MN->getNumNodes();
1123 }
1124 case Matcher::EmitCopyToReg: {
1125 const auto *C2RMatcher = cast<EmitCopyToRegMatcher>(Val: N);
1126 int Bytes = 3;
1127 const CodeGenRegister *Reg = C2RMatcher->getDestPhysReg();
1128 unsigned Slot = C2RMatcher->getSrcSlot();
1129 if (Reg->EnumValue > 255) {
1130 assert(isUInt<16>(Reg->EnumValue) && "not handled");
1131 OS << "OPC_EmitCopyToRegTwoByte, " << Slot << ", "
1132 << "TARGET_VAL(" << getQualifiedName(R: Reg->TheDef) << "),";
1133 ++Bytes;
1134 } else {
1135 if (Slot < 8) {
1136 OS << "OPC_EmitCopyToReg" << Slot << ", "
1137 << getQualifiedName(R: Reg->TheDef) << ",";
1138 --Bytes;
1139 } else {
1140 OS << "OPC_EmitCopyToReg, " << Slot << ", "
1141 << getQualifiedName(R: Reg->TheDef) << ",";
1142 }
1143 }
1144 if (!OmitComments)
1145 OS << " // = #" << Slot;
1146
1147 OS << '\n';
1148 return Bytes;
1149 }
1150 case Matcher::EmitNodeXForm: {
1151 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(Val: N);
1152 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(Rec: XF->getNodeXForm()) << ", "
1153 << XF->getSlot() << ',';
1154 if (!OmitComments)
1155 OS << " // #" << XF->getResultNo() << " = "
1156 << XF->getNodeXForm()->getName() << " #" << XF->getSlot();
1157 OS << '\n';
1158 return 3;
1159 }
1160
1161 case Matcher::EmitNode:
1162 case Matcher::MorphNodeTo: {
1163 auto NumCoveredBytes = 0;
1164 if (InstrumentCoverage) {
1165 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(Val: N)) {
1166 NumCoveredBytes = 3;
1167 OS << "OPC_Coverage, ";
1168 std::string src =
1169 GetPatFromTreePatternNode(N: SNT->getPattern().getSrcPattern());
1170 std::string dst =
1171 GetPatFromTreePatternNode(N: SNT->getPattern().getDstPattern());
1172 const Record *PatRecord = SNT->getPattern().getSrcRecord();
1173 std::string include_src = getIncludePath(R: PatRecord);
1174 unsigned Offset =
1175 getPatternIdxFromTable(P: src + " -> " + dst, include_loc: std::move(include_src));
1176 OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
1177 OS.indent(NumSpaces: FullIndexWidth + Indent);
1178 }
1179 }
1180 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(Val: N);
1181 bool SupportsDeactivationSymbol =
1182 EN->getInstruction().TheDef->getValueAsBit(
1183 FieldName: "supportsDeactivationSymbol");
1184 if (SupportsDeactivationSymbol) {
1185 OS << "OPC_CaptureDeactivationSymbol,\n";
1186 OS.indent(NumSpaces: FullIndexWidth + Indent);
1187 }
1188
1189 bool ByHwMode =
1190 llvm::any_of(Range: EN->getVTList(), P: [](const ValueTypeByHwMode &VT) {
1191 return !VT.isSimple();
1192 });
1193
1194 bool IsEmitNode = isa<EmitNodeMatcher>(Val: EN);
1195 OS << (IsEmitNode ? "OPC_EmitNode" : "OPC_MorphNodeTo");
1196 unsigned NumVTs = EN->getNumVTs();
1197 bool CompressVTs = !ByHwMode && EN->getNumVTs() < 3;
1198 bool CompressNodeInfo = false;
1199 if (CompressVTs) {
1200 OS << NumVTs;
1201 // When NumVTs is zero, only consider compressing the chain flag. Any
1202 // zero result node without chain would be deleted and not eligible for
1203 // isel.
1204 if (NumVTs > 0 && !EN->hasChain() && !EN->hasInGlue() &&
1205 !EN->hasOutGlue() && !EN->hasMemRefs() &&
1206 EN->getNumFixedArityOperands() == -1) {
1207 CompressNodeInfo = true;
1208 OS << "None";
1209 } else if (EN->hasChain() && !EN->hasInGlue() && !EN->hasOutGlue() &&
1210 !EN->hasMemRefs() && EN->getNumFixedArityOperands() == -1) {
1211 CompressNodeInfo = true;
1212 OS << "Chain";
1213 } else if (NumVTs > 0 && !IsEmitNode && !EN->hasChain() &&
1214 EN->hasInGlue() && !EN->hasOutGlue() && !EN->hasMemRefs() &&
1215 EN->getNumFixedArityOperands() == -1) {
1216 CompressNodeInfo = true;
1217 OS << "GlueInput";
1218 } else if (NumVTs > 0 && !IsEmitNode && !EN->hasChain() &&
1219 !EN->hasInGlue() && EN->hasOutGlue() && !EN->hasMemRefs() &&
1220 EN->getNumFixedArityOperands() == -1) {
1221 CompressNodeInfo = true;
1222 OS << "GlueOutput";
1223 }
1224 }
1225
1226 if (ByHwMode)
1227 OS << "ByHwMode";
1228
1229 const CodeGenInstruction &CGI = EN->getInstruction();
1230 OS << ", TARGET_VAL(" << CGI.Namespace << "::" << CGI.TheDef->getName()
1231 << ")";
1232
1233 if (!CompressNodeInfo) {
1234 OS << ", 0";
1235 if (EN->hasChain())
1236 OS << "|OPFL_Chain";
1237 if (EN->hasInGlue())
1238 OS << "|OPFL_GlueInput";
1239 if (EN->hasOutGlue())
1240 OS << "|OPFL_GlueOutput";
1241 if (EN->hasMemRefs())
1242 OS << "|OPFL_MemRefs";
1243 if (EN->getNumFixedArityOperands() != -1)
1244 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
1245 }
1246 OS << ",\n";
1247
1248 OS.indent(NumSpaces: FullIndexWidth + Indent + 4);
1249 if (!CompressVTs) {
1250 OS << EN->getNumVTs();
1251 if (!OmitComments)
1252 OS << "/*#VTs*/";
1253 OS << ",";
1254 }
1255 unsigned NumTypeBytes = 0;
1256 if (ByHwMode) {
1257 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i) {
1258 OS << ' ';
1259 const ValueTypeByHwMode &VTBH = EN->getVT(i);
1260 NumTypeBytes += emitValueTypeByHwMode(VTBH, Index: getValueTypeID(VT: VTBH), OS);
1261 }
1262 } else {
1263 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i) {
1264 OS << ' ';
1265 NumTypeBytes += emitMVT(VT: EN->getVT(i).getSimple(), OS);
1266 }
1267 }
1268
1269 unsigned NumOps = EN->getNumOperands();
1270 OS << ' ' << NumOps;
1271 if (!OmitComments)
1272 OS << "/*#Ops*/";
1273 OS << ',';
1274
1275 unsigned NumOperandBytes = 0;
1276 if (NumOps != 0) {
1277 std::vector<uint8_t> OpBytes;
1278 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
1279 uint8_t Buffer[5];
1280 unsigned Len = encodeULEB128(Value: EN->getOperand(i), p: Buffer);
1281 for (unsigned i = 0; i < Len; ++i)
1282 OpBytes.push_back(x: Buffer[i]);
1283 }
1284 unsigned Index = OperandTable.get(Seq: OpBytes);
1285 OS << ' ';
1286 if (!OmitComments)
1287 OS << "/*OperandList*/";
1288 NumOperandBytes = EmitVBRValue(Val: Index, OS);
1289 }
1290
1291 if (!OmitComments) {
1292 // Print the operand #'s.
1293 ArrayRef<unsigned> Ops = EN->getOperandList();
1294 OS << " // Ops =";
1295 if (Ops.empty())
1296 OS << " None";
1297 else
1298 for (unsigned OpNo : Ops)
1299 OS << " #" << OpNo;
1300
1301 // Print the result #'s for EmitNode.
1302 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(Val: EN)) {
1303 if (unsigned NumResults = EN->getNumVTs()) {
1304 OS << " Results =";
1305 unsigned First = E->getFirstResultSlot();
1306 for (unsigned i = 0; i != NumResults; ++i)
1307 OS << " #" << First + i;
1308 }
1309 }
1310 OS << '\n';
1311
1312 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(Val: N)) {
1313 OS.indent(NumSpaces: FullIndexWidth + Indent)
1314 << "// Src: " << SNT->getPattern().getSrcPattern()
1315 << " - Complexity = " << SNT->getPattern().getPatternComplexity(CGP)
1316 << '\n';
1317 OS.indent(NumSpaces: FullIndexWidth + Indent)
1318 << "// Dst: " << SNT->getPattern().getDstPattern() << '\n';
1319 }
1320 } else {
1321 OS << '\n';
1322 }
1323
1324 return 4 + SupportsDeactivationSymbol + !CompressVTs + !CompressNodeInfo +
1325 NumTypeBytes + NumOperandBytes + NumCoveredBytes;
1326 }
1327 case Matcher::CompleteMatch: {
1328 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(Val: N);
1329 auto NumCoveredBytes = 0;
1330 if (InstrumentCoverage) {
1331 NumCoveredBytes = 3;
1332 OS << "OPC_Coverage, ";
1333 std::string src =
1334 GetPatFromTreePatternNode(N: CM->getPattern().getSrcPattern());
1335 std::string dst =
1336 GetPatFromTreePatternNode(N: CM->getPattern().getDstPattern());
1337 const Record *PatRecord = CM->getPattern().getSrcRecord();
1338 std::string include_src = getIncludePath(R: PatRecord);
1339 unsigned Offset =
1340 getPatternIdxFromTable(P: src + " -> " + dst, include_loc: std::move(include_src));
1341 OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
1342 OS.indent(NumSpaces: FullIndexWidth + Indent);
1343 }
1344 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ",";
1345 unsigned NumResultBytes = 0;
1346 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i) {
1347 OS << ' ';
1348 NumResultBytes += EmitVBRValue(Val: CM->getResult(R: i), OS);
1349 }
1350 OS << '\n';
1351 if (!OmitComments) {
1352 OS.indent(NumSpaces: FullIndexWidth + Indent)
1353 << " // Src: " << CM->getPattern().getSrcPattern()
1354 << " - Complexity = " << CM->getPattern().getPatternComplexity(CGP)
1355 << '\n';
1356 OS.indent(NumSpaces: FullIndexWidth + Indent)
1357 << " // Dst: " << CM->getPattern().getDstPattern();
1358 }
1359 OS << '\n';
1360 return 2 + NumResultBytes + NumCoveredBytes;
1361 }
1362 }
1363 llvm_unreachable("Unreachable");
1364}
1365
1366/// This function traverses the matcher tree and emits all the nodes.
1367/// The nodes have already been sized.
1368unsigned MatcherTableEmitter::EmitMatcherList(const MatcherList &ML,
1369 const unsigned Indent,
1370 unsigned CurrentIdx,
1371 raw_ostream &OS) {
1372 unsigned Size = 0;
1373 for (const Matcher *N : ML) {
1374 if (!OmitComments)
1375 OS << "/*" << format_decimal(N: CurrentIdx, Width: IndexWidth) << "*/";
1376 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
1377 Size += MatcherSize;
1378 CurrentIdx += MatcherSize;
1379 }
1380 return Size;
1381}
1382
1383void MatcherTableEmitter::EmitOperandLists(raw_ostream &OS) {
1384 OperandTable.emit(OS, Print: [](raw_ostream &OS, uint8_t O) { OS << (unsigned)O; });
1385}
1386
1387void MatcherTableEmitter::EmitNodePredicatesFunction(
1388 const std::vector<TreePattern *> &Preds, StringRef Decl, raw_ostream &OS) {
1389 if (Preds.empty())
1390 return;
1391
1392 BeginEmitFunction(OS, RetType: "bool", Decl, AddOverride: true /*AddOverride*/);
1393 OS << "{\n";
1394 OS << " switch (PredNo) {\n";
1395 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
1396 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1397 // Emit the predicate code corresponding to this pattern.
1398 TreePredicateFn PredFn(Preds[i]);
1399 assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
1400 std::string PredFnCodeStr = PredFn.getCodeToRunOnSDNode();
1401
1402 OS << " case " << i << ": {\n";
1403 for (auto *SimilarPred : NodePredicatesByCodeToRun[PredFnCodeStr])
1404 OS << " // " << TreePredicateFn(SimilarPred).getFnName() << '\n';
1405 OS << PredFnCodeStr << "\n }\n";
1406 }
1407 OS << " }\n";
1408 OS << "}\n";
1409 EndEmitFunction(OS);
1410}
1411
1412void MatcherTableEmitter::EmitPredicateFunctions(raw_ostream &OS) {
1413 // Emit pattern predicates.
1414 if (!PatternPredicates.empty()) {
1415 BeginEmitFunction(OS, RetType: "bool",
1416 Decl: "CheckPatternPredicate(unsigned PredNo) const",
1417 AddOverride: true /*AddOverride*/);
1418 OS << "{\n";
1419 OS << " switch (PredNo) {\n";
1420 OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
1421 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
1422 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
1423 OS << " }\n";
1424 OS << "}\n";
1425 EndEmitFunction(OS);
1426 }
1427
1428 // Emit Node predicates.
1429 EmitNodePredicatesFunction(
1430 Preds: NodePredicates, Decl: "CheckNodePredicate(SDValue Op, unsigned PredNo) const",
1431 OS);
1432 EmitNodePredicatesFunction(
1433 Preds: NodePredicatesWithOperands,
1434 Decl: "CheckNodePredicateWithOperands(SDValue Op, unsigned PredNo, "
1435 "ArrayRef<SDValue> Operands) const",
1436 OS);
1437
1438 // Emit CompletePattern matchers.
1439 // FIXME: This should be const.
1440 if (!ComplexPatterns.empty()) {
1441 BeginEmitFunction(
1442 OS, RetType: "bool",
1443 Decl: "CheckComplexPattern(SDNode *Root, SDNode *Parent,\n"
1444 " SDValue N, unsigned PatternNo,\n"
1445 " SmallVectorImpl<std::pair<SDValue, SDNode *>> &Result)",
1446 AddOverride: true /*AddOverride*/);
1447 OS << "{\n";
1448 OS << " unsigned NextRes = Result.size();\n";
1449 OS << " switch (PatternNo) {\n";
1450 OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
1451 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
1452 const ComplexPattern &P = *ComplexPatterns[i];
1453 unsigned NumOps = P.getNumOperands();
1454
1455 if (P.hasProperty(Prop: SDNPHasChain))
1456 ++NumOps; // Get the chained node too.
1457
1458 OS << " case " << i << ":\n";
1459 if (InstrumentCoverage)
1460 OS << " {\n";
1461 OS << " Result.resize(NextRes+" << NumOps << ");\n";
1462 if (InstrumentCoverage)
1463 OS << " bool Succeeded = " << P.getSelectFunc();
1464 else
1465 OS << " return " << P.getSelectFunc();
1466
1467 OS << "(";
1468 // If the complex pattern wants the root of the match, pass it in as the
1469 // first argument.
1470 if (P.wantsRoot())
1471 OS << "Root, ";
1472
1473 // If the complex pattern wants the parent of the operand being matched,
1474 // pass it in as the next argument.
1475 if (P.wantsParent())
1476 OS << "Parent, ";
1477
1478 OS << "N";
1479 for (unsigned i = 0; i != NumOps; ++i)
1480 OS << ", Result[NextRes+" << i << "].first";
1481 OS << ");\n";
1482 if (InstrumentCoverage) {
1483 OS << " if (Succeeded)\n";
1484 OS << " dbgs() << \"\\nCOMPLEX_PATTERN: " << P.getSelectFunc()
1485 << "\\n\" ;\n";
1486 OS << " return Succeeded;\n";
1487 OS << " }\n";
1488 }
1489 }
1490 OS << " }\n";
1491 OS << "}\n";
1492 EndEmitFunction(OS);
1493 }
1494
1495 // Emit SDNodeXForm handlers.
1496 // FIXME: This should be const.
1497 if (!NodeXForms.empty()) {
1498 BeginEmitFunction(OS, RetType: "SDValue",
1499 Decl: "RunSDNodeXForm(SDValue V, unsigned XFormNo)",
1500 AddOverride: true /*AddOverride*/);
1501 OS << "{\n";
1502 OS << " switch (XFormNo) {\n";
1503 OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
1504
1505 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
1506 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
1507 const CodeGenDAGPatterns::NodeXForm &Entry =
1508 CGP.getSDNodeTransform(R: NodeXForms[i]);
1509
1510 const Record *SDNode = Entry.first;
1511 const std::string &Code = Entry.second;
1512
1513 OS << " case " << i << ": { ";
1514 if (!OmitComments)
1515 OS << "// " << NodeXForms[i]->getName();
1516 OS << '\n';
1517
1518 std::string ClassName = CGP.getSDNodeInfo(R: SDNode).getSDClassName().str();
1519 if (ClassName == "SDNode")
1520 OS << " SDNode *N = V.getNode();\n";
1521 else
1522 OS << " " << ClassName << " *N = cast<" << ClassName
1523 << ">(V.getNode());\n";
1524 OS << Code << "\n }\n";
1525 }
1526 OS << " }\n";
1527 OS << "}\n";
1528 EndEmitFunction(OS);
1529 }
1530}
1531
1532void MatcherTableEmitter::EmitValueTypeFunction(raw_ostream &OS) {
1533 if (ValueTypeMap.empty())
1534 return;
1535
1536 BeginEmitFunction(OS, RetType: "MVT", Decl: "getValueTypeForHwMode(unsigned Index) const",
1537 /*AddOverride=*/true);
1538 OS << "{\n";
1539
1540 OS << " switch (Index) {\n";
1541 OS << " default: llvm_unreachable(\"Unexpected index\");\n";
1542
1543 for (const auto &[VTs, IdxAndCount] : ValueTypeMap) {
1544 const auto &[Idx, Count] = IdxAndCount;
1545 OS << " case " << (Idx - 1) << ":\n";
1546 if (VTs.isSimple()) {
1547 OS << " return " << getEnumName(T: VTs.getSimple()) << ";\n";
1548 } else {
1549 OS << " switch (HwMode) {\n";
1550 if (!VTs.hasDefault())
1551 OS << " default:\n return MVT();\n";
1552 for (const auto [Mode, VT] : VTs) {
1553 if (Mode == DefaultMode)
1554 OS << " default:\n";
1555 else
1556 OS << " case " << Mode << ":\n";
1557 OS << " return " << getEnumName(T: VT) << ";\n";
1558 }
1559
1560 OS << " }\n";
1561 OS << " break;\n";
1562 }
1563 }
1564
1565 OS << " }\n";
1566
1567 OS << "}\n";
1568 EndEmitFunction(OS);
1569}
1570
1571static StringRef getOpcodeString(Matcher::KindTy Kind) {
1572 switch (Kind) {
1573 case Matcher::Scope:
1574 return "OPC_Scope";
1575 case Matcher::RecordNode:
1576 return "OPC_RecordNode";
1577 case Matcher::RecordChild:
1578 return "OPC_RecordChild";
1579 case Matcher::RecordMemRef:
1580 return "OPC_RecordMemRef";
1581 case Matcher::CaptureGlueInput:
1582 return "OPC_CaptureGlueInput";
1583 case Matcher::MoveChild:
1584 return "OPC_MoveChild";
1585 case Matcher::MoveSibling:
1586 return "OPC_MoveSibling";
1587 case Matcher::MoveParent:
1588 return "OPC_MoveParent";
1589 case Matcher::CheckSame:
1590 return "OPC_CheckSame";
1591 case Matcher::CheckChildSame:
1592 return "OPC_CheckChildSame";
1593 case Matcher::CheckPatternPredicate:
1594 return "OPC_CheckPatternPredicate";
1595 case Matcher::CheckPredicate:
1596 return "OPC_CheckPredicate";
1597 case Matcher::CheckOpcode:
1598 return "OPC_CheckOpcode";
1599 case Matcher::SwitchOpcode:
1600 return "OPC_SwitchOpcode";
1601 case Matcher::CheckType:
1602 return "OPC_CheckType";
1603 case Matcher::SwitchType:
1604 return "OPC_SwitchType";
1605 case Matcher::CheckChildType:
1606 return "OPC_CheckChildType";
1607 case Matcher::CheckInteger:
1608 return "OPC_CheckInteger";
1609 case Matcher::CheckChildInteger:
1610 return "OPC_CheckChildInteger";
1611 case Matcher::SwitchChildInteger:
1612 return "OPC_SwitchChildInteger";
1613 case Matcher::CheckCondCode:
1614 return "OPC_CheckCondCode";
1615 case Matcher::CheckChild2CondCode:
1616 return "OPC_CheckChild2CondCode";
1617 case Matcher::CheckValueType:
1618 return "OPC_CheckValueType";
1619 case Matcher::CheckComplexPat:
1620 return "OPC_CheckComplexPat";
1621 case Matcher::CheckAndImm:
1622 return "OPC_CheckAndImm";
1623 case Matcher::CheckOrImm:
1624 return "OPC_CheckOrImm";
1625 case Matcher::CheckFoldableChainNode:
1626 return "OPC_CheckFoldableChainNode";
1627 case Matcher::CheckImmAllOnesV:
1628 return "OPC_CheckImmAllOnesV";
1629 case Matcher::CheckImmAllZerosV:
1630 return "OPC_CheckImmAllZerosV";
1631 case Matcher::CheckUndef:
1632 return "OPC_CheckUndef";
1633 case Matcher::EmitInteger:
1634 return "OPC_EmitInteger";
1635 case Matcher::EmitRegister:
1636 return "OPC_EmitRegister";
1637 case Matcher::EmitConvertToTarget:
1638 return "OPC_EmitConvertToTarget";
1639 case Matcher::EmitMergeInputChains:
1640 return "OPC_EmitMergeInputChains";
1641 case Matcher::EmitCopyToReg:
1642 return "OPC_EmitCopyToReg";
1643 case Matcher::EmitNode:
1644 return "OPC_EmitNode";
1645 case Matcher::MorphNodeTo:
1646 return "OPC_MorphNodeTo";
1647 case Matcher::EmitNodeXForm:
1648 return "OPC_EmitNodeXForm";
1649 case Matcher::CompleteMatch:
1650 return "OPC_CompleteMatch";
1651 }
1652
1653 llvm_unreachable("Unhandled opcode?");
1654}
1655
1656void MatcherTableEmitter::EmitHistogram(raw_ostream &OS) {
1657 if (OmitComments)
1658 return;
1659
1660 OS << " // Opcode Histogram:\n";
1661 for (unsigned i = 0, e = OpcodeCounts.size(); i != e; ++i) {
1662 OS << " // #"
1663 << left_justify(Str: getOpcodeString(Kind: (Matcher::KindTy)i), Width: HistOpcWidth)
1664 << " = " << OpcodeCounts[i] << '\n';
1665 }
1666 OS << '\n';
1667}
1668
1669void llvm::EmitMatcherTable(MatcherList &TheMatcherList,
1670 const CodeGenDAGPatterns &CGP, raw_ostream &OS) {
1671 OS << "#if defined(GET_DAGISEL_DECL) && defined(GET_DAGISEL_BODY)\n";
1672 OS << "#error GET_DAGISEL_DECL and GET_DAGISEL_BODY cannot be both defined, ";
1673 OS << "undef both for inline definitions\n";
1674 OS << "#endif\n\n";
1675
1676 // Emit a check for omitted class name.
1677 OS << "#ifdef GET_DAGISEL_BODY\n";
1678 OS << "#define LOCAL_DAGISEL_STRINGIZE(X) LOCAL_DAGISEL_STRINGIZE_(X)\n";
1679 OS << "#define LOCAL_DAGISEL_STRINGIZE_(X) #X\n";
1680 OS << "static_assert(sizeof(LOCAL_DAGISEL_STRINGIZE(GET_DAGISEL_BODY)) > 1,"
1681 "\n";
1682 OS << " \"GET_DAGISEL_BODY is empty: it should be defined with the class "
1683 "name\");\n";
1684 OS << "#undef LOCAL_DAGISEL_STRINGIZE_\n";
1685 OS << "#undef LOCAL_DAGISEL_STRINGIZE\n";
1686 OS << "#endif\n\n";
1687
1688 OS << "#if !defined(GET_DAGISEL_DECL) && !defined(GET_DAGISEL_BODY)\n";
1689 OS << "#define DAGISEL_INLINE 1\n";
1690 OS << "#else\n";
1691 OS << "#define DAGISEL_INLINE 0\n";
1692 OS << "#endif\n\n";
1693
1694 OS << "#if !DAGISEL_INLINE\n";
1695 OS << "#define DAGISEL_CLASS_COLONCOLON GET_DAGISEL_BODY ::\n";
1696 OS << "#else\n";
1697 OS << "#define DAGISEL_CLASS_COLONCOLON\n";
1698 OS << "#endif\n\n";
1699
1700 BeginEmitFunction(OS, RetType: "void", Decl: "SelectCode(SDNode *N)", AddOverride: false /*AddOverride*/);
1701 MatcherTableEmitter MatcherEmitter(TheMatcherList, CGP);
1702
1703 // First we size all the children of the three kinds of matchers that have
1704 // them. This is done by sharing the code in EmitMatcher(). but we don't
1705 // want to emit anything, so we turn off comments and use a null stream.
1706 bool SaveOmitComments = OmitComments;
1707 OmitComments = true;
1708 raw_null_ostream NullOS;
1709 unsigned TotalSize = MatcherEmitter.SizeMatcherList(ML&: TheMatcherList, OS&: NullOS);
1710 OmitComments = SaveOmitComments;
1711
1712 // Now that the matchers are sized, we can emit the code for them to the
1713 // final stream.
1714 OS << "{\n";
1715 OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
1716 OS << " // this. Coverage indexes are emitted as 4 bytes,\n";
1717 OS << " // COVERAGE_IDX_VAL handles this.\n";
1718 OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
1719 OS << " #define COVERAGE_IDX_VAL(X) X & 255, (unsigned(X) >> 8) & 255, ";
1720 OS << "(unsigned(X) >> 16) & 255, (unsigned(X) >> 24) & 255\n";
1721 OS << " static const uint8_t MatcherTable[] = {\n";
1722 TotalSize = MatcherEmitter.EmitMatcherList(ML: TheMatcherList, Indent: 1, CurrentIdx: 0, OS);
1723 OS << " }; // Total Array size is " << TotalSize << " bytes\n\n";
1724
1725 MatcherEmitter.EmitHistogram(OS);
1726
1727 OS << " static const uint8_t OperandLists[] = {\n";
1728 MatcherEmitter.EmitOperandLists(OS);
1729 OS << " };\n\n";
1730
1731 OS << " #undef COVERAGE_IDX_VAL\n";
1732 OS << " #undef TARGET_VAL\n";
1733 OS << " SelectCodeCommon(N, MatcherTable, sizeof(MatcherTable),\n";
1734 OS << " OperandLists);\n";
1735 OS << "}\n";
1736 EndEmitFunction(OS);
1737
1738 // Next up, emit the function for node and pattern predicates:
1739 MatcherEmitter.EmitPredicateFunctions(OS);
1740
1741 MatcherEmitter.EmitValueTypeFunction(OS);
1742
1743 if (InstrumentCoverage)
1744 MatcherEmitter.EmitPatternMatchTable(OS);
1745
1746 // Clean up the preprocessor macros.
1747 OS << "\n";
1748 OS << "#ifdef DAGISEL_INLINE\n";
1749 OS << "#undef DAGISEL_INLINE\n";
1750 OS << "#endif\n";
1751 OS << "#ifdef DAGISEL_CLASS_COLONCOLON\n";
1752 OS << "#undef DAGISEL_CLASS_COLONCOLON\n";
1753 OS << "#endif\n";
1754 OS << "#ifdef GET_DAGISEL_DECL\n";
1755 OS << "#undef GET_DAGISEL_DECL\n";
1756 OS << "#endif\n";
1757 OS << "#ifdef GET_DAGISEL_BODY\n";
1758 OS << "#undef GET_DAGISEL_BODY\n";
1759 OS << "#endif\n";
1760}
1761