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 "Common/CodeGenDAGPatterns.h"
11#include "Common/CodeGenInstruction.h"
12#include "Common/CodeGenRegisters.h"
13#include "Common/CodeGenTarget.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/raw_ostream.h"
16#include "llvm/TableGen/Record.h"
17using namespace llvm;
18
19void Matcher::anchor() {}
20
21void Matcher::dump() const { printOne(OS&: dbgs()); }
22
23void Matcher::printOne(raw_ostream &OS, indent Indent) const {
24 printImpl(OS, Indent: indent(0));
25}
26
27/// canMoveBeforeNode - Return true if it is safe to move the current matcher
28/// across the specified one.
29bool Matcher::canMoveBeforeNode(const Matcher *Other) const {
30 // We can move simple predicates before record nodes.
31 if (isSimplePredicateNode())
32 return Other->isSimplePredicateOrRecordNode();
33
34 // We can move record nodes across simple predicates.
35 if (isSimplePredicateOrRecordNode())
36 return isSimplePredicateNode();
37
38 // We can't move record nodes across each other etc.
39 return false;
40}
41
42CheckPredicateMatcher::CheckPredicateMatcher(const TreePredicateFn &pred,
43 ArrayRef<unsigned> Ops)
44 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
45 Operands(Ops) {}
46
47TreePredicateFn CheckPredicateMatcher::getPredicate() const {
48 return TreePredicateFn(Pred);
49}
50
51unsigned CheckPredicateMatcher::getNumOperands() const {
52 return Operands.size();
53}
54
55unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
56 assert(i < Operands.size());
57 return Operands[i];
58}
59
60// printImpl methods.
61
62void ScopeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
63 OS << Indent << "Scope\n";
64 for (const MatcherList &C : Children) {
65 if (C.empty())
66 OS << Indent + 1 << "NULL POINTER\n";
67 else
68 C.print(OS, Indent: Indent + 2);
69 }
70}
71
72void RecordMatcher::printImpl(raw_ostream &OS, indent Indent) const {
73 OS << Indent << "Record\n";
74}
75
76void RecordChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
77 OS << Indent << "RecordChild: " << ChildNo << '\n';
78}
79
80void RecordMemRefMatcher::printImpl(raw_ostream &OS, indent Indent) const {
81 OS << Indent << "RecordMemRef\n";
82}
83
84void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, indent Indent) const {
85 OS << Indent << "CaptureGlueInput\n";
86}
87
88void MoveChildMatcher::printImpl(raw_ostream &OS, indent Indent) const {
89 OS << Indent << "MoveChild " << ChildNo << '\n';
90}
91
92void MoveSiblingMatcher::printImpl(raw_ostream &OS, indent Indent) const {
93 OS << Indent << "MoveSibling " << SiblingNo << '\n';
94}
95
96void MoveParentMatcher::printImpl(raw_ostream &OS, indent Indent) const {
97 OS << Indent << "MoveParent\n";
98}
99
100void CheckSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
101 OS << Indent << "CheckSame " << MatchNumber << '\n';
102}
103
104void CheckChildSameMatcher::printImpl(raw_ostream &OS, indent Indent) const {
105 OS << Indent << "CheckChildSame " << ChildNo << ' ' << MatchNumber << '\n';
106}
107
108void CheckPatternPredicateMatcher::printImpl(raw_ostream &OS,
109 indent Indent) const {
110 OS << Indent << "CheckPatternPredicate " << Predicate << '\n';
111}
112
113void CheckPredicateMatcher::printImpl(raw_ostream &OS, indent Indent) const {
114 OS << Indent << "CheckPredicate " << getPredicate().getFnName() << '\n';
115}
116
117void CheckOpcodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
118 OS << Indent << "CheckOpcode " << Opcode.getEnumName() << '\n';
119}
120
121void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
122 OS << Indent << "SwitchOpcode: {\n";
123 for (const auto &C : Cases) {
124 OS << Indent << "case " << C.first->getEnumName() << ":\n";
125 C.second.print(OS, Indent: Indent + 2);
126 }
127 OS << Indent << "}\n";
128}
129
130void CheckTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
131 OS << Indent << "CheckType " << Type << ", ResNo=" << ResNo << '\n';
132}
133
134void SwitchTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
135 OS << Indent << "SwitchType: {\n";
136 for (const auto &C : Cases) {
137 OS << Indent << "case " << getEnumName(T: C.first) << ":\n";
138 C.second.print(OS, Indent: Indent + 2);
139 }
140 OS << Indent << "}\n";
141}
142
143void CheckChildTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
144 OS << Indent << "CheckChildType " << ChildNo << " " << Type << '\n';
145}
146
147void CheckIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
148 OS << Indent << "CheckInteger " << Value << '\n';
149}
150
151void CheckChildIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
152 OS << Indent << "CheckChildInteger " << ChildNo << " " << Value << '\n';
153}
154
155void SwitchChildIntegerMatcher::printImpl(raw_ostream &OS,
156 indent Indent) const {
157 OS << Indent << "SwitchChildInteger " << ChildNo << ": {\n";
158 for (const auto &C : Cases) {
159 OS << Indent << "case " << C.first << ":\n";
160 C.second.print(OS, Indent: Indent + 2);
161 }
162 OS << Indent << "}\n";
163}
164
165void CheckCondCodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
166 OS << Indent << "CheckCondCode ISD::" << CondCodeName << '\n';
167}
168
169void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
170 indent Indent) const {
171 OS << Indent << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
172}
173
174void CheckValueTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
175 OS << Indent << "CheckValueType " << getEnumName(T: VT) << '\n';
176}
177
178void CheckComplexPatMatcher::printImpl(raw_ostream &OS, indent Indent) const {
179 OS << Indent << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
180}
181
182void CheckAndImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
183 OS << Indent << "CheckAndImm " << Value << '\n';
184}
185
186void CheckOrImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
187 OS << Indent << "CheckOrImm " << Value << '\n';
188}
189
190void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
191 indent Indent) const {
192 OS << Indent << "CheckFoldableChainNode\n";
193}
194
195void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
196 OS << Indent << "CheckAllOnesV\n";
197}
198
199void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
200 OS << Indent << "CheckAllZerosV\n";
201}
202
203void CheckUndefMatcher::printImpl(raw_ostream &OS, indent Indent) const {
204 OS << Indent << "CheckUndef\n";
205}
206
207void EmitIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
208 OS << Indent << "EmitInteger " << Val << " VT=" << VT << '\n';
209}
210
211void EmitRegisterMatcher::printImpl(raw_ostream &OS, indent Indent) const {
212 OS << Indent << "EmitRegister ";
213 if (Reg)
214 OS << Reg->getName();
215 else
216 OS << "zero_reg";
217 OS << " VT=" << VT << '\n';
218}
219
220void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
221 indent Indent) const {
222 OS << Indent << "EmitConvertToTarget " << Slot << '\n';
223}
224
225void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
226 indent Indent) const {
227 OS << Indent << "EmitMergeInputChains <todo: args>\n";
228}
229
230void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, indent Indent) const {
231 OS << Indent << "EmitCopyToReg <todo: args>\n";
232}
233
234void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, indent Indent) const {
235 OS << Indent << "EmitNodeXForm " << NodeXForm->getName() << " Slot=" << Slot
236 << '\n';
237}
238
239void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, indent Indent) const {
240 OS << Indent;
241 OS << (isa<MorphNodeToMatcher>(Val: this) ? "MorphNodeTo: " : "EmitNode: ")
242 << CGI.Namespace << "::" << CGI.getName() << ": <todo flags> ";
243
244 for (const ValueTypeByHwMode &VT : VTs)
245 OS << ' ' << VT;
246 OS << '(';
247 for (unsigned Operand : Operands)
248 OS << Operand << ' ';
249 OS << ")\n";
250}
251
252void CompleteMatchMatcher::printImpl(raw_ostream &OS, indent Indent) const {
253 OS << Indent << "CompleteMatch <todo args>\n";
254 OS << Indent << "Src = " << Pattern.getSrcPattern() << "\n";
255 OS << Indent << "Dst = " << Pattern.getDstPattern() << "\n";
256}
257
258bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
259 // Note: pointer equality isn't enough here, we have to check the enum names
260 // to ensure that the nodes are for the same opcode.
261 return cast<CheckOpcodeMatcher>(Val: M)->Opcode.getEnumName() ==
262 Opcode.getEnumName();
263}
264
265bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
266 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(Val: m);
267 return &M->CGI == &CGI && M->VTs == VTs && M->Operands == Operands &&
268 M->HasChain == HasChain && M->HasInGlue == HasInGlue &&
269 M->HasOutGlue == HasOutGlue && M->HasMemRefs == HasMemRefs &&
270 M->NumFixedArityOperands == NumFixedArityOperands;
271}
272
273void EmitNodeMatcher::anchor() {}
274
275void MorphNodeToMatcher::anchor() {}
276
277// isContradictoryImpl Implementations.
278
279// Check if two simple MVT types are contradictory.
280static bool TypesAreContradictory(MVT T1, MVT T2) {
281 // If the two types are the same, then they don't contradict.
282 if (T1 == T2)
283 return false;
284
285 if (T1 == MVT::pAny)
286 return TypesAreContradictory(T1: MVT::iPTR, T2) &&
287 TypesAreContradictory(T1: MVT::cPTR, T2);
288
289 if (T2 == MVT::pAny)
290 return TypesAreContradictory(T1, T2: MVT::iPTR) &&
291 TypesAreContradictory(T1, T2: MVT::cPTR);
292
293 // If either type is about iPtr, then they don't conflict unless the other
294 // one is not a scalar integer type.
295 if (T1 == MVT::iPTR)
296 return !T2.isInteger() || T2.isVector();
297
298 if (T2 == MVT::iPTR)
299 return !T1.isInteger() || T1.isVector();
300
301 if (T1 == MVT::cPTR)
302 return !T2.isCheriCapability() || T2.isVector();
303
304 if (T2 == MVT::cPTR)
305 return !T1.isCheriCapability() || T1.isVector();
306
307 // Otherwise, they are two different non-iPTR/cPTR types, they conflict.
308 return true;
309}
310
311static bool TypesAreContradictory(const ValueTypeByHwMode &VT1,
312 const ValueTypeByHwMode &VT2) {
313 // If the two types are the same, then they are the same, so they don't
314 // contradict.
315 if (VT1 == VT2)
316 return false;
317
318 // For simple types, use the simple comparison.
319 if (VT1.isSimple() && VT2.isSimple())
320 return TypesAreContradictory(T1: VT1.getSimple(), T2: VT2.getSimple());
321
322 // For non-simple types, we need to check all hardware modes.
323 // The types are contradictory only if they contradict for ALL modes.
324 // If they can be compatible for at least one mode, they don't contradict.
325
326 SmallVector<unsigned, 4> Modes;
327 union_modes(A: VT1, B: VT2, Modes);
328
329 for (unsigned Mode : Modes) {
330 // get() asserts if the mode doesn't exist and there's no default.
331 // If either type can't provide a value for this mode, be conservative
332 // and assume they don't contradict.
333 if (!VT1.hasMode(M: Mode) && !VT1.hasDefault())
334 return false;
335 if (!VT2.hasMode(M: Mode) && !VT2.hasDefault())
336 return false;
337
338 MVT T1 = VT1.get(Mode);
339 MVT T2 = VT2.get(Mode);
340 if (!TypesAreContradictory(T1, T2))
341 return false;
342 }
343
344 // All modes have contradictory types.
345 return true;
346}
347
348bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
349 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(Val: M)) {
350 // One node can't have two different opcodes!
351 // Note: pointer equality isn't enough here, we have to check the enum names
352 // to ensure that the nodes are for the same opcode.
353 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
354 }
355
356 // If the node has a known type, and if the type we're checking for is
357 // different, then we know they contradict. For example, a check for
358 // ISD::STORE will never be true at the same time a check for Type i32 is.
359 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M)) {
360 // If checking for a result the opcode doesn't have, it can't match.
361 if (CT->getResNo() >= getOpcode().getNumResults())
362 return true;
363
364 MVT NodeType = getOpcode().getKnownType(ResNo: CT->getResNo());
365 if (NodeType != MVT::Other)
366 return TypesAreContradictory(VT1: NodeType, VT2: CT->getType());
367 }
368
369 return false;
370}
371
372bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
373 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M)) {
374 // If the two checks are about different results, we don't know if they
375 // conflict!
376 if (getResNo() != CT->getResNo())
377 return false;
378
379 return TypesAreContradictory(VT1: getType(), VT2: CT->getType());
380 }
381 return false;
382}
383
384bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
385 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(Val: M)) {
386 // If the two checks are about different nodes, we don't know if they
387 // conflict!
388 if (CC->getChildNo() != getChildNo())
389 return false;
390
391 return TypesAreContradictory(VT1: getType(), VT2: CC->getType());
392 }
393 return false;
394}
395
396bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
397 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(Val: M))
398 return CIM->getValue() != getValue();
399 return false;
400}
401
402bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
403 if (const CheckChildIntegerMatcher *CCIM =
404 dyn_cast<CheckChildIntegerMatcher>(Val: M)) {
405 // If the two checks are about different nodes, we don't know if they
406 // conflict!
407 if (CCIM->getChildNo() != getChildNo())
408 return false;
409
410 return CCIM->getValue() != getValue();
411 }
412 return false;
413}
414
415bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
416 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(Val: M))
417 return CVT->getVT() != getVT();
418 return false;
419}
420
421bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
422 // AllZeros is contradictory.
423 return isa<CheckImmAllZerosVMatcher>(Val: M);
424}
425
426bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
427 // AllOnes is contradictory.
428 return isa<CheckImmAllOnesVMatcher>(Val: M);
429}
430
431bool CheckUndefMatcher::isContradictoryImpl(const Matcher *M) const {
432 if (const auto *COM = dyn_cast<CheckOpcodeMatcher>(Val: M)) {
433 // If the opcode is neither UNDEF nor POISON, then it is contradictory with
434 // a check that the node is undef/poison.
435 StringRef Opcode = COM->getOpcode().getEnumName();
436 return Opcode != "ISD::UNDEF" && Opcode != "ISD::POISON";
437 }
438 return false;
439}
440
441bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
442 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(Val: M))
443 return CCCM->getCondCodeName() != getCondCodeName();
444 return false;
445}
446
447bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
448 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(Val: M))
449 return CCCCM->getCondCodeName() != getCondCodeName();
450 return false;
451}
452
453void MatcherList::print(raw_ostream &OS, indent Indent) const {
454 for (const Matcher *M : *this)
455 M->printOne(OS, Indent);
456}
457
458void MatcherList::dump() const { print(OS&: dbgs()); }
459