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 "llvm/TableGen/Record.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/ADT/StringExtras.h"
16 using namespace llvm;
17
dump() const18 void Matcher::dump() const {
19 print(errs(), 0);
20 }
21
print(raw_ostream & OS,unsigned indent) const22 void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
24 if (Next)
25 return Next->print(OS, indent);
26 }
27
printOne(raw_ostream & OS) const28 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.
unlinkNode(Matcher * Other)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.
canMoveBefore(const Matcher * Other) const53 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.
canMoveBeforeNode(const Matcher * Other) const66 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
~ScopeMatcher()80 ScopeMatcher::~ScopeMatcher() {
81 for (unsigned i = 0, e = Children.size(); i != e; ++i)
82 delete Children[i];
83 }
84
85
CheckPredicateMatcher(const TreePredicateFn & pred)86 CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn &pred)
87 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()) {}
88
getPredicate() const89 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
90 return TreePredicateFn(Pred);
91 }
92
93
94
95 // printImpl methods.
96
printImpl(raw_ostream & OS,unsigned indent) const97 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
98 OS.indent(indent) << "Scope\n";
99 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
100 if (getChild(i) == 0)
101 OS.indent(indent+1) << "NULL POINTER\n";
102 else
103 getChild(i)->print(OS, indent+2);
104 }
105 }
106
printImpl(raw_ostream & OS,unsigned indent) const107 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
108 OS.indent(indent) << "Record\n";
109 }
110
printImpl(raw_ostream & OS,unsigned indent) const111 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
112 OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
113 }
114
printImpl(raw_ostream & OS,unsigned indent) const115 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
116 OS.indent(indent) << "RecordMemRef\n";
117 }
118
printImpl(raw_ostream & OS,unsigned indent) const119 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{
120 OS.indent(indent) << "CaptureGlueInput\n";
121 }
122
printImpl(raw_ostream & OS,unsigned indent) const123 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
124 OS.indent(indent) << "MoveChild " << ChildNo << '\n';
125 }
126
printImpl(raw_ostream & OS,unsigned indent) const127 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
128 OS.indent(indent) << "MoveParent\n";
129 }
130
printImpl(raw_ostream & OS,unsigned indent) const131 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
132 OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
133 }
134
135 void CheckPatternPredicateMatcher::
printImpl(raw_ostream & OS,unsigned indent) const136 printImpl(raw_ostream &OS, unsigned indent) const {
137 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
138 }
139
printImpl(raw_ostream & OS,unsigned indent) const140 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
141 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
142 }
143
printImpl(raw_ostream & OS,unsigned indent) const144 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
145 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
146 }
147
printImpl(raw_ostream & OS,unsigned indent) const148 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
149 OS.indent(indent) << "SwitchOpcode: {\n";
150 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
151 OS.indent(indent) << "case " << Cases[i].first->getEnumName() << ":\n";
152 Cases[i].second->print(OS, indent+2);
153 }
154 OS.indent(indent) << "}\n";
155 }
156
157
printImpl(raw_ostream & OS,unsigned indent) const158 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
159 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo="
160 << ResNo << '\n';
161 }
162
printImpl(raw_ostream & OS,unsigned indent) const163 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
164 OS.indent(indent) << "SwitchType: {\n";
165 for (unsigned i = 0, e = Cases.size(); i != e; ++i) {
166 OS.indent(indent) << "case " << getEnumName(Cases[i].first) << ":\n";
167 Cases[i].second->print(OS, indent+2);
168 }
169 OS.indent(indent) << "}\n";
170 }
171
printImpl(raw_ostream & OS,unsigned indent) const172 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
173 OS.indent(indent) << "CheckChildType " << ChildNo << " "
174 << getEnumName(Type) << '\n';
175 }
176
177
printImpl(raw_ostream & OS,unsigned indent) const178 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
179 OS.indent(indent) << "CheckInteger " << Value << '\n';
180 }
181
printImpl(raw_ostream & OS,unsigned indent) const182 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
183 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
184 }
185
printImpl(raw_ostream & OS,unsigned indent) const186 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
187 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n';
188 }
189
printImpl(raw_ostream & OS,unsigned indent) const190 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
191 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
192 }
193
printImpl(raw_ostream & OS,unsigned indent) const194 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
195 OS.indent(indent) << "CheckAndImm " << Value << '\n';
196 }
197
printImpl(raw_ostream & OS,unsigned indent) const198 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
199 OS.indent(indent) << "CheckOrImm " << Value << '\n';
200 }
201
printImpl(raw_ostream & OS,unsigned indent) const202 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
203 unsigned indent) const {
204 OS.indent(indent) << "CheckFoldableChainNode\n";
205 }
206
printImpl(raw_ostream & OS,unsigned indent) const207 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
208 OS.indent(indent) << "EmitInteger " << Val << " VT=" << VT << '\n';
209 }
210
211 void EmitStringIntegerMatcher::
printImpl(raw_ostream & OS,unsigned indent) const212 printImpl(raw_ostream &OS, unsigned indent) const {
213 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << VT << '\n';
214 }
215
printImpl(raw_ostream & OS,unsigned indent) const216 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
217 OS.indent(indent) << "EmitRegister ";
218 if (Reg)
219 OS << Reg->getName();
220 else
221 OS << "zero_reg";
222 OS << " VT=" << VT << '\n';
223 }
224
225 void EmitConvertToTargetMatcher::
printImpl(raw_ostream & OS,unsigned indent) const226 printImpl(raw_ostream &OS, unsigned indent) const {
227 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
228 }
229
230 void EmitMergeInputChainsMatcher::
printImpl(raw_ostream & OS,unsigned indent) const231 printImpl(raw_ostream &OS, unsigned indent) const {
232 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
233 }
234
printImpl(raw_ostream & OS,unsigned indent) const235 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
236 OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
237 }
238
printImpl(raw_ostream & OS,unsigned indent) const239 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
240 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
241 << " Slot=" << Slot << '\n';
242 }
243
244
printImpl(raw_ostream & OS,unsigned indent) const245 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
246 OS.indent(indent);
247 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
248 << OpcodeName << ": <todo flags> ";
249
250 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
251 OS << ' ' << getEnumName(VTs[i]);
252 OS << '(';
253 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
254 OS << Operands[i] << ' ';
255 OS << ")\n";
256 }
257
printImpl(raw_ostream & OS,unsigned indent) const258 void MarkGlueResultsMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
259 OS.indent(indent) << "MarkGlueResults <todo: args>\n";
260 }
261
printImpl(raw_ostream & OS,unsigned indent) const262 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
263 OS.indent(indent) << "CompleteMatch <todo args>\n";
264 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n";
265 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n";
266 }
267
268 // getHashImpl Implementation.
269
getHashImpl() const270 unsigned CheckPatternPredicateMatcher::getHashImpl() const {
271 return HashString(Predicate);
272 }
273
getHashImpl() const274 unsigned CheckPredicateMatcher::getHashImpl() const {
275 return HashString(getPredicate().getFnName());
276 }
277
getHashImpl() const278 unsigned CheckOpcodeMatcher::getHashImpl() const {
279 return HashString(Opcode.getEnumName());
280 }
281
getHashImpl() const282 unsigned CheckCondCodeMatcher::getHashImpl() const {
283 return HashString(CondCodeName);
284 }
285
getHashImpl() const286 unsigned CheckValueTypeMatcher::getHashImpl() const {
287 return HashString(TypeName);
288 }
289
getHashImpl() const290 unsigned EmitStringIntegerMatcher::getHashImpl() const {
291 return HashString(Val) ^ VT;
292 }
293
294 template<typename It>
HashUnsigneds(It I,It E)295 static unsigned HashUnsigneds(It I, It E) {
296 unsigned Result = 0;
297 for (; I != E; ++I)
298 Result = (Result<<3) ^ *I;
299 return Result;
300 }
301
getHashImpl() const302 unsigned EmitMergeInputChainsMatcher::getHashImpl() const {
303 return HashUnsigneds(ChainNodes.begin(), ChainNodes.end());
304 }
305
isEqualImpl(const Matcher * M) const306 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
307 // Note: pointer equality isn't enough here, we have to check the enum names
308 // to ensure that the nodes are for the same opcode.
309 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
310 Opcode.getEnumName();
311 }
312
isEqualImpl(const Matcher * m) const313 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
314 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
315 return M->OpcodeName == OpcodeName && M->VTs == VTs &&
316 M->Operands == Operands && M->HasChain == HasChain &&
317 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue &&
318 M->HasMemRefs == HasMemRefs &&
319 M->NumFixedArityOperands == NumFixedArityOperands;
320 }
321
getHashImpl() const322 unsigned EmitNodeMatcherCommon::getHashImpl() const {
323 return (HashString(OpcodeName) << 4) | Operands.size();
324 }
325
326
getHashImpl() const327 unsigned MarkGlueResultsMatcher::getHashImpl() const {
328 return HashUnsigneds(GlueResultNodes.begin(), GlueResultNodes.end());
329 }
330
getHashImpl() const331 unsigned CompleteMatchMatcher::getHashImpl() const {
332 return HashUnsigneds(Results.begin(), Results.end()) ^
333 ((unsigned)(intptr_t)&Pattern << 8);
334 }
335
336 // isContradictoryImpl Implementations.
337
TypesAreContradictory(MVT::SimpleValueType T1,MVT::SimpleValueType T2)338 static bool TypesAreContradictory(MVT::SimpleValueType T1,
339 MVT::SimpleValueType T2) {
340 // If the two types are the same, then they are the same, so they don't
341 // contradict.
342 if (T1 == T2) return false;
343
344 // If either type is about iPtr, then they don't conflict unless the other
345 // one is not a scalar integer type.
346 if (T1 == MVT::iPTR)
347 return !MVT(T2).isInteger() || MVT(T2).isVector();
348
349 if (T2 == MVT::iPTR)
350 return !MVT(T1).isInteger() || MVT(T1).isVector();
351
352 // Otherwise, they are two different non-iPTR types, they conflict.
353 return true;
354 }
355
isContradictoryImpl(const Matcher * M) const356 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
357 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
358 // One node can't have two different opcodes!
359 // Note: pointer equality isn't enough here, we have to check the enum names
360 // to ensure that the nodes are for the same opcode.
361 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
362 }
363
364 // If the node has a known type, and if the type we're checking for is
365 // different, then we know they contradict. For example, a check for
366 // ISD::STORE will never be true at the same time a check for Type i32 is.
367 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
368 // If checking for a result the opcode doesn't have, it can't match.
369 if (CT->getResNo() >= getOpcode().getNumResults())
370 return true;
371
372 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
373 if (NodeType != MVT::Other)
374 return TypesAreContradictory(NodeType, CT->getType());
375 }
376
377 return false;
378 }
379
isContradictoryImpl(const Matcher * M) const380 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
381 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
382 return TypesAreContradictory(getType(), CT->getType());
383 return false;
384 }
385
isContradictoryImpl(const Matcher * M) const386 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
387 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
388 // If the two checks are about different nodes, we don't know if they
389 // conflict!
390 if (CC->getChildNo() != getChildNo())
391 return false;
392
393 return TypesAreContradictory(getType(), CC->getType());
394 }
395 return false;
396 }
397
isContradictoryImpl(const Matcher * M) const398 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
399 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
400 return CIM->getValue() != getValue();
401 return false;
402 }
403
isContradictoryImpl(const Matcher * M) const404 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
405 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
406 return CVT->getTypeName() != getTypeName();
407 return false;
408 }
409
410