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