Generalize my hack to use SDNodeInfo to find out when a
[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     getChild(i)->print(OS, indent+2);
44 }
45
46 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
47   OS.indent(indent) << "Record\n";
48 }
49
50 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
51   OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
52 }
53
54 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
55   OS.indent(indent) << "RecordMemRef\n";
56 }
57
58 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
59   OS.indent(indent) << "CaptureFlagInput\n";
60 }
61
62 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
63   OS.indent(indent) << "MoveChild " << ChildNo << '\n';
64 }
65
66 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67   OS.indent(indent) << "MoveParent\n";
68 }
69
70 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
71   OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
72 }
73
74 void CheckPatternPredicateMatcher::
75 printImpl(raw_ostream &OS, unsigned indent) const {
76   OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
77 }
78
79 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
80   OS.indent(indent) << "CheckPredicate " << PredName << '\n';
81 }
82
83 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
84   OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
85 }
86
87 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
88   OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
89 }
90
91 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
92   OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
93 }
94
95 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
96   OS.indent(indent) << "CheckChildType " << ChildNo << " "
97     << getEnumName(Type) << '\n';
98 }
99
100
101 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
102   OS.indent(indent) << "CheckInteger " << Value << '\n';
103 }
104
105 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
107 }
108
109 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
110   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
111 }
112
113 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
114   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
115 }
116
117 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
118   OS.indent(indent) << "CheckAndImm " << Value << '\n';
119 }
120
121 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
122   OS.indent(indent) << "CheckOrImm " << Value << '\n';
123 }
124
125 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
126                                               unsigned indent) const {
127   OS.indent(indent) << "CheckFoldableChainNode\n";
128 }
129
130 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
131                                               unsigned indent) const {
132   OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
133 }
134
135 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
136   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
137 }
138
139 void EmitStringIntegerMatcher::
140 printImpl(raw_ostream &OS, unsigned indent) const {
141   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
142 }
143
144 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
145   OS.indent(indent) << "EmitRegister ";
146   if (Reg)
147     OS << Reg->getName();
148   else
149     OS << "zero_reg";
150   OS << " VT=" << VT << '\n';
151 }
152
153 void EmitConvertToTargetMatcher::
154 printImpl(raw_ostream &OS, unsigned indent) const {
155   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
156 }
157
158 void EmitMergeInputChainsMatcher::
159 printImpl(raw_ostream &OS, unsigned indent) const {
160   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
161 }
162
163 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
165 }
166
167 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
168   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
169      << " Slot=" << Slot << '\n';
170 }
171
172
173 void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
174   OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
175
176   for (unsigned i = 0, e = VTs.size(); i != e; ++i)
177     OS << ' ' << getEnumName(VTs[i]);
178   OS << '(';
179   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
180     OS << Operands[i] << ' ';
181   OS << ")\n";
182 }
183
184 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
185   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
186 }
187
188 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
189   OS.indent(indent) << "CompleteMatch <todo args>\n";
190   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
191   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
192 }
193
194 // getHashImpl Implementation.
195
196 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
197   return HashString(Predicate);
198 }
199
200 unsigned CheckPredicateMatcher::getHashImpl() const {
201   return HashString(PredName);
202 }
203
204 unsigned CheckOpcodeMatcher::getHashImpl() const {
205   return HashString(Opcode.getEnumName());
206 }
207
208 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
209   unsigned Result = 0;
210   for (unsigned i = 0, e = Opcodes.size(); i != e; ++i)
211     Result |= HashString(Opcodes[i]->getEnumName());
212   return Result;
213 }
214
215 unsigned CheckCondCodeMatcher::getHashImpl() const {
216   return HashString(CondCodeName);
217 }
218
219 unsigned CheckValueTypeMatcher::getHashImpl() const {
220   return HashString(TypeName);
221 }
222
223 unsigned EmitStringIntegerMatcher::getHashImpl() const {
224   return HashString(Val) ^ VT;
225 }
226
227 template<typename It>
228 static unsigned HashUnsigneds(It I, It E) {
229   unsigned Result = 0;
230   for (; I != E; ++I)
231     Result = (Result<<3) ^ *I;
232   return Result;
233 }
234
235 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
236   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
237 }
238
239 bool EmitNodeMatcher::isEqualImpl(const Matcher *m) const {
240   const EmitNodeMatcher *M = cast<EmitNodeMatcher>(m);
241   return M->OpcodeName == OpcodeName && M->VTs == VTs &&
242          M->Operands == Operands && M->HasChain == HasChain &&
243          M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
244          M->NumFixedArityOperands == NumFixedArityOperands;
245 }
246
247 unsigned EmitNodeMatcher::getHashImpl() const {
248   return (HashString(OpcodeName) << 4) | Operands.size();
249 }
250
251
252 unsigned MarkFlagResultsMatcher::getHashImpl() const {
253   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
254 }
255
256 unsigned CompleteMatchMatcher::getHashImpl() const {
257   return HashUnsigneds(Results.begin(), Results.end()) ^ 
258           ((unsigned)(intptr_t)&Pattern << 8);
259 }
260
261 // isContradictoryImpl Implementations.
262
263 static bool TypesAreContradictory(MVT::SimpleValueType T1,
264                                   MVT::SimpleValueType T2) {
265   // If the two types are the same, then they are the same, so they don't
266   // contradict.
267   if (T1 == T2) return false;
268   
269   // If either type is about iPtr, then they don't conflict unless the other
270   // one is not a scalar integer type.
271   if (T1 == MVT::iPTR)
272     return !MVT(T2).isInteger() || MVT(T2).isVector();
273   
274   if (T2 == MVT::iPTR)
275     return !MVT(T1).isInteger() || MVT(T1).isVector();
276   
277   // Otherwise, they are two different non-iPTR types, they conflict.
278   return true;
279 }
280
281 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
282   if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
283     // One node can't have two different opcodes!
284     return &COM->getOpcode() != &getOpcode();
285   }
286   
287   // TODO: CheckMultiOpcodeMatcher?
288   
289   // If the node has a known type, and if the type we're checking for is
290   // different, then we know they contradict.  For example, a check for
291   // ISD::STORE will never be true at the same time a check for Type i32 is.
292   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
293     // FIXME: What result is this referring to?
294     unsigned NodeType;
295     if (getOpcode().getNumResults() == 0)
296       NodeType = MVT::isVoid;
297     else
298       NodeType = getOpcode().getKnownType();
299     if (NodeType != EEVT::isUnknown)
300       return TypesAreContradictory((MVT::SimpleValueType)NodeType,
301                                    CT->getType());
302   }
303   
304   return false;
305 }
306
307 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
308   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
309     return TypesAreContradictory(getType(), CT->getType());
310   return false;
311 }
312
313 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
314   if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
315     // If the two checks are about different nodes, we don't know if they
316     // conflict!
317     if (CC->getChildNo() != getChildNo())
318       return false;
319     
320     return TypesAreContradictory(getType(), CC->getType());
321   }
322   return false;
323 }
324   
325 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
326   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
327     return CIM->getValue() != getValue();
328   return false;
329 }