blob: 085682fd1bc5cdad94749a916d91b0e222e10fae [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 Lattner82781b92010-02-27 07:49:13 +000028void Matcher::printOne(raw_ostream &OS) const {
29 printImpl(OS, 0);
30}
31
Chris Lattnerd6c84722010-02-25 19:00:39 +000032ScopeMatcher::~ScopeMatcher() {
33 for (unsigned i = 0, e = Children.size(); i != e; ++i)
34 delete Children[i];
35}
36
37
38// printImpl methods.
39
Chris Lattnera5028a62010-02-25 06:53:39 +000040void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner60df53e2010-02-25 01:56:48 +000041 OS.indent(indent) << "Scope\n";
Chris Lattnerc78f2a32010-02-28 20:49:53 +000042 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
43 if (getChild(i) == 0)
44 OS.indent(indent+1) << "NULL POINTER\n";
45 else
46 getChild(i)->print(OS, indent+2);
47 }
Chris Lattnerda272d12010-02-15 08:04:42 +000048}
49
Chris Lattnera5028a62010-02-25 06:53:39 +000050void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000051 OS.indent(indent) << "Record\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000052}
53
Chris Lattnera5028a62010-02-25 06:53:39 +000054void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner19b5a752010-02-24 07:31:45 +000055 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +000056}
57
Chris Lattnera5028a62010-02-25 06:53:39 +000058void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +000059 OS.indent(indent) << "RecordMemRef\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000060}
61
Chris Lattnera5028a62010-02-25 06:53:39 +000062void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner8e946be2010-02-21 03:22:59 +000063 OS.indent(indent) << "CaptureFlagInput\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000064}
65
Chris Lattnera5028a62010-02-25 06:53:39 +000066void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000067 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000068}
69
Chris Lattnera5028a62010-02-25 06:53:39 +000070void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000071 OS.indent(indent) << "MoveParent\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000072}
73
Chris Lattnera5028a62010-02-25 06:53:39 +000074void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000075 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000076}
77
Chris Lattnerb21ba712010-02-25 02:04:40 +000078void CheckPatternPredicateMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +000079printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000080 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000081}
82
Chris Lattnera5028a62010-02-25 06:53:39 +000083void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000084 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000085}
86
Chris Lattnera5028a62010-02-25 06:53:39 +000087void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnera230f962010-02-27 21:48:43 +000088 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000089}
90
Chris Lattnera5028a62010-02-25 06:53:39 +000091void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner12a667c2010-02-22 22:30:37 +000092 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
Chris Lattner12a667c2010-02-22 22:30:37 +000093}
94
Chris Lattnera5028a62010-02-25 06:53:39 +000095void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000096 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000097}
98
Chris Lattnera5028a62010-02-25 06:53:39 +000099void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner23cfda72010-02-24 20:15:25 +0000100 OS.indent(indent) << "CheckChildType " << ChildNo << " "
101 << getEnumName(Type) << '\n';
Chris Lattner23cfda72010-02-24 20:15:25 +0000102}
103
104
Chris Lattnera5028a62010-02-25 06:53:39 +0000105void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000106 OS.indent(indent) << "CheckInteger " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000107}
108
Chris Lattnera5028a62010-02-25 06:53:39 +0000109void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000110 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000111}
112
Chris Lattnera5028a62010-02-25 06:53:39 +0000113void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000114 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000115}
116
Chris Lattnera5028a62010-02-25 06:53:39 +0000117void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000118 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000119}
120
Chris Lattnera5028a62010-02-25 06:53:39 +0000121void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000122 OS.indent(indent) << "CheckAndImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000123}
124
Chris Lattnera5028a62010-02-25 06:53:39 +0000125void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000126 OS.indent(indent) << "CheckOrImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000127}
128
Chris Lattnera5028a62010-02-25 06:53:39 +0000129void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
Chris Lattner21390d72010-02-16 19:15:55 +0000130 unsigned indent) const {
131 OS.indent(indent) << "CheckFoldableChainNode\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000132}
Chris Lattner9a747f12010-02-17 06:23:39 +0000133
Chris Lattnera5028a62010-02-25 06:53:39 +0000134void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
Chris Lattner9a747f12010-02-17 06:23:39 +0000135 unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000136 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000137}
Chris Lattner845c0422010-02-18 22:03:03 +0000138
Chris Lattnera5028a62010-02-25 06:53:39 +0000139void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000140 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000141}
142
Chris Lattnerb21ba712010-02-25 02:04:40 +0000143void EmitStringIntegerMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000144printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000145 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000146}
147
Chris Lattnera5028a62010-02-25 06:53:39 +0000148void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000149 OS.indent(indent) << "EmitRegister ";
Chris Lattner845c0422010-02-18 22:03:03 +0000150 if (Reg)
151 OS << Reg->getName();
152 else
153 OS << "zero_reg";
154 OS << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000155}
156
Chris Lattnerb21ba712010-02-25 02:04:40 +0000157void EmitConvertToTargetMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000158printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000159 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000160}
161
Chris Lattnerb21ba712010-02-25 02:04:40 +0000162void EmitMergeInputChainsMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000163printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000164 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000165}
166
Chris Lattnera5028a62010-02-25 06:53:39 +0000167void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000168 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000169}
170
Chris Lattnera5028a62010-02-25 06:53:39 +0000171void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000172 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
173 << " Slot=" << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000174}
175
176
Chris Lattnere86097a2010-02-28 02:31:26 +0000177void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
178 OS.indent(indent);
Chris Lattner6281cda2010-02-28 02:41:25 +0000179 OS << (isa<SelectNodeToMatcher>(this) ? "SelectNodeTo: " : "EmitNode: ")
Chris Lattnere86097a2010-02-28 02:31:26 +0000180 << OpcodeName << ": <todo flags> ";
Chris Lattner8e946be2010-02-21 03:22:59 +0000181
182 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
183 OS << ' ' << getEnumName(VTs[i]);
184 OS << '(';
185 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
186 OS << Operands[i] << ' ';
187 OS << ")\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000188}
189
Chris Lattnera5028a62010-02-25 06:53:39 +0000190void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner02f73582010-02-24 05:33:42 +0000191 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
Chris Lattner02f73582010-02-24 05:33:42 +0000192}
193
Chris Lattnera5028a62010-02-25 06:53:39 +0000194void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner77f2e272010-02-21 06:03:07 +0000195 OS.indent(indent) << "CompleteMatch <todo args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000196 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
197 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000198}
199
Chris Lattner58aa8342010-02-25 06:49:58 +0000200// getHashImpl Implementation.
201
202unsigned CheckPatternPredicateMatcher::getHashImpl() const {
203 return HashString(Predicate);
204}
205
206unsigned CheckPredicateMatcher::getHashImpl() const {
207 return HashString(PredName);
208}
209
210unsigned CheckOpcodeMatcher::getHashImpl() const {
Chris Lattnera230f962010-02-27 21:48:43 +0000211 return HashString(Opcode.getEnumName());
Chris Lattner58aa8342010-02-25 06:49:58 +0000212}
213
214unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
215 unsigned Result = 0;
Chris Lattnera230f962010-02-27 21:48:43 +0000216 for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
217 Result |= HashString(Opcodes[i]->getEnumName());
Chris Lattner58aa8342010-02-25 06:49:58 +0000218 return Result;
219}
220
221unsigned CheckCondCodeMatcher::getHashImpl() const {
222 return HashString(CondCodeName);
223}
224
225unsigned CheckValueTypeMatcher::getHashImpl() const {
226 return HashString(TypeName);
227}
228
229unsigned EmitStringIntegerMatcher::getHashImpl() const {
230 return HashString(Val) ^ VT;
231}
232
233template<typename It>
234static unsigned HashUnsigneds(It I, It E) {
235 unsigned Result = 0;
236 for (; I != E; ++I)
237 Result = (Result<<3) ^ *I;
238 return Result;
239}
240
241unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
242 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
243}
244
Chris Lattnere86097a2010-02-28 02:31:26 +0000245bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
246 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
Chris Lattner58aa8342010-02-25 06:49:58 +0000247 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
248 M->Operands == Operands && M->HasChain == HasChain &&
249 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
250 M->NumFixedArityOperands == NumFixedArityOperands;
251}
252
Chris Lattnere86097a2010-02-28 02:31:26 +0000253unsigned EmitNodeMatcherCommon::getHashImpl() const {
Chris Lattner58aa8342010-02-25 06:49:58 +0000254 return (HashString(OpcodeName) << 4) | Operands.size();
255}
256
257
258unsigned MarkFlagResultsMatcher::getHashImpl() const {
259 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
260}
261
262unsigned CompleteMatchMatcher::getHashImpl() const {
263 return HashUnsigneds(Results.begin(), Results.end()) ^
264 ((unsigned)(intptr_t)&Pattern << 8);
265}
Chris Lattner82781b92010-02-27 07:49:13 +0000266
267// isContradictoryImpl Implementations.
268
Chris Lattner82781b92010-02-27 07:49:13 +0000269static bool TypesAreContradictory(MVT::SimpleValueType T1,
270 MVT::SimpleValueType T2) {
271 // If the two types are the same, then they are the same, so they don't
272 // contradict.
273 if (T1 == T2) return false;
274
275 // If either type is about iPtr, then they don't conflict unless the other
276 // one is not a scalar integer type.
277 if (T1 == MVT::iPTR)
278 return !MVT(T2).isInteger() || MVT(T2).isVector();
279
280 if (T2 == MVT::iPTR)
281 return !MVT(T1).isInteger() || MVT(T1).isVector();
282
283 // Otherwise, they are two different non-iPTR types, they conflict.
284 return true;
285}
286
Chris Lattner22579812010-02-28 00:22:30 +0000287bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
288 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
289 // One node can't have two different opcodes!
290 return &COM->getOpcode() != &getOpcode();
291 }
292
293 // TODO: CheckMultiOpcodeMatcher?
294
295 // If the node has a known type, and if the type we're checking for is
296 // different, then we know they contradict. For example, a check for
297 // ISD::STORE will never be true at the same time a check for Type i32 is.
298 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
299 // FIXME: What result is this referring to?
300 unsigned NodeType;
301 if (getOpcode().getNumResults() == 0)
302 NodeType = MVT::isVoid;
303 else
304 NodeType = getOpcode().getKnownType();
305 if (NodeType != EEVT::isUnknown)
306 return TypesAreContradictory((MVT::SimpleValueType)NodeType,
307 CT->getType());
308 }
309
310 return false;
311}
312
Chris Lattner82781b92010-02-27 07:49:13 +0000313bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
314 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
315 return TypesAreContradictory(getType(), CT->getType());
316 return false;
317}
318
319bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
320 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
321 // If the two checks are about different nodes, we don't know if they
322 // conflict!
323 if (CC->getChildNo() != getChildNo())
324 return false;
325
326 return TypesAreContradictory(getType(), CC->getType());
327 }
328 return false;
329}
330
Chris Lattner24789622010-02-27 08:11:15 +0000331bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
332 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
333 return CIM->getValue() != getValue();
334 return false;
335}