DAGISelMatcher.cpp 14 KB

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