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