DAGISelMatcherEmitter.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains code to generate C++ code for a matcher.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "DAGISelMatcher.h"
  14. #include "CodeGenDAGPatterns.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/FormattedStream.h"
  20. #include "llvm/TableGen/Record.h"
  21. using namespace llvm;
  22. enum {
  23. CommentIndent = 30
  24. };
  25. // To reduce generated source code size.
  26. static cl::opt<bool>
  27. OmitComments("omit-comments", cl::desc("Do not generate comments"),
  28. cl::init(false));
  29. namespace {
  30. class MatcherTableEmitter {
  31. const CodeGenDAGPatterns &CGP;
  32. DenseMap<TreePattern *, unsigned> NodePredicateMap;
  33. std::vector<TreePredicateFn> NodePredicates;
  34. StringMap<unsigned> PatternPredicateMap;
  35. std::vector<std::string> PatternPredicates;
  36. DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
  37. std::vector<const ComplexPattern*> ComplexPatterns;
  38. DenseMap<Record*, unsigned> NodeXFormMap;
  39. std::vector<Record*> NodeXForms;
  40. public:
  41. MatcherTableEmitter(const CodeGenDAGPatterns &cgp)
  42. : CGP(cgp) {}
  43. unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
  44. unsigned StartIdx, formatted_raw_ostream &OS);
  45. void EmitPredicateFunctions(formatted_raw_ostream &OS);
  46. void EmitHistogram(const Matcher *N, formatted_raw_ostream &OS);
  47. private:
  48. unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
  49. formatted_raw_ostream &OS);
  50. unsigned getNodePredicate(TreePredicateFn Pred) {
  51. unsigned &Entry = NodePredicateMap[Pred.getOrigPatFragRecord()];
  52. if (Entry == 0) {
  53. NodePredicates.push_back(Pred);
  54. Entry = NodePredicates.size();
  55. }
  56. return Entry-1;
  57. }
  58. unsigned getPatternPredicate(StringRef PredName) {
  59. unsigned &Entry = PatternPredicateMap[PredName];
  60. if (Entry == 0) {
  61. PatternPredicates.push_back(PredName.str());
  62. Entry = PatternPredicates.size();
  63. }
  64. return Entry-1;
  65. }
  66. unsigned getComplexPat(const ComplexPattern &P) {
  67. unsigned &Entry = ComplexPatternMap[&P];
  68. if (Entry == 0) {
  69. ComplexPatterns.push_back(&P);
  70. Entry = ComplexPatterns.size();
  71. }
  72. return Entry-1;
  73. }
  74. unsigned getNodeXFormID(Record *Rec) {
  75. unsigned &Entry = NodeXFormMap[Rec];
  76. if (Entry == 0) {
  77. NodeXForms.push_back(Rec);
  78. Entry = NodeXForms.size();
  79. }
  80. return Entry-1;
  81. }
  82. };
  83. } // end anonymous namespace.
  84. static unsigned GetVBRSize(unsigned Val) {
  85. if (Val <= 127) return 1;
  86. unsigned NumBytes = 0;
  87. while (Val >= 128) {
  88. Val >>= 7;
  89. ++NumBytes;
  90. }
  91. return NumBytes+1;
  92. }
  93. /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
  94. /// bytes emitted.
  95. static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
  96. if (Val <= 127) {
  97. OS << Val << ", ";
  98. return 1;
  99. }
  100. uint64_t InVal = Val;
  101. unsigned NumBytes = 0;
  102. while (Val >= 128) {
  103. OS << (Val&127) << "|128,";
  104. Val >>= 7;
  105. ++NumBytes;
  106. }
  107. OS << Val;
  108. if (!OmitComments)
  109. OS << "/*" << InVal << "*/";
  110. OS << ", ";
  111. return NumBytes+1;
  112. }
  113. /// EmitMatcher - Emit bytes for the specified matcher and return
  114. /// the number of bytes emitted.
  115. unsigned MatcherTableEmitter::
  116. EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
  117. formatted_raw_ostream &OS) {
  118. OS.PadToColumn(Indent*2);
  119. switch (N->getKind()) {
  120. case Matcher::Scope: {
  121. const ScopeMatcher *SM = cast<ScopeMatcher>(N);
  122. assert(SM->getNext() == nullptr && "Shouldn't have next after scope");
  123. unsigned StartIdx = CurrentIdx;
  124. // Emit all of the children.
  125. for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
  126. if (i == 0) {
  127. OS << "OPC_Scope, ";
  128. ++CurrentIdx;
  129. } else {
  130. if (!OmitComments) {
  131. OS << "/*" << CurrentIdx << "*/";
  132. OS.PadToColumn(Indent*2) << "/*Scope*/ ";
  133. } else
  134. OS.PadToColumn(Indent*2);
  135. }
  136. // We need to encode the child and the offset of the failure code before
  137. // emitting either of them. Handle this by buffering the output into a
  138. // string while we get the size. Unfortunately, the offset of the
  139. // children depends on the VBR size of the child, so for large children we
  140. // have to iterate a bit.
  141. SmallString<128> TmpBuf;
  142. unsigned ChildSize = 0;
  143. unsigned VBRSize = 0;
  144. do {
  145. VBRSize = GetVBRSize(ChildSize);
  146. TmpBuf.clear();
  147. raw_svector_ostream OS(TmpBuf);
  148. formatted_raw_ostream FOS(OS);
  149. ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
  150. CurrentIdx+VBRSize, FOS);
  151. } while (GetVBRSize(ChildSize) != VBRSize);
  152. assert(ChildSize != 0 && "Should not have a zero-sized child!");
  153. CurrentIdx += EmitVBRValue(ChildSize, OS);
  154. if (!OmitComments) {
  155. OS << "/*->" << CurrentIdx+ChildSize << "*/";
  156. if (i == 0)
  157. OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
  158. << " children in Scope";
  159. }
  160. OS << '\n' << TmpBuf;
  161. CurrentIdx += ChildSize;
  162. }
  163. // Emit a zero as a sentinel indicating end of 'Scope'.
  164. if (!OmitComments)
  165. OS << "/*" << CurrentIdx << "*/";
  166. OS.PadToColumn(Indent*2) << "0, ";
  167. if (!OmitComments)
  168. OS << "/*End of Scope*/";
  169. OS << '\n';
  170. return CurrentIdx - StartIdx + 1;
  171. }
  172. case Matcher::RecordNode:
  173. OS << "OPC_RecordNode,";
  174. if (!OmitComments)
  175. OS.PadToColumn(CommentIndent) << "// #"
  176. << cast<RecordMatcher>(N)->getResultNo() << " = "
  177. << cast<RecordMatcher>(N)->getWhatFor();
  178. OS << '\n';
  179. return 1;
  180. case Matcher::RecordChild:
  181. OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
  182. << ',';
  183. if (!OmitComments)
  184. OS.PadToColumn(CommentIndent) << "// #"
  185. << cast<RecordChildMatcher>(N)->getResultNo() << " = "
  186. << cast<RecordChildMatcher>(N)->getWhatFor();
  187. OS << '\n';
  188. return 1;
  189. case Matcher::RecordMemRef:
  190. OS << "OPC_RecordMemRef,\n";
  191. return 1;
  192. case Matcher::CaptureGlueInput:
  193. OS << "OPC_CaptureGlueInput,\n";
  194. return 1;
  195. case Matcher::MoveChild:
  196. OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
  197. return 2;
  198. case Matcher::MoveParent:
  199. OS << "OPC_MoveParent,\n";
  200. return 1;
  201. case Matcher::CheckSame:
  202. OS << "OPC_CheckSame, "
  203. << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
  204. return 2;
  205. case Matcher::CheckChildSame:
  206. OS << "OPC_CheckChild"
  207. << cast<CheckChildSameMatcher>(N)->getChildNo() << "Same, "
  208. << cast<CheckChildSameMatcher>(N)->getMatchNumber() << ",\n";
  209. return 2;
  210. case Matcher::CheckPatternPredicate: {
  211. StringRef Pred =cast<CheckPatternPredicateMatcher>(N)->getPredicate();
  212. OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
  213. if (!OmitComments)
  214. OS.PadToColumn(CommentIndent) << "// " << Pred;
  215. OS << '\n';
  216. return 2;
  217. }
  218. case Matcher::CheckPredicate: {
  219. TreePredicateFn Pred = cast<CheckPredicateMatcher>(N)->getPredicate();
  220. OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
  221. if (!OmitComments)
  222. OS.PadToColumn(CommentIndent) << "// " << Pred.getFnName();
  223. OS << '\n';
  224. return 2;
  225. }
  226. case Matcher::CheckOpcode:
  227. OS << "OPC_CheckOpcode, TARGET_VAL("
  228. << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << "),\n";
  229. return 3;
  230. case Matcher::SwitchOpcode:
  231. case Matcher::SwitchType: {
  232. unsigned StartIdx = CurrentIdx;
  233. unsigned NumCases;
  234. if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
  235. OS << "OPC_SwitchOpcode ";
  236. NumCases = SOM->getNumCases();
  237. } else {
  238. OS << "OPC_SwitchType ";
  239. NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
  240. }
  241. if (!OmitComments)
  242. OS << "/*" << NumCases << " cases */";
  243. OS << ", ";
  244. ++CurrentIdx;
  245. // For each case we emit the size, then the opcode, then the matcher.
  246. for (unsigned i = 0, e = NumCases; i != e; ++i) {
  247. const Matcher *Child;
  248. unsigned IdxSize;
  249. if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
  250. Child = SOM->getCaseMatcher(i);
  251. IdxSize = 2; // size of opcode in table is 2 bytes.
  252. } else {
  253. Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
  254. IdxSize = 1; // size of type in table is 1 byte.
  255. }
  256. // We need to encode the opcode and the offset of the case code before
  257. // emitting the case code. Handle this by buffering the output into a
  258. // string while we get the size. Unfortunately, the offset of the
  259. // children depends on the VBR size of the child, so for large children we
  260. // have to iterate a bit.
  261. SmallString<128> TmpBuf;
  262. unsigned ChildSize = 0;
  263. unsigned VBRSize = 0;
  264. do {
  265. VBRSize = GetVBRSize(ChildSize);
  266. TmpBuf.clear();
  267. raw_svector_ostream OS(TmpBuf);
  268. formatted_raw_ostream FOS(OS);
  269. ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+IdxSize,
  270. FOS);
  271. } while (GetVBRSize(ChildSize) != VBRSize);
  272. assert(ChildSize != 0 && "Should not have a zero-sized child!");
  273. if (i != 0) {
  274. if (!OmitComments)
  275. OS << "/*" << CurrentIdx << "*/";
  276. OS.PadToColumn(Indent*2);
  277. if (!OmitComments)
  278. OS << (isa<SwitchOpcodeMatcher>(N) ?
  279. "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
  280. }
  281. // Emit the VBR.
  282. CurrentIdx += EmitVBRValue(ChildSize, OS);
  283. if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
  284. OS << "TARGET_VAL(" << SOM->getCaseOpcode(i).getEnumName() << "),";
  285. else
  286. OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i)) << ',';
  287. CurrentIdx += IdxSize;
  288. if (!OmitComments)
  289. OS << "// ->" << CurrentIdx+ChildSize;
  290. OS << '\n';
  291. OS << TmpBuf;
  292. CurrentIdx += ChildSize;
  293. }
  294. // Emit the final zero to terminate the switch.
  295. if (!OmitComments)
  296. OS << "/*" << CurrentIdx << "*/";
  297. OS.PadToColumn(Indent*2) << "0, ";
  298. if (!OmitComments)
  299. OS << (isa<SwitchOpcodeMatcher>(N) ?
  300. "// EndSwitchOpcode" : "// EndSwitchType");
  301. OS << '\n';
  302. ++CurrentIdx;
  303. return CurrentIdx-StartIdx;
  304. }
  305. case Matcher::CheckType:
  306. assert(cast<CheckTypeMatcher>(N)->getResNo() == 0 &&
  307. "FIXME: Add support for CheckType of resno != 0");
  308. OS << "OPC_CheckType, "
  309. << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
  310. return 2;
  311. case Matcher::CheckChildType:
  312. OS << "OPC_CheckChild"
  313. << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
  314. << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
  315. return 2;
  316. case Matcher::CheckInteger: {
  317. OS << "OPC_CheckInteger, ";
  318. unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
  319. OS << '\n';
  320. return Bytes;
  321. }
  322. case Matcher::CheckChildInteger: {
  323. OS << "OPC_CheckChild" << cast<CheckChildIntegerMatcher>(N)->getChildNo()
  324. << "Integer, ";
  325. unsigned Bytes=1+EmitVBRValue(cast<CheckChildIntegerMatcher>(N)->getValue(),
  326. OS);
  327. OS << '\n';
  328. return Bytes;
  329. }
  330. case Matcher::CheckCondCode:
  331. OS << "OPC_CheckCondCode, ISD::"
  332. << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
  333. return 2;
  334. case Matcher::CheckValueType:
  335. OS << "OPC_CheckValueType, MVT::"
  336. << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
  337. return 2;
  338. case Matcher::CheckComplexPat: {
  339. const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
  340. const ComplexPattern &Pattern = CCPM->getPattern();
  341. OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
  342. << CCPM->getMatchNumber() << ',';
  343. if (!OmitComments) {
  344. OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
  345. OS << ":$" << CCPM->getName();
  346. for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
  347. OS << " #" << CCPM->getFirstResult()+i;
  348. if (Pattern.hasProperty(SDNPHasChain))
  349. OS << " + chain result";
  350. }
  351. OS << '\n';
  352. return 3;
  353. }
  354. case Matcher::CheckAndImm: {
  355. OS << "OPC_CheckAndImm, ";
  356. unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
  357. OS << '\n';
  358. return Bytes;
  359. }
  360. case Matcher::CheckOrImm: {
  361. OS << "OPC_CheckOrImm, ";
  362. unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
  363. OS << '\n';
  364. return Bytes;
  365. }
  366. case Matcher::CheckFoldableChainNode:
  367. OS << "OPC_CheckFoldableChainNode,\n";
  368. return 1;
  369. case Matcher::EmitInteger: {
  370. int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
  371. OS << "OPC_EmitInteger, "
  372. << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
  373. unsigned Bytes = 2+EmitVBRValue(Val, OS);
  374. OS << '\n';
  375. return Bytes;
  376. }
  377. case Matcher::EmitStringInteger: {
  378. const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
  379. // These should always fit into one byte.
  380. OS << "OPC_EmitInteger, "
  381. << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
  382. << Val << ",\n";
  383. return 3;
  384. }
  385. case Matcher::EmitRegister: {
  386. const EmitRegisterMatcher *Matcher = cast<EmitRegisterMatcher>(N);
  387. const CodeGenRegister *Reg = Matcher->getReg();
  388. // If the enum value of the register is larger than one byte can handle,
  389. // use EmitRegister2.
  390. if (Reg && Reg->EnumValue > 255) {
  391. OS << "OPC_EmitRegister2, " << getEnumName(Matcher->getVT()) << ", ";
  392. OS << "TARGET_VAL(" << getQualifiedName(Reg->TheDef) << "),\n";
  393. return 4;
  394. } else {
  395. OS << "OPC_EmitRegister, " << getEnumName(Matcher->getVT()) << ", ";
  396. if (Reg) {
  397. OS << getQualifiedName(Reg->TheDef) << ",\n";
  398. } else {
  399. OS << "0 ";
  400. if (!OmitComments)
  401. OS << "/*zero_reg*/";
  402. OS << ",\n";
  403. }
  404. return 3;
  405. }
  406. }
  407. case Matcher::EmitConvertToTarget:
  408. OS << "OPC_EmitConvertToTarget, "
  409. << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
  410. return 2;
  411. case Matcher::EmitMergeInputChains: {
  412. const EmitMergeInputChainsMatcher *MN =
  413. cast<EmitMergeInputChainsMatcher>(N);
  414. // Handle the specialized forms OPC_EmitMergeInputChains1_0 and 1_1.
  415. if (MN->getNumNodes() == 1 && MN->getNode(0) < 2) {
  416. OS << "OPC_EmitMergeInputChains1_" << MN->getNode(0) << ",\n";
  417. return 1;
  418. }
  419. OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
  420. for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
  421. OS << MN->getNode(i) << ", ";
  422. OS << '\n';
  423. return 2+MN->getNumNodes();
  424. }
  425. case Matcher::EmitCopyToReg:
  426. OS << "OPC_EmitCopyToReg, "
  427. << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
  428. << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
  429. << ",\n";
  430. return 3;
  431. case Matcher::EmitNodeXForm: {
  432. const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
  433. OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
  434. << XF->getSlot() << ',';
  435. if (!OmitComments)
  436. OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
  437. OS <<'\n';
  438. return 3;
  439. }
  440. case Matcher::EmitNode:
  441. case Matcher::MorphNodeTo: {
  442. const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
  443. OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
  444. OS << ", TARGET_VAL(" << EN->getOpcodeName() << "), 0";
  445. if (EN->hasChain()) OS << "|OPFL_Chain";
  446. if (EN->hasInFlag()) OS << "|OPFL_GlueInput";
  447. if (EN->hasOutFlag()) OS << "|OPFL_GlueOutput";
  448. if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
  449. if (EN->getNumFixedArityOperands() != -1)
  450. OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
  451. OS << ",\n";
  452. OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
  453. if (!OmitComments)
  454. OS << "/*#VTs*/";
  455. OS << ", ";
  456. for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
  457. OS << getEnumName(EN->getVT(i)) << ", ";
  458. OS << EN->getNumOperands();
  459. if (!OmitComments)
  460. OS << "/*#Ops*/";
  461. OS << ", ";
  462. unsigned NumOperandBytes = 0;
  463. for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
  464. NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
  465. if (!OmitComments) {
  466. // Print the result #'s for EmitNode.
  467. if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
  468. if (unsigned NumResults = EN->getNumVTs()) {
  469. OS.PadToColumn(CommentIndent) << "// Results =";
  470. unsigned First = E->getFirstResultSlot();
  471. for (unsigned i = 0; i != NumResults; ++i)
  472. OS << " #" << First+i;
  473. }
  474. }
  475. OS << '\n';
  476. if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
  477. OS.PadToColumn(Indent*2) << "// Src: "
  478. << *SNT->getPattern().getSrcPattern() << " - Complexity = "
  479. << SNT->getPattern().getPatternComplexity(CGP) << '\n';
  480. OS.PadToColumn(Indent*2) << "// Dst: "
  481. << *SNT->getPattern().getDstPattern() << '\n';
  482. }
  483. } else
  484. OS << '\n';
  485. return 6+EN->getNumVTs()+NumOperandBytes;
  486. }
  487. case Matcher::MarkGlueResults: {
  488. const MarkGlueResultsMatcher *CFR = cast<MarkGlueResultsMatcher>(N);
  489. OS << "OPC_MarkGlueResults, " << CFR->getNumNodes() << ", ";
  490. unsigned NumOperandBytes = 0;
  491. for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
  492. NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
  493. OS << '\n';
  494. return 2+NumOperandBytes;
  495. }
  496. case Matcher::CompleteMatch: {
  497. const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
  498. OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
  499. unsigned NumResultBytes = 0;
  500. for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
  501. NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
  502. OS << '\n';
  503. if (!OmitComments) {
  504. OS.PadToColumn(Indent*2) << "// Src: "
  505. << *CM->getPattern().getSrcPattern() << " - Complexity = "
  506. << CM->getPattern().getPatternComplexity(CGP) << '\n';
  507. OS.PadToColumn(Indent*2) << "// Dst: "
  508. << *CM->getPattern().getDstPattern();
  509. }
  510. OS << '\n';
  511. return 2 + NumResultBytes;
  512. }
  513. }
  514. llvm_unreachable("Unreachable");
  515. }
  516. /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
  517. unsigned MatcherTableEmitter::
  518. EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
  519. formatted_raw_ostream &OS) {
  520. unsigned Size = 0;
  521. while (N) {
  522. if (!OmitComments)
  523. OS << "/*" << CurrentIdx << "*/";
  524. unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
  525. Size += MatcherSize;
  526. CurrentIdx += MatcherSize;
  527. // If there are other nodes in this list, iterate to them, otherwise we're
  528. // done.
  529. N = N->getNext();
  530. }
  531. return Size;
  532. }
  533. void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
  534. // Emit pattern predicates.
  535. if (!PatternPredicates.empty()) {
  536. OS << "bool CheckPatternPredicate(unsigned PredNo) const override {\n";
  537. OS << " switch (PredNo) {\n";
  538. OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
  539. for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
  540. OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
  541. OS << " }\n";
  542. OS << "}\n\n";
  543. }
  544. // Emit Node predicates.
  545. // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
  546. StringMap<TreePattern*> PFsByName;
  547. for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
  548. I != E; ++I)
  549. PFsByName[I->first->getName()] = I->second.get();
  550. if (!NodePredicates.empty()) {
  551. OS << "bool CheckNodePredicate(SDNode *Node,\n";
  552. OS << " unsigned PredNo) const override {\n";
  553. OS << " switch (PredNo) {\n";
  554. OS << " default: llvm_unreachable(\"Invalid predicate in table?\");\n";
  555. for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
  556. // Emit the predicate code corresponding to this pattern.
  557. TreePredicateFn PredFn = NodePredicates[i];
  558. assert(!PredFn.isAlwaysTrue() && "No code in this predicate");
  559. OS << " case " << i << ": { // " << NodePredicates[i].getFnName() <<'\n';
  560. OS << PredFn.getCodeToRunOnSDNode() << "\n }\n";
  561. }
  562. OS << " }\n";
  563. OS << "}\n\n";
  564. }
  565. // Emit CompletePattern matchers.
  566. // FIXME: This should be const.
  567. if (!ComplexPatterns.empty()) {
  568. OS << "bool CheckComplexPattern(SDNode *Root, SDNode *Parent,\n";
  569. OS << " SDValue N, unsigned PatternNo,\n";
  570. OS << " SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) override {\n";
  571. OS << " unsigned NextRes = Result.size();\n";
  572. OS << " switch (PatternNo) {\n";
  573. OS << " default: llvm_unreachable(\"Invalid pattern # in table?\");\n";
  574. for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
  575. const ComplexPattern &P = *ComplexPatterns[i];
  576. unsigned NumOps = P.getNumOperands();
  577. if (P.hasProperty(SDNPHasChain))
  578. ++NumOps; // Get the chained node too.
  579. OS << " case " << i << ":\n";
  580. OS << " Result.resize(NextRes+" << NumOps << ");\n";
  581. OS << " return " << P.getSelectFunc();
  582. OS << "(";
  583. // If the complex pattern wants the root of the match, pass it in as the
  584. // first argument.
  585. if (P.hasProperty(SDNPWantRoot))
  586. OS << "Root, ";
  587. // If the complex pattern wants the parent of the operand being matched,
  588. // pass it in as the next argument.
  589. if (P.hasProperty(SDNPWantParent))
  590. OS << "Parent, ";
  591. OS << "N";
  592. for (unsigned i = 0; i != NumOps; ++i)
  593. OS << ", Result[NextRes+" << i << "].first";
  594. OS << ");\n";
  595. }
  596. OS << " }\n";
  597. OS << "}\n\n";
  598. }
  599. // Emit SDNodeXForm handlers.
  600. // FIXME: This should be const.
  601. if (!NodeXForms.empty()) {
  602. OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) override {\n";
  603. OS << " switch (XFormNo) {\n";
  604. OS << " default: llvm_unreachable(\"Invalid xform # in table?\");\n";
  605. // FIXME: The node xform could take SDValue's instead of SDNode*'s.
  606. for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
  607. const CodeGenDAGPatterns::NodeXForm &Entry =
  608. CGP.getSDNodeTransform(NodeXForms[i]);
  609. Record *SDNode = Entry.first;
  610. const std::string &Code = Entry.second;
  611. OS << " case " << i << ": { ";
  612. if (!OmitComments)
  613. OS << "// " << NodeXForms[i]->getName();
  614. OS << '\n';
  615. std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
  616. if (ClassName == "SDNode")
  617. OS << " SDNode *N = V.getNode();\n";
  618. else
  619. OS << " " << ClassName << " *N = cast<" << ClassName
  620. << ">(V.getNode());\n";
  621. OS << Code << "\n }\n";
  622. }
  623. OS << " }\n";
  624. OS << "}\n\n";
  625. }
  626. }
  627. static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
  628. for (; M != nullptr; M = M->getNext()) {
  629. // Count this node.
  630. if (unsigned(M->getKind()) >= OpcodeFreq.size())
  631. OpcodeFreq.resize(M->getKind()+1);
  632. OpcodeFreq[M->getKind()]++;
  633. // Handle recursive nodes.
  634. if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
  635. for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
  636. BuildHistogram(SM->getChild(i), OpcodeFreq);
  637. } else if (const SwitchOpcodeMatcher *SOM =
  638. dyn_cast<SwitchOpcodeMatcher>(M)) {
  639. for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
  640. BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
  641. } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
  642. for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
  643. BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
  644. }
  645. }
  646. }
  647. void MatcherTableEmitter::EmitHistogram(const Matcher *M,
  648. formatted_raw_ostream &OS) {
  649. if (OmitComments)
  650. return;
  651. std::vector<unsigned> OpcodeFreq;
  652. BuildHistogram(M, OpcodeFreq);
  653. OS << " // Opcode Histogram:\n";
  654. for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
  655. OS << " // #";
  656. switch ((Matcher::KindTy)i) {
  657. case Matcher::Scope: OS << "OPC_Scope"; break;
  658. case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
  659. case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
  660. case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
  661. case Matcher::CaptureGlueInput: OS << "OPC_CaptureGlueInput"; break;
  662. case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
  663. case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
  664. case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
  665. case Matcher::CheckChildSame: OS << "OPC_CheckChildSame"; break;
  666. case Matcher::CheckPatternPredicate:
  667. OS << "OPC_CheckPatternPredicate"; break;
  668. case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
  669. case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
  670. case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
  671. case Matcher::CheckType: OS << "OPC_CheckType"; break;
  672. case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
  673. case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
  674. case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
  675. case Matcher::CheckChildInteger: OS << "OPC_CheckChildInteger"; break;
  676. case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
  677. case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
  678. case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
  679. case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
  680. case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
  681. case Matcher::CheckFoldableChainNode:
  682. OS << "OPC_CheckFoldableChainNode"; break;
  683. case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
  684. case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
  685. case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
  686. case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
  687. case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
  688. case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
  689. case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
  690. case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
  691. case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
  692. case Matcher::MarkGlueResults: OS << "OPC_MarkGlueResults"; break;
  693. case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
  694. }
  695. OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
  696. }
  697. OS << '\n';
  698. }
  699. void llvm::EmitMatcherTable(const Matcher *TheMatcher,
  700. const CodeGenDAGPatterns &CGP,
  701. raw_ostream &O) {
  702. formatted_raw_ostream OS(O);
  703. OS << "// The main instruction selector code.\n";
  704. OS << "SDNode *SelectCode(SDNode *N) {\n";
  705. MatcherTableEmitter MatcherEmitter(CGP);
  706. OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
  707. OS << " // this.\n";
  708. OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
  709. OS << " static const unsigned char MatcherTable[] = {\n";
  710. unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 6, 0, OS);
  711. OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
  712. MatcherEmitter.EmitHistogram(TheMatcher, OS);
  713. OS << " #undef TARGET_VAL\n";
  714. OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
  715. OS << '\n';
  716. // Next up, emit the function for node and pattern predicates:
  717. MatcherEmitter.EmitPredicateFunctions(OS);
  718. }