xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/Common/GlobalISel/PatternParser.h (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 //===- PatternParser.h ------------------------------------------*- 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 /// \file Contains tools to parse MIR patterns from TableGen DAG elements.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_UTILS_GLOBALISEL_PATTERNPARSER_H
14 #define LLVM_UTILS_GLOBALISEL_PATTERNPARSER_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLFunctionalExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/SMLoc.h"
20 #include <memory>
21 
22 namespace llvm {
23 class CodeGenTarget;
24 class DagInit;
25 class Init;
26 class Record;
27 class StringRef;
28 class StringInit;
29 
30 namespace gi {
31 class InstructionPattern;
32 class Pattern;
33 class PatFrag;
34 
35 /// Helper class to parse MIR Pattern lists.
36 ///
37 /// e.g., `(match (G_FADD $x, $y, $z), (G_FNEG $y, $z))`
38 class PatternParser {
39   const CodeGenTarget &CGT;
40   ArrayRef<SMLoc> DiagLoc;
41 
42   mutable SmallPtrSet<const PatFrag *, 2> SeenPatFrags;
43 
44 public:
45   PatternParser(const CodeGenTarget &CGT, ArrayRef<SMLoc> DiagLoc)
46       : CGT(CGT), DiagLoc(DiagLoc) {}
47 
48   /// Parses a list of patterns such as:
49   ///   (Operator (Pattern1 ...), (Pattern2 ...))
50   /// \param List         DagInit of the expected pattern list.
51   /// \param ParseAction  Callback to handle a succesfully parsed pattern.
52   /// \param Operator     The name of the operator, e.g. "match"
53   /// \param AnonPatNamePrefix Prefix for anonymous pattern names.
54   /// \return true on success, false on failure.
55   bool
56   parsePatternList(const DagInit &List,
57                    function_ref<bool(std::unique_ptr<Pattern>)> ParseAction,
58                    StringRef Operator, StringRef AnonPatNamePrefix);
59 
60   /// \returns all PatFrags encountered by this PatternParser.
61   const auto &getSeenPatFrags() const { return SeenPatFrags; }
62 
63 private:
64   /// Parse any InstructionPattern from a TableGen Init.
65   /// \param Arg Init to parse.
66   /// \param PatName Name of the pattern that will be parsed.
67   /// \return the parsed pattern on success, nullptr on failure.
68   std::unique_ptr<Pattern> parseInstructionPattern(const Init &Arg,
69                                                    StringRef PatName);
70 
71   /// Parse a WipOpcodeMatcher from a TableGen Init.
72   /// \param Arg Init to parse.
73   /// \param PatName Name of the pattern that will be parsed.
74   /// \return the parsed pattern on success, nullptr on failure.
75   std::unique_ptr<Pattern> parseWipMatchOpcodeMatcher(const Init &Arg,
76                                                       StringRef PatName);
77 
78   /// Parses an Operand of an InstructionPattern from a TableGen Init.
79   /// \param IP InstructionPattern for which we're parsing.
80   /// \param OpInit Init to parse.
81   /// \param OpName Name of the operand to parse.
82   /// \return true on success, false on failure.
83   bool parseInstructionPatternOperand(InstructionPattern &IP,
84                                       const Init *OpInit,
85                                       const StringInit *OpName);
86 
87   /// Parses a MIFlag for an InstructionPattern from a TableGen Init.
88   /// \param IP InstructionPattern for which we're parsing.
89   /// \param Op Init to parse.
90   /// \return true on success, false on failure.
91   bool parseInstructionPatternMIFlags(InstructionPattern &IP,
92                                       const DagInit *Op);
93 
94   /// (Uncached) PatFrag parsing implementation.
95   /// \param Def PatFrag def to parsee.
96   /// \return the parsed PatFrag on success, nullptr on failure.
97   std::unique_ptr<PatFrag> parsePatFragImpl(const Record *Def);
98 
99   /// Parses the in or out parameter list of a PatFrag.
100   /// \param OpsList Init to parse.
101   /// \param ParseAction Callback on successful parse, with the name of
102   ///                     the parameter and its \ref PatFrag::ParamKind
103   /// \return true on success, false on failure.
104   bool
105   parsePatFragParamList(const DagInit &OpsList,
106                         function_ref<bool(StringRef, unsigned)> ParseAction);
107 
108   /// Cached PatFrag parser. This avoids duplicate work by keeping track of
109   /// already-parsed PatFrags.
110   /// \param Def PatFrag def to parsee.
111   /// \return the parsed PatFrag on success, nullptr on failure.
112   const PatFrag *parsePatFrag(const Record *Def);
113 };
114 
115 } // namespace gi
116 } // namespace llvm
117 
118 #endif
119