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