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