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