introduce a new SwitchTypeMatcher node (which is analogous to
[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 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
101 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
102   OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
103 }
104
105 void 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
114 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
115   OS.indent(indent) << "CheckChildType " << ChildNo << " "
116     << getEnumName(Type) << '\n';
117 }
118
119
120 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
121   OS.indent(indent) << "CheckInteger " << Value << '\n';
122 }
123
124 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
125   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
126 }
127
128 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
129   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
130 }
131
132 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
133   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
134 }
135
136 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
137   OS.indent(indent) << "CheckAndImm " << Value << '\n';
138 }
139
140 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
141   OS.indent(indent) << "CheckOrImm " << Value << '\n';
142 }
143
144 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
145                                               unsigned indent) const {
146   OS.indent(indent) << "CheckFoldableChainNode\n";
147 }
148
149 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
150   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
151 }
152
153 void EmitStringIntegerMatcher::
154 printImpl(raw_ostream &OS, unsigned indent) const {
155   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
156 }
157
158 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
159   OS.indent(indent) << "EmitRegister ";
160   if (Reg)
161     OS << Reg->getName();
162   else
163     OS << "zero_reg";
164   OS << " VT=" << VT << '\n';
165 }
166
167 void EmitConvertToTargetMatcher::
168 printImpl(raw_ostream &OS, unsigned indent) const {
169   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
170 }
171
172 void EmitMergeInputChainsMatcher::
173 printImpl(raw_ostream &OS, unsigned indent) const {
174   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
175 }
176
177 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
178   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
179 }
180
181 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
182   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
183      << " Slot=" << Slot << '\n';
184 }
185
186
187 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
188   OS.indent(indent);
189   OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
190      << OpcodeName << ": <todo flags> ";
191
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";
198 }
199
200 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
201   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
202 }
203
204 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
205   OS.indent(indent) << "CompleteMatch <todo args>\n";
206   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
207   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
208 }
209
210 // getHashImpl Implementation.
211
212 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
213   return HashString(Predicate);
214 }
215
216 unsigned CheckPredicateMatcher::getHashImpl() const {
217   return HashString(PredName);
218 }
219
220 unsigned CheckOpcodeMatcher::getHashImpl() const {
221   return HashString(Opcode.getEnumName());
222 }
223
224 unsigned CheckCondCodeMatcher::getHashImpl() const {
225   return HashString(CondCodeName);
226 }
227
228 unsigned CheckValueTypeMatcher::getHashImpl() const {
229   return HashString(TypeName);
230 }
231
232 unsigned EmitStringIntegerMatcher::getHashImpl() const {
233   return HashString(Val) ^ VT;
234 }
235
236 template<typename It>
237 static 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
244 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
245   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
246 }
247
248 bool 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
256 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
257   const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
258   return M->OpcodeName == OpcodeName && M->VTs == VTs &&
259          M->Operands == Operands && M->HasChain == HasChain &&
260          M->HasInFlag == HasInFlag && M->HasOutFlag == HasOutFlag &&
261          M->HasMemRefs == HasMemRefs &&
262          M->NumFixedArityOperands == NumFixedArityOperands;
263 }
264
265 unsigned EmitNodeMatcherCommon::getHashImpl() const {
266   return (HashString(OpcodeName) << 4) | Operands.size();
267 }
268
269
270 unsigned MarkFlagResultsMatcher::getHashImpl() const {
271   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
272 }
273
274 unsigned CompleteMatchMatcher::getHashImpl() const {
275   return HashUnsigneds(Results.begin(), Results.end()) ^ 
276           ((unsigned)(intptr_t)&Pattern << 8);
277 }
278
279 // isContradictoryImpl Implementations.
280
281 static 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
299 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
300   if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
301     // One node can't have two different opcodes!
302     // 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();
305   }
306   
307   // 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
325 bool 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
331 bool 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   
343 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
344   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
345     return CIM->getValue() != getValue();
346   return false;
347 }