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