1 //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "DAGISelMatcher.h"
10 #include "CodeGenDAGPatterns.h"
11 #include "CodeGenInstruction.h"
12 #include "CodeGenRegisters.h"
13 #include "CodeGenTarget.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/TableGen/Record.h"
16 using namespace llvm;
17
anchor()18 void Matcher::anchor() {}
19
dump() const20 void Matcher::dump() const { print(errs()); }
21
print(raw_ostream & OS,indent Indent) const22 void Matcher::print(raw_ostream &OS, indent 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 { printImpl(OS, indent(0)); }
29
30 /// unlinkNode - Unlink the specified node from this chain. If Other == this,
31 /// we unlink the next pointer and return it. Otherwise we unlink Other from
32 /// the list and return this.
unlinkNode(Matcher * Other)33 Matcher *Matcher::unlinkNode(Matcher *Other) {
34 if (this == Other)
35 return takeNext();
36
37 // Scan until we find the predecessor of Other.
38 Matcher *Cur = this;
39 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext())
40 /*empty*/;
41
42 if (!Cur)
43 return nullptr;
44 Cur->takeNext();
45 Cur->setNext(Other->takeNext());
46 return this;
47 }
48
49 /// canMoveBefore - Return true if this matcher is the same as Other, or if
50 /// we can move this matcher past all of the nodes in-between Other and this
51 /// node. Other must be equal to or before this.
canMoveBefore(const Matcher * Other) const52 bool Matcher::canMoveBefore(const Matcher *Other) const {
53 for (;; Other = Other->getNext()) {
54 assert(Other && "Other didn't come before 'this'?");
55 if (this == Other)
56 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 /// canMoveBeforeNode - 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
~ScopeMatcher()79 ScopeMatcher::~ScopeMatcher() {
80 for (Matcher *C : Children)
81 delete C;
82 }
83
~SwitchOpcodeMatcher()84 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
85 for (auto &C : Cases)
86 delete C.second;
87 }
88
~SwitchTypeMatcher()89 SwitchTypeMatcher::~SwitchTypeMatcher() {
90 for (auto &C : Cases)
91 delete C.second;
92 }
93
CheckPredicateMatcher(const TreePredicateFn & pred,ArrayRef<unsigned> Ops)94 CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn &pred,
95 ArrayRef<unsigned> Ops)
96 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
97 Operands(Ops) {}
98
getPredicate() const99 TreePredicateFn CheckPredicateMatcher::getPredicate() const {
100 return TreePredicateFn(Pred);
101 }
102
getNumOperands() const103 unsigned CheckPredicateMatcher::getNumOperands() const {
104 return Operands.size();
105 }
106
getOperandNo(unsigned i) const107 unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
108 assert(i < Operands.size());
109 return Operands[i];
110 }
111
112 // printImpl methods.
113
printImpl(raw_ostream & OS,indent Indent) const114 void ScopeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
115 OS << Indent << "Scope\n";
116 for (const Matcher *C : Children) {
117 if (!C)
118 OS << Indent + 1 << "NULL POINTER\n";
119 else
120 C->print(OS, Indent + 2);
121 }
122 }
123
printImpl(raw_ostream & OS,indent Indent) const124 void RecordMatcher::printImpl(raw_ostream &OS, indent Indent) const {
125 OS << Indent << "Record\n";
126 }
127
printImpl(raw_ostream & OS,indent Indent) const128 void RecordChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
129 OS << Indent << "RecordChild: " << ChildNo << '\n';
130 }
131
printImpl(raw_ostream & OS,indent Indent) const132 void RecordMemRefMatcher::printImpl(raw_ostream &OS, indent Indent) const {
133 OS << Indent << "RecordMemRef\n";
134 }
135
printImpl(raw_ostream & OS,indent Indent) const136 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, indent Indent) const {
137 OS << Indent << "CaptureGlueInput\n";
138 }
139
printImpl(raw_ostream & OS,indent Indent) const140 void MoveChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
141 OS << Indent << "MoveChild " << ChildNo << '\n';
142 }
143
printImpl(raw_ostream & OS,indent Indent) const144 void MoveSiblingMatcher::printImpl(raw_ostream &OS, indent Indent) const {
145 OS << Indent << "MoveSibling " << SiblingNo << '\n';
146 }
147
printImpl(raw_ostream & OS,indent Indent) const148 void MoveParentMatcher::printImpl(raw_ostream &OS, indent Indent) const {
149 OS << Indent << "MoveParent\n";
150 }
151
printImpl(raw_ostream & OS,indent Indent) const152 void CheckSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
153 OS << Indent << "CheckSame " << MatchNumber << '\n';
154 }
155
printImpl(raw_ostream & OS,indent Indent) const156 void CheckChildSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
157 OS << Indent << "CheckChild" << ChildNo << "Same\n";
158 }
159
printImpl(raw_ostream & OS,indent Indent) const160 void CheckPatternPredicateMatcher::printImpl(raw_ostream &OS,
161 indent Indent) const {
162 OS << Indent << "CheckPatternPredicate " << Predicate << '\n';
163 }
164
printImpl(raw_ostream & OS,indent Indent) const165 void CheckPredicateMatcher::printImpl(raw_ostream &OS, indent Indent) const {
166 OS << Indent << "CheckPredicate " << getPredicate().getFnName() << '\n';
167 }
168
printImpl(raw_ostream & OS,indent Indent) const169 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
170 OS << Indent << "CheckOpcode " << Opcode.getEnumName() << '\n';
171 }
172
printImpl(raw_ostream & OS,indent Indent) const173 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
174 OS << Indent << "SwitchOpcode: {\n";
175 for (const auto &C : Cases) {
176 OS << Indent << "case " << C.first->getEnumName() << ":\n";
177 C.second->print(OS, Indent + 2);
178 }
179 OS << Indent << "}\n";
180 }
181
printImpl(raw_ostream & OS,indent Indent) const182 void CheckTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
183 OS << Indent << "CheckType " << getEnumName(Type) << ", ResNo=" << ResNo
184 << '\n';
185 }
186
printImpl(raw_ostream & OS,indent Indent) const187 void SwitchTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
188 OS << Indent << "SwitchType: {\n";
189 for (const auto &C : Cases) {
190 OS << Indent << "case " << getEnumName(C.first) << ":\n";
191 C.second->print(OS, Indent + 2);
192 }
193 OS << Indent << "}\n";
194 }
195
printImpl(raw_ostream & OS,indent Indent) const196 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
197 OS << Indent << "CheckChildType " << ChildNo << " " << getEnumName(Type)
198 << '\n';
199 }
200
printImpl(raw_ostream & OS,indent Indent) const201 void CheckIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
202 OS << Indent << "CheckInteger " << Value << '\n';
203 }
204
printImpl(raw_ostream & OS,indent Indent) const205 void CheckChildIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
206 OS << Indent << "CheckChildInteger " << ChildNo << " " << Value << '\n';
207 }
208
printImpl(raw_ostream & OS,indent Indent) const209 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
210 OS << Indent << "CheckCondCode ISD::" << CondCodeName << '\n';
211 }
212
printImpl(raw_ostream & OS,indent Indent) const213 void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
214 indent Indent) const {
215 OS << Indent << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
216 }
217
printImpl(raw_ostream & OS,indent Indent) const218 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
219 OS << Indent << "CheckValueType " << getEnumName(VT) << '\n';
220 }
221
printImpl(raw_ostream & OS,indent Indent) const222 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, indent Indent) const {
223 OS << Indent << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
224 }
225
printImpl(raw_ostream & OS,indent Indent) const226 void CheckAndImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
227 OS << Indent << "CheckAndImm " << Value << '\n';
228 }
229
printImpl(raw_ostream & OS,indent Indent) const230 void CheckOrImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
231 OS << Indent << "CheckOrImm " << Value << '\n';
232 }
233
printImpl(raw_ostream & OS,indent Indent) const234 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
235 indent Indent) const {
236 OS << Indent << "CheckFoldableChainNode\n";
237 }
238
printImpl(raw_ostream & OS,indent Indent) const239 void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
240 OS << Indent << "CheckAllOnesV\n";
241 }
242
printImpl(raw_ostream & OS,indent Indent) const243 void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
244 OS << Indent << "CheckAllZerosV\n";
245 }
246
printImpl(raw_ostream & OS,indent Indent) const247 void EmitIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
248 OS << Indent << "EmitInteger " << Val << " VT=" << getEnumName(VT) << '\n';
249 }
250
printImpl(raw_ostream & OS,indent Indent) const251 void EmitStringIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
252 OS << Indent << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
253 << '\n';
254 }
255
printImpl(raw_ostream & OS,indent Indent) const256 void EmitRegisterMatcher::printImpl(raw_ostream &OS, indent Indent) const {
257 OS << Indent << "EmitRegister ";
258 if (Reg)
259 OS << Reg->getName();
260 else
261 OS << "zero_reg";
262 OS << " VT=" << getEnumName(VT) << '\n';
263 }
264
printImpl(raw_ostream & OS,indent Indent) const265 void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
266 indent Indent) const {
267 OS << Indent << "EmitConvertToTarget " << Slot << '\n';
268 }
269
printImpl(raw_ostream & OS,indent Indent) const270 void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
271 indent Indent) const {
272 OS << Indent << "EmitMergeInputChains <todo: args>\n";
273 }
274
printImpl(raw_ostream & OS,indent Indent) const275 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, indent Indent) const {
276 OS << Indent << "EmitCopyToReg <todo: args>\n";
277 }
278
printImpl(raw_ostream & OS,indent Indent) const279 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, indent Indent) const {
280 OS << Indent << "EmitNodeXForm " << NodeXForm->getName() << " Slot=" << Slot
281 << '\n';
282 }
283
printImpl(raw_ostream & OS,indent Indent) const284 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, indent Indent) const {
285 OS << Indent;
286 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ")
287 << CGI.Namespace << "::" << CGI.TheDef->getName() << ": <todo flags> ";
288
289 for (MVT::SimpleValueType VT : VTs)
290 OS << ' ' << getEnumName(VT);
291 OS << '(';
292 for (unsigned Operand : Operands)
293 OS << Operand << ' ';
294 OS << ")\n";
295 }
296
printImpl(raw_ostream & OS,indent Indent) const297 void CompleteMatchMatcher::printImpl(raw_ostream &OS, indent Indent) const {
298 OS << Indent << "CompleteMatch <todo args>\n";
299 OS << Indent << "Src = " << Pattern.getSrcPattern() << "\n";
300 OS << Indent << "Dst = " << Pattern.getDstPattern() << "\n";
301 }
302
isEqualImpl(const Matcher * M) const303 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
304 // Note: pointer equality isn't enough here, we have to check the enum names
305 // to ensure that the nodes are for the same opcode.
306 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() ==
307 Opcode.getEnumName();
308 }
309
isEqualImpl(const Matcher * m) const310 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
311 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m);
312 return &M->CGI == &CGI && M->VTs == VTs && M->Operands == Operands &&
313 M->HasChain == HasChain && M->HasInGlue == HasInGlue &&
314 M->HasOutGlue == HasOutGlue && M->HasMemRefs == HasMemRefs &&
315 M->NumFixedArityOperands == NumFixedArityOperands;
316 }
317
anchor()318 void EmitNodeMatcher::anchor() {}
319
anchor()320 void MorphNodeToMatcher::anchor() {}
321
322 // isContradictoryImpl Implementations.
323
TypesAreContradictory(MVT::SimpleValueType T1,MVT::SimpleValueType T2)324 static bool TypesAreContradictory(MVT::SimpleValueType T1,
325 MVT::SimpleValueType T2) {
326 // If the two types are the same, then they are the same, so they don't
327 // contradict.
328 if (T1 == T2)
329 return false;
330
331 // If either type is about iPtr, then they don't conflict unless the other
332 // one is not a scalar integer type.
333 if (T1 == MVT::iPTR)
334 return !MVT(T2).isInteger() || MVT(T2).isVector();
335
336 if (T2 == MVT::iPTR)
337 return !MVT(T1).isInteger() || MVT(T1).isVector();
338
339 // Otherwise, they are two different non-iPTR types, they conflict.
340 return true;
341 }
342
isContradictoryImpl(const Matcher * M) const343 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
344 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) {
345 // One node can't have two different opcodes!
346 // Note: pointer equality isn't enough here, we have to check the enum names
347 // to ensure that the nodes are for the same opcode.
348 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
349 }
350
351 // If the node has a known type, and if the type we're checking for is
352 // different, then we know they contradict. For example, a check for
353 // ISD::STORE will never be true at the same time a check for Type i32 is.
354 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) {
355 // If checking for a result the opcode doesn't have, it can't match.
356 if (CT->getResNo() >= getOpcode().getNumResults())
357 return true;
358
359 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo());
360 if (NodeType != MVT::Other)
361 return TypesAreContradictory(NodeType, CT->getType());
362 }
363
364 return false;
365 }
366
isContradictoryImpl(const Matcher * M) const367 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
368 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
369 return TypesAreContradictory(getType(), CT->getType());
370 return false;
371 }
372
isContradictoryImpl(const Matcher * M) const373 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
374 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) {
375 // If the two checks are about different nodes, we don't know if they
376 // conflict!
377 if (CC->getChildNo() != getChildNo())
378 return false;
379
380 return TypesAreContradictory(getType(), CC->getType());
381 }
382 return false;
383 }
384
isContradictoryImpl(const Matcher * M) const385 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
386 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
387 return CIM->getValue() != getValue();
388 return false;
389 }
390
isContradictoryImpl(const Matcher * M) const391 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
392 if (const CheckChildIntegerMatcher *CCIM =
393 dyn_cast<CheckChildIntegerMatcher>(M)) {
394 // If the two checks are about different nodes, we don't know if they
395 // conflict!
396 if (CCIM->getChildNo() != getChildNo())
397 return false;
398
399 return CCIM->getValue() != getValue();
400 }
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->getVT() != getVT();
407 return false;
408 }
409
isContradictoryImpl(const Matcher * M) const410 bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
411 // AllZeros is contradictory.
412 return isa<CheckImmAllZerosVMatcher>(M);
413 }
414
isContradictoryImpl(const Matcher * M) const415 bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
416 // AllOnes is contradictory.
417 return isa<CheckImmAllOnesVMatcher>(M);
418 }
419
isContradictoryImpl(const Matcher * M) const420 bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
421 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(M))
422 return CCCM->getCondCodeName() != getCondCodeName();
423 return false;
424 }
425
isContradictoryImpl(const Matcher * M) const426 bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
427 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(M))
428 return CCCCM->getCondCodeName() != getCondCodeName();
429 return false;
430 }
431