1 //===- GIMatchDagInstr.h - Represent a instruction to be matched ----------===// 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_GIMATCHDAGINSTR_H 10 #define LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/Support/raw_ostream.h" 15 16 namespace llvm { 17 class CodeGenInstruction; 18 class GIMatchDag; 19 class GIMatchDagOperandList; 20 21 /// Represents an instruction in the match DAG. This object knows very little 22 /// about the actual instruction to be matched as the bulk of that is in 23 /// predicates that are associated with the match DAG. It merely knows the names 24 /// and indices of any operands that need to be matched in order to allow edges 25 /// to link to them. 26 /// 27 /// Instances of this class objects are owned by the GIMatchDag and are not 28 /// shareable between instances of GIMatchDag. This is because the Name, 29 /// IsMatchRoot, and OpcodeAnnotation are likely to differ between GIMatchDag 30 /// instances. 31 class GIMatchDagInstr { 32 public: 33 using const_user_assigned_operand_names_iterator = 34 DenseMap<unsigned, StringRef>::const_iterator; 35 36 protected: 37 /// The match DAG this instruction belongs to. 38 GIMatchDag &Dag; 39 40 /// The name of the instruction in the pattern. For example: 41 /// (FOO $a, $b, $c):$name 42 /// will cause name to be assigned to this member. Anonymous instructions will 43 /// have a name assigned for debugging purposes. 44 StringRef Name; 45 46 /// The name of the instruction in the pattern as assigned by the user. For 47 /// example: 48 /// (FOO $a, $b, $c):$name 49 /// will cause name to be assigned to this member. If a name is not provided, 50 /// this will be empty. This name is used to bind variables from rules to the 51 /// matched instruction. 52 StringRef UserAssignedName; 53 54 /// The name of each operand (if any) that was assigned by the user. For 55 /// example: 56 /// (FOO $a, $b, $c):$name 57 /// will cause {0, "a"}, {1, "b"}, {2, "c} to be inserted into this map. 58 DenseMap<unsigned, StringRef> UserAssignedNamesForOperands; 59 60 /// The operand list for this instruction. This object may be shared with 61 /// other instructions of a similar 'shape'. 62 const GIMatchDagOperandList &OperandInfo; 63 64 /// For debugging purposes, it's helpful to have access to a description of 65 /// the Opcode. However, this object shouldn't use it for more than debugging 66 /// output since predicates are expected to be handled outside the DAG. 67 CodeGenInstruction *OpcodeAnnotation = nullptr; 68 69 /// When true, this instruction will be a starting point for a match attempt. 70 bool IsMatchRoot = false; 71 72 public: GIMatchDagInstr(GIMatchDag & Dag,StringRef Name,StringRef UserAssignedName,const GIMatchDagOperandList & OperandInfo)73 GIMatchDagInstr(GIMatchDag &Dag, StringRef Name, StringRef UserAssignedName, 74 const GIMatchDagOperandList &OperandInfo) 75 : Dag(Dag), Name(Name), UserAssignedName(UserAssignedName), 76 OperandInfo(OperandInfo) {} 77 getOperandInfo()78 const GIMatchDagOperandList &getOperandInfo() const { return OperandInfo; } getName()79 StringRef getName() const { return Name; } getUserAssignedName()80 StringRef getUserAssignedName() const { return UserAssignedName; } assignNameToOperand(unsigned Idx,StringRef Name)81 void assignNameToOperand(unsigned Idx, StringRef Name) { 82 assert(UserAssignedNamesForOperands[Idx].empty() && "Cannot assign twice"); 83 UserAssignedNamesForOperands[Idx] = Name; 84 } 85 86 const_user_assigned_operand_names_iterator user_assigned_operand_names_begin()87 user_assigned_operand_names_begin() const { 88 return UserAssignedNamesForOperands.begin(); 89 } 90 const_user_assigned_operand_names_iterator user_assigned_operand_names_end()91 user_assigned_operand_names_end() const { 92 return UserAssignedNamesForOperands.end(); 93 } 94 iterator_range<const_user_assigned_operand_names_iterator> user_assigned_operand_names()95 user_assigned_operand_names() const { 96 return make_range(user_assigned_operand_names_begin(), 97 user_assigned_operand_names_end()); 98 } 99 100 /// Mark this instruction as being a root of the match. This means that the 101 /// matcher will start from this node when attempting to match MIR. 102 void setMatchRoot(); isMatchRoot()103 bool isMatchRoot() const { return IsMatchRoot; } 104 setOpcodeAnnotation(CodeGenInstruction * I)105 void setOpcodeAnnotation(CodeGenInstruction *I) { OpcodeAnnotation = I; } getOpcodeAnnotation()106 CodeGenInstruction *getOpcodeAnnotation() const { return OpcodeAnnotation; } 107 108 void print(raw_ostream &OS) const; 109 110 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) dump()111 LLVM_DUMP_METHOD void dump() const { print(errs()); } 112 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 113 }; 114 115 raw_ostream &operator<<(raw_ostream &OS, const GIMatchDagInstr &N); 116 117 } // end namespace llvm 118 #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H 119