Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/DAGISelMatcher.cpp
35290 views
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
18
void Matcher::anchor() {}
19
20
void Matcher::dump() const { print(errs(), 0); }
21
22
void Matcher::print(raw_ostream &OS, unsigned indent) const {
23
printImpl(OS, indent);
24
if (Next)
25
return Next->print(OS, indent);
26
}
27
28
void Matcher::printOne(raw_ostream &OS) const { printImpl(OS, 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.
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.
52
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.
66
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
79
ScopeMatcher::~ScopeMatcher() {
80
for (Matcher *C : Children)
81
delete C;
82
}
83
84
SwitchOpcodeMatcher::~SwitchOpcodeMatcher() {
85
for (auto &C : Cases)
86
delete C.second;
87
}
88
89
SwitchTypeMatcher::~SwitchTypeMatcher() {
90
for (auto &C : Cases)
91
delete C.second;
92
}
93
94
CheckPredicateMatcher::CheckPredicateMatcher(
95
const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops)
96
: Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()),
97
Operands(Ops.begin(), Ops.end()) {}
98
99
TreePredicateFn CheckPredicateMatcher::getPredicate() const {
100
return TreePredicateFn(Pred);
101
}
102
103
unsigned CheckPredicateMatcher::getNumOperands() const {
104
return Operands.size();
105
}
106
107
unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const {
108
assert(i < Operands.size());
109
return Operands[i];
110
}
111
112
// printImpl methods.
113
114
void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
115
OS.indent(indent) << "Scope\n";
116
for (const Matcher *C : Children) {
117
if (!C)
118
OS.indent(indent + 1) << "NULL POINTER\n";
119
else
120
C->print(OS, indent + 2);
121
}
122
}
123
124
void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
125
OS.indent(indent) << "Record\n";
126
}
127
128
void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
129
OS.indent(indent) << "RecordChild: " << ChildNo << '\n';
130
}
131
132
void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
133
OS.indent(indent) << "RecordMemRef\n";
134
}
135
136
void CaptureGlueInputMatcher::printImpl(raw_ostream &OS,
137
unsigned indent) const {
138
OS.indent(indent) << "CaptureGlueInput\n";
139
}
140
141
void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
142
OS.indent(indent) << "MoveChild " << ChildNo << '\n';
143
}
144
145
void MoveSiblingMatcher::printImpl(raw_ostream &OS, unsigned Indent) const {
146
OS.indent(Indent) << "MoveSibling " << SiblingNo << '\n';
147
}
148
149
void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
150
OS.indent(indent) << "MoveParent\n";
151
}
152
153
void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
154
OS.indent(indent) << "CheckSame " << MatchNumber << '\n';
155
}
156
157
void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
158
OS.indent(indent) << "CheckChild" << ChildNo << "Same\n";
159
}
160
161
void CheckPatternPredicateMatcher::printImpl(raw_ostream &OS,
162
unsigned indent) const {
163
OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n';
164
}
165
166
void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
167
OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n';
168
}
169
170
void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
171
OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n';
172
}
173
174
void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
175
OS.indent(indent) << "SwitchOpcode: {\n";
176
for (const auto &C : Cases) {
177
OS.indent(indent) << "case " << C.first->getEnumName() << ":\n";
178
C.second->print(OS, indent + 2);
179
}
180
OS.indent(indent) << "}\n";
181
}
182
183
void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
184
OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo=" << ResNo
185
<< '\n';
186
}
187
188
void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
189
OS.indent(indent) << "SwitchType: {\n";
190
for (const auto &C : Cases) {
191
OS.indent(indent) << "case " << getEnumName(C.first) << ":\n";
192
C.second->print(OS, indent + 2);
193
}
194
OS.indent(indent) << "}\n";
195
}
196
197
void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
198
OS.indent(indent) << "CheckChildType " << ChildNo << " " << getEnumName(Type)
199
<< '\n';
200
}
201
202
void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
203
OS.indent(indent) << "CheckInteger " << Value << '\n';
204
}
205
206
void CheckChildIntegerMatcher::printImpl(raw_ostream &OS,
207
unsigned indent) const {
208
OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n';
209
}
210
211
void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
212
OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n';
213
}
214
215
void CheckChild2CondCodeMatcher::printImpl(raw_ostream &OS,
216
unsigned indent) const {
217
OS.indent(indent) << "CheckChild2CondCode ISD::" << CondCodeName << '\n';
218
}
219
220
void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
221
OS.indent(indent) << "CheckValueType " << getEnumName(VT) << '\n';
222
}
223
224
void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
225
OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n';
226
}
227
228
void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
229
OS.indent(indent) << "CheckAndImm " << Value << '\n';
230
}
231
232
void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
233
OS.indent(indent) << "CheckOrImm " << Value << '\n';
234
}
235
236
void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS,
237
unsigned indent) const {
238
OS.indent(indent) << "CheckFoldableChainNode\n";
239
}
240
241
void CheckImmAllOnesVMatcher::printImpl(raw_ostream &OS,
242
unsigned indent) const {
243
OS.indent(indent) << "CheckAllOnesV\n";
244
}
245
246
void CheckImmAllZerosVMatcher::printImpl(raw_ostream &OS,
247
unsigned indent) const {
248
OS.indent(indent) << "CheckAllZerosV\n";
249
}
250
251
void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
252
OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT)
253
<< '\n';
254
}
255
256
void EmitStringIntegerMatcher::printImpl(raw_ostream &OS,
257
unsigned indent) const {
258
OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT)
259
<< '\n';
260
}
261
262
void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
263
OS.indent(indent) << "EmitRegister ";
264
if (Reg)
265
OS << Reg->getName();
266
else
267
OS << "zero_reg";
268
OS << " VT=" << getEnumName(VT) << '\n';
269
}
270
271
void EmitConvertToTargetMatcher::printImpl(raw_ostream &OS,
272
unsigned indent) const {
273
OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n';
274
}
275
276
void EmitMergeInputChainsMatcher::printImpl(raw_ostream &OS,
277
unsigned indent) const {
278
OS.indent(indent) << "EmitMergeInputChains <todo: args>\n";
279
}
280
281
void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
282
OS.indent(indent) << "EmitCopyToReg <todo: args>\n";
283
}
284
285
void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
286
OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName()
287
<< " Slot=" << Slot << '\n';
288
}
289
290
void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const {
291
OS.indent(indent);
292
OS << (isa<MorphNodeToMatcher>(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(VTs[i]);
297
OS << '(';
298
for (unsigned i = 0, e = Operands.size(); i != e; ++i)
299
OS << Operands[i] << ' ';
300
OS << ")\n";
301
}
302
303
void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const {
304
OS.indent(indent) << "CompleteMatch <todo args>\n";
305
OS.indent(indent) << "Src = " << Pattern.getSrcPattern() << "\n";
306
OS.indent(indent) << "Dst = " << Pattern.getDstPattern() << "\n";
307
}
308
309
bool 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>(M)->Opcode.getEnumName() ==
313
Opcode.getEnumName();
314
}
315
316
bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const {
317
const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(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
324
void EmitNodeMatcher::anchor() {}
325
326
void MorphNodeToMatcher::anchor() {}
327
328
// isContradictoryImpl Implementations.
329
330
static 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
349
bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const {
350
if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(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>(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(CT->getResNo());
366
if (NodeType != MVT::Other)
367
return TypesAreContradictory(NodeType, CT->getType());
368
}
369
370
return false;
371
}
372
373
bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const {
374
if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M))
375
return TypesAreContradictory(getType(), CT->getType());
376
return false;
377
}
378
379
bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const {
380
if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(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(getType(), CC->getType());
387
}
388
return false;
389
}
390
391
bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
392
if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M))
393
return CIM->getValue() != getValue();
394
return false;
395
}
396
397
bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const {
398
if (const CheckChildIntegerMatcher *CCIM =
399
dyn_cast<CheckChildIntegerMatcher>(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
410
bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const {
411
if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M))
412
return CVT->getVT() != getVT();
413
return false;
414
}
415
416
bool CheckImmAllOnesVMatcher::isContradictoryImpl(const Matcher *M) const {
417
// AllZeros is contradictory.
418
return isa<CheckImmAllZerosVMatcher>(M);
419
}
420
421
bool CheckImmAllZerosVMatcher::isContradictoryImpl(const Matcher *M) const {
422
// AllOnes is contradictory.
423
return isa<CheckImmAllOnesVMatcher>(M);
424
}
425
426
bool CheckCondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
427
if (const auto *CCCM = dyn_cast<CheckCondCodeMatcher>(M))
428
return CCCM->getCondCodeName() != getCondCodeName();
429
return false;
430
}
431
432
bool CheckChild2CondCodeMatcher::isContradictoryImpl(const Matcher *M) const {
433
if (const auto *CCCCM = dyn_cast<CheckChild2CondCodeMatcher>(M))
434
return CCCCM->getCondCodeName() != getCondCodeName();
435
return false;
436
}
437
438