aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h
blob: f191db2b304ee67f7d658a3436ab87b4359f9e23 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
#define RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/Support/Error.h"

#include <cstdint>
#include <optional>
#include <span>
#include <string_view>
#include <vector>

namespace rcc {

class MachineFunction;
class MachineInstr;
class TargetDescription;

enum class GIMatchTypeKind : std::uint8_t {
  Any,
  Scalar,
  Pointer,
};

enum class GIMatchImmediateTransform : std::uint8_t {
  Identity,
  Negate,
  PlusOne,
};

enum class GIMatchTableOpcode : std::uint8_t {
  CheckOpcode,
  CheckOperandType,
  CheckInstructionFlag,
  ClearInstructionFlags,
  CheckOperandBank,
  CheckOperandClass,
  CheckIntegerPredicate,
  CheckFloatingPredicate,
  CheckMemorySize,
  CheckTargetPredicate,
  CheckOperandImmediateRanges,
  CheckOperandDefConstantRanges,
  CheckOperandDefOpcode,
  CheckOperandDefIntegerPredicate,
  CheckOperandDefOperandConstantRanges,
  CaptureDefOperand,
  CreateTemporary,
  CreateTemporaryType,
  MutateOpcode,
  BuildInstruction,
  RenderOperand,
  RenderComplexOperand,
  RenderCapturedOperand,
  RenderCapturedTypeBits,
  RenderPhysicalRegister,
  RenderTemporary,
  RenderCapturedImmediate,
  RenderImmediate,
  ReplaceRootWithCapturedOperand,
  EraseOperandDefinition,
  Complete,
};

struct GIMatchTableInstruction final {
  GIMatchTableOpcode Opcode = GIMatchTableOpcode::Complete;
  std::uint16_t OperandIndex = 0;
  std::uint32_t Value = 0;
  std::uint32_t Auxiliary = 0;
  std::int64_t Immediate = 0;
  std::string_view Symbol;
};

struct GIMatchComplexInstruction final {
  Opcode ResultOpcode;
  LowLevelType ResultType;
  std::vector<MachineOperand> Operands;
};

struct GIMatchComplexOperand final {
  std::optional<MachineOperand> Operand;
  std::optional<GIMatchComplexInstruction> Instruction;
};

struct GIMatchTableConstantRange final {
  std::int64_t Minimum = 0;
  std::int64_t Maximum = 0;
};

struct GIMatchTableRule final {
  std::string_view Name;
  std::uint32_t Priority = 0;
  std::uint32_t FirstInstruction = 0;
  std::uint32_t InstructionCount = 0;
};

enum class GIMatchTableKind : std::uint8_t {
  Selection,
  Combiner,
};

struct GIMatchTable final {
  std::span<const GIMatchTableInstruction> Instructions;
  std::span<const GIMatchTableRule> Rules;
  std::span<const GIMatchTableConstantRange> ConstantRanges;
  GIMatchTableKind Kind = GIMatchTableKind::Selection;
};

class GIMatchTableExecutor {
public:
  virtual ~GIMatchTableExecutor() = default;

  [[nodiscard]] Expected<bool> match(const GIMatchTable &Table, const MachineInstr &Instruction,
                                     const MachineFunction &Function,
                                     const TargetDescription &Target) const;

  // A false result means that no table rule matched. The executor completes all
  // predicates before applying the selected rule to MachineIR.
  [[nodiscard]] Expected<bool> select(const GIMatchTable &Table, MachineInstr &Instruction,
                                      MachineFunction &Function,
                                      const TargetDescription &Target) const;

  // Match and apply one generated GlobalISel combiner rule.
  [[nodiscard]] Expected<bool> combine(const GIMatchTable &Table, MachineInstr &Instruction,
                                       MachineFunction &Function,
                                       const TargetDescription &Target) const;

protected:
  [[nodiscard]] virtual Expected<bool>
  matchesTargetPredicate(std::string_view Name, const MachineInstr &Instruction,
                         const MachineFunction &Function, const TargetDescription &Target) const;

  [[nodiscard]] virtual Expected<std::optional<GIMatchComplexOperand>>
  renderComplexOperand(std::string_view Name, const MachineInstr &Instruction,
                       const MachineFunction &Function, const TargetDescription &Target,
                       std::uint32_t OperandIndex) const;

  [[nodiscard]] virtual Expected<void>
  finalizeGeneratedInstruction(MachineInstr &Output, MachineFunction &Function,
                               const TargetDescription &Target, MachineInstrFlags InputFlags,
                               bool InputMayRaiseFPException) const;
};

} // namespace rcc

#endif // RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H