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