blob: e01292801c6e3abdd1f0063d9edc5fd9a3da08c3 [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 Lattnera5028a62010-02-25 06:53:39 +000028void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner60df53e2010-02-25 01:56:48 +000029 OS.indent(indent) << "Scope\n";
30 Check->print(OS, indent+2);
Chris Lattnerda272d12010-02-15 08:04:42 +000031}
32
Chris Lattnera5028a62010-02-25 06:53:39 +000033void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000034 OS.indent(indent) << "Record\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000035}
36
Chris Lattnera5028a62010-02-25 06:53:39 +000037void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner19b5a752010-02-24 07:31:45 +000038 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +000039}
40
Chris Lattnera5028a62010-02-25 06:53:39 +000041void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +000042 OS.indent(indent) << "RecordMemRef\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000043}
44
Chris Lattnera5028a62010-02-25 06:53:39 +000045void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner8e946be2010-02-21 03:22:59 +000046 OS.indent(indent) << "CaptureFlagInput\n";
Chris Lattner8e946be2010-02-21 03:22:59 +000047}
48
Chris Lattnera5028a62010-02-25 06:53:39 +000049void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000050 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000051}
52
Chris Lattnera5028a62010-02-25 06:53:39 +000053void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000054 OS.indent(indent) << "MoveParent\n";
Chris Lattnerda272d12010-02-15 08:04:42 +000055}
56
Chris Lattnera5028a62010-02-25 06:53:39 +000057void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000058 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000059}
60
Chris Lattnerb21ba712010-02-25 02:04:40 +000061void CheckPatternPredicateMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +000062printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000063 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000064}
65
Chris Lattnera5028a62010-02-25 06:53:39 +000066void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000067 OS.indent(indent) << "CheckPredicate " << PredName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000068}
69
Chris Lattnera5028a62010-02-25 06:53:39 +000070void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000071 OS.indent(indent) << "CheckOpcode " << OpcodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000072}
73
Chris Lattnera5028a62010-02-25 06:53:39 +000074void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
Chris Lattner12a667c2010-02-22 22:30:37 +000075 OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
Chris Lattner12a667c2010-02-22 22:30:37 +000076}
77
Chris Lattnera5028a62010-02-25 06:53:39 +000078void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000079 OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000080}
81
Chris Lattnera5028a62010-02-25 06:53:39 +000082void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner23cfda72010-02-24 20:15:25 +000083 OS.indent(indent) << "CheckChildType " << ChildNo << " "
84 << getEnumName(Type) << '\n';
Chris Lattner23cfda72010-02-24 20:15:25 +000085}
86
87
Chris Lattnera5028a62010-02-25 06:53:39 +000088void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000089 OS.indent(indent) << "CheckInteger " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000090}
91
Chris Lattnera5028a62010-02-25 06:53:39 +000092void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000093 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000094}
95
Chris Lattnera5028a62010-02-25 06:53:39 +000096void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +000097 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000098}
99
Chris Lattnera5028a62010-02-25 06:53:39 +0000100void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000101 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000102}
103
Chris Lattnera5028a62010-02-25 06:53:39 +0000104void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000105 OS.indent(indent) << "CheckAndImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000106}
107
Chris Lattnera5028a62010-02-25 06:53:39 +0000108void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattnerda272d12010-02-15 08:04:42 +0000109 OS.indent(indent) << "CheckOrImm " << Value << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000110}
111
Chris Lattnera5028a62010-02-25 06:53:39 +0000112void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
Chris Lattner21390d72010-02-16 19:15:55 +0000113 unsigned indent) const {
114 OS.indent(indent) << "CheckFoldableChainNode\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000115}
Chris Lattner9a747f12010-02-17 06:23:39 +0000116
Chris Lattnera5028a62010-02-25 06:53:39 +0000117void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
Chris Lattner9a747f12010-02-17 06:23:39 +0000118 unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000119 OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000120}
Chris Lattner845c0422010-02-18 22:03:03 +0000121
Chris Lattnera5028a62010-02-25 06:53:39 +0000122void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000123 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000124}
125
Chris Lattnerb21ba712010-02-25 02:04:40 +0000126void EmitStringIntegerMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000127printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000128 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000129}
130
Chris Lattnera5028a62010-02-25 06:53:39 +0000131void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000132 OS.indent(indent) << "EmitRegister ";
Chris Lattner845c0422010-02-18 22:03:03 +0000133 if (Reg)
134 OS << Reg->getName();
135 else
136 OS << "zero_reg";
137 OS << " VT=" << VT << '\n';
Chris Lattner845c0422010-02-18 22:03:03 +0000138}
139
Chris Lattnerb21ba712010-02-25 02:04:40 +0000140void EmitConvertToTargetMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000141printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000142 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000143}
144
Chris Lattnerb21ba712010-02-25 02:04:40 +0000145void EmitMergeInputChainsMatcher::
Chris Lattnera5028a62010-02-25 06:53:39 +0000146printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000147 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000148}
149
Chris Lattnera5028a62010-02-25 06:53:39 +0000150void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000151 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000152}
153
Chris Lattnera5028a62010-02-25 06:53:39 +0000154void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000155 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
156 << " Slot=" << Slot << '\n';
Chris Lattner8e946be2010-02-21 03:22:59 +0000157}
158
159
Chris Lattnera5028a62010-02-25 06:53:39 +0000160void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner8e946be2010-02-21 03:22:59 +0000161 OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
162
163 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
164 OS << ' ' << getEnumName(VTs[i]);
165 OS << '(';
166 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
167 OS << Operands[i] << ' ';
168 OS << ")\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000169}
170
Chris Lattnera5028a62010-02-25 06:53:39 +0000171void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner02f73582010-02-24 05:33:42 +0000172 OS.indent(indent) << "MarkFlagResults <todo: args>\n";
Chris Lattner02f73582010-02-24 05:33:42 +0000173}
174
Chris Lattnera5028a62010-02-25 06:53:39 +0000175void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
Chris Lattner77f2e272010-02-21 06:03:07 +0000176 OS.indent(indent) << "CompleteMatch <todo args>\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000177 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
178 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000179}
180
Chris Lattner58aa8342010-02-25 06:49:58 +0000181// getHashImpl Implementation.
182
183unsigned CheckPatternPredicateMatcher::getHashImpl() const {
184 return HashString(Predicate);
185}
186
187unsigned CheckPredicateMatcher::getHashImpl() const {
188 return HashString(PredName);
189}
190
191unsigned CheckOpcodeMatcher::getHashImpl() const {
192 return HashString(OpcodeName);
193}
194
195unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
196 unsigned Result = 0;
197 for (unsigned i = 0, e = OpcodeNames.size(); i != e; ++i)
198 Result |= HashString(OpcodeNames[i]);
199 return Result;
200}
201
202unsigned CheckCondCodeMatcher::getHashImpl() const {
203 return HashString(CondCodeName);
204}
205
206unsigned CheckValueTypeMatcher::getHashImpl() const {
207 return HashString(TypeName);
208}
209
210unsigned EmitStringIntegerMatcher::getHashImpl() const {
211 return HashString(Val) ^ VT;
212}
213
214template<typename It>
215static unsigned HashUnsigneds(It I, It E) {
216 unsigned Result = 0;
217 for (; I != E; ++I)
218 Result = (Result<<3) ^ *I;
219 return Result;
220}
221
222unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
223 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
224}
225
226bool EmitNodeMatcher::isEqualImpl(const Matcher *m) const {
227 const EmitNodeMatcher *M = cast<EmitNodeMatcher>(m);
228 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
229 M->Operands == Operands && M->HasChain == HasChain &&
230 M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
231 M->NumFixedArityOperands == NumFixedArityOperands;
232}
233
234unsigned EmitNodeMatcher::getHashImpl() const {
235 return (HashString(OpcodeName) << 4) | Operands.size();
236}
237
238
239unsigned MarkFlagResultsMatcher::getHashImpl() const {
240 return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
241}
242
243unsigned CompleteMatchMatcher::getHashImpl() const {
244 return HashUnsigneds(Results.begin(), Results.end()) ^
245 ((unsigned)(intptr_t)&Pattern << 8);
246}