e01292801c6e3abdd1f0063d9edc5fd9a3da08c3
[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 ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
29   OS.indent(indent) << "Scope\n";
30   Check->print(OS, indent+2);
31 }
32
33 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
34   OS.indent(indent) << "Record\n";
35 }
36
37 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
38   OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
39 }
40
41 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
42   OS.indent(indent) << "RecordMemRef\n";
43 }
44
45 void CaptureFlagInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
46   OS.indent(indent) << "CaptureFlagInput\n";
47 }
48
49 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
50   OS.indent(indent) << "MoveChild " << ChildNo << '\n';
51 }
52
53 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
54   OS.indent(indent) << "MoveParent\n";
55 }
56
57 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
58   OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
59 }
60
61 void CheckPatternPredicateMatcher::
62 printImpl(raw_ostream &OS, unsigned indent) const {
63   OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
64 }
65
66 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
67   OS.indent(indent) << "CheckPredicate " << PredName << '\n';
68 }
69
70 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
71   OS.indent(indent) << "CheckOpcode " << OpcodeName << '\n';
72 }
73
74 void CheckMultiOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
75   OS.indent(indent) << "CheckMultiOpcode <todo args>\n";
76 }
77
78 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
79   OS.indent(indent) << "CheckType " << getEnumName(Type) << '\n';
80 }
81
82 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
83   OS.indent(indent) << "CheckChildType " << ChildNo << " "
84     << getEnumName(Type) << '\n';
85 }
86
87
88 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
89   OS.indent(indent) << "CheckInteger " << Value << '\n';
90 }
91
92 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
93   OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
94 }
95
96 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
97   OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
98 }
99
100 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
101   OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
102 }
103
104 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
105   OS.indent(indent) << "CheckAndImm " << Value << '\n';
106 }
107
108 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
109   OS.indent(indent) << "CheckOrImm " << Value << '\n';
110 }
111
112 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
113                                               unsigned indent) const {
114   OS.indent(indent) << "CheckFoldableChainNode\n";
115 }
116
117 void CheckChainCompatibleMatcher::printImpl(raw_ostream &OS,
118                                               unsigned indent) const {
119   OS.indent(indent) << "CheckChainCompatible " << PreviousOp << "\n";
120 }
121
122 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
123   OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
124 }
125
126 void EmitStringIntegerMatcher::
127 printImpl(raw_ostream &OS, unsigned indent) const {
128   OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
129 }
130
131 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132   OS.indent(indent) << "EmitRegister ";
133   if (Reg)
134     OS << Reg->getName();
135   else
136     OS << "zero_reg";
137   OS << " VT=" << VT << '\n';
138 }
139
140 void EmitConvertToTargetMatcher::
141 printImpl(raw_ostream &OS, unsigned indent) const {
142   OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
143 }
144
145 void EmitMergeInputChainsMatcher::
146 printImpl(raw_ostream &OS, unsigned indent) const {
147   OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
148 }
149
150 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
151   OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
152 }
153
154 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
155   OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
156      << " Slot=" << Slot << '\n';
157 }
158
159
160 void EmitNodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
161   OS.indent(indent) << "EmitNode: " << OpcodeName << ": <todo flags> ";
162
163   for (unsigned i = 0, e = VTs.size(); i != e; ++i)
164     OS << ' ' << getEnumName(VTs[i]);
165   OS << '(';
166   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
167     OS << Operands[i] << ' ';
168   OS << ")\n";
169 }
170
171 void MarkFlagResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
172   OS.indent(indent) << "MarkFlagResults <todo: args>\n";
173 }
174
175 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
176   OS.indent(indent) << "CompleteMatch <todo args>\n";
177   OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
178   OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
179 }
180
181 // getHashImpl Implementation.
182
183 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
184   return HashString(Predicate);
185 }
186
187 unsigned CheckPredicateMatcher::getHashImpl() const {
188   return HashString(PredName);
189 }
190
191 unsigned CheckOpcodeMatcher::getHashImpl() const {
192   return HashString(OpcodeName);
193 }
194
195 unsigned CheckMultiOpcodeMatcher::getHashImpl() const {
196   unsigned Result = 0;
197   for (unsigned i = 0, e = OpcodeNames.size(); i != e; ++i)
198     Result |= HashString(OpcodeNames[i]);
199   return Result;
200 }
201
202 unsigned CheckCondCodeMatcher::getHashImpl() const {
203   return HashString(CondCodeName);
204 }
205
206 unsigned CheckValueTypeMatcher::getHashImpl() const {
207   return HashString(TypeName);
208 }
209
210 unsigned EmitStringIntegerMatcher::getHashImpl() const {
211   return HashString(Val) ^ VT;
212 }
213
214 template<typename It>
215 static unsigned HashUnsigneds(It I, It E) {
216   unsigned Result = 0;
217   for (; I != E; ++I)
218     Result = (Result<<3) ^ *I;
219   return Result;
220 }
221
222 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
223   return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
224 }
225
226 bool EmitNodeMatcher::isEqualImpl(const Matcher *m) const {
227   const EmitNodeMatcher *M = cast<EmitNodeMatcher>(m);
228   return M->OpcodeName == OpcodeName && M->VTs == VTs &&
229          M->Operands == Operands && M->HasChain == HasChain &&
230          M->HasFlag == HasFlag && M->HasMemRefs == HasMemRefs &&
231          M->NumFixedArityOperands == NumFixedArityOperands;
232 }
233
234 unsigned EmitNodeMatcher::getHashImpl() const {
235   return (HashString(OpcodeName) << 4) | Operands.size();
236 }
237
238
239 unsigned MarkFlagResultsMatcher::getHashImpl() const {
240   return HashUnsigneds(FlagResultNodes.begin(), FlagResultNodes.end());
241 }
242
243 unsigned CompleteMatchMatcher::getHashImpl() const {
244   return HashUnsigneds(Results.begin(), Results.end()) ^ 
245           ((unsigned)(intptr_t)&Pattern << 8);
246 }