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 CheckCondCodeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
156 OS << Indent << "CheckCondCode ISD::" << CondCodeName << '\n';
157}
158
159void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
160 indent Indent) const {
161 OS << Indent << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
162}
163
164void CheckValueTypeMatcher::printImpl(raw_ostream &OS, indent Indent) const {
165 OS << Indent << "CheckValueType " << getEnumName(T: VT) << '\n';
166}
167
168void CheckComplexPatMatcher::printImpl(raw_ostream &OS, indent Indent) const {
169 OS << Indent << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
170}
171
172void CheckAndImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
173 OS << Indent << "CheckAndImm " << Value << '\n';
174}
175
176void CheckOrImmMatcher::printImpl(raw_ostream &OS, indent Indent) const {
177 OS << Indent << "CheckOrImm " << Value << '\n';
178}
179
180void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
181 indent Indent) const {
182 OS << Indent << "CheckFoldableChainNode\n";
183}
184
185void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
186 OS << Indent << "CheckAllOnesV\n";
187}
188
189void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS, indent Indent) const {
190 OS << Indent << "CheckAllZerosV\n";
191}
192
193void CheckUndefMatcher::printImpl(raw_ostream &OS, indent Indent) const {
194 OS << Indent << "CheckUndef\n";
195}
196
197void EmitIntegerMatcher::printImpl(raw_ostream &OS, indent Indent) const {
198 OS << Indent << "EmitInteger " << Val << " VT=" << VT << '\n';
199}
200
201void EmitRegisterMatcher::printImpl(raw_ostream &OS, indent Indent) const {
202 OS << Indent << "EmitRegister ";
203 if (Reg)
204 OS << Reg->getName();
205 else
206 OS << "zero_reg";
207 OS << " VT=" << VT << '\n';
208}
209
210void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
211 indent Indent) const {
212 OS << Indent << "EmitConvertToTarget " << Slot << '\n';
213}
214
215void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
216 indent Indent) const {
217 OS << Indent << "EmitMergeInputChains <todo: args>\n";
218}
219
220void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, indent Indent) const {
221 OS << Indent << "EmitCopyToReg <todo: args>\n";
222}
223
224void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, indent Indent) const {
225 OS << Indent << "EmitNodeXForm " << NodeXForm->getName() << " Slot=" << Slot
226 << '\n';
227}
228
229void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, indent Indent) const {
230 OS << Indent;
231 OS << (isa<MorphNodeToMatcher>(Val: this) ? "MorphNodeTo: " : "EmitNode: ")
232 << CGI.Namespace << "::" << CGI.getName() << ": <todo flags> ";
233
234 for (const ValueTypeByHwMode &VT : VTs)
235 OS << ' ' << VT;
236 OS << '(';
237 for (unsigned Operand : Operands)
238 OS << Operand << ' ';
239 OS << ")\n";
240}
241
242void CompleteMatchMatcher::printImpl(raw_ostream &OS, indent Indent) const {
243 OS << Indent << "CompleteMatch <todo args>\n";
244 OS << Indent << "Src = " << Pattern.getSrcPattern() << "\n";
245 OS << Indent << "Dst = " << Pattern.getDstPattern() << "\n";
246}
247
248bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const {
249 // Note: pointer equality isn't enough here, we have to check the enum names
250 // to ensure that the nodes are for the same opcode.
251 return cast<CheckOpcodeMatcher>(Val: M)->Opcode.getEnumName() ==
252 Opcode.getEnumName();
253}
254
255bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
256 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(Val: m);
257 return &M->CGI == &CGI && M->VTs == VTs && M->Operands == Operands &&
258 M->HasChain == HasChain && M->HasInGlue == HasInGlue &&
259 M->HasOutGlue == HasOutGlue && M->HasMemRefs == HasMemRefs &&
260 M->NumFixedArityOperands == NumFixedArityOperands;
261}
262
263void EmitNodeMatcher::anchor() {}
264
265void MorphNodeToMatcher::anchor() {}
266
267// isContradictoryImpl Implementations.
268
269// Check if two simple MVT types are contradictory.
270static bool TypesAreContradictory(MVT T1, MVT T2) {
271 // If the two types are the same, then they don't contradict.
272 if (T1 == T2)
273 return false;
274
275 if (T1 == MVT::pAny)
276 return TypesAreContradictory(T1: MVT::iPTR, T2) &&
277 TypesAreContradictory(T1: MVT::cPTR, T2);
278
279 if (T2 == MVT::pAny)
280 return TypesAreContradictory(T1, T2: MVT::iPTR) &&
281 TypesAreContradictory(T1, T2: MVT::cPTR);
282
283 // If either type is about iPtr, then they don't conflict unless the other
284 // one is not a scalar integer type.
285 if (T1 == MVT::iPTR)
286 return !T2.isInteger() || T2.isVector();
287
288 if (T2 == MVT::iPTR)
289 return !T1.isInteger() || T1.isVector();
290
291 if (T1 == MVT::cPTR)
292 return !T2.isCheriCapability() || T2.isVector();
293
294 if (T2 == MVT::cPTR)
295 return !T1.isCheriCapability() || T1.isVector();
296
297 // Otherwise, they are two different non-iPTR/cPTR types, they conflict.
298 return true;
299}
300
301static bool TypesAreContradictory(const ValueTypeByHwMode &VT1,
302 const ValueTypeByHwMode &VT2) {
303 // If the two types are the same, then they are the same, so they don't
304 // contradict.
305 if (VT1 == VT2)
306 return false;
307
308 // For simple types, use the simple comparison.
309 if (VT1.isSimple() && VT2.isSimple())
310 return TypesAreContradictory(T1: VT1.getSimple(), T2: VT2.getSimple());
311
312 // For non-simple types, we need to check all hardware modes.
313 // The types are contradictory only if they contradict for ALL modes.
314 // If they can be compatible for at least one mode, they don't contradict.
315
316 SmallVector<unsigned, 4> Modes;
317 union_modes(A: VT1, B: VT2, Modes);
318
319 for (unsigned Mode : Modes) {
320 // get() asserts if the mode doesn't exist and there's no default.
321 // If either type can't provide a value for this mode, be conservative
322 // and assume they don't contradict.
323 if (!VT1.hasMode(M: Mode) && !VT1.hasDefault())
324 return false;
325 if (!VT2.hasMode(M: Mode) && !VT2.hasDefault())
326 return false;
327
328 MVT T1 = VT1.get(Mode);
329 MVT T2 = VT2.get(Mode);
330 if (!TypesAreContradictory(T1, T2))
331 return false;
332 }
333
334 // All modes have contradictory types.
335 return true;
336}
337
338bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
339 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(Val: M)) {
340 // One node can't have two different opcodes!
341 // Note: pointer equality isn't enough here, we have to check the enum names
342 // to ensure that the nodes are for the same opcode.
343 return COM->getOpcode().getEnumName() != getOpcode().getEnumName();
344 }
345
346 // If the node has a known type, and if the type we're checking for is
347 // different, then we know they contradict. For example, a check for
348 // ISD::STORE will never be true at the same time a check for Type i32 is.
349 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M)) {
350 // If checking for a result the opcode doesn't have, it can't match.
351 if (CT->getResNo() >= getOpcode().getNumResults())
352 return true;
353
354 MVT NodeType = getOpcode().getKnownType(ResNo: CT->getResNo());
355 if (NodeType != MVT::Other)
356 return TypesAreContradictory(VT1: NodeType, VT2: CT->getType());
357 }
358
359 return false;
360}
361
362bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
363 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(Val: M)) {
364 // If the two checks are about different results, we don't know if they
365 // conflict!
366 if (getResNo() != CT->getResNo())
367 return false;
368
369 return TypesAreContradictory(VT1: getType(), VT2: CT->getType());
370 }
371 return false;
372}
373
374bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
375 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(Val: M)) {
376 // If the two checks are about different nodes, we don't know if they
377 // conflict!
378 if (CC->getChildNo() != getChildNo())
379 return false;
380
381 return TypesAreContradictory(VT1: getType(), VT2: CC->getType());
382 }
383 return false;
384}
385
386bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
387 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(Val: M))
388 return CIM->getValue() != getValue();
389 return false;
390}
391
392bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
393 if (const CheckChildIntegerMatcher *CCIM =
394 dyn_cast<CheckChildIntegerMatcher>(Val: M)) {
395 // If the two checks are about different nodes, we don't know if they
396 // conflict!
397 if (CCIM->getChildNo() != getChildNo())
398 return false;
399
400 return CCIM->getValue() != getValue();
401 }
402 return false;
403}
404
405bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
406 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(Val: M))
407 return CVT->getVT() != getVT();
408 return false;
409}
410
411bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
412 // AllZeros is contradictory.
413 return isa<CheckImmAllZerosVMatcher>(Val: M);
414}
415
416bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
417 // AllOnes is contradictory.
418 return isa<CheckImmAllOnesVMatcher>(Val: M);
419}
420
421bool CheckUndefMatcher::isContradictoryImpl(const Matcher *M) const {
422 if (const auto *COM = dyn_cast<CheckOpcodeMatcher>(Val: M)) {
423 // If the opcode is neither UNDEF nor POISON, then it is contradictory with
424 // a check that the node is undef/poison.
425 StringRef Opcode = COM->getOpcode().getEnumName();
426 return Opcode != "ISD::UNDEF" && Opcode != "ISD::POISON";
427 }
428 return false;
429}
430
431bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
432 if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(Val: M))
433 return CCCM->getCondCodeName() != getCondCodeName();
434 return false;
435}
436
437bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
438 if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(Val: M))
439 return CCCCM->getCondCodeName() != getCondCodeName();
440 return false;
441}
442
443void MatcherList::print(raw_ostream &OS, indent Indent) const {
444 for (const Matcher *M : *this)
445 M->printOne(OS, Indent);
446}
447
448void MatcherList::dump() const { print(OS&: dbgs()); }
449