DAGISelMatcherOpt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //===- DAGISelMatcherOpt.cpp - Optimize a DAG Matcher ---------------------===//
  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 implements the DAG Matcher optimizer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "DAGISelMatcher.h"
  14. #include "CodeGenDAGPatterns.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/StringSet.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "isel-opt"
  21. /// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
  22. /// into single compound nodes like RecordChild.
  23. static void ContractNodes(std::unique_ptr<Matcher> &MatcherPtr,
  24. const CodeGenDAGPatterns &CGP) {
  25. // If we reached the end of the chain, we're done.
  26. Matcher *N = MatcherPtr.get();
  27. if (!N) return;
  28. // If we have a scope node, walk down all of the children.
  29. if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
  30. for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
  31. std::unique_ptr<Matcher> Child(Scope->takeChild(i));
  32. ContractNodes(Child, CGP);
  33. Scope->resetChild(i, Child.release());
  34. }
  35. return;
  36. }
  37. // If we found a movechild node with a node that comes in a 'foochild' form,
  38. // transform it.
  39. if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N)) {
  40. Matcher *New = nullptr;
  41. if (RecordMatcher *RM = dyn_cast<RecordMatcher>(MC->getNext()))
  42. if (MC->getChildNo() < 8) // Only have RecordChild0...7
  43. New = new RecordChildMatcher(MC->getChildNo(), RM->getWhatFor(),
  44. RM->getResultNo());
  45. if (CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(MC->getNext()))
  46. if (MC->getChildNo() < 8 && // Only have CheckChildType0...7
  47. CT->getResNo() == 0) // CheckChildType checks res #0
  48. New = new CheckChildTypeMatcher(MC->getChildNo(), CT->getType());
  49. if (CheckSameMatcher *CS = dyn_cast<CheckSameMatcher>(MC->getNext()))
  50. if (MC->getChildNo() < 4) // Only have CheckChildSame0...3
  51. New = new CheckChildSameMatcher(MC->getChildNo(), CS->getMatchNumber());
  52. if (CheckIntegerMatcher *CS = dyn_cast<CheckIntegerMatcher>(MC->getNext()))
  53. if (MC->getChildNo() < 5) // Only have CheckChildInteger0...4
  54. New = new CheckChildIntegerMatcher(MC->getChildNo(), CS->getValue());
  55. if (New) {
  56. // Insert the new node.
  57. New->setNext(MatcherPtr.release());
  58. MatcherPtr.reset(New);
  59. // Remove the old one.
  60. MC->setNext(MC->getNext()->takeNext());
  61. return ContractNodes(MatcherPtr, CGP);
  62. }
  63. }
  64. // Zap movechild -> moveparent.
  65. if (MoveChildMatcher *MC = dyn_cast<MoveChildMatcher>(N))
  66. if (MoveParentMatcher *MP =
  67. dyn_cast<MoveParentMatcher>(MC->getNext())) {
  68. MatcherPtr.reset(MP->takeNext());
  69. return ContractNodes(MatcherPtr, CGP);
  70. }
  71. // Turn EmitNode->MarkFlagResults->CompleteMatch into
  72. // MarkFlagResults->EmitNode->CompleteMatch when we can to encourage
  73. // MorphNodeTo formation. This is safe because MarkFlagResults never refers
  74. // to the root of the pattern.
  75. if (isa<EmitNodeMatcher>(N) && isa<MarkGlueResultsMatcher>(N->getNext()) &&
  76. isa<CompleteMatchMatcher>(N->getNext()->getNext())) {
  77. // Unlink the two nodes from the list.
  78. Matcher *EmitNode = MatcherPtr.release();
  79. Matcher *MFR = EmitNode->takeNext();
  80. Matcher *Tail = MFR->takeNext();
  81. // Relink them.
  82. MatcherPtr.reset(MFR);
  83. MFR->setNext(EmitNode);
  84. EmitNode->setNext(Tail);
  85. return ContractNodes(MatcherPtr, CGP);
  86. }
  87. // Turn EmitNode->CompleteMatch into MorphNodeTo if we can.
  88. if (EmitNodeMatcher *EN = dyn_cast<EmitNodeMatcher>(N))
  89. if (CompleteMatchMatcher *CM =
  90. dyn_cast<CompleteMatchMatcher>(EN->getNext())) {
  91. // We can only use MorphNodeTo if the result values match up.
  92. unsigned RootResultFirst = EN->getFirstResultSlot();
  93. bool ResultsMatch = true;
  94. for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
  95. if (CM->getResult(i) != RootResultFirst+i)
  96. ResultsMatch = false;
  97. // If the selected node defines a subset of the glue/chain results, we
  98. // can't use MorphNodeTo. For example, we can't use MorphNodeTo if the
  99. // matched pattern has a chain but the root node doesn't.
  100. const PatternToMatch &Pattern = CM->getPattern();
  101. if (!EN->hasChain() &&
  102. Pattern.getSrcPattern()->NodeHasProperty(SDNPHasChain, CGP))
  103. ResultsMatch = false;
  104. // If the matched node has glue and the output root doesn't, we can't
  105. // use MorphNodeTo.
  106. //
  107. // NOTE: Strictly speaking, we don't have to check for glue here
  108. // because the code in the pattern generator doesn't handle it right. We
  109. // do it anyway for thoroughness.
  110. if (!EN->hasOutFlag() &&
  111. Pattern.getSrcPattern()->NodeHasProperty(SDNPOutGlue, CGP))
  112. ResultsMatch = false;
  113. // If the root result node defines more results than the source root node
  114. // *and* has a chain or glue input, then we can't match it because it
  115. // would end up replacing the extra result with the chain/glue.
  116. #if 0
  117. if ((EN->hasGlue() || EN->hasChain()) &&
  118. EN->getNumNonChainGlueVTs() > ... need to get no results reliably ...)
  119. ResultMatch = false;
  120. #endif
  121. if (ResultsMatch) {
  122. const SmallVectorImpl<MVT::SimpleValueType> &VTs = EN->getVTList();
  123. const SmallVectorImpl<unsigned> &Operands = EN->getOperandList();
  124. MatcherPtr.reset(new MorphNodeToMatcher(EN->getOpcodeName(),
  125. VTs, Operands,
  126. EN->hasChain(), EN->hasInFlag(),
  127. EN->hasOutFlag(),
  128. EN->hasMemRefs(),
  129. EN->getNumFixedArityOperands(),
  130. Pattern));
  131. return;
  132. }
  133. // FIXME2: Kill off all the SelectionDAG::SelectNodeTo and getMachineNode
  134. // variants.
  135. }
  136. ContractNodes(N->getNextPtr(), CGP);
  137. // If we have a CheckType/CheckChildType/Record node followed by a
  138. // CheckOpcode, invert the two nodes. We prefer to do structural checks
  139. // before type checks, as this opens opportunities for factoring on targets
  140. // like X86 where many operations are valid on multiple types.
  141. if ((isa<CheckTypeMatcher>(N) || isa<CheckChildTypeMatcher>(N) ||
  142. isa<RecordMatcher>(N)) &&
  143. isa<CheckOpcodeMatcher>(N->getNext())) {
  144. // Unlink the two nodes from the list.
  145. Matcher *CheckType = MatcherPtr.release();
  146. Matcher *CheckOpcode = CheckType->takeNext();
  147. Matcher *Tail = CheckOpcode->takeNext();
  148. // Relink them.
  149. MatcherPtr.reset(CheckOpcode);
  150. CheckOpcode->setNext(CheckType);
  151. CheckType->setNext(Tail);
  152. return ContractNodes(MatcherPtr, CGP);
  153. }
  154. }
  155. /// SinkPatternPredicates - Pattern predicates can be checked at any level of
  156. /// the matching tree. The generator dumps them at the top level of the pattern
  157. /// though, which prevents factoring from being able to see past them. This
  158. /// optimization sinks them as far down into the pattern as possible.
  159. ///
  160. /// Conceptually, we'd like to sink these predicates all the way to the last
  161. /// matcher predicate in the series. However, it turns out that some
  162. /// ComplexPatterns have side effects on the graph, so we really don't want to
  163. /// run a complex pattern if the pattern predicate will fail. For this
  164. /// reason, we refuse to sink the pattern predicate past a ComplexPattern.
  165. ///
  166. static void SinkPatternPredicates(std::unique_ptr<Matcher> &MatcherPtr) {
  167. // Recursively scan for a PatternPredicate.
  168. // If we reached the end of the chain, we're done.
  169. Matcher *N = MatcherPtr.get();
  170. if (!N) return;
  171. // Walk down all members of a scope node.
  172. if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
  173. for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
  174. std::unique_ptr<Matcher> Child(Scope->takeChild(i));
  175. SinkPatternPredicates(Child);
  176. Scope->resetChild(i, Child.release());
  177. }
  178. return;
  179. }
  180. // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
  181. // we find one.
  182. CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
  183. if (!CPPM)
  184. return SinkPatternPredicates(N->getNextPtr());
  185. // Ok, we found one, lets try to sink it. Check if we can sink it past the
  186. // next node in the chain. If not, we won't be able to change anything and
  187. // might as well bail.
  188. if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
  189. return;
  190. // Okay, we know we can sink it past at least one node. Unlink it from the
  191. // chain and scan for the new insertion point.
  192. MatcherPtr.release(); // Don't delete CPPM.
  193. MatcherPtr.reset(CPPM->takeNext());
  194. N = MatcherPtr.get();
  195. while (N->getNext()->isSafeToReorderWithPatternPredicate())
  196. N = N->getNext();
  197. // At this point, we want to insert CPPM after N.
  198. CPPM->setNext(N->takeNext());
  199. N->setNext(CPPM);
  200. }
  201. /// FindNodeWithKind - Scan a series of matchers looking for a matcher with a
  202. /// specified kind. Return null if we didn't find one otherwise return the
  203. /// matcher.
  204. static Matcher *FindNodeWithKind(Matcher *M, Matcher::KindTy Kind) {
  205. for (; M; M = M->getNext())
  206. if (M->getKind() == Kind)
  207. return M;
  208. return nullptr;
  209. }
  210. /// FactorNodes - Turn matches like this:
  211. /// Scope
  212. /// OPC_CheckType i32
  213. /// ABC
  214. /// OPC_CheckType i32
  215. /// XYZ
  216. /// into:
  217. /// OPC_CheckType i32
  218. /// Scope
  219. /// ABC
  220. /// XYZ
  221. ///
  222. static void FactorNodes(std::unique_ptr<Matcher> &MatcherPtr) {
  223. // If we reached the end of the chain, we're done.
  224. Matcher *N = MatcherPtr.get();
  225. if (!N) return;
  226. // If this is not a push node, just scan for one.
  227. ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N);
  228. if (!Scope)
  229. return FactorNodes(N->getNextPtr());
  230. // Okay, pull together the children of the scope node into a vector so we can
  231. // inspect it more easily. While we're at it, bucket them up by the hash
  232. // code of their first predicate.
  233. SmallVector<Matcher*, 32> OptionsToMatch;
  234. for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
  235. // Factor the subexpression.
  236. std::unique_ptr<Matcher> Child(Scope->takeChild(i));
  237. FactorNodes(Child);
  238. if (Matcher *N = Child.release())
  239. OptionsToMatch.push_back(N);
  240. }
  241. SmallVector<Matcher*, 32> NewOptionsToMatch;
  242. // Loop over options to match, merging neighboring patterns with identical
  243. // starting nodes into a shared matcher.
  244. for (unsigned OptionIdx = 0, e = OptionsToMatch.size(); OptionIdx != e;) {
  245. // Find the set of matchers that start with this node.
  246. Matcher *Optn = OptionsToMatch[OptionIdx++];
  247. if (OptionIdx == e) {
  248. NewOptionsToMatch.push_back(Optn);
  249. continue;
  250. }
  251. // See if the next option starts with the same matcher. If the two
  252. // neighbors *do* start with the same matcher, we can factor the matcher out
  253. // of at least these two patterns. See what the maximal set we can merge
  254. // together is.
  255. SmallVector<Matcher*, 8> EqualMatchers;
  256. EqualMatchers.push_back(Optn);
  257. // Factor all of the known-equal matchers after this one into the same
  258. // group.
  259. while (OptionIdx != e && OptionsToMatch[OptionIdx]->isEqual(Optn))
  260. EqualMatchers.push_back(OptionsToMatch[OptionIdx++]);
  261. // If we found a non-equal matcher, see if it is contradictory with the
  262. // current node. If so, we know that the ordering relation between the
  263. // current sets of nodes and this node don't matter. Look past it to see if
  264. // we can merge anything else into this matching group.
  265. unsigned Scan = OptionIdx;
  266. while (1) {
  267. // If we ran out of stuff to scan, we're done.
  268. if (Scan == e) break;
  269. Matcher *ScanMatcher = OptionsToMatch[Scan];
  270. // If we found an entry that matches out matcher, merge it into the set to
  271. // handle.
  272. if (Optn->isEqual(ScanMatcher)) {
  273. // If is equal after all, add the option to EqualMatchers and remove it
  274. // from OptionsToMatch.
  275. EqualMatchers.push_back(ScanMatcher);
  276. OptionsToMatch.erase(OptionsToMatch.begin()+Scan);
  277. --e;
  278. continue;
  279. }
  280. // If the option we're checking for contradicts the start of the list,
  281. // skip over it.
  282. if (Optn->isContradictory(ScanMatcher)) {
  283. ++Scan;
  284. continue;
  285. }
  286. // If we're scanning for a simple node, see if it occurs later in the
  287. // sequence. If so, and if we can move it up, it might be contradictory
  288. // or the same as what we're looking for. If so, reorder it.
  289. if (Optn->isSimplePredicateOrRecordNode()) {
  290. Matcher *M2 = FindNodeWithKind(ScanMatcher, Optn->getKind());
  291. if (M2 && M2 != ScanMatcher &&
  292. M2->canMoveBefore(ScanMatcher) &&
  293. (M2->isEqual(Optn) || M2->isContradictory(Optn))) {
  294. Matcher *MatcherWithoutM2 = ScanMatcher->unlinkNode(M2);
  295. M2->setNext(MatcherWithoutM2);
  296. OptionsToMatch[Scan] = M2;
  297. continue;
  298. }
  299. }
  300. // Otherwise, we don't know how to handle this entry, we have to bail.
  301. break;
  302. }
  303. if (Scan != e &&
  304. // Don't print it's obvious nothing extra could be merged anyway.
  305. Scan+1 != e) {
  306. DEBUG(errs() << "Couldn't merge this:\n";
  307. Optn->print(errs(), 4);
  308. errs() << "into this:\n";
  309. OptionsToMatch[Scan]->print(errs(), 4);
  310. if (Scan+1 != e)
  311. OptionsToMatch[Scan+1]->printOne(errs());
  312. if (Scan+2 < e)
  313. OptionsToMatch[Scan+2]->printOne(errs());
  314. errs() << "\n");
  315. }
  316. // If we only found one option starting with this matcher, no factoring is
  317. // possible.
  318. if (EqualMatchers.size() == 1) {
  319. NewOptionsToMatch.push_back(EqualMatchers[0]);
  320. continue;
  321. }
  322. // Factor these checks by pulling the first node off each entry and
  323. // discarding it. Take the first one off the first entry to reuse.
  324. Matcher *Shared = Optn;
  325. Optn = Optn->takeNext();
  326. EqualMatchers[0] = Optn;
  327. // Remove and delete the first node from the other matchers we're factoring.
  328. for (unsigned i = 1, e = EqualMatchers.size(); i != e; ++i) {
  329. Matcher *Tmp = EqualMatchers[i]->takeNext();
  330. delete EqualMatchers[i];
  331. EqualMatchers[i] = Tmp;
  332. }
  333. Shared->setNext(new ScopeMatcher(EqualMatchers));
  334. // Recursively factor the newly created node.
  335. FactorNodes(Shared->getNextPtr());
  336. NewOptionsToMatch.push_back(Shared);
  337. }
  338. // If we're down to a single pattern to match, then we don't need this scope
  339. // anymore.
  340. if (NewOptionsToMatch.size() == 1) {
  341. MatcherPtr.reset(NewOptionsToMatch[0]);
  342. return;
  343. }
  344. if (NewOptionsToMatch.empty()) {
  345. MatcherPtr.reset();
  346. return;
  347. }
  348. // If our factoring failed (didn't achieve anything) see if we can simplify in
  349. // other ways.
  350. // Check to see if all of the leading entries are now opcode checks. If so,
  351. // we can convert this Scope to be a OpcodeSwitch instead.
  352. bool AllOpcodeChecks = true, AllTypeChecks = true;
  353. for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
  354. // Check to see if this breaks a series of CheckOpcodeMatchers.
  355. if (AllOpcodeChecks &&
  356. !isa<CheckOpcodeMatcher>(NewOptionsToMatch[i])) {
  357. #if 0
  358. if (i > 3) {
  359. errs() << "FAILING OPC #" << i << "\n";
  360. NewOptionsToMatch[i]->dump();
  361. }
  362. #endif
  363. AllOpcodeChecks = false;
  364. }
  365. // Check to see if this breaks a series of CheckTypeMatcher's.
  366. if (AllTypeChecks) {
  367. CheckTypeMatcher *CTM =
  368. cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
  369. Matcher::CheckType));
  370. if (!CTM ||
  371. // iPTR checks could alias any other case without us knowing, don't
  372. // bother with them.
  373. CTM->getType() == MVT::iPTR ||
  374. // SwitchType only works for result #0.
  375. CTM->getResNo() != 0 ||
  376. // If the CheckType isn't at the start of the list, see if we can move
  377. // it there.
  378. !CTM->canMoveBefore(NewOptionsToMatch[i])) {
  379. #if 0
  380. if (i > 3 && AllTypeChecks) {
  381. errs() << "FAILING TYPE #" << i << "\n";
  382. NewOptionsToMatch[i]->dump();
  383. }
  384. #endif
  385. AllTypeChecks = false;
  386. }
  387. }
  388. }
  389. // If all the options are CheckOpcode's, we can form the SwitchOpcode, woot.
  390. if (AllOpcodeChecks) {
  391. StringSet<> Opcodes;
  392. SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
  393. for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
  394. CheckOpcodeMatcher *COM = cast<CheckOpcodeMatcher>(NewOptionsToMatch[i]);
  395. assert(Opcodes.insert(COM->getOpcode().getEnumName()).second &&
  396. "Duplicate opcodes not factored?");
  397. Cases.push_back(std::make_pair(&COM->getOpcode(), COM->getNext()));
  398. }
  399. MatcherPtr.reset(new SwitchOpcodeMatcher(Cases));
  400. return;
  401. }
  402. // If all the options are CheckType's, we can form the SwitchType, woot.
  403. if (AllTypeChecks) {
  404. DenseMap<unsigned, unsigned> TypeEntry;
  405. SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
  406. for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i) {
  407. CheckTypeMatcher *CTM =
  408. cast_or_null<CheckTypeMatcher>(FindNodeWithKind(NewOptionsToMatch[i],
  409. Matcher::CheckType));
  410. Matcher *MatcherWithoutCTM = NewOptionsToMatch[i]->unlinkNode(CTM);
  411. MVT::SimpleValueType CTMTy = CTM->getType();
  412. delete CTM;
  413. unsigned &Entry = TypeEntry[CTMTy];
  414. if (Entry != 0) {
  415. // If we have unfactored duplicate types, then we should factor them.
  416. Matcher *PrevMatcher = Cases[Entry-1].second;
  417. if (ScopeMatcher *SM = dyn_cast<ScopeMatcher>(PrevMatcher)) {
  418. SM->setNumChildren(SM->getNumChildren()+1);
  419. SM->resetChild(SM->getNumChildren()-1, MatcherWithoutCTM);
  420. continue;
  421. }
  422. Matcher *Entries[2] = { PrevMatcher, MatcherWithoutCTM };
  423. Cases[Entry-1].second = new ScopeMatcher(Entries);
  424. continue;
  425. }
  426. Entry = Cases.size()+1;
  427. Cases.push_back(std::make_pair(CTMTy, MatcherWithoutCTM));
  428. }
  429. if (Cases.size() != 1) {
  430. MatcherPtr.reset(new SwitchTypeMatcher(Cases));
  431. } else {
  432. // If we factored and ended up with one case, create it now.
  433. MatcherPtr.reset(new CheckTypeMatcher(Cases[0].first, 0));
  434. MatcherPtr->setNext(Cases[0].second);
  435. }
  436. return;
  437. }
  438. // Reassemble the Scope node with the adjusted children.
  439. Scope->setNumChildren(NewOptionsToMatch.size());
  440. for (unsigned i = 0, e = NewOptionsToMatch.size(); i != e; ++i)
  441. Scope->resetChild(i, NewOptionsToMatch[i]);
  442. }
  443. void
  444. llvm::OptimizeMatcher(std::unique_ptr<Matcher> &MatcherPtr,
  445. const CodeGenDAGPatterns &CGP) {
  446. ContractNodes(MatcherPtr, CGP);
  447. SinkPatternPredicates(MatcherPtr);
  448. FactorNodes(MatcherPtr);
  449. }