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