1f1bb60c37c52393b16202a8476a70cb88e46541
[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 void Matcher::printOne(raw_ostream &OS) const {
29   printImpl(OS, 0);
30 }
31
32 ScopeMatcher::~ScopeMatcher() {
33   for (unsigned i = 0, e = Children.size(); i != e; ++i)
34     delete Children[i];
35 }
36
37
38 // printImpl methods.
39
40 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
41   OS.indent(indent) << "Scope\n";
42   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   }
48 }
49
50 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
51   OS.indent(indent) << "Record\n";
52 }
53
54 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
55   OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
56 }
57
58 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
59   OS.indent(indent) << "RecordMemRef\n";
60 }
61
62 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
63   OS.indent(indent) << "CaptureFlagInput\n";
64 }
65
66 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67   OS.indent(indent) << "MoveChild " << ChildNo << '\n';
68 }
69
70 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
71   OS.indent(indent) << "MoveParent\n";
72 }
73
74 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
75   OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
76 }
77
78 void CheckPatternPredicateMatcher::
79 printImpl(raw_ostream &OS, unsigned indent) const {
80   OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
81 }
82
83 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
84   OS.indent(indent) << "CheckPredicate " << PredName << '\n';
85 }
86
87 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
88   OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
89 }
90
91 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
92   OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
93 }
94
95 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
96   OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
97 }
98
99 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
100   OS.indent(indent) << "CheckChildType " << ChildNo << " "
101     << getEnumName(Type) << '\n';
102 }
103
104
105 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106   OS.indent(indent) << "CheckInteger " << Value << '\n';
107 }
108
109 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
111 }
112
113 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
114   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
115 }
116
117 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
119 }
120
121 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
122   OS.indent(indent) << "CheckAndImm " << Value << '\n';
123 }
124
125 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
126   OS.indent(indent) << "CheckOrImm " << Value << '\n';
127 }
128
129 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
130                                               unsigned indent) const {
131   OS.indent(indent) << "CheckFoldableChainNode\n";
132 }
133
134 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
135                                               unsigned indent) const {
136   OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
137 }
138
139 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
140   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
141 }
142
143 void EmitStringIntegerMatcher::
144 printImpl(raw_ostream &OS, unsigned indent) const {
145   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
146 }
147
148 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
149   OS.indent(indent) << "EmitRegister ";
150   if (Reg)
151     OS << Reg->getName();
152   else
153     OS << "zero_reg";
154   OS << " VT=" << VT << '\n';
155 }
156
157 void EmitConvertToTargetMatcher::
158 printImpl(raw_ostream &OS, unsigned indent) const {
159   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
160 }
161
162 void EmitMergeInputChainsMatcher::
163 printImpl(raw_ostream &OS, unsigned indent) const {
164   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
165 }
166
167 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
168   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
169 }
170
171 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
172   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
173      << " Slot=" << Slot << '\n';
174 }
175
176
177 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
178   OS.indent(indent);
179   OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
180      << OpcodeName << ": <todo flags> ";
181
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";
188 }
189
190 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
191   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
192 }
193
194 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
195   OS.indent(indent) << "CompleteMatch <todo args>\n";
196   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
197   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
198 }
199
200 // getHashImpl Implementation.
201
202 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
203   return HashString(Predicate);
204 }
205
206 unsigned CheckPredicateMatcher::getHashImpl() const {
207   return HashString(PredName);
208 }
209
210 unsigned CheckOpcodeMatcher::getHashImpl() const {
211   return HashString(Opcode.getEnumName());
212 }
213
214 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
215   unsigned Result = 0;
216   for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
217     Result |= HashString(Opcodes[i]->getEnumName());
218   return Result;
219 }
220
221 unsigned CheckCondCodeMatcher::getHashImpl() const {
222   return HashString(CondCodeName);
223 }
224
225 unsigned CheckValueTypeMatcher::getHashImpl() const {
226   return HashString(TypeName);
227 }
228
229 unsigned EmitStringIntegerMatcher::getHashImpl() const {
230   return HashString(Val) ^ VT;
231 }
232
233 template<typename It>
234 static 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
241 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
242   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
243 }
244
245 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
246   const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
247   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
253 unsigned EmitNodeMatcherCommon::getHashImpl() const {
254   return (HashString(OpcodeName) << 4) | Operands.size();
255 }
256
257
258 unsigned MarkFlagResultsMatcher::getHashImpl() const {
259   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
260 }
261
262 unsigned CompleteMatchMatcher::getHashImpl() const {
263   return HashUnsigneds(Results.begin(), Results.end()) ^ 
264           ((unsigned)(intptr_t)&Pattern << 8);
265 }
266
267 // isContradictoryImpl Implementations.
268
269 static 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
287 bool 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
313 bool 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
319 bool 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   
331 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
332   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
333     return CIM->getValue() != getValue();
334   return false;
335 }