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()); }
21
22void Matcher::print(raw_ostream &OS, indent 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: 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(const TreePredicateFn &pred,
95 ArrayRef<unsigned> Ops)
96 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
97 Operands(Ops) {}
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, 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: Indent + 2);
121 }
122}
123
124void RecordMatcher::printImpl(raw_ostream &OS, indent Indent) const {
125 OS << Indent << "Record\n";
126}
127
128void RecordChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
129 OS << Indent << "RecordChild: " << ChildNo << '\n';
130}
131
132void RecordMemRefMatcher::printImpl(raw_ostream &OS, indent Indent) const {
133 OS << Indent << "RecordMemRef\n";
134}
135
136void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, indent Indent) const {
137 OS << Indent << "CaptureGlueInput\n";
138}
139
140void MoveChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
141 OS << Indent << "MoveChild " << ChildNo << '\n';
142}
143
144void MoveSiblingMatcher::printImpl(raw_ostream &OS, indent Indent) const {
145 OS << Indent << "MoveSibling " << SiblingNo << '\n';
146}
147
148void MoveParentMatcher::printImpl(raw_ostream &OS, indent Indent) const {
149 OS << Indent << "MoveParent\n";
150}
151
152void CheckSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
153 OS << Indent << "CheckSame " << MatchNumber << '\n';
154}
155
156void CheckChildSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
157 OS << Indent << "CheckChild" << ChildNo << "Same\n";
158}
159
160void CheckPatternPredicateMatcher::printImpl(raw_ostream &OS,
161 indent Indent) const {
162 OS << Indent << "CheckPatternPredicate " << Predicate << '\n';
163}
164
165void CheckPredicateMatcher::printImpl(raw_ostream &OS, indent Indent) const {
166 OS << Indent << "CheckPredicate " << getPredicate().getFnName() << '\n';
167}
168
169void CheckOpcodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
170 OS << Indent << "CheckOpcode " << Opcode.getEnumName() << '\n';
171}
172
173void 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: Indent + 2);
178 }
179 OS << Indent << "}\n";
180}
181
182void CheckTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
183 OS << Indent << "CheckType " << getEnumName(T: Type) << ", ResNo=" << ResNo
184 << '\n';
185}
186
187void SwitchTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
188 OS << Indent << "SwitchType: {\n";
189 for (const auto &C : Cases) {
190 OS << Indent << "case " << getEnumName(T: C.first) << ":\n";
191 C.second->print(OS, Indent: Indent + 2);
192 }
193 OS << Indent << "}\n";
194}
195
196void CheckChildTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
197 OS << Indent << "CheckChildType " << ChildNo << " " << getEnumName(T: Type)
198 << '\n';
199}
200
201void CheckIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
202 OS << Indent << "CheckInteger " << Value << '\n';
203}
204
205void CheckChildIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
206 OS << Indent << "CheckChildInteger " << ChildNo << " " << Value << '\n';
207}
208
209void CheckCondCodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
210 OS << Indent << "CheckCondCode ISD::" << CondCodeName << '\n';
211}
212
213void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
214 indent Indent) const {
215 OS << Indent << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
216}
217
218void CheckValueTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
219 OS << Indent << "CheckValueType " << getEnumName(T: VT) << '\n';
220}
221
222void CheckComplexPatMatcher::printImpl(raw_ostream &OS, indent Indent) const {
223 OS << Indent << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
224}
225
226void CheckAndImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
227 OS << Indent << "CheckAndImm " << Value << '\n';
228}
229
230void CheckOrImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
231 OS << Indent << "CheckOrImm " << Value << '\n';
232}
233
234void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
235 indent Indent) const {
236 OS << Indent << "CheckFoldableChainNode\n";
237}
238
239void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
240 OS << Indent << "CheckAllOnesV\n";
241}
242
243void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
244 OS << Indent << "CheckAllZerosV\n";
245}
246
247void EmitIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
248 OS << Indent << "EmitInteger " << Val << " VT=" << getEnumName(T: VT) << '\n';
249}
250
251void EmitStringIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
252 OS << Indent << "EmitStringInteger " << Val << " VT=" << getEnumName(T: VT)
253 << '\n';
254}
255
256void 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(T: VT) << '\n';
263}
264
265void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
266 indent Indent) const {
267 OS << Indent << "EmitConvertToTarget " << Slot << '\n';
268}
269
270void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
271 indent Indent) const {
272 OS << Indent << "EmitMergeInputChains <todo: args>\n";
273}
274
275void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, indent Indent) const {
276 OS << Indent << "EmitCopyToReg <todo: args>\n";
277}
278
279void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, indent Indent) const {
280 OS << Indent << "EmitNodeXForm " << NodeXForm->getName() << " Slot=" << Slot
281 << '\n';
282}
283
284void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, indent Indent) const {
285 OS << Indent;
286 OS << (isa<MorphNodeToMatcher>(Val: this) ? "MorphNodeTo: " : "EmitNode: ")
287 << CGI.Namespace << "::" << CGI.TheDef->getName() << ": <todo flags> ";
288
289 for (MVT::SimpleValueType VT : VTs)
290 OS << ' ' << getEnumName(T: VT);
291 OS << '(';
292 for (unsigned Operand : Operands)
293 OS << Operand << ' ';
294 OS << ")\n";
295}
296
297void 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
303bool 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>(Val: M)->Opcode.getEnumName() ==
307 Opcode.getEnumName();
308}
309
310bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
311 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(Val: 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
318void EmitNodeMatcher::anchor() {}
319
320void MorphNodeToMatcher::anchor() {}
321
322// isContradictoryImpl Implementations.
323
324static 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
343bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
344 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(Val: 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>(Val: 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(ResNo: CT->getResNo());
360 if (NodeType != MVT::Other)
361 return TypesAreContradictory(T1: NodeType, T2: CT->getType());
362 }
363
364 return false;
365}
366
367bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
368 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M))
369 return TypesAreContradictory(T1: getType(), T2: CT->getType());
370 return false;
371}
372
373bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
374 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(Val: 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(T1: getType(), T2: CC->getType());
381 }
382 return false;
383}
384
385bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
386 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(Val: M))
387 return CIM->getValue() != getValue();
388 return false;
389}
390
391bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
392 if (const CheckChildIntegerMatcher *CCIM =
393 dyn_cast<CheckChildIntegerMatcher>(Val: 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
404bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
405 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(Val: M))
406 return CVT->getVT() != getVT();
407 return false;
408}
409
410bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
411 // AllZeros is contradictory.
412 return isa<CheckImmAllZerosVMatcher>(Val: M);
413}
414
415bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
416 // AllOnes is contradictory.
417 return isa<CheckImmAllOnesVMatcher>(Val: M);
418}
419
420bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
421 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(Val: M))
422 return CCCM->getCondCodeName() != getCondCodeName();
423 return false;
424}
425
426bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
427 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(Val: M))
428 return CCCCM->getCondCodeName() != getCondCodeName();
429 return false;
430}
431