1//===- DAGISelMatcher.h - Representation of DAG pattern matcher -*- C++ -*-===//
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#ifndef LLVM_UTILS_TABLEGEN_COMMON_DAGISELMATCHER_H
10#define LLVM_UTILS_TABLEGEN_COMMON_DAGISELMATCHER_H
11
12#include "Common/InfoByHwMode.h"
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/CodeGenTypes/MachineValueType.h"
17#include "llvm/Support/Casting.h"
18#include <cassert>
19#include <cstddef>
20#include <memory>
21#include <string>
22#include <utility>
23
24namespace llvm {
25class CodeGenRegister;
26class CodeGenDAGPatterns;
27class CodeGenInstruction;
28class Matcher;
29class PatternToMatch;
30class raw_ostream;
31class ComplexPattern;
32class Record;
33class SDNodeInfo;
34class TreePredicateFn;
35class TreePattern;
36
37Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
38 unsigned Variant,
39 const CodeGenDAGPatterns &CGP);
40void OptimizeMatcher(std::unique_ptr<Matcher> &Matcher,
41 const CodeGenDAGPatterns &CGP);
42void EmitMatcherTable(Matcher *Matcher, const CodeGenDAGPatterns &CGP,
43 raw_ostream &OS);
44
45/// Matcher - Base class for all the DAG ISel Matcher representation
46/// nodes.
47class Matcher {
48 // The next matcher node that is executed after this one. Null if this is
49 // the last stage of a match.
50 std::unique_ptr<Matcher> Next;
51 size_t Size = 0; // Size in bytes of matcher and all its children (if any).
52 virtual void anchor();
53
54public:
55 enum KindTy {
56 // Matcher state manipulation.
57 Scope, // Push a checking scope.
58 RecordNode, // Record the current node.
59 RecordChild, // Record a child of the current node.
60 RecordMemRef, // Record the memref in the current node.
61 CaptureGlueInput, // If the current node has an input glue, save it.
62 MoveChild, // Move current node to specified child.
63 MoveSibling, // Move current node to specified sibling.
64 MoveParent, // Move current node to parent.
65
66 // Predicate checking.
67 CheckSame, // Fail if not same as prev match.
68 CheckChildSame, // Fail if child not same as prev match.
69 CheckPatternPredicate,
70 CheckPredicate, // Fail if node predicate fails.
71 CheckOpcode, // Fail if not opcode.
72 SwitchOpcode, // Dispatch based on opcode.
73 CheckType, // Fail if not correct type.
74 SwitchType, // Dispatch based on type.
75 CheckChildType, // Fail if child has wrong type.
76 CheckInteger, // Fail if wrong val.
77 CheckChildInteger, // Fail if child is wrong val.
78 CheckCondCode, // Fail if not condcode.
79 CheckChild2CondCode, // Fail if child is wrong condcode.
80 CheckValueType,
81 CheckComplexPat,
82 CheckAndImm,
83 CheckOrImm,
84 CheckImmAllOnesV,
85 CheckImmAllZerosV,
86 CheckFoldableChainNode,
87
88 // Node creation/emisssion.
89 EmitInteger, // Create a TargetConstant
90 EmitRegister, // Create a register.
91 EmitConvertToTarget, // Convert a imm/fpimm to target imm/fpimm
92 EmitMergeInputChains, // Merge together a chains for an input.
93 EmitCopyToReg, // Emit a copytoreg into a physreg.
94 EmitNode, // Create a DAG node
95 EmitNodeXForm, // Run a SDNodeXForm
96 CompleteMatch, // Finish a match and update the results.
97 MorphNodeTo, // Build a node, finish a match and update results.
98
99 // Highest enum value; watch out when adding more.
100 HighestKind = MorphNodeTo
101 };
102 const KindTy Kind;
103
104protected:
105 Matcher(KindTy K) : Kind(K) {}
106
107public:
108 virtual ~Matcher() = default;
109
110 unsigned getSize() const { return Size; }
111 void setSize(unsigned sz) { Size = sz; }
112 KindTy getKind() const { return Kind; }
113
114 Matcher *getNext() { return Next.get(); }
115 const Matcher *getNext() const { return Next.get(); }
116 void setNext(Matcher *C) { Next.reset(p: C); }
117 Matcher *takeNext() { return Next.release(); }
118
119 std::unique_ptr<Matcher> &getNextPtr() { return Next; }
120
121 bool isEqual(const Matcher *M) const {
122 if (getKind() != M->getKind())
123 return false;
124 return isEqualImpl(M);
125 }
126
127 /// isSimplePredicateNode - Return true if this is a simple predicate that
128 /// operates on the node or its children without potential side effects or a
129 /// change of the current node.
130 bool isSimplePredicateNode() const {
131 switch (getKind()) {
132 default:
133 return false;
134 case CheckSame:
135 case CheckChildSame:
136 case CheckPatternPredicate:
137 case CheckPredicate:
138 case CheckOpcode:
139 case CheckType:
140 case CheckChildType:
141 case CheckInteger:
142 case CheckChildInteger:
143 case CheckCondCode:
144 case CheckChild2CondCode:
145 case CheckValueType:
146 case CheckAndImm:
147 case CheckOrImm:
148 case CheckImmAllOnesV:
149 case CheckImmAllZerosV:
150 case CheckFoldableChainNode:
151 return true;
152 }
153 }
154
155 /// isSimplePredicateOrRecordNode - Return true if this is a record node or
156 /// a simple predicate.
157 bool isSimplePredicateOrRecordNode() const {
158 return isSimplePredicateNode() || getKind() == RecordNode ||
159 getKind() == RecordChild;
160 }
161
162 /// unlinkNode - Unlink the specified node from this chain. If Other ==
163 /// this, we unlink the next pointer and return it. Otherwise we unlink
164 /// Other from the list and return this.
165 Matcher *unlinkNode(Matcher *Other);
166
167 /// canMoveBefore - Return true if this matcher is the same as Other, or if
168 /// we can move this matcher past all of the nodes in-between Other and this
169 /// node. Other must be equal to or before this.
170 bool canMoveBefore(const Matcher *Other) const;
171
172 /// canMoveBeforeNode - Return true if it is safe to move the current
173 /// matcher across the specified one.
174 bool canMoveBeforeNode(const Matcher *Other) const;
175
176 /// isContradictory - Return true of these two matchers could never match on
177 /// the same node.
178 bool isContradictory(const Matcher *Other) const {
179 // Since this predicate is reflexive, we canonicalize the ordering so that
180 // we always match a node against nodes with kinds that are greater or
181 // equal to them. For example, we'll pass in a CheckType node as an
182 // argument to the CheckOpcode method, not the other way around.
183 if (getKind() < Other->getKind())
184 return isContradictoryImpl(M: Other);
185 return Other->isContradictoryImpl(M: this);
186 }
187
188 void print(raw_ostream &OS, indent Indent = indent(0)) const;
189 void printOne(raw_ostream &OS) const;
190 void dump() const;
191
192protected:
193 virtual void printImpl(raw_ostream &OS, indent Indent) const = 0;
194 virtual bool isEqualImpl(const Matcher *M) const = 0;
195 virtual bool isContradictoryImpl(const Matcher *M) const { return false; }
196};
197
198/// ScopeMatcher - This attempts to match each of its children to find the first
199/// one that successfully matches. If one child fails, it tries the next child.
200/// If none of the children match then this check fails. It never has a 'next'.
201class ScopeMatcher : public Matcher {
202 SmallVector<Matcher *, 4> Children;
203
204public:
205 ScopeMatcher(SmallVectorImpl<Matcher *> &&children)
206 : Matcher(Scope), Children(std::move(children)) {}
207 ~ScopeMatcher() override;
208
209 unsigned getNumChildren() const { return Children.size(); }
210
211 Matcher *getChild(unsigned i) { return Children[i]; }
212 const Matcher *getChild(unsigned i) const { return Children[i]; }
213
214 void resetChild(unsigned i, Matcher *N) {
215 delete Children[i];
216 Children[i] = N;
217 }
218
219 Matcher *takeChild(unsigned i) {
220 Matcher *Res = Children[i];
221 Children[i] = nullptr;
222 return Res;
223 }
224
225 SmallVectorImpl<Matcher *> &getChildren() { return Children; }
226
227 static bool classof(const Matcher *N) { return N->getKind() == Scope; }
228
229private:
230 void printImpl(raw_ostream &OS, indent Indent) const override;
231 bool isEqualImpl(const Matcher *M) const override { return false; }
232};
233
234/// RecordMatcher - Save the current node in the operand list.
235class RecordMatcher : public Matcher {
236 /// WhatFor - This is a string indicating why we're recording this. This
237 /// should only be used for comment generation not anything semantic.
238 std::string WhatFor;
239
240 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
241 /// just printed as a comment.
242 unsigned ResultNo;
243
244public:
245 RecordMatcher(const std::string &whatfor, unsigned resultNo)
246 : Matcher(RecordNode), WhatFor(whatfor), ResultNo(resultNo) {}
247
248 const std::string &getWhatFor() const { return WhatFor; }
249 unsigned getResultNo() const { return ResultNo; }
250
251 static bool classof(const Matcher *N) { return N->getKind() == RecordNode; }
252
253private:
254 void printImpl(raw_ostream &OS, indent Indent) const override;
255 bool isEqualImpl(const Matcher *M) const override { return true; }
256};
257
258/// RecordChildMatcher - Save a numbered child of the current node, or fail
259/// the match if it doesn't exist. This is logically equivalent to:
260/// MoveChild N + RecordNode + MoveParent.
261class RecordChildMatcher : public Matcher {
262 unsigned ChildNo;
263
264 /// WhatFor - This is a string indicating why we're recording this. This
265 /// should only be used for comment generation not anything semantic.
266 std::string WhatFor;
267
268 /// ResultNo - The slot number in the RecordedNodes vector that this will be,
269 /// just printed as a comment.
270 unsigned ResultNo;
271
272public:
273 RecordChildMatcher(unsigned childno, const std::string &whatfor,
274 unsigned resultNo)
275 : Matcher(RecordChild), ChildNo(childno), WhatFor(whatfor),
276 ResultNo(resultNo) {}
277
278 unsigned getChildNo() const { return ChildNo; }
279 const std::string &getWhatFor() const { return WhatFor; }
280 unsigned getResultNo() const { return ResultNo; }
281
282 static bool classof(const Matcher *N) { return N->getKind() == RecordChild; }
283
284private:
285 void printImpl(raw_ostream &OS, indent Indent) const override;
286 bool isEqualImpl(const Matcher *M) const override {
287 return cast<RecordChildMatcher>(Val: M)->getChildNo() == getChildNo();
288 }
289};
290
291/// RecordMemRefMatcher - Save the current node's memref.
292class RecordMemRefMatcher : public Matcher {
293public:
294 RecordMemRefMatcher() : Matcher(RecordMemRef) {}
295
296 static bool classof(const Matcher *N) { return N->getKind() == RecordMemRef; }
297
298private:
299 void printImpl(raw_ostream &OS, indent Indent) const override;
300 bool isEqualImpl(const Matcher *M) const override { return true; }
301};
302
303/// CaptureGlueInputMatcher - If the current record has a glue input, record
304/// it so that it is used as an input to the generated code.
305class CaptureGlueInputMatcher : public Matcher {
306public:
307 CaptureGlueInputMatcher() : Matcher(CaptureGlueInput) {}
308
309 static bool classof(const Matcher *N) {
310 return N->getKind() == CaptureGlueInput;
311 }
312
313private:
314 void printImpl(raw_ostream &OS, indent Indent) const override;
315 bool isEqualImpl(const Matcher *M) const override { return true; }
316};
317
318/// MoveChildMatcher - This tells the interpreter to move into the
319/// specified child node.
320class MoveChildMatcher : public Matcher {
321 unsigned ChildNo;
322
323public:
324 MoveChildMatcher(unsigned childNo) : Matcher(MoveChild), ChildNo(childNo) {}
325
326 unsigned getChildNo() const { return ChildNo; }
327
328 static bool classof(const Matcher *N) { return N->getKind() == MoveChild; }
329
330private:
331 void printImpl(raw_ostream &OS, indent Indent) const override;
332 bool isEqualImpl(const Matcher *M) const override {
333 return cast<MoveChildMatcher>(Val: M)->getChildNo() == getChildNo();
334 }
335};
336
337/// MoveSiblingMatcher - This tells the interpreter to move into the
338/// specified sibling node.
339class MoveSiblingMatcher : public Matcher {
340 unsigned SiblingNo;
341
342public:
343 MoveSiblingMatcher(unsigned SiblingNo)
344 : Matcher(MoveSibling), SiblingNo(SiblingNo) {}
345
346 unsigned getSiblingNo() const { return SiblingNo; }
347
348 static bool classof(const Matcher *N) { return N->getKind() == MoveSibling; }
349
350private:
351 void printImpl(raw_ostream &OS, indent Indent) const override;
352 bool isEqualImpl(const Matcher *M) const override {
353 return cast<MoveSiblingMatcher>(Val: M)->getSiblingNo() == getSiblingNo();
354 }
355};
356
357/// MoveParentMatcher - This tells the interpreter to move to the parent
358/// of the current node.
359class MoveParentMatcher : public Matcher {
360public:
361 MoveParentMatcher() : Matcher(MoveParent) {}
362
363 static bool classof(const Matcher *N) { return N->getKind() == MoveParent; }
364
365private:
366 void printImpl(raw_ostream &OS, indent Indent) const override;
367 bool isEqualImpl(const Matcher *M) const override { return true; }
368};
369
370/// CheckSameMatcher - This checks to see if this node is exactly the same
371/// node as the specified match that was recorded with 'Record'. This is used
372/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
373class CheckSameMatcher : public Matcher {
374 unsigned MatchNumber;
375
376public:
377 CheckSameMatcher(unsigned matchnumber)
378 : Matcher(CheckSame), MatchNumber(matchnumber) {}
379
380 unsigned getMatchNumber() const { return MatchNumber; }
381
382 static bool classof(const Matcher *N) { return N->getKind() == CheckSame; }
383
384private:
385 void printImpl(raw_ostream &OS, indent Indent) const override;
386 bool isEqualImpl(const Matcher *M) const override {
387 return cast<CheckSameMatcher>(Val: M)->getMatchNumber() == getMatchNumber();
388 }
389};
390
391/// CheckChildSameMatcher - This checks to see if child node is exactly the same
392/// node as the specified match that was recorded with 'Record'. This is used
393/// when patterns have the same name in them, like '(mul GPR:$in, GPR:$in)'.
394class CheckChildSameMatcher : public Matcher {
395 unsigned ChildNo;
396 unsigned MatchNumber;
397
398public:
399 CheckChildSameMatcher(unsigned childno, unsigned matchnumber)
400 : Matcher(CheckChildSame), ChildNo(childno), MatchNumber(matchnumber) {}
401
402 unsigned getChildNo() const { return ChildNo; }
403 unsigned getMatchNumber() const { return MatchNumber; }
404
405 static bool classof(const Matcher *N) {
406 return N->getKind() == CheckChildSame;
407 }
408
409private:
410 void printImpl(raw_ostream &OS, indent Indent) const override;
411 bool isEqualImpl(const Matcher *M) const override {
412 return cast<CheckChildSameMatcher>(Val: M)->ChildNo == ChildNo &&
413 cast<CheckChildSameMatcher>(Val: M)->MatchNumber == MatchNumber;
414 }
415};
416
417/// CheckPatternPredicateMatcher - This checks the target-specific predicate
418/// to see if the entire pattern is capable of matching. This predicate does
419/// not take a node as input. This is used for subtarget feature checks etc.
420class CheckPatternPredicateMatcher : public Matcher {
421 std::string Predicate;
422
423public:
424 CheckPatternPredicateMatcher(StringRef predicate)
425 : Matcher(CheckPatternPredicate), Predicate(predicate) {}
426
427 StringRef getPredicate() const { return Predicate; }
428
429 static bool classof(const Matcher *N) {
430 return N->getKind() == CheckPatternPredicate;
431 }
432
433private:
434 void printImpl(raw_ostream &OS, indent Indent) const override;
435 bool isEqualImpl(const Matcher *M) const override {
436 return cast<CheckPatternPredicateMatcher>(Val: M)->getPredicate() == Predicate;
437 }
438};
439
440/// CheckPredicateMatcher - This checks the target-specific predicate to
441/// see if the node is acceptable.
442class CheckPredicateMatcher : public Matcher {
443 TreePattern *Pred;
444 const SmallVector<unsigned, 4> Operands;
445
446public:
447 CheckPredicateMatcher(const TreePredicateFn &pred,
448 ArrayRef<unsigned> Operands);
449
450 TreePredicateFn getPredicate() const;
451 unsigned getNumOperands() const;
452 unsigned getOperandNo(unsigned i) const;
453
454 static bool classof(const Matcher *N) {
455 return N->getKind() == CheckPredicate;
456 }
457
458private:
459 void printImpl(raw_ostream &OS, indent Indent) const override;
460 bool isEqualImpl(const Matcher *M) const override {
461 return cast<CheckPredicateMatcher>(Val: M)->Pred == Pred;
462 }
463};
464
465/// CheckOpcodeMatcher - This checks to see if the current node has the
466/// specified opcode, if not it fails to match.
467class CheckOpcodeMatcher : public Matcher {
468 const SDNodeInfo &Opcode;
469
470public:
471 CheckOpcodeMatcher(const SDNodeInfo &opcode)
472 : Matcher(CheckOpcode), Opcode(opcode) {}
473
474 const SDNodeInfo &getOpcode() const { return Opcode; }
475
476 static bool classof(const Matcher *N) { return N->getKind() == CheckOpcode; }
477
478private:
479 void printImpl(raw_ostream &OS, indent Indent) const override;
480 bool isEqualImpl(const Matcher *M) const override;
481 bool isContradictoryImpl(const Matcher *M) const override;
482};
483
484/// SwitchOpcodeMatcher - Switch based on the current node's opcode, dispatching
485/// to one matcher per opcode. If the opcode doesn't match any of the cases,
486/// then the match fails. This is semantically equivalent to a Scope node where
487/// every child does a CheckOpcode, but is much faster.
488class SwitchOpcodeMatcher : public Matcher {
489 SmallVector<std::pair<const SDNodeInfo *, Matcher *>, 8> Cases;
490
491public:
492 SwitchOpcodeMatcher(
493 SmallVectorImpl<std::pair<const SDNodeInfo *, Matcher *>> &&cases)
494 : Matcher(SwitchOpcode), Cases(std::move(cases)) {}
495 ~SwitchOpcodeMatcher() override;
496
497 static bool classof(const Matcher *N) { return N->getKind() == SwitchOpcode; }
498
499 unsigned getNumCases() const { return Cases.size(); }
500
501 const SDNodeInfo &getCaseOpcode(unsigned i) const { return *Cases[i].first; }
502 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
503 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
504
505private:
506 void printImpl(raw_ostream &OS, indent Indent) const override;
507 bool isEqualImpl(const Matcher *M) const override { return false; }
508};
509
510/// CheckTypeMatcher - This checks to see if the current node has the
511/// specified type at the specified result, if not it fails to match.
512class CheckTypeMatcher : public Matcher {
513 ValueTypeByHwMode Type;
514 unsigned ResNo;
515
516public:
517 CheckTypeMatcher(ValueTypeByHwMode type, unsigned resno)
518 : Matcher(CheckType), Type(std::move(type)), ResNo(resno) {}
519
520 const ValueTypeByHwMode &getType() const { return Type; }
521 unsigned getResNo() const { return ResNo; }
522
523 static bool classof(const Matcher *N) { return N->getKind() == CheckType; }
524
525private:
526 void printImpl(raw_ostream &OS, indent Indent) const override;
527 bool isEqualImpl(const Matcher *M) const override {
528 return cast<CheckTypeMatcher>(Val: M)->Type == Type;
529 }
530 bool isContradictoryImpl(const Matcher *M) const override;
531};
532
533/// SwitchTypeMatcher - Switch based on the current node's type, dispatching
534/// to one matcher per case. If the type doesn't match any of the cases,
535/// then the match fails. This is semantically equivalent to a Scope node where
536/// every child does a CheckType, but is much faster.
537class SwitchTypeMatcher : public Matcher {
538 SmallVector<std::pair<MVT, Matcher *>, 8> Cases;
539
540public:
541 SwitchTypeMatcher(SmallVectorImpl<std::pair<MVT, Matcher *>> &&cases)
542 : Matcher(SwitchType), Cases(std::move(cases)) {}
543 ~SwitchTypeMatcher() override;
544
545 static bool classof(const Matcher *N) { return N->getKind() == SwitchType; }
546
547 unsigned getNumCases() const { return Cases.size(); }
548
549 MVT getCaseType(unsigned i) const { return Cases[i].first; }
550 Matcher *getCaseMatcher(unsigned i) { return Cases[i].second; }
551 const Matcher *getCaseMatcher(unsigned i) const { return Cases[i].second; }
552
553private:
554 void printImpl(raw_ostream &OS, indent Indent) const override;
555 bool isEqualImpl(const Matcher *M) const override { return false; }
556};
557
558/// CheckChildTypeMatcher - This checks to see if a child node has the
559/// specified type, if not it fails to match.
560class CheckChildTypeMatcher : public Matcher {
561 unsigned ChildNo;
562 ValueTypeByHwMode Type;
563
564public:
565 CheckChildTypeMatcher(unsigned childno, ValueTypeByHwMode type)
566 : Matcher(CheckChildType), ChildNo(childno), Type(std::move(type)) {}
567
568 unsigned getChildNo() const { return ChildNo; }
569 const ValueTypeByHwMode &getType() const { return Type; }
570
571 static bool classof(const Matcher *N) {
572 return N->getKind() == CheckChildType;
573 }
574
575private:
576 void printImpl(raw_ostream &OS, indent Indent) const override;
577 bool isEqualImpl(const Matcher *M) const override {
578 return cast<CheckChildTypeMatcher>(Val: M)->ChildNo == ChildNo &&
579 cast<CheckChildTypeMatcher>(Val: M)->Type == Type;
580 }
581 bool isContradictoryImpl(const Matcher *M) const override;
582};
583
584/// CheckIntegerMatcher - This checks to see if the current node is a
585/// ConstantSDNode with the specified integer value, if not it fails to match.
586class CheckIntegerMatcher : public Matcher {
587 int64_t Value;
588
589public:
590 CheckIntegerMatcher(int64_t value) : Matcher(CheckInteger), Value(value) {}
591
592 int64_t getValue() const { return Value; }
593
594 static bool classof(const Matcher *N) { return N->getKind() == CheckInteger; }
595
596private:
597 void printImpl(raw_ostream &OS, indent Indent) const override;
598 bool isEqualImpl(const Matcher *M) const override {
599 return cast<CheckIntegerMatcher>(Val: M)->Value == Value;
600 }
601 bool isContradictoryImpl(const Matcher *M) const override;
602};
603
604/// CheckChildIntegerMatcher - This checks to see if the child node is a
605/// ConstantSDNode with a specified integer value, if not it fails to match.
606class CheckChildIntegerMatcher : public Matcher {
607 unsigned ChildNo;
608 int64_t Value;
609
610public:
611 CheckChildIntegerMatcher(unsigned childno, int64_t value)
612 : Matcher(CheckChildInteger), ChildNo(childno), Value(value) {}
613
614 unsigned getChildNo() const { return ChildNo; }
615 int64_t getValue() const { return Value; }
616
617 static bool classof(const Matcher *N) {
618 return N->getKind() == CheckChildInteger;
619 }
620
621private:
622 void printImpl(raw_ostream &OS, indent Indent) const override;
623 bool isEqualImpl(const Matcher *M) const override {
624 return cast<CheckChildIntegerMatcher>(Val: M)->ChildNo == ChildNo &&
625 cast<CheckChildIntegerMatcher>(Val: M)->Value == Value;
626 }
627 bool isContradictoryImpl(const Matcher *M) const override;
628};
629
630/// CheckCondCodeMatcher - This checks to see if the current node is a
631/// CondCodeSDNode with the specified condition, if not it fails to match.
632class CheckCondCodeMatcher : public Matcher {
633 StringRef CondCodeName;
634
635public:
636 CheckCondCodeMatcher(StringRef condcodename)
637 : Matcher(CheckCondCode), CondCodeName(condcodename) {}
638
639 StringRef getCondCodeName() const { return CondCodeName; }
640
641 static bool classof(const Matcher *N) {
642 return N->getKind() == CheckCondCode;
643 }
644
645private:
646 void printImpl(raw_ostream &OS, indent Indent) const override;
647 bool isEqualImpl(const Matcher *M) const override {
648 return cast<CheckCondCodeMatcher>(Val: M)->CondCodeName == CondCodeName;
649 }
650 bool isContradictoryImpl(const Matcher *M) const override;
651};
652
653/// CheckChild2CondCodeMatcher - This checks to see if child 2 node is a
654/// CondCodeSDNode with the specified condition, if not it fails to match.
655class CheckChild2CondCodeMatcher : public Matcher {
656 StringRef CondCodeName;
657
658public:
659 CheckChild2CondCodeMatcher(StringRef condcodename)
660 : Matcher(CheckChild2CondCode), CondCodeName(condcodename) {}
661
662 StringRef getCondCodeName() const { return CondCodeName; }
663
664 static bool classof(const Matcher *N) {
665 return N->getKind() == CheckChild2CondCode;
666 }
667
668private:
669 void printImpl(raw_ostream &OS, indent Indent) const override;
670 bool isEqualImpl(const Matcher *M) const override {
671 return cast<CheckChild2CondCodeMatcher>(Val: M)->CondCodeName == CondCodeName;
672 }
673 bool isContradictoryImpl(const Matcher *M) const override;
674};
675
676/// CheckValueTypeMatcher - This checks to see if the current node is a
677/// VTSDNode with the specified type, if not it fails to match.
678class CheckValueTypeMatcher : public Matcher {
679 MVT VT;
680
681public:
682 CheckValueTypeMatcher(MVT SimpleVT) : Matcher(CheckValueType), VT(SimpleVT) {}
683
684 MVT getVT() const { return VT; }
685
686 static bool classof(const Matcher *N) {
687 return N->getKind() == CheckValueType;
688 }
689
690private:
691 void printImpl(raw_ostream &OS, indent Indent) const override;
692 bool isEqualImpl(const Matcher *M) const override {
693 return cast<CheckValueTypeMatcher>(Val: M)->VT == VT;
694 }
695 bool isContradictoryImpl(const Matcher *M) const override;
696};
697
698/// CheckComplexPatMatcher - This node runs the specified ComplexPattern on
699/// the current node.
700class CheckComplexPatMatcher : public Matcher {
701 const ComplexPattern &Pattern;
702
703 /// MatchNumber - This is the recorded nodes slot that contains the node we
704 /// want to match against.
705 unsigned MatchNumber;
706
707 /// Name - The name of the node we're matching, for comment emission.
708 StringRef Name;
709
710 /// FirstResult - This is the first slot in the RecordedNodes list that the
711 /// result of the match populates.
712 unsigned FirstResult;
713
714public:
715 CheckComplexPatMatcher(const ComplexPattern &pattern, unsigned matchnumber,
716 StringRef name, unsigned firstresult)
717 : Matcher(CheckComplexPat), Pattern(pattern), MatchNumber(matchnumber),
718 Name(name), FirstResult(firstresult) {}
719
720 const ComplexPattern &getPattern() const { return Pattern; }
721 unsigned getMatchNumber() const { return MatchNumber; }
722
723 StringRef getName() const { return Name; }
724 unsigned getFirstResult() const { return FirstResult; }
725
726 static bool classof(const Matcher *N) {
727 return N->getKind() == CheckComplexPat;
728 }
729
730private:
731 void printImpl(raw_ostream &OS, indent Indent) const override;
732 bool isEqualImpl(const Matcher *M) const override {
733 return &cast<CheckComplexPatMatcher>(Val: M)->Pattern == &Pattern &&
734 cast<CheckComplexPatMatcher>(Val: M)->MatchNumber == MatchNumber;
735 }
736};
737
738/// CheckAndImmMatcher - This checks to see if the current node is an 'and'
739/// with something equivalent to the specified immediate.
740class CheckAndImmMatcher : public Matcher {
741 int64_t Value;
742
743public:
744 CheckAndImmMatcher(int64_t value) : Matcher(CheckAndImm), Value(value) {}
745
746 int64_t getValue() const { return Value; }
747
748 static bool classof(const Matcher *N) { return N->getKind() == CheckAndImm; }
749
750private:
751 void printImpl(raw_ostream &OS, indent Indent) const override;
752 bool isEqualImpl(const Matcher *M) const override {
753 return cast<CheckAndImmMatcher>(Val: M)->Value == Value;
754 }
755};
756
757/// CheckOrImmMatcher - This checks to see if the current node is an 'and'
758/// with something equivalent to the specified immediate.
759class CheckOrImmMatcher : public Matcher {
760 int64_t Value;
761
762public:
763 CheckOrImmMatcher(int64_t value) : Matcher(CheckOrImm), Value(value) {}
764
765 int64_t getValue() const { return Value; }
766
767 static bool classof(const Matcher *N) { return N->getKind() == CheckOrImm; }
768
769private:
770 void printImpl(raw_ostream &OS, indent Indent) const override;
771 bool isEqualImpl(const Matcher *M) const override {
772 return cast<CheckOrImmMatcher>(Val: M)->Value == Value;
773 }
774};
775
776/// CheckImmAllOnesVMatcher - This checks if the current node is a build_vector
777/// or splat_vector of all ones.
778class CheckImmAllOnesVMatcher : public Matcher {
779public:
780 CheckImmAllOnesVMatcher() : Matcher(CheckImmAllOnesV) {}
781
782 static bool classof(const Matcher *N) {
783 return N->getKind() == CheckImmAllOnesV;
784 }
785
786private:
787 void printImpl(raw_ostream &OS, indent Indent) const override;
788 bool isEqualImpl(const Matcher *M) const override { return true; }
789 bool isContradictoryImpl(const Matcher *M) const override;
790};
791
792/// CheckImmAllZerosVMatcher - This checks if the current node is a
793/// build_vector or splat_vector of all zeros.
794class CheckImmAllZerosVMatcher : public Matcher {
795public:
796 CheckImmAllZerosVMatcher() : Matcher(CheckImmAllZerosV) {}
797
798 static bool classof(const Matcher *N) {
799 return N->getKind() == CheckImmAllZerosV;
800 }
801
802private:
803 void printImpl(raw_ostream &OS, indent Indent) const override;
804 bool isEqualImpl(const Matcher *M) const override { return true; }
805 bool isContradictoryImpl(const Matcher *M) const override;
806};
807
808/// CheckFoldableChainNodeMatcher - This checks to see if the current node
809/// (which defines a chain operand) is safe to fold into a larger pattern.
810class CheckFoldableChainNodeMatcher : public Matcher {
811public:
812 CheckFoldableChainNodeMatcher() : Matcher(CheckFoldableChainNode) {}
813
814 static bool classof(const Matcher *N) {
815 return N->getKind() == CheckFoldableChainNode;
816 }
817
818private:
819 void printImpl(raw_ostream &OS, indent Indent) const override;
820 bool isEqualImpl(const Matcher *M) const override { return true; }
821};
822
823/// EmitIntegerMatcher - This creates a new TargetConstant.
824class EmitIntegerMatcher : public Matcher {
825 // Optional string to give the value a symbolic name for readability.
826 std::string Str;
827 int64_t Val;
828 ValueTypeByHwMode VT;
829
830 unsigned ResultNo;
831
832public:
833 EmitIntegerMatcher(int64_t val, ValueTypeByHwMode vt, unsigned resultNo)
834 : Matcher(EmitInteger), Val(val), VT(std::move(vt)), ResultNo(resultNo) {}
835 EmitIntegerMatcher(const std::string &str, int64_t val, MVT vt,
836 unsigned resultNo)
837 : Matcher(EmitInteger), Str(str), Val(val), VT(vt), ResultNo(resultNo) {}
838
839 const std::string &getString() const { return Str; }
840 int64_t getValue() const { return Val; }
841 const ValueTypeByHwMode &getVT() const { return VT; }
842 unsigned getResultNo() const { return ResultNo; }
843
844 static bool classof(const Matcher *N) { return N->getKind() == EmitInteger; }
845
846private:
847 void printImpl(raw_ostream &OS, indent Indent) const override;
848 bool isEqualImpl(const Matcher *M) const override {
849 return cast<EmitIntegerMatcher>(Val: M)->Val == Val &&
850 cast<EmitIntegerMatcher>(Val: M)->VT == VT &&
851 cast<EmitIntegerMatcher>(Val: M)->Str == Str;
852 }
853};
854
855/// EmitRegisterMatcher - This creates a new TargetConstant.
856class EmitRegisterMatcher : public Matcher {
857 /// Reg - The def for the register that we're emitting. If this is null, then
858 /// this is a reference to zero_reg.
859 const CodeGenRegister *Reg;
860 ValueTypeByHwMode VT;
861
862 unsigned ResultNo;
863
864public:
865 EmitRegisterMatcher(const CodeGenRegister *reg, ValueTypeByHwMode vt,
866 unsigned resultNo)
867 : Matcher(EmitRegister), Reg(reg), VT(std::move(vt)), ResultNo(resultNo) {
868 }
869
870 const CodeGenRegister *getReg() const { return Reg; }
871 const ValueTypeByHwMode &getVT() const { return VT; }
872 unsigned getResultNo() const { return ResultNo; }
873
874 static bool classof(const Matcher *N) { return N->getKind() == EmitRegister; }
875
876private:
877 void printImpl(raw_ostream &OS, indent Indent) const override;
878 bool isEqualImpl(const Matcher *M) const override {
879 return cast<EmitRegisterMatcher>(Val: M)->Reg == Reg &&
880 cast<EmitRegisterMatcher>(Val: M)->VT == VT;
881 }
882};
883
884/// EmitConvertToTargetMatcher - Emit an operation that reads a specified
885/// recorded node and converts it from being a ISD::Constant to
886/// ISD::TargetConstant, likewise for ConstantFP.
887class EmitConvertToTargetMatcher : public Matcher {
888 // Recorded Node
889 unsigned Slot;
890
891 // Result
892 unsigned ResultNo;
893
894public:
895 EmitConvertToTargetMatcher(unsigned slot, unsigned resultNo)
896 : Matcher(EmitConvertToTarget), Slot(slot), ResultNo(resultNo) {}
897
898 unsigned getSlot() const { return Slot; }
899 unsigned getResultNo() const { return ResultNo; }
900
901 static bool classof(const Matcher *N) {
902 return N->getKind() == EmitConvertToTarget;
903 }
904
905private:
906 void printImpl(raw_ostream &OS, indent Indent) const override;
907 bool isEqualImpl(const Matcher *M) const override {
908 return cast<EmitConvertToTargetMatcher>(Val: M)->Slot == Slot;
909 }
910};
911
912/// EmitMergeInputChainsMatcher - Emit a node that merges a list of input
913/// chains together with a token factor. The list of nodes are the nodes in the
914/// matched pattern that have chain input/outputs. This node adds all input
915/// chains of these nodes if they are not themselves a node in the pattern.
916class EmitMergeInputChainsMatcher : public Matcher {
917 SmallVector<unsigned, 3> ChainNodes;
918
919public:
920 EmitMergeInputChainsMatcher(ArrayRef<unsigned> nodes)
921 : Matcher(EmitMergeInputChains), ChainNodes(nodes) {}
922
923 unsigned getNumNodes() const { return ChainNodes.size(); }
924
925 unsigned getNode(unsigned i) const {
926 assert(i < ChainNodes.size());
927 return ChainNodes[i];
928 }
929
930 static bool classof(const Matcher *N) {
931 return N->getKind() == EmitMergeInputChains;
932 }
933
934private:
935 void printImpl(raw_ostream &OS, indent Indent) const override;
936 bool isEqualImpl(const Matcher *M) const override {
937 return cast<EmitMergeInputChainsMatcher>(Val: M)->ChainNodes == ChainNodes;
938 }
939};
940
941/// EmitCopyToRegMatcher - Emit a CopyToReg node from a value to a physreg,
942/// pushing the chain and glue results.
943///
944class EmitCopyToRegMatcher : public Matcher {
945 // Value to copy into the physreg.
946 unsigned SrcSlot;
947 // Register Destination
948 const CodeGenRegister *DestPhysReg;
949
950public:
951 EmitCopyToRegMatcher(unsigned srcSlot, const CodeGenRegister *destPhysReg)
952 : Matcher(EmitCopyToReg), SrcSlot(srcSlot), DestPhysReg(destPhysReg) {}
953
954 unsigned getSrcSlot() const { return SrcSlot; }
955 const CodeGenRegister *getDestPhysReg() const { return DestPhysReg; }
956
957 static bool classof(const Matcher *N) {
958 return N->getKind() == EmitCopyToReg;
959 }
960
961private:
962 void printImpl(raw_ostream &OS, indent Indent) const override;
963 bool isEqualImpl(const Matcher *M) const override {
964 return cast<EmitCopyToRegMatcher>(Val: M)->SrcSlot == SrcSlot &&
965 cast<EmitCopyToRegMatcher>(Val: M)->DestPhysReg == DestPhysReg;
966 }
967};
968
969/// EmitNodeXFormMatcher - Emit an operation that runs an SDNodeXForm on a
970/// recorded node and records the result.
971class EmitNodeXFormMatcher : public Matcher {
972 // Recorded Node
973 unsigned Slot;
974 // Transform
975 const Record *NodeXForm;
976
977 // Result
978 unsigned ResultNo;
979
980public:
981 EmitNodeXFormMatcher(unsigned slot, const Record *nodeXForm,
982 unsigned resultNo)
983 : Matcher(EmitNodeXForm), Slot(slot), NodeXForm(nodeXForm),
984 ResultNo(resultNo) {}
985
986 unsigned getSlot() const { return Slot; }
987 const Record *getNodeXForm() const { return NodeXForm; }
988 unsigned getResultNo() const { return ResultNo; }
989
990 static bool classof(const Matcher *N) {
991 return N->getKind() == EmitNodeXForm;
992 }
993
994private:
995 void printImpl(raw_ostream &OS, indent Indent) const override;
996 bool isEqualImpl(const Matcher *M) const override {
997 return cast<EmitNodeXFormMatcher>(Val: M)->Slot == Slot &&
998 cast<EmitNodeXFormMatcher>(Val: M)->NodeXForm == NodeXForm;
999 }
1000};
1001
1002/// EmitNodeMatcherCommon - Common class shared between EmitNode and
1003/// MorphNodeTo.
1004class EmitNodeMatcherCommon : public Matcher {
1005 const CodeGenInstruction &CGI;
1006 const SmallVector<ValueTypeByHwMode, 3> VTs;
1007 const SmallVector<unsigned, 6> Operands;
1008 bool HasChain, HasInGlue, HasOutGlue, HasMemRefs;
1009
1010 /// NumFixedArityOperands - If this is a fixed arity node, this is set to -1.
1011 /// If this is a varidic node, this is set to the number of fixed arity
1012 /// operands in the root of the pattern. The rest are appended to this node.
1013 int NumFixedArityOperands;
1014
1015public:
1016 EmitNodeMatcherCommon(const CodeGenInstruction &cgi,
1017 ArrayRef<ValueTypeByHwMode> vts,
1018 ArrayRef<unsigned> operands, bool hasChain,
1019 bool hasInGlue, bool hasOutGlue, bool hasmemrefs,
1020 int numfixedarityoperands, bool isMorphNodeTo)
1021 : Matcher(isMorphNodeTo ? MorphNodeTo : EmitNode), CGI(cgi), VTs(vts),
1022 Operands(operands), HasChain(hasChain), HasInGlue(hasInGlue),
1023 HasOutGlue(hasOutGlue), HasMemRefs(hasmemrefs),
1024 NumFixedArityOperands(numfixedarityoperands) {}
1025
1026 const CodeGenInstruction &getInstruction() const { return CGI; }
1027
1028 unsigned getNumVTs() const { return VTs.size(); }
1029 const ValueTypeByHwMode &getVT(unsigned i) const {
1030 assert(i < VTs.size());
1031 return VTs[i];
1032 }
1033
1034 unsigned getNumOperands() const { return Operands.size(); }
1035 unsigned getOperand(unsigned i) const {
1036 assert(i < Operands.size());
1037 return Operands[i];
1038 }
1039
1040 ArrayRef<ValueTypeByHwMode> getVTList() const { return VTs; }
1041 ArrayRef<unsigned> getOperandList() const { return Operands; }
1042
1043 bool hasChain() const { return HasChain; }
1044 bool hasInGlue() const { return HasInGlue; }
1045 bool hasOutGlue() const { return HasOutGlue; }
1046 bool hasMemRefs() const { return HasMemRefs; }
1047 int getNumFixedArityOperands() const { return NumFixedArityOperands; }
1048
1049 static bool classof(const Matcher *N) {
1050 return N->getKind() == EmitNode || N->getKind() == MorphNodeTo;
1051 }
1052
1053private:
1054 void printImpl(raw_ostream &OS, indent Indent) const override;
1055 bool isEqualImpl(const Matcher *M) const override;
1056};
1057
1058/// EmitNodeMatcher - This signals a successful match and generates a node.
1059class EmitNodeMatcher : public EmitNodeMatcherCommon {
1060 void anchor() override;
1061 unsigned FirstResultSlot;
1062
1063public:
1064 EmitNodeMatcher(const CodeGenInstruction &cgi,
1065 ArrayRef<ValueTypeByHwMode> vts, ArrayRef<unsigned> operands,
1066 bool hasChain, bool hasInGlue, bool hasOutGlue,
1067 bool hasmemrefs, int numfixedarityoperands,
1068 unsigned firstresultslot)
1069 : EmitNodeMatcherCommon(cgi, vts, operands, hasChain, hasInGlue,
1070 hasOutGlue, hasmemrefs, numfixedarityoperands,
1071 false),
1072 FirstResultSlot(firstresultslot) {}
1073
1074 unsigned getFirstResultSlot() const { return FirstResultSlot; }
1075
1076 static bool classof(const Matcher *N) { return N->getKind() == EmitNode; }
1077};
1078
1079class MorphNodeToMatcher : public EmitNodeMatcherCommon {
1080 void anchor() override;
1081 const PatternToMatch &Pattern;
1082
1083public:
1084 MorphNodeToMatcher(const CodeGenInstruction &cgi,
1085 ArrayRef<ValueTypeByHwMode> vts,
1086 ArrayRef<unsigned> operands, bool hasChain, bool hasInGlue,
1087 bool hasOutGlue, bool hasmemrefs,
1088 int numfixedarityoperands, const PatternToMatch &pattern)
1089 : EmitNodeMatcherCommon(cgi, vts, operands, hasChain, hasInGlue,
1090 hasOutGlue, hasmemrefs, numfixedarityoperands,
1091 true),
1092 Pattern(pattern) {}
1093
1094 const PatternToMatch &getPattern() const { return Pattern; }
1095
1096 static bool classof(const Matcher *N) { return N->getKind() == MorphNodeTo; }
1097};
1098
1099/// CompleteMatchMatcher - Complete a match by replacing the results of the
1100/// pattern with the newly generated nodes. This also prints a comment
1101/// indicating the source and dest patterns.
1102class CompleteMatchMatcher : public Matcher {
1103 SmallVector<unsigned, 2> Results;
1104 const PatternToMatch &Pattern;
1105
1106public:
1107 CompleteMatchMatcher(ArrayRef<unsigned> results,
1108 const PatternToMatch &pattern)
1109 : Matcher(CompleteMatch), Results(results), Pattern(pattern) {}
1110
1111 unsigned getNumResults() const { return Results.size(); }
1112 unsigned getResult(unsigned R) const { return Results[R]; }
1113 const PatternToMatch &getPattern() const { return Pattern; }
1114
1115 static bool classof(const Matcher *N) {
1116 return N->getKind() == CompleteMatch;
1117 }
1118
1119private:
1120 void printImpl(raw_ostream &OS, indent Indent) const override;
1121 bool isEqualImpl(const Matcher *M) const override {
1122 return cast<CompleteMatchMatcher>(Val: M)->Results == Results &&
1123 &cast<CompleteMatchMatcher>(Val: M)->Pattern == &Pattern;
1124 }
1125};
1126
1127} // end namespace llvm
1128
1129#endif // LLVM_UTILS_TABLEGEN_COMMON_DAGISELMATCHER_H
1130