PageRenderTime 1799ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/llvm/utils/TableGen/DAGISelMatcher.cpp

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