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"
16using namespace llvm;
17
18void Matcher::anchor() {}
19
20void Matcher::dump() const { print(OS&: errs(), indent: 0); }
21
22void Matcher::print(raw_ostream &OS, unsigned indent) const {
23 printImpl(OS, indent);
24 if (Next)
25 return Next->print(OS, indent);
26}
27
28void 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.
33Matcher *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.
52bool 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.
66bool 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
79ScopeMatcher::~ScopeMatcher() {
80 for (Matcher *C : Children)
81 delete C;
82}
83
84SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
85 for (auto &C : Cases)
86 delete C.second;
87}
88
89SwitchTypeMatcher::~SwitchTypeMatcher() {
90 for (auto &C : Cases)
91 delete C.second;
92}
93
94CheckPredicateMatcher::CheckPredicateMatcher(
95 const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
96 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
97 Operands(Ops.begin(), Ops.end()) {}
98
99TreePredicateFn CheckPredicateMatcher::getPredicate() const {
100 return TreePredicateFn(Pred);
101}
102
103unsigned CheckPredicateMatcher::getNumOperands() const {
104 return Operands.size();
105}
106
107unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
108 assert(i < Operands.size());
109 return Operands[i];
110}
111
112// printImpl methods.
113
114void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
115 OS.indent(NumSpaces: indent) << "Scope\n";
116 for (const Matcher *C : Children) {
117 if (!C)
118 OS.indent(NumSpaces: indent + 1) << "NULL POINTER\n";
119 else
120 C->print(OS, indent: indent + 2);
121 }
122}
123
124void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
125 OS.indent(NumSpaces: indent) << "Record\n";
126}
127
128void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
129 OS.indent(NumSpaces: indent) << "RecordChild: " << ChildNo << '\n';
130}
131
132void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
133 OS.indent(NumSpaces: indent) << "RecordMemRef\n";
134}
135
136void CaptureGlueInputMatcher::printImpl(raw_ostream &OS,
137 unsigned indent) const {
138 OS.indent(NumSpaces: indent) << "CaptureGlueInput\n";
139}
140
141void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
142 OS.indent(NumSpaces: indent) << "MoveChild " << ChildNo << '\n';
143}
144
145void MoveSiblingMatcher::printImpl(raw_ostream &OS, unsigned Indent) const {
146 OS.indent(NumSpaces: Indent) << "MoveSibling " << SiblingNo << '\n';
147}
148
149void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
150 OS.indent(NumSpaces: indent) << "MoveParent\n";
151}
152
153void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
154 OS.indent(NumSpaces: indent) << "CheckSame " << MatchNumber << '\n';
155}
156
157void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
158 OS.indent(NumSpaces: indent) << "CheckChild" << ChildNo << "Same\n";
159}
160
161void CheckPatternPredicateMatcher::printImpl(raw_ostream &OS,
162 unsigned indent) const {
163 OS.indent(NumSpaces: indent) << "CheckPatternPredicate " << Predicate << '\n';
164}
165
166void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
167 OS.indent(NumSpaces: indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
168}
169
170void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
171 OS.indent(NumSpaces: indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
172}
173
174void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
175 OS.indent(NumSpaces: indent) << "SwitchOpcode: {\n";
176 for (const auto &C : Cases) {
177 OS.indent(NumSpaces: indent) << "case " << C.first->getEnumName() << ":\n";
178 C.second->print(OS, indent: indent + 2);
179 }
180 OS.indent(NumSpaces: indent) << "}\n";
181}
182
183void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
184 OS.indent(NumSpaces: indent) << "CheckType " << getEnumName(T: Type) << ", ResNo=" << ResNo
185 << '\n';
186}
187
188void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
189 OS.indent(NumSpaces: indent) << "SwitchType: {\n";
190 for (const auto &C : Cases) {
191 OS.indent(NumSpaces: indent) << "case " << getEnumName(T: C.first) << ":\n";
192 C.second->print(OS, indent: indent + 2);
193 }
194 OS.indent(NumSpaces: indent) << "}\n";
195}
196
197void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
198 OS.indent(NumSpaces: indent) << "CheckChildType " << ChildNo << " " << getEnumName(T: Type)
199 << '\n';
200}
201
202void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
203 OS.indent(NumSpaces: indent) << "CheckInteger " << Value << '\n';
204}
205
206void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
207 unsigned indent) const {
208 OS.indent(NumSpaces: indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
209}
210
211void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
212 OS.indent(NumSpaces: indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
213}
214
215void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
216 unsigned indent) const {
217 OS.indent(NumSpaces: indent) << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
218}
219
220void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
221 OS.indent(NumSpaces: indent) << "CheckValueType " << getEnumName(T: VT) << '\n';
222}
223
224void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
225 OS.indent(NumSpaces: indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
226}
227
228void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
229 OS.indent(NumSpaces: indent) << "CheckAndImm " << Value << '\n';
230}
231
232void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
233 OS.indent(NumSpaces: indent) << "CheckOrImm " << Value << '\n';
234}
235
236void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
237 unsigned indent) const {
238 OS.indent(NumSpaces: indent) << "CheckFoldableChainNode\n";
239}
240
241void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS,
242 unsigned indent) const {
243 OS.indent(NumSpaces: indent) << "CheckAllOnesV\n";
244}
245
246void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS,
247 unsigned indent) const {
248 OS.indent(NumSpaces: indent) << "CheckAllZerosV\n";
249}
250
251void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
252 OS.indent(NumSpaces: indent) << "EmitInteger " << Val << " VT=" << getEnumName(T: VT)
253 << '\n';
254}
255
256void EmitStringIntegerMatcher::printImpl(raw_ostream &OS,
257 unsigned indent) const {
258 OS.indent(NumSpaces: indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(T: VT)
259 << '\n';
260}
261
262void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
263 OS.indent(NumSpaces: indent) << "EmitRegister ";
264 if (Reg)
265 OS << Reg->getName();
266 else
267 OS << "zero_reg";
268 OS << " VT=" << getEnumName(T: VT) << '\n';
269}
270
271void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
272 unsigned indent) const {
273 OS.indent(NumSpaces: indent) << "EmitConvertToTarget " << Slot << '\n';
274}
275
276void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
277 unsigned indent) const {
278 OS.indent(NumSpaces: indent) << "EmitMergeInputChains <todo: args>\n";
279}
280
281void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
282 OS.indent(NumSpaces: indent) << "EmitCopyToReg <todo: args>\n";
283}
284
285void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
286 OS.indent(NumSpaces: indent) << "EmitNodeXForm " << NodeXForm->getName()
287 << " Slot=" << Slot << '\n';
288}
289
290void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
291 OS.indent(NumSpaces: indent);
292 OS << (isa<MorphNodeToMatcher>(Val: this) ? "MorphNodeTo: " : "EmitNode: ")
293 << CGI.Namespace << "::" << CGI.TheDef->getName() << ": <todo flags> ";
294
295 for (unsigned i = 0, e = VTs.size(); i != e; ++i)
296 OS << ' ' << getEnumName(T: VTs[i]);
297 OS << '(';
298 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
299 OS << Operands[i] << ' ';
300 OS << ")\n";
301}
302
303void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
304 OS.indent(NumSpaces: indent) << "CompleteMatch <todo args>\n";
305 OS.indent(NumSpaces: indent) << "Src = " << Pattern.getSrcPattern() << "\n";
306 OS.indent(NumSpaces: indent) << "Dst = " << Pattern.getDstPattern() << "\n";
307}
308
309bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
310 // Note: pointer equality isn't enough here, we have to check the enum names
311 // to ensure that the nodes are for the same opcode.
312 return cast<CheckOpcodeMatcher>(Val: M)->Opcode.getEnumName() ==
313 Opcode.getEnumName();
314}
315
316bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
317 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(Val: m);
318 return &M->CGI == &CGI && M->VTs == VTs && M->Operands == Operands &&
319 M->HasChain == HasChain && M->HasInGlue == HasInGlue &&
320 M->HasOutGlue == HasOutGlue && M->HasMemRefs == HasMemRefs &&
321 M->NumFixedArityOperands == NumFixedArityOperands;
322}
323
324void EmitNodeMatcher::anchor() {}
325
326void MorphNodeToMatcher::anchor() {}
327
328// isContradictoryImpl Implementations.
329
330static bool TypesAreContradictory(MVT::SimpleValueType T1,
331 MVT::SimpleValueType T2) {
332 // If the two types are the same, then they are the same, so they don't
333 // contradict.
334 if (T1 == T2)
335 return false;
336
337 // If either type is about iPtr, then they don't conflict unless the other
338 // one is not a scalar integer type.
339 if (T1 == MVT::iPTR)
340 return !MVT(T2).isInteger() || MVT(T2).isVector();
341
342 if (T2 == MVT::iPTR)
343 return !MVT(T1).isInteger() || MVT(T1).isVector();
344
345 // Otherwise, they are two different non-iPTR types, they conflict.
346 return true;
347}
348
349bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
350 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(Val: M)) {
351 // One node can't have two different opcodes!
352 // Note: pointer equality isn't enough here, we have to check the enum names
353 // to ensure that the nodes are for the same opcode.
354 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
355 }
356
357 // If the node has a known type, and if the type we're checking for is
358 // different, then we know they contradict. For example, a check for
359 // ISD::STORE will never be true at the same time a check for Type i32 is.
360 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M)) {
361 // If checking for a result the opcode doesn't have, it can't match.
362 if (CT->getResNo() >= getOpcode().getNumResults())
363 return true;
364
365 MVT::SimpleValueType NodeType = getOpcode().getKnownType(ResNo: CT->getResNo());
366 if (NodeType != MVT::Other)
367 return TypesAreContradictory(T1: NodeType, T2: CT->getType());
368 }
369
370 return false;
371}
372
373bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
374 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M))
375 return TypesAreContradictory(T1: getType(), T2: CT->getType());
376 return false;
377}
378
379bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
380 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(Val: M)) {
381 // If the two checks are about different nodes, we don't know if they
382 // conflict!
383 if (CC->getChildNo() != getChildNo())
384 return false;
385
386 return TypesAreContradictory(T1: getType(), T2: CC->getType());
387 }
388 return false;
389}
390
391bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
392 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(Val: M))
393 return CIM->getValue() != getValue();
394 return false;
395}
396
397bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
398 if (const CheckChildIntegerMatcher *CCIM =
399 dyn_cast<CheckChildIntegerMatcher>(Val: M)) {
400 // If the two checks are about different nodes, we don't know if they
401 // conflict!
402 if (CCIM->getChildNo() != getChildNo())
403 return false;
404
405 return CCIM->getValue() != getValue();
406 }
407 return false;
408}
409
410bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
411 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(Val: M))
412 return CVT->getVT() != getVT();
413 return false;
414}
415
416bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
417 // AllZeros is contradictory.
418 return isa<CheckImmAllZerosVMatcher>(Val: M);
419}
420
421bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
422 // AllOnes is contradictory.
423 return isa<CheckImmAllOnesVMatcher>(Val: M);
424}
425
426bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
427 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(Val: M))
428 return CCCM->getCondCodeName() != getCondCodeName();
429 return false;
430}
431
432bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
433 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(Val: M))
434 return CCCCM->getCondCodeName() != getCondCodeName();
435 return false;
436}
437