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