DAGISelMatcher.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. //===- DAGISelMatcher.h - Representation of DAG pattern matcher -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
  9. #define LLVM_UTILS_TABLEGEN_DAGISELMATCHER_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Support/Casting.h"
  14. #include "llvm/Support/MachineValueType.h"
  15. namespace llvm {
  16. struct CodeGenRegister;
  17. class CodeGenDAGPatterns;
  18. class Matcher;
  19. class PatternToMatch;
  20. class raw_ostream;
  21. class ComplexPattern;
  22. class Record;
  23. class SDNodeInfo;
  24. class TreePredicateFn;
  25. class TreePattern;
  26. Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,unsigned Variant,
  27. const CodeGenDAGPatterns &CGP);
  28. void OptimizeMatcher(std::unique_ptr<Matcher> &Matcher,
  29. const CodeGenDAGPatterns &CGP);
  30. void EmitMatcherTable(Matcher *Matcher, const CodeGenDAGPatterns &CGP,
  31. raw_ostream &OS);
  32. /// Matcher - Base class for all the DAG ISel Matcher representation
  33. /// nodes.
  34. class Matcher {
  35. // The next matcher node that is executed after this one. Null if this is the
  36. // last stage of a match.
  37. std::unique_ptr<Matcher> Next;
  38. size_t Size; // Size in bytes of matcher and all its children (if any).
  39. virtual void anchor();
  40. public:
  41. enum KindTy {
  42. // Matcher state manipulation.
  43. Scope, // Push a checking scope.
  44. RecordNode, // Record the current node.
  45. RecordChild, // Record a child of the current node.
  46. RecordMemRef, // Record the memref in the current node.
  47. CaptureGlueInput, // If the current node has an input glue, save it.
  48. MoveChild, // Move current node to specified child.
  49. MoveParent, // Move current node to parent.
  50. // Predicate checking.
  51. CheckSame, // Fail if not same as prev match.
  52. CheckChildSame, // Fail if child not same as prev match.
  53. CheckPatternPredicate,
  54. CheckPredicate, // Fail if node predicate fails.
  55. CheckOpcode, // Fail if not opcode.
  56. SwitchOpcode, // Dispatch based on opcode.
  57. CheckType, // Fail if not correct type.
  58. SwitchType, // Dispatch based on type.
  59. CheckChildType, // Fail if child has wrong type.
  60. CheckInteger, // Fail if wrong val.
  61. CheckChildInteger, // Fail if child is wrong val.
  62. CheckCondCode, // Fail if not condcode.
  63. CheckChild2CondCode, // Fail if child is wrong condcode.
  64. CheckValueType,
  65. CheckComplexPat,
  66. CheckAndImm,
  67. CheckOrImm,
  68. CheckImmAllOnesV,
  69. CheckImmAllZerosV,
  70. CheckFoldableChainNode,
  71. // Node creation/emisssion.
  72. EmitInteger, // Create a TargetConstant
  73. EmitStringInteger, // Create a TargetConstant from a string.
  74. EmitRegister, // Create a register.
  75. EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
  76. EmitMergeInputChains, // Merge together a chains for an input.
  77. EmitCopyToReg, // Emit a copytoreg into a physreg.
  78. EmitNode, // Create a DAG node
  79. EmitNodeXForm, // Run a SDNodeXForm
  80. CompleteMatch, // Finish a match and update the results.
  81. MorphNodeTo, // Build a node, finish a match and update results.
  82. // Highest enum value; watch out when adding more.
  83. HighestKind = MorphNodeTo
  84. };
  85. const KindTy Kind;
  86. protected:
  87. Matcher(KindTy K) : Kind(K) {}
  88. public:
  89. virtual ~Matcher() {}
  90. unsigned getSize() const { return Size; }
  91. void setSize(unsigned sz) { Size = sz; }
  92. KindTy getKind() const { return Kind; }
  93. Matcher *getNext() { return Next.get(); }
  94. const Matcher *getNext() const { return Next.get(); }
  95. void setNext(Matcher *C) { Next.reset(C); }
  96. Matcher *takeNext() { return Next.release(); }
  97. std::unique_ptr<Matcher> &getNextPtr() { return Next; }
  98. bool isEqual(const Matcher *M) const {
  99. if (getKind() != M->getKind()) return false;
  100. return isEqualImpl(M);
  101. }
  102. /// isSimplePredicateNode - Return true if this is a simple predicate that
  103. /// operates on the node or its children without potential side effects or a
  104. /// change of the current node.
  105. bool isSimplePredicateNode() const {
  106. switch (getKind()) {
  107. default: return false;
  108. case CheckSame:
  109. case CheckChildSame:
  110. case CheckPatternPredicate:
  111. case CheckPredicate:
  112. case CheckOpcode:
  113. case CheckType:
  114. case CheckChildType:
  115. case CheckInteger:
  116. case CheckChildInteger:
  117. case CheckCondCode:
  118. case CheckChild2CondCode:
  119. case CheckValueType:
  120. case CheckAndImm:
  121. case CheckOrImm:
  122. case CheckImmAllOnesV:
  123. case CheckImmAllZerosV:
  124. case CheckFoldableChainNode:
  125. return true;
  126. }
  127. }
  128. /// isSimplePredicateOrRecordNode - Return true if this is a record node or
  129. /// a simple predicate.
  130. bool isSimplePredicateOrRecordNode() const {
  131. return isSimplePredicateNode() ||
  132. getKind() == RecordNode || getKind() == RecordChild;
  133. }
  134. /// unlinkNode - Unlink the specified node from this chain. If Other == this,
  135. /// we unlink the next pointer and return it. Otherwise we unlink Other from
  136. /// the list and return this.
  137. Matcher *unlinkNode(Matcher *Other);
  138. /// canMoveBefore - Return true if this matcher is the same as Other, or if
  139. /// we can move this matcher past all of the nodes in-between Other and this
  140. /// node. Other must be equal to or before this.
  141. bool canMoveBefore(const Matcher *Other) const;
  142. /// canMoveBeforeNode - Return true if it is safe to move the current matcher
  143. /// across the specified one.
  144. bool canMoveBeforeNode(const Matcher *Other) const;
  145. /// isContradictory - Return true of these two matchers could never match on
  146. /// the same node.
  147. bool isContradictory(const Matcher *Other) const {
  148. // Since this predicate is reflexive, we canonicalize the ordering so that
  149. // we always match a node against nodes with kinds that are greater or equal
  150. // to them. For example, we'll pass in a CheckType node as an argument to
  151. // the CheckOpcode method, not the other way around.
  152. if (getKind() < Other->getKind())
  153. return isContradictoryImpl(Other);
  154. return Other->isContradictoryImpl(this);
  155. }
  156. void print(raw_ostream &OS, unsigned indent = 0) const;
  157. void printOne(raw_ostream &OS) const;
  158. void dump() const;
  159. protected:
  160. virtual void printImpl(raw_ostream &OS, unsigned indent) const = 0;
  161. virtual bool isEqualImpl(const Matcher *M) const = 0;
  162. virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
  163. };
  164. /// ScopeMatcher - This attempts to match each of its children to find the first
  165. /// one that successfully matches. If one child fails, it tries the next child.
  166. /// If none of the children match then this check fails. It never has a 'next'.
  167. class ScopeMatcher : public Matcher {
  168. SmallVector<Matcher*, 4> Children;
  169. public:
  170. ScopeMatcher(ArrayRef<Matcher *> children)
  171. : Matcher(Scope), Children(children.begin(), children.end()) {
  172. }
  173. ~ScopeMatcher() override;
  174. unsigned getNumChildren() const { return Children.size(); }
  175. Matcher *getChild(unsigned i) { return Children[i]; }
  176. const Matcher *getChild(unsigned i) const { return Children[i]; }
  177. void resetChild(unsigned i, Matcher *N) {
  178. delete Children[i];
  179. Children[i] = N;
  180. }
  181. Matcher *takeChild(unsigned i) {
  182. Matcher *Res = Children[i];
  183. Children[i] = nullptr;
  184. return Res;
  185. }
  186. void setNumChildren(unsigned NC) {
  187. if (NC < Children.size()) {
  188. // delete any children we're about to lose pointers to.
  189. for (unsigned i = NC, e = Children.size(); i != e; ++i)
  190. delete Children[i];
  191. }
  192. Children.resize(NC);
  193. }
  194. static bool classof(const Matcher *N) {
  195. return N->getKind() == Scope;
  196. }
  197. private:
  198. void printImpl(raw_ostream &OS, unsigned indent) const override;
  199. bool isEqualImpl(const Matcher *M) const override { return false; }
  200. };
  201. /// RecordMatcher - Save the current node in the operand list.
  202. class RecordMatcher : public Matcher {
  203. /// WhatFor - This is a string indicating why we're recording this. This
  204. /// should only be used for comment generation not anything semantic.
  205. std::string WhatFor;
  206. /// ResultNo - The slot number in the RecordedNodes vector that this will be,
  207. /// just printed as a comment.
  208. unsigned ResultNo;
  209. public:
  210. RecordMatcher(const std::string &whatfor, unsigned resultNo)
  211. : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
  212. const std::string &getWhatFor() const { return WhatFor; }
  213. unsigned getResultNo() const { return ResultNo; }
  214. static bool classof(const Matcher *N) {
  215. return N->getKind() == RecordNode;
  216. }
  217. private:
  218. void printImpl(raw_ostream &OS, unsigned indent) const override;
  219. bool isEqualImpl(const Matcher *M) const override { return true; }
  220. };
  221. /// RecordChildMatcher - Save a numbered child of the current node, or fail
  222. /// the match if it doesn't exist. This is logically equivalent to:
  223. /// MoveChild N + RecordNode + MoveParent.
  224. class RecordChildMatcher : public Matcher {
  225. unsigned ChildNo;
  226. /// WhatFor - This is a string indicating why we're recording this. This
  227. /// should only be used for comment generation not anything semantic.
  228. std::string WhatFor;
  229. /// ResultNo - The slot number in the RecordedNodes vector that this will be,
  230. /// just printed as a comment.
  231. unsigned ResultNo;
  232. public:
  233. RecordChildMatcher(unsigned childno, const std::string &whatfor,
  234. unsigned resultNo)
  235. : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
  236. ResultNo(resultNo) {}
  237. unsigned getChildNo() const { return ChildNo; }
  238. const std::string &getWhatFor() const { return WhatFor; }
  239. unsigned getResultNo() const { return ResultNo; }
  240. static bool classof(const Matcher *N) {
  241. return N->getKind() == RecordChild;
  242. }
  243. private:
  244. void printImpl(raw_ostream &OS, unsigned indent) const override;
  245. bool isEqualImpl(const Matcher *M) const override {
  246. return cast<RecordChildMatcher>(M)->getChildNo() == getChildNo();
  247. }
  248. };
  249. /// RecordMemRefMatcher - Save the current node's memref.
  250. class RecordMemRefMatcher : public Matcher {
  251. public:
  252. RecordMemRefMatcher() : Matcher(RecordMemRef) {}
  253. static bool classof(const Matcher *N) {
  254. return N->getKind() == RecordMemRef;
  255. }
  256. private:
  257. void printImpl(raw_ostream &OS, unsigned indent) const override;
  258. bool isEqualImpl(const Matcher *M) const override { return true; }
  259. };
  260. /// CaptureGlueInputMatcher - If the current record has a glue input, record
  261. /// it so that it is used as an input to the generated code.
  262. class CaptureGlueInputMatcher : public Matcher {
  263. public:
  264. CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
  265. static bool classof(const Matcher *N) {
  266. return N->getKind() == CaptureGlueInput;
  267. }
  268. private:
  269. void printImpl(raw_ostream &OS, unsigned indent) const override;
  270. bool isEqualImpl(const Matcher *M) const override { return true; }
  271. };
  272. /// MoveChildMatcher - This tells the interpreter to move into the
  273. /// specified child node.
  274. class MoveChildMatcher : public Matcher {
  275. unsigned ChildNo;
  276. public:
  277. MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
  278. unsigned getChildNo() const { return ChildNo; }
  279. static bool classof(const Matcher *N) {
  280. return N->getKind() == MoveChild;
  281. }
  282. private:
  283. void printImpl(raw_ostream &OS, unsigned indent) const override;
  284. bool isEqualImpl(const Matcher *M) const override {
  285. return cast<MoveChildMatcher>(M)->getChildNo() == getChildNo();
  286. }
  287. };
  288. /// MoveParentMatcher - This tells the interpreter to move to the parent
  289. /// of the current node.
  290. class MoveParentMatcher : public Matcher {
  291. public:
  292. MoveParentMatcher() : Matcher(MoveParent) {}
  293. static bool classof(const Matcher *N) {
  294. return N->getKind() == MoveParent;
  295. }
  296. private:
  297. void printImpl(raw_ostream &OS, unsigned indent) const override;
  298. bool isEqualImpl(const Matcher *M) const override { return true; }
  299. };
  300. /// CheckSameMatcher - This checks to see if this node is exactly the same
  301. /// node as the specified match that was recorded with 'Record'. This is used
  302. /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
  303. class CheckSameMatcher : public Matcher {
  304. unsigned MatchNumber;
  305. public:
  306. CheckSameMatcher(unsigned matchnumber)
  307. : Matcher(CheckSame), MatchNumber(matchnumber) {}
  308. unsigned getMatchNumber() const { return MatchNumber; }
  309. static bool classof(const Matcher *N) {
  310. return N->getKind() == CheckSame;
  311. }
  312. private:
  313. void printImpl(raw_ostream &OS, unsigned indent) const override;
  314. bool isEqualImpl(const Matcher *M) const override {
  315. return cast<CheckSameMatcher>(M)->getMatchNumber() == getMatchNumber();
  316. }
  317. };
  318. /// CheckChildSameMatcher - This checks to see if child node is exactly the same
  319. /// node as the specified match that was recorded with 'Record'. This is used
  320. /// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
  321. class CheckChildSameMatcher : public Matcher {
  322. unsigned ChildNo;
  323. unsigned MatchNumber;
  324. public:
  325. CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
  326. : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
  327. unsigned getChildNo() const { return ChildNo; }
  328. unsigned getMatchNumber() const { return MatchNumber; }
  329. static bool classof(const Matcher *N) {
  330. return N->getKind() == CheckChildSame;
  331. }
  332. private:
  333. void printImpl(raw_ostream &OS, unsigned indent) const override;
  334. bool isEqualImpl(const Matcher *M) const override {
  335. return cast<CheckChildSameMatcher>(M)->ChildNo == ChildNo &&
  336. cast<CheckChildSameMatcher>(M)->MatchNumber == MatchNumber;
  337. }
  338. };
  339. /// CheckPatternPredicateMatcher - This checks the target-specific predicate
  340. /// to see if the entire pattern is capable of matching. This predicate does
  341. /// not take a node as input. This is used for subtarget feature checks etc.
  342. class CheckPatternPredicateMatcher : public Matcher {
  343. std::string Predicate;
  344. public:
  345. CheckPatternPredicateMatcher(StringRef predicate)
  346. : Matcher(CheckPatternPredicate), Predicate(predicate) {}
  347. StringRef getPredicate() const { return Predicate; }
  348. static bool classof(const Matcher *N) {
  349. return N->getKind() == CheckPatternPredicate;
  350. }
  351. private:
  352. void printImpl(raw_ostream &OS, unsigned indent) const override;
  353. bool isEqualImpl(const Matcher *M) const override {
  354. return cast<CheckPatternPredicateMatcher>(M)->getPredicate() == Predicate;
  355. }
  356. };
  357. /// CheckPredicateMatcher - This checks the target-specific predicate to
  358. /// see if the node is acceptable.
  359. class CheckPredicateMatcher : public Matcher {
  360. TreePattern *Pred;
  361. const SmallVector<unsigned, 4> Operands;
  362. public:
  363. CheckPredicateMatcher(const TreePredicateFn &pred,
  364. const SmallVectorImpl<unsigned> &Operands);
  365. TreePredicateFn getPredicate() const;
  366. unsigned getNumOperands() const;
  367. unsigned getOperandNo(unsigned i) const;
  368. static bool classof(const Matcher *N) {
  369. return N->getKind() == CheckPredicate;
  370. }
  371. private:
  372. void printImpl(raw_ostream &OS, unsigned indent) const override;
  373. bool isEqualImpl(const Matcher *M) const override {
  374. return cast<CheckPredicateMatcher>(M)->Pred == Pred;
  375. }
  376. };
  377. /// CheckOpcodeMatcher - This checks to see if the current node has the
  378. /// specified opcode, if not it fails to match.
  379. class CheckOpcodeMatcher : public Matcher {
  380. const SDNodeInfo &Opcode;
  381. public:
  382. CheckOpcodeMatcher(const SDNodeInfo &opcode)
  383. : Matcher(CheckOpcode), Opcode(opcode) {}
  384. const SDNodeInfo &getOpcode() const { return Opcode; }
  385. static bool classof(const Matcher *N) {
  386. return N->getKind() == CheckOpcode;
  387. }
  388. private:
  389. void printImpl(raw_ostream &OS, unsigned indent) const override;
  390. bool isEqualImpl(const Matcher *M) const override;
  391. bool isContradictoryImpl(const Matcher *M) const override;
  392. };
  393. /// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
  394. /// to one matcher per opcode. If the opcode doesn't match any of the cases,
  395. /// then the match fails. This is semantically equivalent to a Scope node where
  396. /// every child does a CheckOpcode, but is much faster.
  397. class SwitchOpcodeMatcher : public Matcher {
  398. SmallVector<std::pair<const SDNodeInfo*, Matcher*>, 8> Cases;
  399. public:
  400. SwitchOpcodeMatcher(ArrayRef<std::pair<const SDNodeInfo*, Matcher*> > cases)
  401. : Matcher(SwitchOpcode), Cases(cases.begin(), cases.end()) {}
  402. ~SwitchOpcodeMatcher() override;
  403. static bool classof(const Matcher *N) {
  404. return N->getKind() == SwitchOpcode;
  405. }
  406. unsigned getNumCases() const { return Cases.size(); }
  407. const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
  408. Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
  409. const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
  410. private:
  411. void printImpl(raw_ostream &OS, unsigned indent) const override;
  412. bool isEqualImpl(const Matcher *M) const override { return false; }
  413. };
  414. /// CheckTypeMatcher - This checks to see if the current node has the
  415. /// specified type at the specified result, if not it fails to match.
  416. class CheckTypeMatcher : public Matcher {
  417. MVT::SimpleValueType Type;
  418. unsigned ResNo;
  419. public:
  420. CheckTypeMatcher(MVT::SimpleValueType type, unsigned resno)
  421. : Matcher(CheckType), Type(type), ResNo(resno) {}
  422. MVT::SimpleValueType getType() const { return Type; }
  423. unsigned getResNo() const { return ResNo; }
  424. static bool classof(const Matcher *N) {
  425. return N->getKind() == CheckType;
  426. }
  427. private:
  428. void printImpl(raw_ostream &OS, unsigned indent) const override;
  429. bool isEqualImpl(const Matcher *M) const override {
  430. return cast<CheckTypeMatcher>(M)->Type == Type;
  431. }
  432. bool isContradictoryImpl(const Matcher *M) const override;
  433. };
  434. /// SwitchTypeMatcher - Switch based on the current node's type, dispatching
  435. /// to one matcher per case. If the type doesn't match any of the cases,
  436. /// then the match fails. This is semantically equivalent to a Scope node where
  437. /// every child does a CheckType, but is much faster.
  438. class SwitchTypeMatcher : public Matcher {
  439. SmallVector<std::pair<MVT::SimpleValueType, Matcher*>, 8> Cases;
  440. public:
  441. SwitchTypeMatcher(ArrayRef<std::pair<MVT::SimpleValueType, Matcher*> > cases)
  442. : Matcher(SwitchType), Cases(cases.begin(), cases.end()) {}
  443. ~SwitchTypeMatcher() override;
  444. static bool classof(const Matcher *N) {
  445. return N->getKind() == SwitchType;
  446. }
  447. unsigned getNumCases() const { return Cases.size(); }
  448. MVT::SimpleValueType getCaseType(unsigned i) const { return Cases[i].first; }
  449. Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
  450. const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
  451. private:
  452. void printImpl(raw_ostream &OS, unsigned indent) const override;
  453. bool isEqualImpl(const Matcher *M) const override { return false; }
  454. };
  455. /// CheckChildTypeMatcher - This checks to see if a child node has the
  456. /// specified type, if not it fails to match.
  457. class CheckChildTypeMatcher : public Matcher {
  458. unsigned ChildNo;
  459. MVT::SimpleValueType Type;
  460. public:
  461. CheckChildTypeMatcher(unsigned childno, MVT::SimpleValueType type)
  462. : Matcher(CheckChildType), ChildNo(childno), Type(type) {}
  463. unsigned getChildNo() const { return ChildNo; }
  464. MVT::SimpleValueType getType() const { return Type; }
  465. static bool classof(const Matcher *N) {
  466. return N->getKind() == CheckChildType;
  467. }
  468. private:
  469. void printImpl(raw_ostream &OS, unsigned indent) const override;
  470. bool isEqualImpl(const Matcher *M) const override {
  471. return cast<CheckChildTypeMatcher>(M)->ChildNo == ChildNo &&
  472. cast<CheckChildTypeMatcher>(M)->Type == Type;
  473. }
  474. bool isContradictoryImpl(const Matcher *M) const override;
  475. };
  476. /// CheckIntegerMatcher - This checks to see if the current node is a
  477. /// ConstantSDNode with the specified integer value, if not it fails to match.
  478. class CheckIntegerMatcher : public Matcher {
  479. int64_t Value;
  480. public:
  481. CheckIntegerMatcher(int64_t value)
  482. : Matcher(CheckInteger), Value(value) {}
  483. int64_t getValue() const { return Value; }
  484. static bool classof(const Matcher *N) {
  485. return N->getKind() == CheckInteger;
  486. }
  487. private:
  488. void printImpl(raw_ostream &OS, unsigned indent) const override;
  489. bool isEqualImpl(const Matcher *M) const override {
  490. return cast<CheckIntegerMatcher>(M)->Value == Value;
  491. }
  492. bool isContradictoryImpl(const Matcher *M) const override;
  493. };
  494. /// CheckChildIntegerMatcher - This checks to see if the child node is a
  495. /// ConstantSDNode with a specified integer value, if not it fails to match.
  496. class CheckChildIntegerMatcher : public Matcher {
  497. unsigned ChildNo;
  498. int64_t Value;
  499. public:
  500. CheckChildIntegerMatcher(unsigned childno, int64_t value)
  501. : Matcher(CheckChildInteger), ChildNo(childno), Value(value) {}
  502. unsigned getChildNo() const { return ChildNo; }
  503. int64_t getValue() const { return Value; }
  504. static bool classof(const Matcher *N) {
  505. return N->getKind() == CheckChildInteger;
  506. }
  507. private:
  508. void printImpl(raw_ostream &OS, unsigned indent) const override;
  509. bool isEqualImpl(const Matcher *M) const override {
  510. return cast<CheckChildIntegerMatcher>(M)->ChildNo == ChildNo &&
  511. cast<CheckChildIntegerMatcher>(M)->Value == Value;
  512. }
  513. bool isContradictoryImpl(const Matcher *M) const override;
  514. };
  515. /// CheckCondCodeMatcher - This checks to see if the current node is a
  516. /// CondCodeSDNode with the specified condition, if not it fails to match.
  517. class CheckCondCodeMatcher : public Matcher {
  518. StringRef CondCodeName;
  519. public:
  520. CheckCondCodeMatcher(StringRef condcodename)
  521. : Matcher(CheckCondCode), CondCodeName(condcodename) {}
  522. StringRef getCondCodeName() const { return CondCodeName; }
  523. static bool classof(const Matcher *N) {
  524. return N->getKind() == CheckCondCode;
  525. }
  526. private:
  527. void printImpl(raw_ostream &OS, unsigned indent) const override;
  528. bool isEqualImpl(const Matcher *M) const override {
  529. return cast<CheckCondCodeMatcher>(M)->CondCodeName == CondCodeName;
  530. }
  531. bool isContradictoryImpl(const Matcher *M) const override;
  532. };
  533. /// CheckChild2CondCodeMatcher - This checks to see if child 2 node is a
  534. /// CondCodeSDNode with the specified condition, if not it fails to match.
  535. class CheckChild2CondCodeMatcher : public Matcher {
  536. StringRef CondCodeName;
  537. public:
  538. CheckChild2CondCodeMatcher(StringRef condcodename)
  539. : Matcher(CheckChild2CondCode), CondCodeName(condcodename) {}
  540. StringRef getCondCodeName() const { return CondCodeName; }
  541. static bool classof(const Matcher *N) {
  542. return N->getKind() == CheckChild2CondCode;
  543. }
  544. private:
  545. void printImpl(raw_ostream &OS, unsigned indent) const override;
  546. bool isEqualImpl(const Matcher *M) const override {
  547. return cast<CheckChild2CondCodeMatcher>(M)->CondCodeName == CondCodeName;
  548. }
  549. bool isContradictoryImpl(const Matcher *M) const override;
  550. };
  551. /// CheckValueTypeMatcher - This checks to see if the current node is a
  552. /// VTSDNode with the specified type, if not it fails to match.
  553. class CheckValueTypeMatcher : public Matcher {
  554. StringRef TypeName;
  555. public:
  556. CheckValueTypeMatcher(StringRef type_name)
  557. : Matcher(CheckValueType), TypeName(type_name) {}
  558. StringRef getTypeName() const { return TypeName; }
  559. static bool classof(const Matcher *N) {
  560. return N->getKind() == CheckValueType;
  561. }
  562. private:
  563. void printImpl(raw_ostream &OS, unsigned indent) const override;
  564. bool isEqualImpl(const Matcher *M) const override {
  565. return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
  566. }
  567. bool isContradictoryImpl(const Matcher *M) const override;
  568. };
  569. /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
  570. /// the current node.
  571. class CheckComplexPatMatcher : public Matcher {
  572. const ComplexPattern &Pattern;
  573. /// MatchNumber - This is the recorded nodes slot that contains the node we
  574. /// want to match against.
  575. unsigned MatchNumber;
  576. /// Name - The name of the node we're matching, for comment emission.
  577. std::string Name;
  578. /// FirstResult - This is the first slot in the RecordedNodes list that the
  579. /// result of the match populates.
  580. unsigned FirstResult;
  581. public:
  582. CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
  583. const std::string &name, unsigned firstresult)
  584. : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
  585. Name(name), FirstResult(firstresult) {}
  586. const ComplexPattern &getPattern() const { return Pattern; }
  587. unsigned getMatchNumber() const { return MatchNumber; }
  588. std::string getName() const { return Name; }
  589. unsigned getFirstResult() const { return FirstResult; }
  590. static bool classof(const Matcher *N) {
  591. return N->getKind() == CheckComplexPat;
  592. }
  593. private:
  594. void printImpl(raw_ostream &OS, unsigned indent) const override;
  595. bool isEqualImpl(const Matcher *M) const override {
  596. return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
  597. cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
  598. }
  599. };
  600. /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
  601. /// with something equivalent to the specified immediate.
  602. class CheckAndImmMatcher : public Matcher {
  603. int64_t Value;
  604. public:
  605. CheckAndImmMatcher(int64_t value)
  606. : Matcher(CheckAndImm), Value(value) {}
  607. int64_t getValue() const { return Value; }
  608. static bool classof(const Matcher *N) {
  609. return N->getKind() == CheckAndImm;
  610. }
  611. private:
  612. void printImpl(raw_ostream &OS, unsigned indent) const override;
  613. bool isEqualImpl(const Matcher *M) const override {
  614. return cast<CheckAndImmMatcher>(M)->Value == Value;
  615. }
  616. };
  617. /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
  618. /// with something equivalent to the specified immediate.
  619. class CheckOrImmMatcher : public Matcher {
  620. int64_t Value;
  621. public:
  622. CheckOrImmMatcher(int64_t value)
  623. : Matcher(CheckOrImm), Value(value) {}
  624. int64_t getValue() const { return Value; }
  625. static bool classof(const Matcher *N) {
  626. return N->getKind() == CheckOrImm;
  627. }
  628. private:
  629. void printImpl(raw_ostream &OS, unsigned indent) const override;
  630. bool isEqualImpl(const Matcher *M) const override {
  631. return cast<CheckOrImmMatcher>(M)->Value == Value;
  632. }
  633. };
  634. /// CheckImmAllOnesVMatcher - This checks if the current node is a build_vector
  635. /// or splat_vector of all ones.
  636. class CheckImmAllOnesVMatcher : public Matcher {
  637. public:
  638. CheckImmAllOnesVMatcher() : Matcher(CheckImmAllOnesV) {}
  639. static bool classof(const Matcher *N) {
  640. return N->getKind() == CheckImmAllOnesV;
  641. }
  642. private:
  643. void printImpl(raw_ostream &OS, unsigned indent) const override;
  644. bool isEqualImpl(const Matcher *M) const override { return true; }
  645. bool isContradictoryImpl(const Matcher *M) const override;
  646. };
  647. /// CheckImmAllZerosVMatcher - This checks if the current node is a
  648. /// build_vector or splat_vector of all zeros.
  649. class CheckImmAllZerosVMatcher : public Matcher {
  650. public:
  651. CheckImmAllZerosVMatcher() : Matcher(CheckImmAllZerosV) {}
  652. static bool classof(const Matcher *N) {
  653. return N->getKind() == CheckImmAllZerosV;
  654. }
  655. private:
  656. void printImpl(raw_ostream &OS, unsigned indent) const override;
  657. bool isEqualImpl(const Matcher *M) const override { return true; }
  658. bool isContradictoryImpl(const Matcher *M) const override;
  659. };
  660. /// CheckFoldableChainNodeMatcher - This checks to see if the current node
  661. /// (which defines a chain operand) is safe to fold into a larger pattern.
  662. class CheckFoldableChainNodeMatcher : public Matcher {
  663. public:
  664. CheckFoldableChainNodeMatcher()
  665. : Matcher(CheckFoldableChainNode) {}
  666. static bool classof(const Matcher *N) {
  667. return N->getKind() == CheckFoldableChainNode;
  668. }
  669. private:
  670. void printImpl(raw_ostream &OS, unsigned indent) const override;
  671. bool isEqualImpl(const Matcher *M) const override { return true; }
  672. };
  673. /// EmitIntegerMatcher - This creates a new TargetConstant.
  674. class EmitIntegerMatcher : public Matcher {
  675. int64_t Val;
  676. MVT::SimpleValueType VT;
  677. public:
  678. EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
  679. : Matcher(EmitInteger), Val(val), VT(vt) {}
  680. int64_t getValue() const { return Val; }
  681. MVT::SimpleValueType getVT() const { return VT; }
  682. static bool classof(const Matcher *N) {
  683. return N->getKind() == EmitInteger;
  684. }
  685. private:
  686. void printImpl(raw_ostream &OS, unsigned indent) const override;
  687. bool isEqualImpl(const Matcher *M) const override {
  688. return cast<EmitIntegerMatcher>(M)->Val == Val &&
  689. cast<EmitIntegerMatcher>(M)->VT == VT;
  690. }
  691. };
  692. /// EmitStringIntegerMatcher - A target constant whose value is represented
  693. /// by a string.
  694. class EmitStringIntegerMatcher : public Matcher {
  695. std::string Val;
  696. MVT::SimpleValueType VT;
  697. public:
  698. EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
  699. : Matcher(EmitStringInteger), Val(val), VT(vt) {}
  700. const std::string &getValue() const { return Val; }
  701. MVT::SimpleValueType getVT() const { return VT; }
  702. static bool classof(const Matcher *N) {
  703. return N->getKind() == EmitStringInteger;
  704. }
  705. private:
  706. void printImpl(raw_ostream &OS, unsigned indent) const override;
  707. bool isEqualImpl(const Matcher *M) const override {
  708. return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
  709. cast<EmitStringIntegerMatcher>(M)->VT == VT;
  710. }
  711. };
  712. /// EmitRegisterMatcher - This creates a new TargetConstant.
  713. class EmitRegisterMatcher : public Matcher {
  714. /// Reg - The def for the register that we're emitting. If this is null, then
  715. /// this is a reference to zero_reg.
  716. const CodeGenRegister *Reg;
  717. MVT::SimpleValueType VT;
  718. public:
  719. EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
  720. : Matcher(EmitRegister), Reg(reg), VT(vt) {}
  721. const CodeGenRegister *getReg() const { return Reg; }
  722. MVT::SimpleValueType getVT() const { return VT; }
  723. static bool classof(const Matcher *N) {
  724. return N->getKind() == EmitRegister;
  725. }
  726. private:
  727. void printImpl(raw_ostream &OS, unsigned indent) const override;
  728. bool isEqualImpl(const Matcher *M) const override {
  729. return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
  730. cast<EmitRegisterMatcher>(M)->VT == VT;
  731. }
  732. };
  733. /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
  734. /// recorded node and converts it from being a ISD::Constant to
  735. /// ISD::TargetConstant, likewise for ConstantFP.
  736. class EmitConvertToTargetMatcher : public Matcher {
  737. unsigned Slot;
  738. public:
  739. EmitConvertToTargetMatcher(unsigned slot)
  740. : Matcher(EmitConvertToTarget), Slot(slot) {}
  741. unsigned getSlot() const { return Slot; }
  742. static bool classof(const Matcher *N) {
  743. return N->getKind() == EmitConvertToTarget;
  744. }
  745. private:
  746. void printImpl(raw_ostream &OS, unsigned indent) const override;
  747. bool isEqualImpl(const Matcher *M) const override {
  748. return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
  749. }
  750. };
  751. /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
  752. /// chains together with a token factor. The list of nodes are the nodes in the
  753. /// matched pattern that have chain input/outputs. This node adds all input
  754. /// chains of these nodes if they are not themselves a node in the pattern.
  755. class EmitMergeInputChainsMatcher : public Matcher {
  756. SmallVector<unsigned, 3> ChainNodes;
  757. public:
  758. EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
  759. : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
  760. unsigned getNumNodes() const { return ChainNodes.size(); }
  761. unsigned getNode(unsigned i) const {
  762. assert(i < ChainNodes.size());
  763. return ChainNodes[i];
  764. }
  765. static bool classof(const Matcher *N) {
  766. return N->getKind() == EmitMergeInputChains;
  767. }
  768. private:
  769. void printImpl(raw_ostream &OS, unsigned indent) const override;
  770. bool isEqualImpl(const Matcher *M) const override {
  771. return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
  772. }
  773. };
  774. /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
  775. /// pushing the chain and glue results.
  776. ///
  777. class EmitCopyToRegMatcher : public Matcher {
  778. unsigned SrcSlot; // Value to copy into the physreg.
  779. const CodeGenRegister *DestPhysReg;
  780. public:
  781. EmitCopyToRegMatcher(unsigned srcSlot,
  782. const CodeGenRegister *destPhysReg)
  783. : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
  784. unsigned getSrcSlot() const { return SrcSlot; }
  785. const CodeGenRegister *getDestPhysReg() const { return DestPhysReg; }
  786. static bool classof(const Matcher *N) {
  787. return N->getKind() == EmitCopyToReg;
  788. }
  789. private:
  790. void printImpl(raw_ostream &OS, unsigned indent) const override;
  791. bool isEqualImpl(const Matcher *M) const override {
  792. return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
  793. cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
  794. }
  795. };
  796. /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
  797. /// recorded node and records the result.
  798. class EmitNodeXFormMatcher : public Matcher {
  799. unsigned Slot;
  800. Record *NodeXForm;
  801. public:
  802. EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
  803. : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
  804. unsigned getSlot() const { return Slot; }
  805. Record *getNodeXForm() const { return NodeXForm; }
  806. static bool classof(const Matcher *N) {
  807. return N->getKind() == EmitNodeXForm;
  808. }
  809. private:
  810. void printImpl(raw_ostream &OS, unsigned indent) const override;
  811. bool isEqualImpl(const Matcher *M) const override {
  812. return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
  813. cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
  814. }
  815. };
  816. /// EmitNodeMatcherCommon - Common class shared between EmitNode and
  817. /// MorphNodeTo.
  818. class EmitNodeMatcherCommon : public Matcher {
  819. std::string OpcodeName;
  820. const SmallVector<MVT::SimpleValueType, 3> VTs;
  821. const SmallVector<unsigned, 6> Operands;
  822. bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
  823. /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
  824. /// If this is a varidic node, this is set to the number of fixed arity
  825. /// operands in the root of the pattern. The rest are appended to this node.
  826. int NumFixedArityOperands;
  827. public:
  828. EmitNodeMatcherCommon(const std::string &opcodeName,
  829. ArrayRef<MVT::SimpleValueType> vts,
  830. ArrayRef<unsigned> operands,
  831. bool hasChain, bool hasInGlue, bool hasOutGlue,
  832. bool hasmemrefs,
  833. int numfixedarityoperands, bool isMorphNodeTo)
  834. : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
  835. VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
  836. HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
  837. HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
  838. const std::string &getOpcodeName() const { return OpcodeName; }
  839. unsigned getNumVTs() const { return VTs.size(); }
  840. MVT::SimpleValueType getVT(unsigned i) const {
  841. assert(i < VTs.size());
  842. return VTs[i];
  843. }
  844. unsigned getNumOperands() const { return Operands.size(); }
  845. unsigned getOperand(unsigned i) const {
  846. assert(i < Operands.size());
  847. return Operands[i];
  848. }
  849. const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
  850. const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
  851. bool hasChain() const { return HasChain; }
  852. bool hasInFlag() const { return HasInGlue; }
  853. bool hasOutFlag() const { return HasOutGlue; }
  854. bool hasMemRefs() const { return HasMemRefs; }
  855. int getNumFixedArityOperands() const { return NumFixedArityOperands; }
  856. static bool classof(const Matcher *N) {
  857. return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
  858. }
  859. private:
  860. void printImpl(raw_ostream &OS, unsigned indent) const override;
  861. bool isEqualImpl(const Matcher *M) const override;
  862. };
  863. /// EmitNodeMatcher - This signals a successful match and generates a node.
  864. class EmitNodeMatcher : public EmitNodeMatcherCommon {
  865. void anchor() override;
  866. unsigned FirstResultSlot;
  867. public:
  868. EmitNodeMatcher(const std::string &opcodeName,
  869. ArrayRef<MVT::SimpleValueType> vts,
  870. ArrayRef<unsigned> operands,
  871. bool hasChain, bool hasInFlag, bool hasOutFlag,
  872. bool hasmemrefs,
  873. int numfixedarityoperands, unsigned firstresultslot)
  874. : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
  875. hasInFlag, hasOutFlag, hasmemrefs,
  876. numfixedarityoperands, false),
  877. FirstResultSlot(firstresultslot) {}
  878. unsigned getFirstResultSlot() const { return FirstResultSlot; }
  879. static bool classof(const Matcher *N) {
  880. return N->getKind() == EmitNode;
  881. }
  882. };
  883. class MorphNodeToMatcher : public EmitNodeMatcherCommon {
  884. void anchor() override;
  885. const PatternToMatch &Pattern;
  886. public:
  887. MorphNodeToMatcher(const std::string &opcodeName,
  888. ArrayRef<MVT::SimpleValueType> vts,
  889. ArrayRef<unsigned> operands,
  890. bool hasChain, bool hasInFlag, bool hasOutFlag,
  891. bool hasmemrefs,
  892. int numfixedarityoperands, const PatternToMatch &pattern)
  893. : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
  894. hasInFlag, hasOutFlag, hasmemrefs,
  895. numfixedarityoperands, true),
  896. Pattern(pattern) {
  897. }
  898. const PatternToMatch &getPattern() const { return Pattern; }
  899. static bool classof(const Matcher *N) {
  900. return N->getKind() == MorphNodeTo;
  901. }
  902. };
  903. /// CompleteMatchMatcher - Complete a match by replacing the results of the
  904. /// pattern with the newly generated nodes. This also prints a comment
  905. /// indicating the source and dest patterns.
  906. class CompleteMatchMatcher : public Matcher {
  907. SmallVector<unsigned, 2> Results;
  908. const PatternToMatch &Pattern;
  909. public:
  910. CompleteMatchMatcher(ArrayRef<unsigned> results,
  911. const PatternToMatch &pattern)
  912. : Matcher(CompleteMatch), Results(results.begin(), results.end()),
  913. Pattern(pattern) {}
  914. unsigned getNumResults() const { return Results.size(); }
  915. unsigned getResult(unsigned R) const { return Results[R]; }
  916. const PatternToMatch &getPattern() const { return Pattern; }
  917. static bool classof(const Matcher *N) {
  918. return N->getKind() == CompleteMatch;
  919. }
  920. private:
  921. void printImpl(raw_ostream &OS, unsigned indent) const override;
  922. bool isEqualImpl(const Matcher *M) const override {
  923. return cast<CompleteMatchMatcher>(M)->Results == Results &&
  924. &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
  925. }
  926. };
  927. } // end namespace llvm
  928. #endif