change the scope node to include a list of children to be checked
[oota-llvm.git] / utils / TableGen / DAGISelMatcher.cpp
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
10 #include "DAGISelMatcher.h"
11 #include "CodeGenDAGPatterns.h"
12 #include "CodeGenTarget.h"
13 #include "Record.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/ADT/StringExtras.h"
16 using namespace llvm;
17
18 void Matcher::dump() const {
19   print(errs(), 0);
20 }
21
22 void Matcher::print(raw_ostream &OS, unsigned indent) const {
23   printImpl(OS, indent);
24   if (Next)
25     return Next->print(OS, indent);
26 }
27
28 ScopeMatcher::~ScopeMatcher() {
29   for (unsigned i = 0, e = Children.size(); i != e; ++i)
30     delete Children[i];
31 }
32
33
34 // printImpl methods.
35
36 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
37   OS.indent(indent) << "Scope\n";
38   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
39     getChild(i)->print(OS, indent+2);
40 }
41
42 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
43   OS.indent(indent) << "Record\n";
44 }
45
46 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
47   OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
48 }
49
50 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
51   OS.indent(indent) << "RecordMemRef\n";
52 }
53
54 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
55   OS.indent(indent) << "CaptureFlagInput\n";
56 }
57
58 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
59   OS.indent(indent) << "MoveChild " << ChildNo << '\n';
60 }
61
62 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
63   OS.indent(indent) << "MoveParent\n";
64 }
65
66 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67   OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
68 }
69
70 void CheckPatternPredicateMatcher::
71 printImpl(raw_ostream &OS, unsigned indent) const {
72   OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
73 }
74
75 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
76   OS.indent(indent) << "CheckPredicate " << PredName << '\n';
77 }
78
79 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
80   OS.indent(indent) << "CheckOpcode " << OpcodeName << '\n';
81 }
82
83 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
84   OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
85 }
86
87 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
88   OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
89 }
90
91 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
92   OS.indent(indent) << "CheckChildType " << ChildNo << " "
93     << getEnumName(Type) << '\n';
94 }
95
96
97 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
98   OS.indent(indent) << "CheckInteger " << Value << '\n';
99 }
100
101 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
102   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
103 }
104
105 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
107 }
108
109 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
111 }
112
113 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
114   OS.indent(indent) << "CheckAndImm " << Value << '\n';
115 }
116
117 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118   OS.indent(indent) << "CheckOrImm " << Value << '\n';
119 }
120
121 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
122                                               unsigned indent) const {
123   OS.indent(indent) << "CheckFoldableChainNode\n";
124 }
125
126 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
127                                               unsigned indent) const {
128   OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
129 }
130
131 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
133 }
134
135 void EmitStringIntegerMatcher::
136 printImpl(raw_ostream &OS, unsigned indent) const {
137   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
138 }
139
140 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
141   OS.indent(indent) << "EmitRegister ";
142   if (Reg)
143     OS << Reg->getName();
144   else
145     OS << "zero_reg";
146   OS << " VT=" << VT << '\n';
147 }
148
149 void EmitConvertToTargetMatcher::
150 printImpl(raw_ostream &OS, unsigned indent) const {
151   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
152 }
153
154 void EmitMergeInputChainsMatcher::
155 printImpl(raw_ostream &OS, unsigned indent) const {
156   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
157 }
158
159 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
160   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
161 }
162
163 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
165      << " Slot=" << Slot << '\n';
166 }
167
168
169 void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
170   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";
178 }
179
180 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
181   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
182 }
183
184 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
185   OS.indent(indent) << "CompleteMatch <todo args>\n";
186   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
187   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
188 }
189
190 // getHashImpl Implementation.
191
192 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
193   return HashString(Predicate);
194 }
195
196 unsigned CheckPredicateMatcher::getHashImpl() const {
197   return HashString(PredName);
198 }
199
200 unsigned CheckOpcodeMatcher::getHashImpl() const {
201   return HashString(OpcodeName);
202 }
203
204 unsigned 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
211 unsigned CheckCondCodeMatcher::getHashImpl() const {
212   return HashString(CondCodeName);
213 }
214
215 unsigned CheckValueTypeMatcher::getHashImpl() const {
216   return HashString(TypeName);
217 }
218
219 unsigned EmitStringIntegerMatcher::getHashImpl() const {
220   return HashString(Val) ^ VT;
221 }
222
223 template<typename It>
224 static 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
231 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
232   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
233 }
234
235 bool 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
243 unsigned EmitNodeMatcher::getHashImpl() const {
244   return (HashString(OpcodeName) << 4) | Operands.size();
245 }
246
247
248 unsigned MarkFlagResultsMatcher::getHashImpl() const {
249   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
250 }
251
252 unsigned CompleteMatchMatcher::getHashImpl() const {
253   return HashUnsigneds(Results.begin(), Results.end()) ^ 
254           ((unsigned)(intptr_t)&Pattern << 8);
255 }