add some missing \n's
[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 CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
106   OS.indent(indent) << "CheckChildType " << ChildNo << " "
107     << getEnumName(Type) << '\n';
108 }
109
110
111 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
112   OS.indent(indent) << "CheckInteger " << Value << '\n';
113 }
114
115 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
116   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
117 }
118
119 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
120   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
121 }
122
123 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
124   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
125 }
126
127 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
128   OS.indent(indent) << "CheckAndImm " << Value << '\n';
129 }
130
131 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132   OS.indent(indent) << "CheckOrImm " << Value << '\n';
133 }
134
135 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
136                                               unsigned indent) const {
137   OS.indent(indent) << "CheckFoldableChainNode\n";
138 }
139
140 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
141                                               unsigned indent) const {
142   OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
143 }
144
145 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
146   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
147 }
148
149 void EmitStringIntegerMatcher::
150 printImpl(raw_ostream &OS, unsigned indent) const {
151   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
152 }
153
154 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
155   OS.indent(indent) << "EmitRegister ";
156   if (Reg)
157     OS << Reg->getName();
158   else
159     OS << "zero_reg";
160   OS << " VT=" << VT << '\n';
161 }
162
163 void EmitConvertToTargetMatcher::
164 printImpl(raw_ostream &OS, unsigned indent) const {
165   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
166 }
167
168 void EmitMergeInputChainsMatcher::
169 printImpl(raw_ostream &OS, unsigned indent) const {
170   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
171 }
172
173 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
174   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
175 }
176
177 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
178   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
179      << " Slot=" << Slot << '\n';
180 }
181
182
183 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
184   OS.indent(indent);
185   OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
186      << OpcodeName << ": <todo flags> ";
187
188   for (unsigned i = 0, e = VTs.size(); i != e; ++i)
189     OS << ' ' << getEnumName(VTs[i]);
190   OS << '(';
191   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
192     OS << Operands[i] << ' ';
193   OS << ")\n";
194 }
195
196 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
197   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
198 }
199
200 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
201   OS.indent(indent) << "CompleteMatch <todo args>\n";
202   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
203   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
204 }
205
206 // getHashImpl Implementation.
207
208 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
209   return HashString(Predicate);
210 }
211
212 unsigned CheckPredicateMatcher::getHashImpl() const {
213   return HashString(PredName);
214 }
215
216 unsigned CheckOpcodeMatcher::getHashImpl() const {
217   return HashString(Opcode.getEnumName());
218 }
219
220 unsigned CheckCondCodeMatcher::getHashImpl() const {
221   return HashString(CondCodeName);
222 }
223
224 unsigned CheckValueTypeMatcher::getHashImpl() const {
225   return HashString(TypeName);
226 }
227
228 unsigned EmitStringIntegerMatcher::getHashImpl() const {
229   return HashString(Val) ^ VT;
230 }
231
232 template<typename It>
233 static unsigned HashUnsigneds(It I, It E) {
234   unsigned Result = 0;
235   for (; I != E; ++I)
236     Result = (Result<<3) ^ *I;
237   return Result;
238 }
239
240 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
241   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
242 }
243
244 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
245   // Note: pointer equality isn't enough here, we have to check the enum names
246   // to ensure that the nodes are for the same opcode. 
247   return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
248           Opcode.getEnumName();
249 }
250
251
252 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
253   const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
254   return M->OpcodeName == OpcodeName && M->VTs == VTs &&
255          M->Operands == Operands && M->HasChain == HasChain &&
256          M->HasInFlag == HasInFlag && M->HasOutFlag == HasOutFlag &&
257          M->HasMemRefs == HasMemRefs &&
258          M->NumFixedArityOperands == NumFixedArityOperands;
259 }
260
261 unsigned EmitNodeMatcherCommon::getHashImpl() const {
262   return (HashString(OpcodeName) << 4) | Operands.size();
263 }
264
265
266 unsigned MarkFlagResultsMatcher::getHashImpl() const {
267   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
268 }
269
270 unsigned CompleteMatchMatcher::getHashImpl() const {
271   return HashUnsigneds(Results.begin(), Results.end()) ^ 
272           ((unsigned)(intptr_t)&Pattern << 8);
273 }
274
275 // isContradictoryImpl Implementations.
276
277 static bool TypesAreContradictory(MVT::SimpleValueType T1,
278                                   MVT::SimpleValueType T2) {
279   // If the two types are the same, then they are the same, so they don't
280   // contradict.
281   if (T1 == T2) return false;
282   
283   // If either type is about iPtr, then they don't conflict unless the other
284   // one is not a scalar integer type.
285   if (T1 == MVT::iPTR)
286     return !MVT(T2).isInteger() || MVT(T2).isVector();
287   
288   if (T2 == MVT::iPTR)
289     return !MVT(T1).isInteger() || MVT(T1).isVector();
290   
291   // Otherwise, they are two different non-iPTR types, they conflict.
292   return true;
293 }
294
295 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
296   if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
297     // One node can't have two different opcodes!
298     // Note: pointer equality isn't enough here, we have to check the enum names
299     // to ensure that the nodes are for the same opcode. 
300     return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
301   }
302   
303   // If the node has a known type, and if the type we're checking for is
304   // different, then we know they contradict.  For example, a check for
305   // ISD::STORE will never be true at the same time a check for Type i32 is.
306   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
307     // FIXME: What result is this referring to?
308     unsigned NodeType;
309     if (getOpcode().getNumResults() == 0)
310       NodeType = MVT::isVoid;
311     else
312       NodeType = getOpcode().getKnownType();
313     if (NodeType != EEVT::isUnknown)
314       return TypesAreContradictory((MVT::SimpleValueType)NodeType,
315                                    CT->getType());
316   }
317   
318   return false;
319 }
320
321 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
322   if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
323     return TypesAreContradictory(getType(), CT->getType());
324   return false;
325 }
326
327 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
328   if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
329     // If the two checks are about different nodes, we don't know if they
330     // conflict!
331     if (CC->getChildNo() != getChildNo())
332       return false;
333     
334     return TypesAreContradictory(getType(), CC->getType());
335   }
336   return false;
337 }
338   
339 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
340   if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
341     return CIM->getValue() != getValue();
342   return false;
343 }