Delete the Matchers stored in the SmallVectors in SwitchOpcodeMatcher/SwitchTypeMatcher.
[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 "llvm/ADT/StringExtras.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/TableGen/Record.h"
16 using namespace llvm;
17
18 void Matcher::anchor() { }
19
20 void Matcher::dump() const {
21   print(errs(), 0);
22 }
23
24 void Matcher::print(raw_ostream &OS, unsigned indent) const {
25   printImpl(OS, indent);
26   if (Next)
27     return Next->print(OS, indent);
28 }
29
30 void Matcher::printOne(raw_ostream &OS) const {
31   printImpl(OS, 0);
32 }
33
34 /// unlinkNode - Unlink the specified node from this chain.  If Other == this,
35 /// we unlink the next pointer and return it.  Otherwise we unlink Other from
36 /// the list and return this.
37 Matcher *Matcher::unlinkNode(Matcher *Other) {
38   if (this == Other)
39     return takeNext();
40
41   // Scan until we find the predecessor of Other.
42   Matcher *Cur = this;
43   for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
44     /*empty*/;
45
46   if (Cur == 0) return 0;
47   Cur->takeNext();
48   Cur->setNext(Other->takeNext());
49   return this;
50 }
51
52 /// canMoveBefore - Return true if this matcher is the same as Other, or if
53 /// we can move this matcher past all of the nodes in-between Other and this
54 /// node.  Other must be equal to or before this.
55 bool Matcher::canMoveBefore(const Matcher *Other) const {
56   for (;; Other = Other->getNext()) {
57     assert(Other && "Other didn't come before 'this'?");
58     if (this == Other) return true;
59
60     // We have to be able to move this node across the Other node.
61     if (!canMoveBeforeNode(Other))
62       return false;
63   }
64 }
65
66 /// canMoveBeforeNode - Return true if it is safe to move the current matcher
67 /// across the specified one.
68 bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
69   // We can move simple predicates before record nodes.
70   if (isSimplePredicateNode())
71     return Other->isSimplePredicateOrRecordNode();
72
73   // We can move record nodes across simple predicates.
74   if (isSimplePredicateOrRecordNode())
75     return isSimplePredicateNode();
76
77   // We can't move record nodes across each other etc.
78   return false;
79 }
80
81
82 ScopeMatcher::~ScopeMatcher() {
83   for (unsigned i = 0, e = Children.size(); i != e; ++i)
84     delete Children[i];
85 }
86
87 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
88   for (unsigned i = 0, e = Cases.size(); i != e; ++i)
89     delete Cases[i].second;
90 }
91
92 SwitchTypeMatcher::~SwitchTypeMatcher() {
93   for (unsigned i = 0, e = Cases.size(); i != e; ++i)
94     delete Cases[i].second;
95 }
96
97 CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn &pred)
98   : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()) {}
99
100 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
101   return TreePredicateFn(Pred);
102 }
103
104
105
106 // printImpl methods.
107
108 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
109   OS.indent(indent) << "Scope\n";
110   for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
111     if (getChild(i) == 0)
112       OS.indent(indent+1) << "NULL POINTER\n";
113     else
114       getChild(i)->print(OS, indent+2);
115   }
116 }
117
118 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
119   OS.indent(indent) << "Record\n";
120 }
121
122 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
123   OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
124 }
125
126 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
127   OS.indent(indent) << "RecordMemRef\n";
128 }
129
130 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
131   OS.indent(indent) << "CaptureGlueInput\n";
132 }
133
134 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
135   OS.indent(indent) << "MoveChild " << ChildNo << '\n';
136 }
137
138 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
139   OS.indent(indent) << "MoveParent\n";
140 }
141
142 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
143   OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
144 }
145
146 void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
147   OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
148 }
149
150 void CheckPatternPredicateMatcher::
151 printImpl(raw_ostream &OS, unsigned indent) const {
152   OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
153 }
154
155 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
156   OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
157 }
158
159 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
160   OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
161 }
162
163 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164   OS.indent(indent) << "SwitchOpcode: {\n";
165   for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
166     OS.indent(indent) << "case " << Cases[i].first->getEnumName() << ":\n";
167     Cases[i].second->print(OS, indent+2);
168   }
169   OS.indent(indent) << "}\n";
170 }
171
172
173 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
174   OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
175     << ResNo << '\n';
176 }
177
178 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
179   OS.indent(indent) << "SwitchType: {\n";
180   for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
181     OS.indent(indent) << "case " << getEnumName(Cases[i].first) << ":\n";
182     Cases[i].second->print(OS, indent+2);
183   }
184   OS.indent(indent) << "}\n";
185 }
186
187 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
188   OS.indent(indent) << "CheckChildType " << ChildNo << " "
189     << getEnumName(Type) << '\n';
190 }
191
192
193 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
194   OS.indent(indent) << "CheckInteger " << Value << '\n';
195 }
196
197 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
198   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
199 }
200
201 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
202   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
203 }
204
205 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
206   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
207 }
208
209 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
210   OS.indent(indent) << "CheckAndImm " << Value << '\n';
211 }
212
213 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
214   OS.indent(indent) << "CheckOrImm " << Value << '\n';
215 }
216
217 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
218                                               unsigned indent) const {
219   OS.indent(indent) << "CheckFoldableChainNode\n";
220 }
221
222 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
223   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
224 }
225
226 void EmitStringIntegerMatcher::
227 printImpl(raw_ostream &OS, unsigned indent) const {
228   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
229 }
230
231 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
232   OS.indent(indent) << "EmitRegister ";
233   if (Reg)
234     OS << Reg->getName();
235   else
236     OS << "zero_reg";
237   OS << " VT=" << VT << '\n';
238 }
239
240 void EmitConvertToTargetMatcher::
241 printImpl(raw_ostream &OS, unsigned indent) const {
242   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
243 }
244
245 void EmitMergeInputChainsMatcher::
246 printImpl(raw_ostream &OS, unsigned indent) const {
247   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
248 }
249
250 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
251   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
252 }
253
254 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
255   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
256      << " Slot=" << Slot << '\n';
257 }
258
259
260 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
261   OS.indent(indent);
262   OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
263      << OpcodeName << ": <todo flags> ";
264
265   for (unsigned i = 0, e = VTs.size(); i != e; ++i)
266     OS << ' ' << getEnumName(VTs[i]);
267   OS << '(';
268   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
269     OS << Operands[i] << ' ';
270   OS << ")\n";
271 }
272
273 void MarkGlueResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
274   OS.indent(indent) << "MarkGlueResults <todo: args>\n";
275 }
276
277 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
278   OS.indent(indent) << "CompleteMatch <todo args>\n";
279   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
280   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
281 }
282
283 // getHashImpl Implementation.
284
285 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
286   return HashString(Predicate);
287 }
288
289 unsigned CheckPredicateMatcher::getHashImpl() const {
290   return HashString(getPredicate().getFnName());
291 }
292
293 unsigned CheckOpcodeMatcher::getHashImpl() const {
294   return HashString(Opcode.getEnumName());
295 }
296
297 unsigned CheckCondCodeMatcher::getHashImpl() const {
298   return HashString(CondCodeName);
299 }
300
301 unsigned CheckValueTypeMatcher::getHashImpl() const {
302   return HashString(TypeName);
303 }
304
305 unsigned EmitStringIntegerMatcher::getHashImpl() const {
306   return HashString(Val) ^ VT;
307 }
308
309 template<typename It>
310 static unsigned HashUnsigneds(It I, It E) {
311   unsigned Result = 0;
312   for (; I != E; ++I)
313     Result = (Result<<3) ^ *I;
314   return Result;
315 }
316
317 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
318   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
319 }
320
321 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
322   // Note: pointer equality isn't enough here, we have to check the enum names
323   // to ensure that the nodes are for the same opcode.
324   return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
325           Opcode.getEnumName();
326 }
327
328 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
329   const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
330   return M->OpcodeName == OpcodeName && M->VTs == VTs &&
331          M->Operands == Operands && M->HasChain == HasChain &&
332          M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
333          M->HasMemRefs == HasMemRefs &&
334          M->NumFixedArityOperands == NumFixedArityOperands;
335 }
336
337 unsigned EmitNodeMatcherCommon::getHashImpl() const {
338   return (HashString(OpcodeName) << 4) | Operands.size();
339 }
340
341
342 void EmitNodeMatcher::anchor() { }
343
344 void MorphNodeToMatcher::anchor() { }
345
346 unsigned MarkGlueResultsMatcher::getHashImpl() const {
347   return HashUnsigneds(GlueResultNodes.begin(), GlueResultNodes.end());
348 }
349
350 unsigned CompleteMatchMatcher::getHashImpl() const {
351   return HashUnsigneds(Results.begin(), Results.end()) ^
352           ((unsigned)(intptr_t)&Pattern << 8);
353 }
354
355 // isContradictoryImpl Implementations.
356
357 static bool TypesAreContradictory(MVT::SimpleValueType T1,
358                                   MVT::SimpleValueType T2) {
359   // If the two types are the same, then they are the same, so they don't
360   // contradict.
361   if (T1 == T2) return false;
362
363   // If either type is about iPtr, then they don't conflict unless the other
364   // one is not a scalar integer type.
365   if (T1 == MVT::iPTR)
366     return !MVT(T2).isInteger() || MVT(T2).isVector();
367
368   if (T2 == MVT::iPTR)
369     return !MVT(T1).isInteger() || MVT(T1).isVector();
370
371   // Otherwise, they are two different non-iPTR types, they conflict.
372   return true;
373 }
374
375 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
376   if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
377     // One node can't have two different opcodes!
378     // Note: pointer equality isn't enough here, we have to check the enum names
379     // to ensure that the nodes are for the same opcode.
380     return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
381   }
382
383   // If the node has a known type, and if the type we're checking for is
384   // different, then we know they contradict.  For example, a check for
385   // ISD::STORE will never be true at the same time a check for Type i32 is.
386   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
387     // If checking for a result the opcode doesn't have, it can't match.
388     if (CT->getResNo() >= getOpcode().getNumResults())
389       return true;
390
391     MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
392     if (NodeType != MVT::Other)
393       return TypesAreContradictory(NodeType, CT->getType());
394   }
395
396   return false;
397 }
398
399 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
400   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
401     return TypesAreContradictory(getType(), CT->getType());
402   return false;
403 }
404
405 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
406   if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
407     // If the two checks are about different nodes, we don't know if they
408     // conflict!
409     if (CC->getChildNo() != getChildNo())
410       return false;
411
412     return TypesAreContradictory(getType(), CC->getType());
413   }
414   return false;
415 }
416
417 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
418   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
419     return CIM->getValue() != getValue();
420   return false;
421 }
422
423 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
424   if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
425     return CVT->getTypeName() != getTypeName();
426   return false;
427 }
428