DAGISelMatcher.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
  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. #include "DAGISelMatcher.h"
  9. #include "CodeGenDAGPatterns.h"
  10. #include "CodeGenTarget.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "llvm/TableGen/Record.h"
  13. using namespace llvm;
  14. void Matcher::anchor() { }
  15. void Matcher::dump() const {
  16. print(errs(), 0);
  17. }
  18. void Matcher::print(raw_ostream &OS, unsigned indent) const {
  19. printImpl(OS, indent);
  20. if (Next)
  21. return Next->print(OS, indent);
  22. }
  23. void Matcher::printOne(raw_ostream &OS) const {
  24. printImpl(OS, 0);
  25. }
  26. /// unlinkNode - Unlink the specified node from this chain. If Other == this,
  27. /// we unlink the next pointer and return it. Otherwise we unlink Other from
  28. /// the list and return this.
  29. Matcher *Matcher::unlinkNode(Matcher *Other) {
  30. if (this == Other)
  31. return takeNext();
  32. // Scan until we find the predecessor of Other.
  33. Matcher *Cur = this;
  34. for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
  35. /*empty*/;
  36. if (!Cur) return nullptr;
  37. Cur->takeNext();
  38. Cur->setNext(Other->takeNext());
  39. return this;
  40. }
  41. /// canMoveBefore - Return true if this matcher is the same as Other, or if
  42. /// we can move this matcher past all of the nodes in-between Other and this
  43. /// node. Other must be equal to or before this.
  44. bool Matcher::canMoveBefore(const Matcher *Other) const {
  45. for (;; Other = Other->getNext()) {
  46. assert(Other && "Other didn't come before 'this'?");
  47. if (this == Other) return true;
  48. // We have to be able to move this node across the Other node.
  49. if (!canMoveBeforeNode(Other))
  50. return false;
  51. }
  52. }
  53. /// canMoveBeforeNode - Return true if it is safe to move the current matcher
  54. /// across the specified one.
  55. bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
  56. // We can move simple predicates before record nodes.
  57. if (isSimplePredicateNode())
  58. return Other->isSimplePredicateOrRecordNode();
  59. // We can move record nodes across simple predicates.
  60. if (isSimplePredicateOrRecordNode())
  61. return isSimplePredicateNode();
  62. // We can't move record nodes across each other etc.
  63. return false;
  64. }
  65. ScopeMatcher::~ScopeMatcher() {
  66. for (Matcher *C : Children)
  67. delete C;
  68. }
  69. SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
  70. for (auto &C : Cases)
  71. delete C.second;
  72. }
  73. SwitchTypeMatcher::~SwitchTypeMatcher() {
  74. for (auto &C : Cases)
  75. delete C.second;
  76. }
  77. CheckPredicateMatcher::CheckPredicateMatcher(
  78. const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
  79. : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
  80. Operands(Ops.begin(), Ops.end()) {}
  81. TreePredicateFn CheckPredicateMatcher::getPredicate() const {
  82. return TreePredicateFn(Pred);
  83. }
  84. unsigned CheckPredicateMatcher::getNumOperands() const {
  85. return Operands.size();
  86. }
  87. unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
  88. assert(i < Operands.size());
  89. return Operands[i];
  90. }
  91. // printImpl methods.
  92. void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  93. OS.indent(indent) << "Scope\n";
  94. for (const Matcher *C : Children) {
  95. if (!C)
  96. OS.indent(indent+1) << "NULL POINTER\n";
  97. else
  98. C->print(OS, indent+2);
  99. }
  100. }
  101. void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  102. OS.indent(indent) << "Record\n";
  103. }
  104. void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  105. OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
  106. }
  107. void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  108. OS.indent(indent) << "RecordMemRef\n";
  109. }
  110. void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
  111. OS.indent(indent) << "CaptureGlueInput\n";
  112. }
  113. void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  114. OS.indent(indent) << "MoveChild " << ChildNo << '\n';
  115. }
  116. void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  117. OS.indent(indent) << "MoveParent\n";
  118. }
  119. void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  120. OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
  121. }
  122. void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  123. OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
  124. }
  125. void CheckPatternPredicateMatcher::
  126. printImpl(raw_ostream &OS, unsigned indent) const {
  127. OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
  128. }
  129. void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  130. OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
  131. }
  132. void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  133. OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
  134. }
  135. void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  136. OS.indent(indent) << "SwitchOpcode: {\n";
  137. for (const auto &C : Cases) {
  138. OS.indent(indent) << "case " << C.first->getEnumName() << ":\n";
  139. C.second->print(OS, indent+2);
  140. }
  141. OS.indent(indent) << "}\n";
  142. }
  143. void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  144. OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
  145. << ResNo << '\n';
  146. }
  147. void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  148. OS.indent(indent) << "SwitchType: {\n";
  149. for (const auto &C : Cases) {
  150. OS.indent(indent) << "case " << getEnumName(C.first) << ":\n";
  151. C.second->print(OS, indent+2);
  152. }
  153. OS.indent(indent) << "}\n";
  154. }
  155. void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  156. OS.indent(indent) << "CheckChildType " << ChildNo << " "
  157. << getEnumName(Type) << '\n';
  158. }
  159. void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  160. OS.indent(indent) << "CheckInteger " << Value << '\n';
  161. }
  162. void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
  163. unsigned indent) const {
  164. OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
  165. }
  166. void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  167. OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
  168. }
  169. void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
  170. unsigned indent) const {
  171. OS.indent(indent) << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
  172. }
  173. void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  174. OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
  175. }
  176. void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  177. OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
  178. }
  179. void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  180. OS.indent(indent) << "CheckAndImm " << Value << '\n';
  181. }
  182. void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  183. OS.indent(indent) << "CheckOrImm " << Value << '\n';
  184. }
  185. void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
  186. unsigned indent) const {
  187. OS.indent(indent) << "CheckFoldableChainNode\n";
  188. }
  189. void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS,
  190. unsigned indent) const {
  191. OS.indent(indent) << "CheckAllOnesV\n";
  192. }
  193. void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS,
  194. unsigned indent) const {
  195. OS.indent(indent) << "CheckAllZerosV\n";
  196. }
  197. void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  198. OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
  199. << '\n';
  200. }
  201. void EmitStringIntegerMatcher::
  202. printImpl(raw_ostream &OS, unsigned indent) const {
  203. OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
  204. << '\n';
  205. }
  206. void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  207. OS.indent(indent) << "EmitRegister ";
  208. if (Reg)
  209. OS << Reg->getName();
  210. else
  211. OS << "zero_reg";
  212. OS << " VT=" << getEnumName(VT) << '\n';
  213. }
  214. void EmitConvertToTargetMatcher::
  215. printImpl(raw_ostream &OS, unsigned indent) const {
  216. OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
  217. }
  218. void EmitMergeInputChainsMatcher::
  219. printImpl(raw_ostream &OS, unsigned indent) const {
  220. OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
  221. }
  222. void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  223. OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
  224. }
  225. void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  226. OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
  227. << " Slot=" << Slot << '\n';
  228. }
  229. void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
  230. OS.indent(indent);
  231. OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
  232. << OpcodeName << ": <todo flags> ";
  233. for (unsigned i = 0, e = VTs.size(); i != e; ++i)
  234. OS << ' ' << getEnumName(VTs[i]);
  235. OS << '(';
  236. for (unsigned i = 0, e = Operands.size(); i != e; ++i)
  237. OS << Operands[i] << ' ';
  238. OS << ")\n";
  239. }
  240. void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
  241. OS.indent(indent) << "CompleteMatch <todo args>\n";
  242. OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
  243. OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
  244. }
  245. bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
  246. // Note: pointer equality isn't enough here, we have to check the enum names
  247. // to ensure that the nodes are for the same opcode.
  248. return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
  249. Opcode.getEnumName();
  250. }
  251. bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
  252. const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
  253. return M->OpcodeName == OpcodeName && M->VTs == VTs &&
  254. M->Operands == Operands && M->HasChain == HasChain &&
  255. M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
  256. M->HasMemRefs == HasMemRefs &&
  257. M->NumFixedArityOperands == NumFixedArityOperands;
  258. }
  259. void EmitNodeMatcher::anchor() { }
  260. void MorphNodeToMatcher::anchor() { }
  261. // isContradictoryImpl Implementations.
  262. static bool TypesAreContradictory(MVT::SimpleValueType T1,
  263. MVT::SimpleValueType T2) {
  264. // If the two types are the same, then they are the same, so they don't
  265. // contradict.
  266. if (T1 == T2) return false;
  267. // If either type is about iPtr, then they don't conflict unless the other
  268. // one is not a scalar integer type.
  269. if (T1 == MVT::iPTR)
  270. return !MVT(T2).isInteger() || MVT(T2).isVector();
  271. if (T2 == MVT::iPTR)
  272. return !MVT(T1).isInteger() || MVT(T1).isVector();
  273. // Otherwise, they are two different non-iPTR types, they conflict.
  274. return true;
  275. }
  276. bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
  277. if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
  278. // One node can't have two different opcodes!
  279. // Note: pointer equality isn't enough here, we have to check the enum names
  280. // to ensure that the nodes are for the same opcode.
  281. return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
  282. }
  283. // If the node has a known type, and if the type we're checking for is
  284. // different, then we know they contradict. For example, a check for
  285. // ISD::STORE will never be true at the same time a check for Type i32 is.
  286. if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
  287. // If checking for a result the opcode doesn't have, it can't match.
  288. if (CT->getResNo() >= getOpcode().getNumResults())
  289. return true;
  290. MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
  291. if (NodeType != MVT::Other)
  292. return TypesAreContradictory(NodeType, CT->getType());
  293. }
  294. return false;
  295. }
  296. bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
  297. if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
  298. return TypesAreContradictory(getType(), CT->getType());
  299. return false;
  300. }
  301. bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
  302. if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
  303. // If the two checks are about different nodes, we don't know if they
  304. // conflict!
  305. if (CC->getChildNo() != getChildNo())
  306. return false;
  307. return TypesAreContradictory(getType(), CC->getType());
  308. }
  309. return false;
  310. }
  311. bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
  312. if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
  313. return CIM->getValue() != getValue();
  314. return false;
  315. }
  316. bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
  317. if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) {
  318. // If the two checks are about different nodes, we don't know if they
  319. // conflict!
  320. if (CCIM->getChildNo() != getChildNo())
  321. return false;
  322. return CCIM->getValue() != getValue();
  323. }
  324. return false;
  325. }
  326. bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
  327. if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
  328. return CVT->getTypeName() != getTypeName();
  329. return false;
  330. }
  331. bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
  332. // AllZeros is contradictory.
  333. return isa<CheckImmAllZerosVMatcher>(M);
  334. }
  335. bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
  336. // AllOnes is contradictory.
  337. return isa<CheckImmAllOnesVMatcher>(M);
  338. }
  339. bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
  340. if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(M))
  341. return CCCM->getCondCodeName() != getCondCodeName();
  342. return false;
  343. }
  344. bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
  345. if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(M))
  346. return CCCCM->getCondCodeName() != getCondCodeName();
  347. return false;
  348. }