blob: c4f1cbfcf058df8742f283f4a20a776e52764d85 [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 Lattnereb669212010-03-01 06:59:22 +000091void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
92 OS.indent(indent) << "SwitchOpcode: {\n";
93 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
94 OS.indent(indent) << "case " << Cases[i].first->getEnumName() << ":\n";
95 Cases[i].second->print(OS, indent+2);
96 }
97 OS.indent(indent) << "}\n";
98}
99
100
Chris Lattnera5028a62010-02-25 06:53:39 +0000101void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000102 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000103}
104
Chris Lattnercfe2eab2010-03-03 06:28:15 +0000105void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106 OS.indent(indent) << "SwitchType: {\n";
107 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
108 OS.indent(indent) << "case " << getEnumName(Cases[i].first) << ":\n";
109 Cases[i].second->print(OS, indent+2);
110 }
111 OS.indent(indent) << "}\n";
112}
113
Chris Lattnera5028a62010-02-25 06:53:39 +0000114void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner23cfda72010-02-24 20:15:25 +0000115 OS.indent(indent) << "CheckChildType " << ChildNo << " "
116 << getEnumName(Type) << '\n';
Chris Lattner23cfda72010-02-24 20:15:25 +0000117}
118
119
Chris Lattnera5028a62010-02-25 06:53:39 +0000120void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000121 OS.indent(indent) << "CheckInteger " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000122}
123
Chris Lattnera5028a62010-02-25 06:53:39 +0000124void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000125 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000126}
127
Chris Lattnera5028a62010-02-25 06:53:39 +0000128void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000129 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000130}
131
Chris Lattnera5028a62010-02-25 06:53:39 +0000132void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000133 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000134}
135
Chris Lattnera5028a62010-02-25 06:53:39 +0000136void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000137 OS.indent(indent) << "CheckAndImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000138}
139
Chris Lattnera5028a62010-02-25 06:53:39 +0000140void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000141 OS.indent(indent) << "CheckOrImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000142}
143
Chris Lattnera5028a62010-02-25 06:53:39 +0000144void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
Chris Lattner21390d72010-02-16 19:15:55 +0000145 unsigned indent) const {
146 OS.indent(indent) << "CheckFoldableChainNode\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000147}
Chris Lattner9a747f12010-02-17 06:23:39 +0000148
Chris Lattnera5028a62010-02-25 06:53:39 +0000149void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000150 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000151}
152
Chris Lattnerb21ba712010-02-25 02:04:40 +0000153void EmitStringIntegerMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000154printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000155 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000156}
157
Chris Lattnera5028a62010-02-25 06:53:39 +0000158void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000159 OS.indent(indent) << "EmitRegister ";
Chris Lattner845c0422010-02-18 22:03:03 +0000160 if (Reg)
161 OS << Reg->getName();
162 else
163 OS << "zero_reg";
164 OS << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000165}
166
Chris Lattnerb21ba712010-02-25 02:04:40 +0000167void EmitConvertToTargetMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000168printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000169 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000170}
171
Chris Lattnerb21ba712010-02-25 02:04:40 +0000172void EmitMergeInputChainsMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000173printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000174 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000175}
176
Chris Lattnera5028a62010-02-25 06:53:39 +0000177void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000178 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000179}
180
Chris Lattnera5028a62010-02-25 06:53:39 +0000181void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000182 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
183 << " Slot=" << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000184}
185
186
Chris Lattnere86097a2010-02-28 02:31:26 +0000187void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
188 OS.indent(indent);
Chris Lattner9a215002010-02-28 20:55:18 +0000189 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
Chris Lattnere86097a2010-02-28 02:31:26 +0000190 << OpcodeName << ": <todo flags> ";
Chris Lattner8e946be2010-02-21 03:22:59 +0000191
192 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
193 OS << ' ' << getEnumName(VTs[i]);
194 OS << '(';
195 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
196 OS << Operands[i] << ' ';
197 OS << ")\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000198}
199
Chris Lattnera5028a62010-02-25 06:53:39 +0000200void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner02f73582010-02-24 05:33:42 +0000201 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
Chris Lattner02f73582010-02-24 05:33:42 +0000202}
203
Chris Lattnera5028a62010-02-25 06:53:39 +0000204void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner77f2e272010-02-21 06:03:07 +0000205 OS.indent(indent) << "CompleteMatch <todo args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000206 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
207 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000208}
209
Chris Lattner58aa8342010-02-25 06:49:58 +0000210// getHashImpl Implementation.
211
212unsigned CheckPatternPredicateMatcher::getHashImpl() const {
213 return HashString(Predicate);
214}
215
216unsigned CheckPredicateMatcher::getHashImpl() const {
217 return HashString(PredName);
218}
219
220unsigned CheckOpcodeMatcher::getHashImpl() const {
Chris Lattnera230f962010-02-27 21:48:43 +0000221 return HashString(Opcode.getEnumName());
Chris Lattner58aa8342010-02-25 06:49:58 +0000222}
223
Chris Lattner58aa8342010-02-25 06:49:58 +0000224unsigned CheckCondCodeMatcher::getHashImpl() const {
225 return HashString(CondCodeName);
226}
227
228unsigned CheckValueTypeMatcher::getHashImpl() const {
229 return HashString(TypeName);
230}
231
232unsigned EmitStringIntegerMatcher::getHashImpl() const {
233 return HashString(Val) ^ VT;
234}
235
236template<typename It>
237static unsigned HashUnsigneds(It I, It E) {
238 unsigned Result = 0;
239 for (; I != E; ++I)
240 Result = (Result<<3) ^ *I;
241 return Result;
242}
243
244unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
245 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
246}
247
Chris Lattnereb669212010-03-01 06:59:22 +0000248bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
249 // Note: pointer equality isn't enough here, we have to check the enum names
250 // to ensure that the nodes are for the same opcode.
251 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
252 Opcode.getEnumName();
253}
254
255
Chris Lattnere86097a2010-02-28 02:31:26 +0000256bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
257 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
Chris Lattner58aa8342010-02-25 06:49:58 +0000258 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
259 M->Operands == Operands && M->HasChain == HasChain &&
Chris Lattnerff7fb602010-02-28 21:53:42 +0000260 M->HasInFlag == HasInFlag && M->HasOutFlag == HasOutFlag &&
261 M->HasMemRefs == HasMemRefs &&
Chris Lattner58aa8342010-02-25 06:49:58 +0000262 M->NumFixedArityOperands == NumFixedArityOperands;
263}
264
Chris Lattnere86097a2010-02-28 02:31:26 +0000265unsigned EmitNodeMatcherCommon::getHashImpl() const {
Chris Lattner58aa8342010-02-25 06:49:58 +0000266 return (HashString(OpcodeName) << 4) | Operands.size();
267}
268
269
270unsigned MarkFlagResultsMatcher::getHashImpl() const {
271 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
272}
273
274unsigned CompleteMatchMatcher::getHashImpl() const {
275 return HashUnsigneds(Results.begin(), Results.end()) ^
276 ((unsigned)(intptr_t)&Pattern << 8);
277}
Chris Lattner82781b92010-02-27 07:49:13 +0000278
279// isContradictoryImpl Implementations.
280
Chris Lattner82781b92010-02-27 07:49:13 +0000281static bool TypesAreContradictory(MVT::SimpleValueType T1,
282 MVT::SimpleValueType T2) {
283 // If the two types are the same, then they are the same, so they don't
284 // contradict.
285 if (T1 == T2) return false;
286
287 // If either type is about iPtr, then they don't conflict unless the other
288 // one is not a scalar integer type.
289 if (T1 == MVT::iPTR)
290 return !MVT(T2).isInteger() || MVT(T2).isVector();
291
292 if (T2 == MVT::iPTR)
293 return !MVT(T1).isInteger() || MVT(T1).isVector();
294
295 // Otherwise, they are two different non-iPTR types, they conflict.
296 return true;
297}
298
Chris Lattner22579812010-02-28 00:22:30 +0000299bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
300 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
301 // One node can't have two different opcodes!
Chris Lattnereb669212010-03-01 06:59:22 +0000302 // Note: pointer equality isn't enough here, we have to check the enum names
303 // to ensure that the nodes are for the same opcode.
304 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
Chris Lattner22579812010-02-28 00:22:30 +0000305 }
306
Chris Lattner22579812010-02-28 00:22:30 +0000307 // If the node has a known type, and if the type we're checking for is
308 // different, then we know they contradict. For example, a check for
309 // ISD::STORE will never be true at the same time a check for Type i32 is.
310 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
311 // FIXME: What result is this referring to?
312 unsigned NodeType;
313 if (getOpcode().getNumResults() == 0)
314 NodeType = MVT::isVoid;
315 else
316 NodeType = getOpcode().getKnownType();
317 if (NodeType != EEVT::isUnknown)
318 return TypesAreContradictory((MVT::SimpleValueType)NodeType,
319 CT->getType());
320 }
321
322 return false;
323}
324
Chris Lattner82781b92010-02-27 07:49:13 +0000325bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
326 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
327 return TypesAreContradictory(getType(), CT->getType());
328 return false;
329}
330
331bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
332 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
333 // If the two checks are about different nodes, we don't know if they
334 // conflict!
335 if (CC->getChildNo() != getChildNo())
336 return false;
337
338 return TypesAreContradictory(getType(), CC->getType());
339 }
340 return false;
341}
342
Chris Lattner24789622010-02-27 08:11:15 +0000343bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
344 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
345 return CIM->getValue() != getValue();
346 return false;
347}