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