pull MarkFlagResult out from between an EmitNode/CompleteMatch
[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->HasInFlag == HasInFlag && M->HasOutFlag == HasOutFlag &&
250          M->HasMemRefs == HasMemRefs &&
251          M->NumFixedArityOperands == NumFixedArityOperands;
252 }
253
254 unsigned EmitNodeMatcherCommon::getHashImpl() const {
255   return (HashString(OpcodeName) << 4) | Operands.size();
256 }
257
258
259 unsigned MarkFlagResultsMatcher::getHashImpl() const {
260   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
261 }
262
263 unsigned CompleteMatchMatcher::getHashImpl() const {
264   return HashUnsigneds(Results.begin(), Results.end()) ^ 
265           ((unsigned)(intptr_t)&Pattern << 8);
266 }
267
268 // isContradictoryImpl Implementations.
269
270 static bool TypesAreContradictory(MVT::SimpleValueType T1,
271                                   MVT::SimpleValueType T2) {
272   // If the two types are the same, then they are the same, so they don't
273   // contradict.
274   if (T1 == T2) return false;
275   
276   // If either type is about iPtr, then they don't conflict unless the other
277   // one is not a scalar integer type.
278   if (T1 == MVT::iPTR)
279     return !MVT(T2).isInteger() || MVT(T2).isVector();
280   
281   if (T2 == MVT::iPTR)
282     return !MVT(T1).isInteger() || MVT(T1).isVector();
283   
284   // Otherwise, they are two different non-iPTR types, they conflict.
285   return true;
286 }
287
288 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
289   if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
290     // One node can't have two different opcodes!
291     return &COM->getOpcode() != &getOpcode();
292   }
293   
294   // TODO: CheckMultiOpcodeMatcher?
295   
296   // If the node has a known type, and if the type we're checking for is
297   // different, then we know they contradict.  For example, a check for
298   // ISD::STORE will never be true at the same time a check for Type i32 is.
299   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
300     // FIXME: What result is this referring to?
301     unsigned NodeType;
302     if (getOpcode().getNumResults() == 0)
303       NodeType = MVT::isVoid;
304     else
305       NodeType = getOpcode().getKnownType();
306     if (NodeType != EEVT::isUnknown)
307       return TypesAreContradictory((MVT::SimpleValueType)NodeType,
308                                    CT->getType());
309   }
310   
311   return false;
312 }
313
314 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
315   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
316     return TypesAreContradictory(getType(), CT->getType());
317   return false;
318 }
319
320 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
321   if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
322     // If the two checks are about different nodes, we don't know if they
323     // conflict!
324     if (CC->getChildNo() != getChildNo())
325       return false;
326     
327     return TypesAreContradictory(getType(), CC->getType());
328   }
329   return false;
330 }
331   
332 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
333   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
334     return CIM->getValue() != getValue();
335   return false;
336 }