aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/MachineIRBuilder.h
blob: 01cd64868005829cfd24c217cf32831b30580879 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_MACHINEIRBUILDER_H
#define RCC_CODEGEN_MACHINEIRBUILDER_H

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

#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace rcc {

class MachineBasicBlock;
class MachineFunction;
class MachineInstr;
class MachineMemOperand;
class RegisterMask;

class MachineInstrBuilder final {
public:
  MachineInstrBuilder(const MachineInstrBuilder &) = delete;
  MachineInstrBuilder &operator=(const MachineInstrBuilder &) = delete;
  MachineInstrBuilder(MachineInstrBuilder &&Other) noexcept;
  MachineInstrBuilder &operator=(MachineInstrBuilder &&Other) noexcept;
  ~MachineInstrBuilder() = default;

  [[nodiscard]] Expected<void> addOperand(MachineOperand &&Operand);
  [[nodiscard]] Expected<void> addImplicitOperand(MachineOperand &&Operand);

  [[nodiscard]] Expected<void> addDef(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                      SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addUse(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                      SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addDefUse(Register Reg, RegisterOperandFlags AdditionalFlags = {},
                                         SubRegIndex SubReg = {}, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addImplicitDef(Register Reg,
                                              RegisterOperandFlags AdditionalFlags = {},
                                              SubRegIndex SubReg = {},
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addImplicitUse(Register Reg,
                                              RegisterOperandFlags AdditionalFlags = {},
                                              SubRegIndex SubReg = {},
                                              std::uint32_t TargetFlags = 0);

  [[nodiscard]] Expected<void> addImmediate(std::int64_t Value, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntegerImmediate(APInt Value, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFloatingImmediate(APFloat Bits, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addBasicBlock(MachineBasicBlock &Block,
                                             std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFrameIndex(FrameIndex Index, std::int64_t Offset = 0,
                                             std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addConstantPool(ConstantPoolIndex Index, std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addJumpTable(JumpTableIndex Index, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addGlobalSymbol(std::string Name, std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addExternalSymbol(std::string Name, std::int64_t Offset = 0,
                                                 std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addBlockAddress(std::string FunctionName, std::string BlockName,
                                               std::int64_t Offset = 0,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                               std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addLiveOutRegisterMask(std::shared_ptr<const RegisterMask> Mask,
                                                      std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addMetadata(MachineMetadataValue Value,
                                           std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addTargetIndex(TargetIndex Index, std::int64_t Offset = 0,
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addMCSymbol(std::string Name, std::int64_t Offset = 0,
                                           std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addCFIInstruction(CFIInstructionIndex Index,
                                                 std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntrinsic(IntrinsicID ID, std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addIntegerPredicate(IntegerPredicate Predicate,
                                                   std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addFloatingPredicate(FloatingPredicate Predicate,
                                                    std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addShuffleMask(std::vector<std::int32_t> Elements,
                                              std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addDebugInstrRef(std::uint64_t InstructionNumber,
                                                std::uint64_t OperandNumber,
                                                std::uint32_t TargetFlags = 0);
  [[nodiscard]] Expected<void> addLaneMask(LaneMask Mask, std::uint32_t TargetFlags = 0);

  [[nodiscard]] Expected<void> tieOperands(std::size_t DefOperand, std::size_t UseOperand);
  [[nodiscard]] Expected<void> addMemOperand(const MachineMemOperand &MemOperand);

  [[nodiscard]] Expected<void> setFlags(MachineInstrFlags NewFlags);

  [[nodiscard]] Expected<void> setSourceLocation(SourceLocation NewLocation);
  [[nodiscard]] Expected<void> clearSourceLocation();
  [[nodiscard]] Expected<void> setDebugLocation(MachineDebugLocation NewLocation);
  [[nodiscard]] Expected<void> clearDebugLocation();

  [[nodiscard]] Expected<void> setOrigin(OriginID NewOrigin);
  [[nodiscard]] Expected<void> clearOrigin();

  [[nodiscard]] Expected<MachineInstr *> build();

private:
  MachineInstrBuilder(MachineFunction &Function, const TargetDescription &Target,
                      const InstructionDescriptor &Descriptor, MachineBasicBlock &InsertBlock,
                      MachineInstr *InsertBefore) noexcept;

  [[nodiscard]] Expected<void> ensurePending() const;
  [[nodiscard]] Expected<void> validateInsertionPoint() const;
  [[nodiscard]] Expected<void> validateFrameReference(FrameIndex Index) const;
  [[nodiscard]] Expected<void> validateConstantPoolReference(ConstantPoolIndex Index) const;
  [[nodiscard]] Expected<void> validateJumpTableReference(JumpTableIndex Index) const;
  [[nodiscard]] Expected<void> validateCFIReference(CFIInstructionIndex Index) const;
  [[nodiscard]] Expected<void> validateOperandOwnership(const MachineOperand &Operand) const;
  [[nodiscard]] std::vector<MachineOperandTie> collectOperandTies() const;
  [[nodiscard]] Expected<void> appendRegister(Register Reg, RegisterOperandFlags Flags,
                                              SubRegIndex SubReg, std::uint32_t TargetFlags,
                                              bool IsImplicit);

  MachineFunction *Function;
  const TargetDescription *Target;
  const InstructionDescriptor *Descriptor;
  MachineBasicBlock *InsertBlock;
  MachineInstr *InsertBefore;
  std::vector<MachineOperand> ExplicitOperands;
  std::vector<MachineOperand> ExtraImplicitOperands;
  std::vector<MachineOperandTie> RequestedTies;
  std::vector<const MachineMemOperand *> MemOperands;
  MachineInstrFlags Flags;
  std::optional<SourceLocation> Location;
  std::optional<MachineDebugLocation> DebugLocation;
  std::optional<OriginID> Origin;
  bool IsBuilt = false;

  friend class MachineIRBuilder;
};

class MachineIRBuilder final {
public:
  MachineIRBuilder(MachineFunction &Function, const TargetDescription &Target) noexcept
      : Function(&Function), Target(&Target) {}
  MachineIRBuilder(const MachineIRBuilder &) = delete;
  MachineIRBuilder &operator=(const MachineIRBuilder &) = delete;
  MachineIRBuilder(MachineIRBuilder &&) = delete;
  MachineIRBuilder &operator=(MachineIRBuilder &&) = delete;

  [[nodiscard]] MachineFunction &function() noexcept {
    return *Function;
  }

  [[nodiscard]] const MachineFunction &function() const noexcept {
    return *Function;
  }

  [[nodiscard]] const TargetDescription &target() const noexcept {
    return *Target;
  }

  [[nodiscard]] MachineBasicBlock *insertionBlock() const noexcept {
    return InsertBlock;
  }

  [[nodiscard]] Expected<void> verifyInsertPoint() const;

  [[nodiscard]] Expected<void> setInsertPoint(MachineBasicBlock &Block);
  [[nodiscard]] Expected<void> setInsertPointBefore(MachineInstr &Instruction);
  [[nodiscard]] Expected<void> setInsertPointAfter(MachineInstr &Instruction);
  void clearInsertPoint() noexcept {
    InsertBlock = nullptr;
    InsertBefore = nullptr;
  }

  [[nodiscard]] Expected<MachineInstrBuilder> buildInstr(Opcode InstructionOpcode) const;
  [[nodiscard]] Expected<MachineInstr *> buildTrunc(Register Destination, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildCopy(Register Destination, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildImplicitDef(Register Destination) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnyExt(Register Destination,
                                                     Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildZExt(Register Destination, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildSExt(Register Destination, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnyExtOrTrunc(Register Destination,
                                                            Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildSExtOrTrunc(Register Destination,
                                                         Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildConstant(Register Destination, APInt Value) const;
  [[nodiscard]] Expected<MachineInstr *> buildConstant(Register Destination,
                                                       std::int64_t Value) const;
  [[nodiscard]] Expected<MachineInstr *> buildSExtInReg(Register Destination, Register Source,
                                                        std::uint32_t Width) const;
  [[nodiscard]] Expected<MachineInstr *> buildAnd(Register Destination, Register Left,
                                                  Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildMergeValues(
      Register Destination, const std::vector<Register> &Sources) const;
  [[nodiscard]] Expected<MachineInstr *> buildUnmergeValues(
      const std::vector<Register> &Destinations, Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildExtract(Register Destination, Register Source,
                                                      std::uint64_t Offset) const;

private:
  [[nodiscard]] Expected<MachineInstr *> buildUnary(Opcode InstructionOpcode,
                                                    Register Destination,
                                                    Register Source) const;
  [[nodiscard]] Expected<MachineInstr *> buildBinary(Opcode InstructionOpcode,
                                                     Register Destination, Register Left,
                                                     Register Right) const;
  [[nodiscard]] Expected<MachineInstr *> buildExtOrTrunc(Opcode ExtensionOpcode,
                                                         Register Destination,
                                                         Register Source) const;
  [[nodiscard]] Expected<LowLevelType> registerType(Register Reg) const;
  [[nodiscard]] Expected<void> validateBlock(const MachineBasicBlock &Block) const;
  [[nodiscard]] Expected<void> validateInstruction(const MachineInstr &Instruction) const;

  MachineFunction *Function;
  const TargetDescription *Target;
  MachineBasicBlock *InsertBlock = nullptr;
  MachineInstr *InsertBefore = nullptr;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEIRBUILDER_H