DAGISelMatcher.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. };
  532. /// CheckChild2CondCodeMatcher - This checks to see if child 2 node is a
  533. /// CondCodeSDNode with the specified condition, if not it fails to match.
  534. class CheckChild2CondCodeMatcher : public Matcher {
  535. StringRef CondCodeName;
  536. public:
  537. CheckChild2CondCodeMatcher(StringRef condcodename)
  538. : Matcher(CheckChild2CondCode), CondCodeName(condcodename) {}
  539. StringRef getCondCodeName() const { return CondCodeName; }
  540. static bool classof(const Matcher *N) {
  541. return N->getKind() == CheckChild2CondCode;
  542. }
  543. private:
  544. void printImpl(raw_ostream &OS, unsigned indent) const override;
  545. bool isEqualImpl(const Matcher *M) const override {
  546. return cast<CheckChild2CondCodeMatcher>(M)->CondCodeName == CondCodeName;
  547. }
  548. };
  549. /// CheckValueTypeMatcher - This checks to see if the current node is a
  550. /// VTSDNode with the specified type, if not it fails to match.
  551. class CheckValueTypeMatcher : public Matcher {
  552. StringRef TypeName;
  553. public:
  554. CheckValueTypeMatcher(StringRef type_name)
  555. : Matcher(CheckValueType), TypeName(type_name) {}
  556. StringRef getTypeName() const { return TypeName; }
  557. static bool classof(const Matcher *N) {
  558. return N->getKind() == CheckValueType;
  559. }
  560. private:
  561. void printImpl(raw_ostream &OS, unsigned indent) const override;
  562. bool isEqualImpl(const Matcher *M) const override {
  563. return cast<CheckValueTypeMatcher>(M)->TypeName == TypeName;
  564. }
  565. bool isContradictoryImpl(const Matcher *M) const override;
  566. };
  567. /// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
  568. /// the current node.
  569. class CheckComplexPatMatcher : public Matcher {
  570. const ComplexPattern &Pattern;
  571. /// MatchNumber - This is the recorded nodes slot that contains the node we
  572. /// want to match against.
  573. unsigned MatchNumber;
  574. /// Name - The name of the node we're matching, for comment emission.
  575. std::string Name;
  576. /// FirstResult - This is the first slot in the RecordedNodes list that the
  577. /// result of the match populates.
  578. unsigned FirstResult;
  579. public:
  580. CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
  581. const std::string &name, unsigned firstresult)
  582. : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
  583. Name(name), FirstResult(firstresult) {}
  584. const ComplexPattern &getPattern() const { return Pattern; }
  585. unsigned getMatchNumber() const { return MatchNumber; }
  586. std::string getName() const { return Name; }
  587. unsigned getFirstResult() const { return FirstResult; }
  588. static bool classof(const Matcher *N) {
  589. return N->getKind() == CheckComplexPat;
  590. }
  591. private:
  592. void printImpl(raw_ostream &OS, unsigned indent) const override;
  593. bool isEqualImpl(const Matcher *M) const override {
  594. return &cast<CheckComplexPatMatcher>(M)->Pattern == &Pattern &&
  595. cast<CheckComplexPatMatcher>(M)->MatchNumber == MatchNumber;
  596. }
  597. };
  598. /// CheckAndImmMatcher - This checks to see if the current node is an 'and'
  599. /// with something equivalent to the specified immediate.
  600. class CheckAndImmMatcher : public Matcher {
  601. int64_t Value;
  602. public:
  603. CheckAndImmMatcher(int64_t value)
  604. : Matcher(CheckAndImm), Value(value) {}
  605. int64_t getValue() const { return Value; }
  606. static bool classof(const Matcher *N) {
  607. return N->getKind() == CheckAndImm;
  608. }
  609. private:
  610. void printImpl(raw_ostream &OS, unsigned indent) const override;
  611. bool isEqualImpl(const Matcher *M) const override {
  612. return cast<CheckAndImmMatcher>(M)->Value == Value;
  613. }
  614. };
  615. /// CheckOrImmMatcher - This checks to see if the current node is an 'and'
  616. /// with something equivalent to the specified immediate.
  617. class CheckOrImmMatcher : public Matcher {
  618. int64_t Value;
  619. public:
  620. CheckOrImmMatcher(int64_t value)
  621. : Matcher(CheckOrImm), Value(value) {}
  622. int64_t getValue() const { return Value; }
  623. static bool classof(const Matcher *N) {
  624. return N->getKind() == CheckOrImm;
  625. }
  626. private:
  627. void printImpl(raw_ostream &OS, unsigned indent) const override;
  628. bool isEqualImpl(const Matcher *M) const override {
  629. return cast<CheckOrImmMatcher>(M)->Value == Value;
  630. }
  631. };
  632. /// CheckImmAllOnesVMatcher - This checks if the current node is a build_vector
  633. /// or splat_vector of all ones.
  634. class CheckImmAllOnesVMatcher : public Matcher {
  635. public:
  636. CheckImmAllOnesVMatcher() : Matcher(CheckImmAllOnesV) {}
  637. static bool classof(const Matcher *N) {
  638. return N->getKind() == CheckImmAllOnesV;
  639. }
  640. private:
  641. void printImpl(raw_ostream &OS, unsigned indent) const override;
  642. bool isEqualImpl(const Matcher *M) const override { return true; }
  643. bool isContradictoryImpl(const Matcher *M) const override;
  644. };
  645. /// CheckImmAllZerosVMatcher - This checks if the current node is a
  646. /// build_vector or splat_vector of all zeros.
  647. class CheckImmAllZerosVMatcher : public Matcher {
  648. public:
  649. CheckImmAllZerosVMatcher() : Matcher(CheckImmAllZerosV) {}
  650. static bool classof(const Matcher *N) {
  651. return N->getKind() == CheckImmAllZerosV;
  652. }
  653. private:
  654. void printImpl(raw_ostream &OS, unsigned indent) const override;
  655. bool isEqualImpl(const Matcher *M) const override { return true; }
  656. bool isContradictoryImpl(const Matcher *M) const override;
  657. };
  658. /// CheckFoldableChainNodeMatcher - This checks to see if the current node
  659. /// (which defines a chain operand) is safe to fold into a larger pattern.
  660. class CheckFoldableChainNodeMatcher : public Matcher {
  661. public:
  662. CheckFoldableChainNodeMatcher()
  663. : Matcher(CheckFoldableChainNode) {}
  664. static bool classof(const Matcher *N) {
  665. return N->getKind() == CheckFoldableChainNode;
  666. }
  667. private:
  668. void printImpl(raw_ostream &OS, unsigned indent) const override;
  669. bool isEqualImpl(const Matcher *M) const override { return true; }
  670. };
  671. /// EmitIntegerMatcher - This creates a new TargetConstant.
  672. class EmitIntegerMatcher : public Matcher {
  673. int64_t Val;
  674. MVT::SimpleValueType VT;
  675. public:
  676. EmitIntegerMatcher(int64_t val, MVT::SimpleValueType vt)
  677. : Matcher(EmitInteger), Val(val), VT(vt) {}
  678. int64_t getValue() const { return Val; }
  679. MVT::SimpleValueType getVT() const { return VT; }
  680. static bool classof(const Matcher *N) {
  681. return N->getKind() == EmitInteger;
  682. }
  683. private:
  684. void printImpl(raw_ostream &OS, unsigned indent) const override;
  685. bool isEqualImpl(const Matcher *M) const override {
  686. return cast<EmitIntegerMatcher>(M)->Val == Val &&
  687. cast<EmitIntegerMatcher>(M)->VT == VT;
  688. }
  689. };
  690. /// EmitStringIntegerMatcher - A target constant whose value is represented
  691. /// by a string.
  692. class EmitStringIntegerMatcher : public Matcher {
  693. std::string Val;
  694. MVT::SimpleValueType VT;
  695. public:
  696. EmitStringIntegerMatcher(const std::string &val, MVT::SimpleValueType vt)
  697. : Matcher(EmitStringInteger), Val(val), VT(vt) {}
  698. const std::string &getValue() const { return Val; }
  699. MVT::SimpleValueType getVT() const { return VT; }
  700. static bool classof(const Matcher *N) {
  701. return N->getKind() == EmitStringInteger;
  702. }
  703. private:
  704. void printImpl(raw_ostream &OS, unsigned indent) const override;
  705. bool isEqualImpl(const Matcher *M) const override {
  706. return cast<EmitStringIntegerMatcher>(M)->Val == Val &&
  707. cast<EmitStringIntegerMatcher>(M)->VT == VT;
  708. }
  709. };
  710. /// EmitRegisterMatcher - This creates a new TargetConstant.
  711. class EmitRegisterMatcher : public Matcher {
  712. /// Reg - The def for the register that we're emitting. If this is null, then
  713. /// this is a reference to zero_reg.
  714. const CodeGenRegister *Reg;
  715. MVT::SimpleValueType VT;
  716. public:
  717. EmitRegisterMatcher(const CodeGenRegister *reg, MVT::SimpleValueType vt)
  718. : Matcher(EmitRegister), Reg(reg), VT(vt) {}
  719. const CodeGenRegister *getReg() const { return Reg; }
  720. MVT::SimpleValueType getVT() const { return VT; }
  721. static bool classof(const Matcher *N) {
  722. return N->getKind() == EmitRegister;
  723. }
  724. private:
  725. void printImpl(raw_ostream &OS, unsigned indent) const override;
  726. bool isEqualImpl(const Matcher *M) const override {
  727. return cast<EmitRegisterMatcher>(M)->Reg == Reg &&
  728. cast<EmitRegisterMatcher>(M)->VT == VT;
  729. }
  730. };
  731. /// EmitConvertToTargetMatcher - Emit an operation that reads a specified
  732. /// recorded node and converts it from being a ISD::Constant to
  733. /// ISD::TargetConstant, likewise for ConstantFP.
  734. class EmitConvertToTargetMatcher : public Matcher {
  735. unsigned Slot;
  736. public:
  737. EmitConvertToTargetMatcher(unsigned slot)
  738. : Matcher(EmitConvertToTarget), Slot(slot) {}
  739. unsigned getSlot() const { return Slot; }
  740. static bool classof(const Matcher *N) {
  741. return N->getKind() == EmitConvertToTarget;
  742. }
  743. private:
  744. void printImpl(raw_ostream &OS, unsigned indent) const override;
  745. bool isEqualImpl(const Matcher *M) const override {
  746. return cast<EmitConvertToTargetMatcher>(M)->Slot == Slot;
  747. }
  748. };
  749. /// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
  750. /// chains together with a token factor. The list of nodes are the nodes in the
  751. /// matched pattern that have chain input/outputs. This node adds all input
  752. /// chains of these nodes if they are not themselves a node in the pattern.
  753. class EmitMergeInputChainsMatcher : public Matcher {
  754. SmallVector<unsigned, 3> ChainNodes;
  755. public:
  756. EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
  757. : Matcher(EmitMergeInputChains), ChainNodes(nodes.begin(), nodes.end()) {}
  758. unsigned getNumNodes() const { return ChainNodes.size(); }
  759. unsigned getNode(unsigned i) const {
  760. assert(i < ChainNodes.size());
  761. return ChainNodes[i];
  762. }
  763. static bool classof(const Matcher *N) {
  764. return N->getKind() == EmitMergeInputChains;
  765. }
  766. private:
  767. void printImpl(raw_ostream &OS, unsigned indent) const override;
  768. bool isEqualImpl(const Matcher *M) const override {
  769. return cast<EmitMergeInputChainsMatcher>(M)->ChainNodes == ChainNodes;
  770. }
  771. };
  772. /// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
  773. /// pushing the chain and glue results.
  774. ///
  775. class EmitCopyToRegMatcher : public Matcher {
  776. unsigned SrcSlot; // Value to copy into the physreg.
  777. const CodeGenRegister *DestPhysReg;
  778. public:
  779. EmitCopyToRegMatcher(unsigned srcSlot,
  780. const CodeGenRegister *destPhysReg)
  781. : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
  782. unsigned getSrcSlot() const { return SrcSlot; }
  783. const CodeGenRegister *getDestPhysReg() const { return DestPhysReg; }
  784. static bool classof(const Matcher *N) {
  785. return N->getKind() == EmitCopyToReg;
  786. }
  787. private:
  788. void printImpl(raw_ostream &OS, unsigned indent) const override;
  789. bool isEqualImpl(const Matcher *M) const override {
  790. return cast<EmitCopyToRegMatcher>(M)->SrcSlot == SrcSlot &&
  791. cast<EmitCopyToRegMatcher>(M)->DestPhysReg == DestPhysReg;
  792. }
  793. };
  794. /// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
  795. /// recorded node and records the result.
  796. class EmitNodeXFormMatcher : public Matcher {
  797. unsigned Slot;
  798. Record *NodeXForm;
  799. public:
  800. EmitNodeXFormMatcher(unsigned slot, Record *nodeXForm)
  801. : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm) {}
  802. unsigned getSlot() const { return Slot; }
  803. Record *getNodeXForm() const { return NodeXForm; }
  804. static bool classof(const Matcher *N) {
  805. return N->getKind() == EmitNodeXForm;
  806. }
  807. private:
  808. void printImpl(raw_ostream &OS, unsigned indent) const override;
  809. bool isEqualImpl(const Matcher *M) const override {
  810. return cast<EmitNodeXFormMatcher>(M)->Slot == Slot &&
  811. cast<EmitNodeXFormMatcher>(M)->NodeXForm == NodeXForm;
  812. }
  813. };
  814. /// EmitNodeMatcherCommon - Common class shared between EmitNode and
  815. /// MorphNodeTo.
  816. class EmitNodeMatcherCommon : public Matcher {
  817. std::string OpcodeName;
  818. const SmallVector<MVT::SimpleValueType, 3> VTs;
  819. const SmallVector<unsigned, 6> Operands;
  820. bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
  821. /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
  822. /// If this is a varidic node, this is set to the number of fixed arity
  823. /// operands in the root of the pattern. The rest are appended to this node.
  824. int NumFixedArityOperands;
  825. public:
  826. EmitNodeMatcherCommon(const std::string &opcodeName,
  827. ArrayRef<MVT::SimpleValueType> vts,
  828. ArrayRef<unsigned> operands,
  829. bool hasChain, bool hasInGlue, bool hasOutGlue,
  830. bool hasmemrefs,
  831. int numfixedarityoperands, bool isMorphNodeTo)
  832. : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), OpcodeName(opcodeName),
  833. VTs(vts.begin(), vts.end()), Operands(operands.begin(), operands.end()),
  834. HasChain(hasChain), HasInGlue(hasInGlue), HasOutGlue(hasOutGlue),
  835. HasMemRefs(hasmemrefs), NumFixedArityOperands(numfixedarityoperands) {}
  836. const std::string &getOpcodeName() const { return OpcodeName; }
  837. unsigned getNumVTs() const { return VTs.size(); }
  838. MVT::SimpleValueType getVT(unsigned i) const {
  839. assert(i < VTs.size());
  840. return VTs[i];
  841. }
  842. unsigned getNumOperands() const { return Operands.size(); }
  843. unsigned getOperand(unsigned i) const {
  844. assert(i < Operands.size());
  845. return Operands[i];
  846. }
  847. const SmallVectorImpl<MVT::SimpleValueType> &getVTList() const { return VTs; }
  848. const SmallVectorImpl<unsigned> &getOperandList() const { return Operands; }
  849. bool hasChain() const { return HasChain; }
  850. bool hasInFlag() const { return HasInGlue; }
  851. bool hasOutFlag() const { return HasOutGlue; }
  852. bool hasMemRefs() const { return HasMemRefs; }
  853. int getNumFixedArityOperands() const { return NumFixedArityOperands; }
  854. static bool classof(const Matcher *N) {
  855. return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
  856. }
  857. private:
  858. void printImpl(raw_ostream &OS, unsigned indent) const override;
  859. bool isEqualImpl(const Matcher *M) const override;
  860. };
  861. /// EmitNodeMatcher - This signals a successful match and generates a node.
  862. class EmitNodeMatcher : public EmitNodeMatcherCommon {
  863. void anchor() override;
  864. unsigned FirstResultSlot;
  865. public:
  866. EmitNodeMatcher(const std::string &opcodeName,
  867. ArrayRef<MVT::SimpleValueType> vts,
  868. ArrayRef<unsigned> operands,
  869. bool hasChain, bool hasInFlag, bool hasOutFlag,
  870. bool hasmemrefs,
  871. int numfixedarityoperands, unsigned firstresultslot)
  872. : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
  873. hasInFlag, hasOutFlag, hasmemrefs,
  874. numfixedarityoperands, false),
  875. FirstResultSlot(firstresultslot) {}
  876. unsigned getFirstResultSlot() const { return FirstResultSlot; }
  877. static bool classof(const Matcher *N) {
  878. return N->getKind() == EmitNode;
  879. }
  880. };
  881. class MorphNodeToMatcher : public EmitNodeMatcherCommon {
  882. void anchor() override;
  883. const PatternToMatch &Pattern;
  884. public:
  885. MorphNodeToMatcher(const std::string &opcodeName,
  886. ArrayRef<MVT::SimpleValueType> vts,
  887. ArrayRef<unsigned> operands,
  888. bool hasChain, bool hasInFlag, bool hasOutFlag,
  889. bool hasmemrefs,
  890. int numfixedarityoperands, const PatternToMatch &pattern)
  891. : EmitNodeMatcherCommon(opcodeName, vts, operands, hasChain,
  892. hasInFlag, hasOutFlag, hasmemrefs,
  893. numfixedarityoperands, true),
  894. Pattern(pattern) {
  895. }
  896. const PatternToMatch &getPattern() const { return Pattern; }
  897. static bool classof(const Matcher *N) {
  898. return N->getKind() == MorphNodeTo;
  899. }
  900. };
  901. /// CompleteMatchMatcher - Complete a match by replacing the results of the
  902. /// pattern with the newly generated nodes. This also prints a comment
  903. /// indicating the source and dest patterns.
  904. class CompleteMatchMatcher : public Matcher {
  905. SmallVector<unsigned, 2> Results;
  906. const PatternToMatch &Pattern;
  907. public:
  908. CompleteMatchMatcher(ArrayRef<unsigned> results,
  909. const PatternToMatch &pattern)
  910. : Matcher(CompleteMatch), Results(results.begin(), results.end()),
  911. Pattern(pattern) {}
  912. unsigned getNumResults() const { return Results.size(); }
  913. unsigned getResult(unsigned R) const { return Results[R]; }
  914. const PatternToMatch &getPattern() const { return Pattern; }
  915. static bool classof(const Matcher *N) {
  916. return N->getKind() == CompleteMatch;
  917. }
  918. private:
  919. void printImpl(raw_ostream &OS, unsigned indent) const override;
  920. bool isEqualImpl(const Matcher *M) const override {
  921. return cast<CompleteMatchMatcher>(M)->Results == Results &&
  922. &cast<CompleteMatchMatcher>(M)->Pattern == &Pattern;
  923. }
  924. };
  925. } // end namespace llvm
  926. #endif