blob: 561d6127e8e164839bf8a23c5e4b10baad01c694 [file] [log] [blame]
Chris Lattnerda272d12010-02-15 08:04:42 +00001//===- 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
10#include "DAGISelMatcher.h"
11#include "CodeGenDAGPatterns.h"
12#include "CodeGenTarget.h"
Chris Lattner845c0422010-02-18 22:03:03 +000013#include "Record.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000014#include "llvm/Support/raw_ostream.h"
Chris Lattner58aa8342010-02-25 06:49:58 +000015#include "llvm/ADT/StringExtras.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000016using namespace llvm;
17
Chris Lattnerb21ba712010-02-25 02:04:40 +000018void Matcher::dump() const {
Chris Lattnera5028a62010-02-25 06:53:39 +000019 print(errs(), 0);
Chris Lattnerda272d12010-02-15 08:04:42 +000020}
21
Chris Lattnera5028a62010-02-25 06:53:39 +000022void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
Chris Lattnerbd8227f2010-02-18 02:53:41 +000024 if (Next)
25 return Next->print(OS, indent);
Chris Lattnerda272d12010-02-15 08:04:42 +000026}
27
Chris Lattnerd6c84722010-02-25 19:00:39 +000028ScopeMatcher::~ScopeMatcher() {
29 for (unsigned i = 0, e = Children.size(); i != e; ++i)
30 delete Children[i];
31}
32
33
34// printImpl methods.
35
Chris Lattnera5028a62010-02-25 06:53:39 +000036void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner60df53e2010-02-25 01:56:48 +000037 OS.indent(indent) << "Scope\n";
Chris Lattnerd6c84722010-02-25 19:00:39 +000038 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
39 getChild(i)->print(OS, indent+2);
Chris Lattnerda272d12010-02-15 08:04:42 +000040}
41
Chris Lattnera5028a62010-02-25 06:53:39 +000042void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000043 OS.indent(indent) << "Record\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000044}
45
Chris Lattnera5028a62010-02-25 06:53:39 +000046void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner19b5a752010-02-24 07:31:45 +000047 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +000048}
49
Chris Lattnera5028a62010-02-25 06:53:39 +000050void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +000051 OS.indent(indent) << "RecordMemRef\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000052}
53
Chris Lattnera5028a62010-02-25 06:53:39 +000054void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner8e946be2010-02-21 03:22:59 +000055 OS.indent(indent) << "CaptureFlagInput\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000056}
57
Chris Lattnera5028a62010-02-25 06:53:39 +000058void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000059 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000060}
61
Chris Lattnera5028a62010-02-25 06:53:39 +000062void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000063 OS.indent(indent) << "MoveParent\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000064}
65
Chris Lattnera5028a62010-02-25 06:53:39 +000066void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000067 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000068}
69
Chris Lattnerb21ba712010-02-25 02:04:40 +000070void CheckPatternPredicateMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +000071printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000072 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000073}
74
Chris Lattnera5028a62010-02-25 06:53:39 +000075void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000076 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000077}
78
Chris Lattnera5028a62010-02-25 06:53:39 +000079void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000080 OS.indent(indent) << "CheckOpcode " << OpcodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000081}
82
Chris Lattnera5028a62010-02-25 06:53:39 +000083void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner12a667c2010-02-22 22:30:37 +000084 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
Chris Lattner12a667c2010-02-22 22:30:37 +000085}
86
Chris Lattnera5028a62010-02-25 06:53:39 +000087void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000088 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000089}
90
Chris Lattnera5028a62010-02-25 06:53:39 +000091void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner23cfda72010-02-24 20:15:25 +000092 OS.indent(indent) << "CheckChildType " << ChildNo << " "
93 << getEnumName(Type) << '\n';
Chris Lattner23cfda72010-02-24 20:15:25 +000094}
95
96
Chris Lattnera5028a62010-02-25 06:53:39 +000097void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000098 OS.indent(indent) << "CheckInteger " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000099}
100
Chris Lattnera5028a62010-02-25 06:53:39 +0000101void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000102 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000103}
104
Chris Lattnera5028a62010-02-25 06:53:39 +0000105void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000106 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000107}
108
Chris Lattnera5028a62010-02-25 06:53:39 +0000109void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000110 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000111}
112
Chris Lattnera5028a62010-02-25 06:53:39 +0000113void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000114 OS.indent(indent) << "CheckAndImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000115}
116
Chris Lattnera5028a62010-02-25 06:53:39 +0000117void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000118 OS.indent(indent) << "CheckOrImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000119}
120
Chris Lattnera5028a62010-02-25 06:53:39 +0000121void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
Chris Lattner21390d72010-02-16 19:15:55 +0000122 unsigned indent) const {
123 OS.indent(indent) << "CheckFoldableChainNode\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000124}
Chris Lattner9a747f12010-02-17 06:23:39 +0000125
Chris Lattnera5028a62010-02-25 06:53:39 +0000126void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
Chris Lattner9a747f12010-02-17 06:23:39 +0000127 unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000128 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000129}
Chris Lattner845c0422010-02-18 22:03:03 +0000130
Chris Lattnera5028a62010-02-25 06:53:39 +0000131void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000132 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000133}
134
Chris Lattnerb21ba712010-02-25 02:04:40 +0000135void EmitStringIntegerMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000136printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000137 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000138}
139
Chris Lattnera5028a62010-02-25 06:53:39 +0000140void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000141 OS.indent(indent) << "EmitRegister ";
Chris Lattner845c0422010-02-18 22:03:03 +0000142 if (Reg)
143 OS << Reg->getName();
144 else
145 OS << "zero_reg";
146 OS << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000147}
148
Chris Lattnerb21ba712010-02-25 02:04:40 +0000149void EmitConvertToTargetMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000150printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000151 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000152}
153
Chris Lattnerb21ba712010-02-25 02:04:40 +0000154void EmitMergeInputChainsMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000155printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000156 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000157}
158
Chris Lattnera5028a62010-02-25 06:53:39 +0000159void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000160 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000161}
162
Chris Lattnera5028a62010-02-25 06:53:39 +0000163void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000164 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
165 << " Slot=" << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000166}
167
168
Chris Lattnera5028a62010-02-25 06:53:39 +0000169void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000170 OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
171
172 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
173 OS << ' ' << getEnumName(VTs[i]);
174 OS << '(';
175 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
176 OS << Operands[i] << ' ';
177 OS << ")\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000178}
179
Chris Lattnera5028a62010-02-25 06:53:39 +0000180void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner02f73582010-02-24 05:33:42 +0000181 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
Chris Lattner02f73582010-02-24 05:33:42 +0000182}
183
Chris Lattnera5028a62010-02-25 06:53:39 +0000184void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner77f2e272010-02-21 06:03:07 +0000185 OS.indent(indent) << "CompleteMatch <todo args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000186 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
187 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000188}
189
Chris Lattner58aa8342010-02-25 06:49:58 +0000190// getHashImpl Implementation.
191
192unsigned CheckPatternPredicateMatcher::getHashImpl() const {
193 return HashString(Predicate);
194}
195
196unsigned CheckPredicateMatcher::getHashImpl() const {
197 return HashString(PredName);
198}
199
200unsigned CheckOpcodeMatcher::getHashImpl() const {
201 return HashString(OpcodeName);
202}
203
204unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
205 unsigned Result = 0;
206 for (unsigned i = 0, e = OpcodeNames.size(); i != e; ++i)
207 Result |= HashString(OpcodeNames[i]);
208 return Result;
209}
210
211unsigned CheckCondCodeMatcher::getHashImpl() const {
212 return HashString(CondCodeName);
213}
214
215unsigned CheckValueTypeMatcher::getHashImpl() const {
216 return HashString(TypeName);
217}
218
219unsigned EmitStringIntegerMatcher::getHashImpl() const {
220 return HashString(Val) ^ VT;
221}
222
223template<typename It>
224static unsigned HashUnsigneds(It I, It E) {
225 unsigned Result = 0;
226 for (; I != E; ++I)
227 Result = (Result<<3) ^ *I;
228 return Result;
229}
230
231unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
232 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
233}
234
235bool EmitNodeMatcher::isEqualImpl(const Matcher *m) const {
236 const EmitNodeMatcher *M = cast<EmitNodeMatcher>(m);
237 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
238 M->Operands == Operands && M->HasChain == HasChain &&
239 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
240 M->NumFixedArityOperands == NumFixedArityOperands;
241}
242
243unsigned EmitNodeMatcher::getHashImpl() const {
244 return (HashString(OpcodeName) << 4) | Operands.size();
245}
246
247
248unsigned MarkFlagResultsMatcher::getHashImpl() const {
249 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
250}
251
252unsigned CompleteMatchMatcher::getHashImpl() const {
253 return HashUnsigneds(Results.begin(), Results.end()) ^
254 ((unsigned)(intptr_t)&Pattern << 8);
255}